diff options
Diffstat (limited to 'JGUI/JGUI.lua')
-rw-r--r-- | JGUI/JGUI.lua | 89 |
1 files changed, 78 insertions, 11 deletions
diff --git a/JGUI/JGUI.lua b/JGUI/JGUI.lua index 6f82896..998a810 100644 --- a/JGUI/JGUI.lua +++ b/JGUI/JGUI.lua @@ -1,36 +1,103 @@ - -- 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, y, width, height, -- boundary + x = 0, y = 0, width = 0, height = 0, -- boundary swallow, -- swallow event or not by default -- event callbacks onClick, -- clicked callback onHover, -- hover callback - -- common functions + -- onEvent, } -function Widget.onEvent(clicked, ) - +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() - +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() - +JGUI.newPanel = function(name, ...) + local p = {} + p.name = name + setmetatable(p, Panel) + if p.init then + p.init(...) + end + return p end - +return JGUI
\ No newline at end of file |