BaseView.lua
4.12 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
BaseView = {}
function BaseView:ctor()
self.nodes = {};
self.nodeIndex = 1;
self.selectedIndex = 1;
end
function BaseView:onButtonClick(ref, type,onComplete)
if 0 == type then
-- MyAudio.playSound("BUTTON_CLICK")
elseif 2 == type then
onComplete(ref, type)
end
end
--确认键处理函数
function BaseView:onKeypadOk()
if not self.nodes[self.nodeIndex] then
return;
end
local node = self.nodes[self.nodeIndex][self.selectedIndex];
if node and node.fn then
node:fn();
end
end
--上键处理函数
function BaseView:onKeypadUp(isBounce)
if isBounce == nil then
isBounce = true;
end
if self.nodeIndex > 1 then
local curNode = self.nodes[self.nodeIndex][self.selectedIndex];
local curPos = curNode:convertToWorldSpace(cc.p(0,0));
self.nodeIndex = self.nodeIndex - 1;
local minDinstance = 9999;
self.selectedIndex = 1;
for i,v in ipairs(self.nodes[self.nodeIndex]) do
local _p = v:convertToWorldSpace(cc.p(0,0));
local _distance = math.abs(_p.x-curPos.x);
if _distance < minDinstance then
minDinstance = _distance;
self.selectedIndex = i;
end
end
self:updateSelectedState(isBounce);
end
end
--下键处理函数
function BaseView:onKeypadDown(isBounce)
if isBounce == nil then
isBounce = true;
end
if self.nodes and self.nodeIndex < #self.nodes then
local curNode = self.nodes[self.nodeIndex][self.selectedIndex];
local curPos = curNode:convertToWorldSpace(cc.p(0,0));
self.nodeIndex = self.nodeIndex + 1;
local minDinstance = 9999;
self.selectedIndex = 1;
for i,v in ipairs(self.nodes[self.nodeIndex]) do
local _p = v:convertToWorldSpace(cc.p(0,0));
local _distance = math.abs(_p.x-curPos.x);
if _distance < minDinstance then
minDinstance = _distance;
self.selectedIndex = i;
end
end
self:updateSelectedState(isBounce);
end
end
--左键处理函数
function BaseView:onKeypadLeft(isBounce)
if isBounce == nil then
isBounce = true;
end
if self.selectedIndex > 1 then
self.selectedIndex = self.selectedIndex - 1;
self:updateSelectedState(isBounce);
end
end
--右键处理函数
function BaseView:onKeypadRight(isBounce)
if isBounce == nil then
isBounce = true;
end
if #self.nodes > 0 and self.selectedIndex < #self.nodes[self.nodeIndex] then
self.selectedIndex = self.selectedIndex + 1;
self:updateSelectedState(isBounce);
end
end
--菜单键处理函数
function BaseView:onKeypadMenu()
end
function BaseView:updateSelectedState(isBounce)
if IS_TV == 0 then
return;
end
if isBounce == nil then
isBounce = true;
end
if #self.nodes == 0 then
logE("该界面没有设置按键节点");
return ;
end
for i,v in ipairs(self.nodes) do
for index,node in ipairs(v) do
local selected = node:getChildByName("selected");
if selected then
selected:setVisible(false);
end
NodeManager.setHighlight(node,false);
end
end
local node = self.nodes[self.nodeIndex][self.selectedIndex];
if node then
local selected = node:getChildByName("selected");
if selected then
selected:setVisible(true);
end
NodeManager.setHighlight(node,true,isBounce);
end
end
function BaseView:close()
local scene = cc.Director:getInstance():getRunningScene();
scene.keypadManager:removeKeypadReceiver(self);
self:setVisible(false)
local delay = cc.DelayTime:create(0.1)
local del = function()
self:removeFromParent(true)
logD("count1:", collectgarbage("count"))
collectgarbage("collect")
collectgarbage("collect")
collectgarbage("collect")
logD("count2:", collectgarbage("count"))
end
self:runAction(cc.Sequence:create(delay, cc.CallFunc:create(del)))
end
return BaseView