-- panel local JGUI = {} local contain = function(x, y, box) return x >= box.x and x <= box.x + box.width and y >= box.y and y <= box.y + box.height end local Widget = { -- common properties name, -- name x = 0, y = 0, width = 0, height = 0, -- boundary swallow, -- swallow event or not by default -- event callbacks onClick, -- clicked callback onHover, -- hover callback -- onEvent, } Widget.__index = Widget function Widget.onEvent(w, e) if e.type == "MouseButtonDown" then if e.button == "Left" then if contain(e.x, e.y, {x = w.x, y = w.y, width = w.width, height = w.height}) then w.onClick() end end elseif e.type == "MouseButtonUp" then if e.button == "Left" then if contain(e.x, e.y, {x = w.x, y = w.y, width = w.width, height = w.height}) then w.onRelease() end end end end function Widget.setSize(widget, w, h) widget.width = w widget.height = h end function Widget.setPosition(widget, x, y) widget.x = x widget.y = y end local Panel = { widgets = {}, x = 0, y = 0, width = 0, height = 0, goon = true, } Panel.__index = Panel function Panel.onEvent(p, e) for _, w in pairs(p.widgets) do if goon then w:onEvent(e) else break end end goon = true end function Panel.onUpdate(p, dt) end function Panel.setSize(p, w, h) p.width = w p.height = h end function Panel.add(p, w) table.insert(p.widgets, w) end local Button = {} Button.__index = Button setmetatable(Button, Widget) ------------------------------------ -- ------------------------------------ JGUI.newButton = function(name, ...) local b = {} b.name = name setmetatable(b, Button) if b.init then b.init(...) end return b end JGUI.newPanel = function(name, ...) local p = {} p.name = name setmetatable(p, Panel) if p.init then p.init(...) end return p end return JGUI