Skip to content

Instantly share code, notes, and snippets.

@devilstower
Created January 11, 2012 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilstower/1592344 to your computer and use it in GitHub Desktop.
Save devilstower/1592344 to your computer and use it in GitHub Desktop.
Spritely II 0.95 (Part 2)
SImage = class()
function SImage:init(name, img)
    self.frame = Frame(0,0,0,0)
    self.name = name
    self.img = img
    self.sel = false
end
function SImage:draw()
    local x, y
    pushStyle()
    fill(243, 242, 242, 255)
    stroke(71, 71, 71, 255)
    self.frame:draw()
    sprite(self.img, self.frame:midx(), self.frame:midy())
    x = self.frame.x1
    y = self.frame.y1
    fill(255, 27, 0, 255)
    if self.sel then
        strokeWidth(2)
        stroke(0, 0, 0, 255)
        ellipse(x + 10, y + 10, 20, 20)
        stroke(206, 206, 209, 255)
        strokeWidth(4)
        line(x+5, y+5, x+15, y+15)
        line(x+5, y+15, x+15, y+5)
    end
    popStyle()
end
function SImage:touched(touch)
    -- Codea does not automatically call this method
end
-- ====================
-- Skey 
-- ver. 0.1
-- a key for use in a simple keyboard
-- ====================
Skey = class()
function Skey:init(x, y, w, h, c)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.w = w
    self.h = h
    self.c = c
    self.img = nil
    self.active = false
end
function Skey:draw()
    pushStyle()
    if not self.active then
        noFill()
    end
    rect(self.x, self.y, self.w, self.h)
    sprite(self.img, self.x, self.y)
    popStyle()
end
function Skey:touched(touch)
    if self.active then
        if (math.abs(self.x - touch.x) < self.w) and
        (math.abs(self.y - touch.y) < self.h) then
            return true
        end
    end
    return false
end
-- ====================
-- Skeyboard 
-- ver. 0.1
-- a keyboard with intentionally limited options
-- designed to allow rudimentary names for keyed items.
-- Requires: SKey & Font10x12 classes.
-- ====================
Skeyboard = class()
function Skeyboard:init(x, y, s)
    self.x = x
    self.y = y
    self.size = s
    self.f = Font10x12()
    self.keys = {}
    self:initKeys()
    self.selected = nil
    self.frame = Frame(x, y, x + 13 * s + 20, y + 5 * s + 20)
end
function Skeyboard:draw()
    local i
    pushStyle()
    tint(0, 0, 0, 255)
    rectMode(CORNER)
    strokeWidth(2)
    fill(82, 82, 82, 255)
    self.frame:draw()
    rectMode(CENTER)
    fill(216, 216, 216, 255)
    for i = 1, 42 do
        self.keys[i]:draw()
    end
    popStyle()
end
function Skeyboard:touch(touch)
    if self.frame:touched(touch) then
        if touch.state ~= ENDED then
            for i = 1, 42 do
                w = self.keys[i].w / 2
                h = self.keys[i].h / 2
                x = self.keys[i].x
                y = self.keys[i].y
                if (math.abs(x - touch.x) < w) and
                (math.abs(y - touch.y) < h) then
                    fill(206, 203, 55, 255)
                    rectMode(CENTER)
                    self.keys[i]:draw()
                    self.selected = self.keys[i].c
                end
                
            end
            --return nil
        else
            temp = self.selected
            self.selected = nil
            --return temp
        end
        img = self.f.chars[string.byte(c)]
        return true
    end
    return false
