1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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 = 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
|