ShopView.lua
6.33 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
local ShopView = dialog.uinode("ui/ShopLayer.csb",import(".BaseView"))
local shopItemType = 1;
--商城
function ShopView:ctor()
ShopView.super.ctor(self);
self:init();
end
function ShopView:show(shopType)
shopItemType = shopType or 1;
local scene = cc.Director:getInstance():getRunningScene()
local inst = ShopView:create();
inst:setName("ShopView");
scene:addChild(inst, dialog.ZORDER_APP);
inst:setContentSize(cc.size(display.width,display.height));
ccui.Helper:doLayout(inst);
inst.layer = cc.Layer:create();
inst:addChild(inst.layer,1);
registerScriptTouchEvent(inst.layer,handler(inst,inst.onTouchBegin),handler(inst,inst.onTouchMove),handler(inst,inst.onTouchEnd));
UmengHelper.onEvent("shop");
return inst;
end
function ShopView:onTouchBegin(event)
self.begin_touch_pos = self.layer:getParent():convertToWorldSpace(event:getLocation());
if self.content.view then
self.content.view.isMoved = false;
end
return true;
end
function ShopView:onTouchMove(event)
local move_touch_pos = self.layer:getParent():convertToWorldSpace(event:getLocation());
local _distance = Point.distance(self.begin_touch_pos,move_touch_pos);
if _distance > 5 and self.content.view then
self.content.view.isMoved = true;
end
end
function ShopView:onTouchEnd(event)
local p = self.layer:convertToWorldSpace(event:getLocation());
end
function ShopView:init()
self.content = self:getChildByName("content");
local btn_close = self:getChildByName("btn_close");
btn_close.fn = handler(self,self.onClose);
btn_close:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type,btn_close.fn)end);
self.nodes[1] = {btn_close};
for i=1,4 do
local tab = self:getChildByName("tab_"..i);
tab:setTag(i);
tab:setSwallowTouches(false);
tab.fn = function ()
self:onTabClick(tab);
end;
tab:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, handler(self,self.onTabClick))end);
self.nodes[#self.nodes+1] = {tab};
end
self.nodesCopy = TableUtil.copyTab(self.nodes);
self:getChildByName("img_money"):getChildByName("txt_money"):setString(StringUtil.getWanString(UserModel.getCoin()));
self:getChildByName("img_coupons"):getChildByName("txt_coupons"):setString(StringUtil.getWanString(UserModel.getCoupons()));
self:getChildByName("img_card"):getChildByName("txt"):setString(UserModel.getItemNum(100009));
self.nodeIndex = shopItemType + 1;
self.selectedIndex = 1;
self:updateTabState();
self:update();
EventListener.addEventListener(self,EnumEvent.UPDATE_ITEMS,handler(self,self.onItemUpdate));
end
function ShopView:onItemUpdate(params)
self:getChildByName("img_money"):getChildByName("txt_money"):setString(StringUtil.getWanString(UserModel.getCoin()));
self:getChildByName("img_coupons"):getChildByName("txt_coupons"):setString(StringUtil.getWanString(UserModel.getCoupons()));
self:getChildByName("img_card"):getChildByName("txt"):setString(UserModel.getItemNum(100009));
end
function ShopView:onTabClick(ref,event)
local tag = tonumber(ref:getTag());
if shopItemType ~= tag then
self:setTab(tag);
end
end
function ShopView:setTab(_shopType)
shopItemType = _shopType;
self:updateTabState();
self:update();
end
function ShopView:updateTabState()
for i=1,4 do
local tab = self:getChildByName("tab_"..i);
local selected = tab:getChildByName("selected");
if i == shopItemType then
selected:setVisible(true);
else
selected:setVisible(false);
end
end
end
function ShopView:update()
self.content:removeAllChildren();
self.content.view = require("app.views.view.ShopListView"):create();
local _nodes = self.content.view:init(shopItemType,handler(self,self.setTab));
self.content:addChild(self.content.view);
self.nodes = TableUtil.merge(self.nodesCopy,_nodes);
self:updateSelectedState();
end
function ShopView:onClose()
self:close();
end
--返回键处理函数
function ShopView:onKeypadBack()
self:close();
end
--确认键处理函数
function ShopView:onKeypadOk()
local node = self.nodes[self.nodeIndex][self.selectedIndex];
if node and node.fn then
node.fn();
end
end
--上键处理函数
function ShopView:onKeypadUp()
if self.nodeIndex ~= #self.nodesCopy + 1 then
ShopView.super.onKeypadUp(self);
end
local node = self.nodes[self.nodeIndex][self.selectedIndex];
if self.nodeIndex > 1 and self.nodeIndex <= #self.nodesCopy and node and node.fn then
node.fn();
elseif self.nodeIndex > #self.nodesCopy and node then
self.content.view:setScrollPosition(node:getParent());
end
end
--下键处理函数
function ShopView:onKeypadDown()
if self.nodeIndex ~= #self.nodesCopy then
ShopView.super.onKeypadDown(self);
end
local node = self.nodes[self.nodeIndex][self.selectedIndex];
if self.nodeIndex > 1 and self.nodeIndex <= #self.nodesCopy and node and node.fn then
node.fn();
elseif self.nodeIndex > #self.nodesCopy and node then
self.content.view:setScrollPosition(node:getParent());
end
end
--左键处理函数
function ShopView:onKeypadLeft()
if self.nodeIndex > #self.nodesCopy and self.selectedIndex == 1 then
self.nodeIndex = shopItemType + 1;
self.selectedIndex = 1;
self:updateSelectedState(false);
elseif self.nodeIndex > #self.nodesCopy then
ShopView.super.onKeypadLeft(self);
end
end
--右键处理函数
function ShopView:onKeypadRight()
if self.nodeIndex > 1 and self.nodeIndex <= #self.nodesCopy then
self.nodeIndex = #self.nodesCopy + 1;
self.selectedIndex = 1;
self:updateSelectedState(false);
elseif self.nodeIndex > #self.nodesCopy then
ShopView.super.onKeypadRight(self);
end
end
function ShopView:updateSelectedState()
if IS_TV == 0 then
return;
end
if #self.nodes == 0 then
logE("该界面没有设置按键节点");
return ;
end
for i,v in ipairs(self.nodes) do
for index,node in ipairs(v) do
NodeManager.setHighlight(node,false);
end
end
local node = self.nodes[self.nodeIndex][self.selectedIndex];
NodeManager.setHighlight(node,true,false);
end
return ShopView