end
function Skeyboard:initKeys()
    local i, s, x, y, w, h
    s = self.size + 5
    for i = 1, 11 do
        x = s * i + self.x
        y = s + self.y
        w = self.size
        h = self.size
        self.keys[i] = Skey(x, y, w, h, " ")
        self:loadkey(i, " ")
        self.keys[i].active = false
    end
    for i = 12, 21 do
        x = s * (i - 11) + self.x + self.size / 2
        y = s + self.y + s
        w = self.size
        h = self.size
        self.keys[i] = Skey(x, y, w, h, " ")
        self:loadkey(i, " ")
        self.keys[i].active = false
    end
    for i = 22, 32 do
        x = s * (i - 21) + self.x
        y = s + self.y + s * 2
        w = self.size
        h = self.size
        self.keys[i] = Skey(x, y, w, h, " ")
        self:loadkey(i, " ")
        self.keys[i].active = false
    end
    for i = 33, 42 do
        x = s * (i - 32) + self.x + self.size / 2
        y = s + self.y + s * 3
        w = self.size
        h = self.size
        self.keys[i] = Skey(x, y, w, h, " ")
        self:loadkey(i, " ")
        self.keys[i].active = false
    end
    
    --self:loadkey(1, " ")
    self:loadkey(2, "Z")
    self:loadkey(3, "X")
    self:loadkey(4, "C")
    self:loadkey(5, "V")
    self:loadkey(6, "B")
    self:loadkey(7, "N")
    self:loadkey(8, "M")
    self:loadkey(12, "A")
    self:loadkey(13, "S")
    self:loadkey(14, "D")
    self:loadkey(15, "F")
    self:loadkey(16, "G")
    self:loadkey(17, "H")
    self:loadkey(18, "J")
    self:loadkey(19, "K")
    self:loadkey(20, "L")
    
    self:loadkey(22, "Q")
    self:loadkey(23, "W")
    self:loadkey(24, "E")
    self:loadkey(25, "R")
    self:loadkey(26, "T")
    self:loadkey(27, "Y")
    self:loadkey(28, "U")
    self:loadkey(29, "I")
    self:loadkey(30, "O")
    self:loadkey(31, "P")
    self:loadkey(32, "<")
    
    self:loadkey(33, "1")
    self:loadkey(34, "2")
    self:loadkey(35, "3")
    self:loadkey(36, "4")
    self:loadkey(37, "5")
    self:loadkey(38, "6")
    self:loadkey(39, "7")
    self:loadkey(40, "8")
    self:loadkey(41, "9")
    self:loadkey(42, "0")
   
end
function Skeyboard:loadkey(i, c)
    self.keys[i].img = self.f:getCharImage(c)
    self.keys[i].c = c
    self.keys[i].active = true
end
-- ====================
-- 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()
    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)
    tint(247, 199, 22, 255)
    afont:drawString(self.frame.x2 - string.len(self.val) * 10,
     y + 20, self.val)
    tint(205, 205, 205, 255)
    afont:drawString(self.frame.x1, y + 20, self.name)
    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
        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 x, y, img
    createImage = loadstring(s)
    img = createImage()
    pushStyle()
    x = (self.frame.x1 + self.frame.x2) / 2
    y = self.frame.y2 - 32
    sprite(img, x, y)
    sprite(img, x, y - 80, img.width * 2, img.height * 2)
    sprite(img, x, y - 190, img.width * 3, img.height * 3)
    tint(255, 255, 255, 255)
    afont:drawString(x - 7, y - 25, "1X")
    afont:drawString(x - 7, y - 120, "2X")
    afont:drawString(x - 7, y - 245, "3X")
end
function SmallPanel:touched(touch)
    if self.frame:touched(touch) then
        return true
    end
    return false
end
SpritelyLoader2 = 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 = SpritelyLoader2()
--
-- 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 SpritelyLoader2: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
-- ====================
-- TextBox 
-- ver. 0.1
-- a control for basic string editing
-- ====================
-- 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.
TextBox = class()
function TextBox:init(x, y, w, s)
    self.x = x
    self.y = y
    self.w = w
    self.text = s
    self.blink = ElapsedTime
    self.blinkstate = true
end
function TextBox:draw()
    local x
    pushStyle()
    rectMode(CORNER)
    strokeWidth(1)
    stroke(0, 0, 0, 255)
    fill(228, 228, 228, 255)
    rect(self.x, self.y, self.w, 24)
    tint(0, 0, 0, 255)
    afont:drawString(self.x + 10, self.y + 10, self.text)
    if self.blink < ElapsedTime - 0.3 then
        self.blink = ElapsedTime
        self.blinkstate = not self.blinkstate
    end
    if self.blinkstate then
        strokeWidth(4)
        stroke(45, 45, 45, 255)
        x = self.x + string.len(self.text) * 10 + 11
        line(x, self.y + 3, x, self.y + 18)
    end
    popStyle()
end
function TextBox:touched(touch)
    -- move cursor? For the moment, touching a textbox has no function
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)
    self.x = x
    self.y = y
    self.text = s
    self.w = string.len(s) * 10 + 20
    self.h = 32
    self.clr = color(52, 52, 52, 255)
end
function TextButton:draw()
    pushStyle()
    stroke(56, 56, 56, 255)
    fill(self.clr.r, self.clr.g, self.clr.b, self.clr.a)
    rect(self.x, self.y, self.w, self.h)
    tint(241, 241, 241, 255)
    afont:drawString(self.x + 15, self.y + 17, self.text)
    popStyle()
end
function TextButton:touched(touch)
    if touch.state == BEGAN then
        if touch.x > self.x and touch.x < self.x + self.w then
            if touch.y > self.y and touch.y < self.y + 32 then
                return true
            end
        end
    end
    return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment