Skip to content

Instantly share code, notes, and snippets.

@devilstower
Created Jan 16, 2012
Embed
What would you like to do?
Spritely Beta 2
-- Slider 
-- ver. 0.1
-- a control that replicates the iparameter slider
-- ====================
-- This control depends on a global instance of
-- the class Font10x12 called afont.  That instance
-- should be created in the Main setup() function
-- before any instance of this class is created.
Slider = class()
function Slider:init(x1, y1, x2, y2, min, max, val, name)
    -- you can accept and set parameters here
    self.frame = Frame(x1, y1, x2, y2)
    self.min = min
    self.max = max
    self.name = name
    self.val = val
    self.scale = ((x2 - x1) - 20) / (max - min)
end
function Slider:draw()
    local x, y
    pushStyle()
    font("Futura-Medium")
    textMode(CORNER)
    fontSize(12)
    y = (self.frame.y1 + self.frame.y2) / 2
    stroke(127, 127, 127, 255)
    strokeWidth(3)
    line(self.frame.x1 + 10, y, self.frame.x2 - 10, y)
    fill(38, 38, 38, 255)
    strokeWidth(2)
    
    x = self.scale * (self.val - self.min) + self.frame.x1 + 10
    stroke(224, 224, 224, 255)
    line(self.frame.x1 + 10, y, x, y)
    ellipse(x, y, 15)
    fill(189, 135, 18, 255)
    textAlign(RIGHT)
    text(self.val, self.frame.x2 - 30, y + 12)
    textAlign(LEFT)
    fill(216, 216, 216, 255)
    text(self.name, self.frame.x1 + 10, y + 12)
    popStyle()
end
function Slider:touched(touch)
    local x
    if touch.state == BEGAN or touch.state == MOVING then
        if self.frame:touched(touch) then
            x = touch.x - self.frame.x1 - 10
            self.val = math.floor(x / self.scale) + self.min
            if self.val < self.min then
                self.val = self.min
            elseif self.val > self.max then
                self.val = self.max
            end
            return true
        end
    end
end
-- SmallPanel 
-- ver. 0.1
-- a handy grouping to simplify the main draw routine.
-- no general utility outside Spritely
-- ====================
SmallPanel = class()
function SmallPanel:init(x1, y1, x2, y2)
    self.frame = Frame(x1, y1, x2, y2)
end
function SmallPanel:draw(s)
    local img
    createImage = loadstring(s)
    img = createImage()
    pushStyle()
    fontSize(12)
    fill(52, 52, 52, 255)
    self.frame:draw()
    x = self.frame:midx()
    y = self.frame.y2 - 32
    sprite(img, x, y)
    sprite(img, x, y - 80, img.width * 2, img.height * 2)
    fill(255, 255, 255, 255)
    textAlign(CENTER)
    text("1x", x, y - 25)
    text("2x", x, y - 120)
    if self.frame:height() > 200 then
        sprite(img, x, y - 190, img.width * 3, img.height * 3)
        text("3x", x, y - 245)
    end
    popStyle()
end
function SmallPanel:touched(touch)
    if self.frame:touched(touch) then
        return true
    end
    return false
end
SpritelyLoader = class()
-- This class takes data from global and dumps it into your project.
-- 
-- To use this class, create an instance in your project.
-- For example:  aloader = SpritelyLoader()
--
-- This call will read both keys and images from the global Spritely
-- storage and copy them into your project space. 
--
-- Once in the project data, load the saved string to 
-- an image using it's key.  For example:
--
-- createImage = loadstring( readProjectData("bob"))
-- anImage = createImage() 
-- To display your images, you'll need to load them into
-- a sprite. The command looks like this
-- sprite(anImage, 100, 100, 32, 32)
-- Good luck!
function SpritelyLoader:init()
    local keys, k, s
    if readGlobalData("SpritelyKeys") == nil then
        print("No Spritely data found.")
    else
        -- read global keys
        keys = readGlobalData("SpritelyKeys")
        saveProjectData("SpritelyKeys", keys)
        print("Moving...")
        for k in string.gmatch(keys,"([^,]+)") do
            print(k)
            s = readGlobalData(k)
            saveProjectData(k, s)
        end
        print("Move complete.")
    end
    
end
-- TextButton 
-- ver. 0.1
-- a control for displaying a simple button
-- ====================
-- This control depends on a global instance of
-- the class Font10x12 called afont.  That instance
-- should be created in the Main setup() function
-- before any instance of this class is created.
TextButton = class()
function TextButton:init(x, y, s)
    local w, h
    pushStyle()
    font("Futura-Medium")
    textMode(CENTER)
    fontSize(18)
    w, h = textSize(s)
    w = w + 15
    h = h + 6
    self.frame = Frame(x, y, x+w, y+h)
    self.w = w
    self.h = h
    self.text = s
    self.clr = color(57, 57, 57, 255)
    popStyle()
end
function TextButton:draw()
    pushStyle()
    font("Futura-Medium")
    textMode(CENTER)
    fontSize(18)
    self.frame:gloss(self.clr, 1)
    --self.frame:draw()
    fill(255, 255, 255, 255)
    text(self.text, self.frame:midx(), self.frame:midy())
    popStyle()
end
function TextButton:touched(touch)
    if touch.state == BEGAN then
        return self.frame:touched(touch)
    end
    return false
end
Ttouch = class()
-- Translatable touch
-- This class is used for instances when the coordinates of a 
-- touch need to be modified.
function Ttouch:init(touch)
    self.x = touch.x
    self.y = touch.y
    self.state = touch.state
    self.prevX = touch.prevX
    self.prevY = touch.prevY
    self.deltaX = touch.deltaX
    self.deltaY = touch.deltaY
    self.id = touch.id
    self.tapCount = touch.tapCount
end
function Ttouch:translate(x, y)
    self.x = self.x - x
    self.y = self.y - y
end
function Ttouch:touched(touch)
    -- Codea does not automatically call this method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment