Hi, nice to see the forum is still alive
Anyway, I'm trying to create a button class and would like to hear any opinions, in particular if any part of the code is clumsy, can be done better, etc as I'm still new to Lua. I'll be adding more functionality (eg labels, etc) if this is ok
By right all the class should be in a different file, but for simplicity I've combined everything.
Ok, basic overview of code,
Class function is from the FBA sample. Kform is just a container where I put all the Kbuttons. Having Kform will somewhat simplify coding, especially if there are a lot of buttons. However Kbuttons is able to work standalone without needing to use Kform if you just need one or two buttons in the app.
Code: -- Class function function class(base,ctor) local c = {} -- a new class instance if not ctor and type(base) == 'function' then ctor = base base = nil elseif type(base) == 'table' then -- our new class is a shallow copy of the base class! for i,v in pairs(base) do c[i] = v end c._base = base end -- the class will be the metatable for all its objects, -- and they will look up their methods in it. c.__index = c -- expose a ctor which can be called by <classname>(<args>) local mt = {} mt.__call = function(class_tbl,...) local obj = {} setmetatable(obj,c) if ctor then ctor(obj,...) else -- make sure that any stuff from the base class is initialized! if base and base.init then base.init(obj,...) end end return obj end c.init = ctor c.is_a = function(self,klass) local m = getmetatable(self) while m do if m == klass then return true end m = m._base end return false end setmetatable(c,mt) return c end
-- Class Kform
local Kform = class( function(a, name)
local component = {} a.component = component
a.name = name end )
function Kform:add(component) self.component[#self.component+1] = component end
function Kform:delete(component) if (#self.component > 0) then for i = 1,#self.component do if (self.component[i] == component) then table.remove(self.component, i) end end end end
function Kform:draw()
if (#self.component > 0) then for i = 1,#self.component do self.component[i]:draw() end end
end
function Kform:stylusdown(x,y) if (#self.component > 0) then for i = 1,#self.component do if (self.component[i]:stylusdown(x,y)) then self.component[i].active = true else self.component[i].active = false end end end end
function Kform:stylusup(x,y) if (#self.component > 0) then for i = 1,#self.component do self.component[i]:stylusup(x,y) end end end
function Kform:stylusmove(x,y) if (#self.component > 0) then for i = 1,#self.component do self.component[i]:stylusmove(x,y) end end end
-- Class Kbutton
local Kbutton = class( function(a, name, x, y, width, height) a.name = name a.x = x a.y = y a.width = width a.height = height a.isdown = 0 a.active = false
local r = {} local g = {} local b = {} a.r = r a.g = g a.b = b a.r[0] = 255 -- color background normal a.g[0] = 255 a.b[0] = 255 a.r[1] = 200 -- color background depressed a.g[1] = 200 a.b[1] = 200 a.r[2] = 50 -- color border a.g[2] = 50 a.b[2] = 50 end )
function Kbutton:draw()
fillrect(self.x, self.y, self.x + self.width, self.y + self.height, self.r[self.isdown], self.g[self.isdown], self.b[self.isdown])
rect(self.x, self.y, self.x + self.width, self.y + self.height, self.r[2], self.g[2], self.b[2]) if (self.active) then rect(self.x+1, self.y+1, self.x + self.width-1, self.y + self.height-1, self.r[2], self.g[2], self.b[2]) end end
function Kbutton:stylusdown(x, y) if ( (x >= self.x and x <= self.x + self.width) and (y >= self.y and y <= self.y + self.height) ) then self.isdown = 1 return true end end
function Kbutton:stylusup(x, y) if ( (x >= self.x and x <= self.x + self.width) and (y >= self.y and y <= self.y + self.height) and (self.isdown==1) ) then self.isdown = 0 kbutton_click(self) return true end end
function Kbutton:stylusmove(x, y) if ( (x < self.x or x > self.x + self.width) or (y < self.y or y > self.y + self.height) ) then self.isdown = 0 end end
-- Program Start
local myform = Kform("myform")
local mybutton = {}
for i = 0,1 do for j = 0,3 do ctr = i + (j*3) + 1 mybutton[ctr] = Kbutton("mybutton " .. i .. " " .. j, 25 + (100 * i), 50 + (50 * j), 75, 25)
myform:add(mybutton[ctr]) end end
local test = "Nil"
function onmainloop() clear(100,100,100) print(20,20,test, 255, 255,255) myform:draw() end
function onstylusdown(x,y) myform:stylusdown(x,y) end
function onstylusup(x,y) myform:stylusup(x,y) end
function onstylusmove(x,y) myform:stylusmove(x,y) end
function kbutton_click(button) test = button.name end
|