BaseViewNew.lua
9.39 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
BaseViewNew = {}
function BaseViewNew:ctor()
self.nodes = {};
self.selected_node = nil;
self.tolerate_value = 10;
end
function BaseViewNew:onButtonClick(ref, type,onComplete)
if 0 == type then
-- MyAudio.playSound("BUTTON_CLICK")
elseif 2 == type then
onComplete(ref, type)
end
end
--确认键处理函数
function BaseViewNew:onKeypadOk()
if self.selected_node and self.selected_node.fn then
self.selected_node:fn();
end
end
--上键处理函数
function BaseViewNew:onKeypadUp(isBounce,is_show_highlight,isLoop)
if self.selected_node == nil or IS_TV == 0 then
return;
end
if isBounce == nil then
isBounce = true;
end
local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
local minDinstance = 9999;
local near_node = nil;
for i,v in ipairs(self.nodes) do
for index,node in ipairs(v) do
local _p = node:convertToWorldSpace(cc.p(0,0));
if _p.y > curPos.y + self.tolerate_value then
_distance = Point.distance(curPos,_p);
if _distance < minDinstance then
minDinstance = _distance;
near_node = node;
end
end
end
end
isLoop = isLoop or false;
if isLoop and not near_node then
for i,v in ipairs(self.nodes) do
if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
local one_line_list = {};
for ii,vv in ipairs(v) do
local _p = vv:convertToWorldSpace(cc.p(0,0));
local x_distance = math.abs(curPos.x - _p.x);
print(x_distance)
if x_distance <= self.range_value then
one_line_list[#one_line_list+1] = vv;
end
end
local max_y = 9999;
for ii,vv in ipairs(one_line_list) do
local _p = vv:convertToWorldSpace(cc.p(0,0));
if _p.y < max_y then
max_y = _p.y;
near_node = vv;
end
end
break;
end
end
end
if near_node then
self:updateSelectedState(near_node,isBounce,is_show_highlight);
end
end
--下键处理函数
function BaseViewNew:onKeypadDown(isBounce,is_show_highlight,isLoop)
if self.selected_node == nil or IS_TV == 0 then
return;
end
if isBounce == nil then
isBounce = true;
end
local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
local minDinstance = 9999;
local near_node = nil;
for i,v in ipairs(self.nodes) do
for index,node in ipairs(v) do
local _p = node:convertToWorldSpace(cc.p(0,0));
if _p.y < curPos.y - self.tolerate_value then
_distance = Point.distance(curPos,_p);
if _distance < minDinstance then
minDinstance = _distance;
near_node = node;
end
end
end
end
isLoop = isLoop or false;
if isLoop and not near_node then
for i,v in ipairs(self.nodes) do
if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
local one_line_list = {};
for ii,vv in ipairs(v) do
local _p = vv:convertToWorldSpace(cc.p(0,0));
local x_distance = math.abs(curPos.x - _p.x);
print(x_distance)
if x_distance <= self.range_value then
one_line_list[#one_line_list+1] = vv;
end
end
local max_y = -9999;
for ii,vv in ipairs(one_line_list) do
local _p = vv:convertToWorldSpace(cc.p(0,0));
if _p.y > max_y then
max_y = _p.y;
near_node = vv;
end
end
break;
end
end
end
if near_node then
self:updateSelectedState(near_node,isBounce,is_show_highlight);
end
end
--左键处理函数
function BaseViewNew:onKeypadLeft(isBounce,is_show_highlight,isLoop)
if self.selected_node == nil or IS_TV == 0 then
return;
end
if isBounce == nil then
isBounce = true;
end
local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
local minDinstance = 9999;
local near_node = nil;
for i,v in ipairs(self.nodes) do
for index,node in ipairs(v) do
local _p = node:convertToWorldSpace(cc.p(0,0));
if _p.x < curPos.x - self.tolerate_value then
_distance = Point.distance(curPos,_p);
if _distance < minDinstance then
minDinstance = _distance;
near_node = node;
end
end
end
end
isLoop = isLoop or false;
if isLoop and not near_node then
for i,v in ipairs(self.nodes) do
if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
local max_x = -9999;
for ii,vv in ipairs(v) do
local _p = vv:convertToWorldSpace(cc.p(0,0));
print(_p.x)
if _p.x > max_x then
max_x = _p.x;
near_node = vv;
end
end
break;
end
end
end
if near_node then
self:updateSelectedState(near_node,isBounce,is_show_highlight);
end
end
--右键处理函数
function BaseViewNew:onKeypadRight(isBounce,is_show_highlight,isLoop)
if self.selected_node == nil or IS_TV == 0 then
return;
end
if isBounce == nil then
isBounce = true;
end
local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
local minDinstance = 9999;
local near_node = nil;
for i,v in ipairs(self.nodes) do
for index,node in ipairs(v) do
local _p = node:convertToWorldSpace(cc.p(0,0));
if _p.x > curPos.x + self.tolerate_value then
_distance = Point.distance(curPos,_p);
if _distance < minDinstance then
minDinstance = _distance;
near_node = node;
end
end
end
end
isLoop = isLoop or false;
if isLoop and not near_node then
for i,v in ipairs(self.nodes) do
if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
local min_x = 9999;
for ii,vv in ipairs(v) do
local _p = vv:convertToWorldSpace(cc.p(0,0));
print(_p.x)
if _p.x < min_x then
min_x = _p.x;
near_node = vv;
end
end
break;
end
end
end
if near_node then
self:updateSelectedState(near_node,isBounce,is_show_highlight);
end
end
--菜单键处理函数
function BaseViewNew:onKeypadMenu()
end
function BaseViewNew:updateSelectedState(near_node,isBounce,is_show_highlight)
if IS_TV == 0 then
return;
end
if isBounce == nil then
isBounce = true;
end
if is_show_highlight == nil then
is_show_highlight = 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
if near_node then
if near_node.is_tab then
for i,v in ipairs(self.nodes) do
if TableUtil.IsInTable(v,near_node) then
if self.selected_node and TableUtil.IsInTable(v,self.selected_node) then
break;
else
for ii,vv in ipairs(v) do
if vv.is_selected then
near_node = vv;
break;
end
end
break;
end
end
end
end
self.selected_node = near_node;
local selected = near_node:getChildByName("selected");
if selected then
selected:setVisible(true);
end
if is_show_highlight then
NodeManager.setHighlight(near_node,true,isBounce);
end
if self.selected_node.is_tab and self.selected_node.fn then
self.selected_node:fn();
end
end
end
function BaseViewNew: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 BaseViewNew