ViewBase.lua
1.84 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
local ViewBase = class("ViewBase", cc.Node)
function ViewBase:ctor(app, name)
self:enableNodeEvents()
self.app_ = app
self.name_ = name
-- check CSB resource file
local res = rawget(self.class, "RESOURCE_FILENAME")
if res then
self:createResoueceNode(res)
end
local binding = rawget(self.class, "RESOURCE_BINDING")
if res and binding then
self:createResoueceBinding(binding)
end
if self.onCreate then self:onCreate() end
end
function ViewBase:getApp()
return self.app_
end
function ViewBase:getName()
return self.name_
end
function ViewBase:getResourceNode()
return self.resourceNode_
end
function ViewBase:createResoueceNode(resourceFilename)
if self.resourceNode_ then
self.resourceNode_:removeSelf()
self.resourceNode_ = nil
end
self.resourceNode_ = cc.CSLoader:createNode(resourceFilename)
assert(self.resourceNode_, string.format("ViewBase:createResoueceNode() - load resouce node from file \"%s\" failed", resourceFilename))
self:addChild(self.resourceNode_)
end
function ViewBase:createResoueceBinding(binding)
assert(self.resourceNode_, "ViewBase:createResoueceBinding() - not load resource node")
for nodeName, nodeBinding in pairs(binding) do
local node = self.resourceNode_:getChildByName(nodeName)
if nodeBinding.varname then
self[nodeBinding.varname] = node
end
for _, event in ipairs(nodeBinding.events or {}) do
if event.event == "touch" then
node:onTouch(handler(self, self[event.method]))
end
end
end
end
function ViewBase:showWithScene(transition, time, more)
self:setVisible(true)
local scene = display.newScene(self.name_)
scene:addChild(self)
display.runScene(scene, transition, time, more)
return self
end
return ViewBase