-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPanel.lua
More file actions
70 lines (56 loc) · 1.65 KB
/
Panel.lua
File metadata and controls
70 lines (56 loc) · 1.65 KB
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
-- Panel.lua
-- A panel is a container for other panels or objects that can be drawn. Used to form a
-- hierarchical representation of what's in the screen
-- objs need to have a translate method since they are added with coords relative
-- to this and need to be translated
import("LIB Table")
Panel = class(PositionObj)
function Panel:init(x,y)
PositionObj.init(self,x,y)
self.active = true -- if not active, touch events are not handled
self.elems = {}
end
-- object should have coordinates relative to this panel
function Panel:add(obj)
Table.map(function(x) assert(x~=obj,"adding duplicate obj") end,self.elems)
obj:translate(self.x,self.y)
table.insert(self.elems,obj)
end
function Panel:remove(obj)
Table.remove(self.elems,obj)
end
function Panel:removeAll()
self.elems = {}
end
function Panel:translate(dx,dy)
PositionObj.translate(self,dx,dy)
self:forwardMsg("translate",false,dx,dy)
end
-- called in every frame update
function Panel:tick()
self:forwardMsg("tick")
end
function Panel:draw()
self:forwardMsg("draw",false)
end
function Panel:keyboard(key)
self:forwardMsg("keyboard",false,key)
end
function Panel:touched(t)
if not self.active then return nil end
self:forwardMsg("touched",true,t)
end
function Panel:setActive(val)
self.active = val
Table.map(function(x)
if x.setActive then x:setActive(val)
else x.active = val end
end, self.elems)
end
function Panel:forwardMsg(message,onClone,...)
local elems = self.elems
if onClone then elems = Table.clone(elems) end
Table.map(function(x)
if x[message] then x[message](x,unpack(arg)) end
end,elems)
end