|
-
- local RadioManager = class("RadioManager")
- --[[
-
- 单选框管理器
- 使用方法:
- 使用接口addItem,传入 checkBox 及对应的值,
- 不需要关注每个 checkBox 的点击事件
- 玩家操作完之后,使用接口 getResult 获取最后的结果
-
- 返回玩家最后选择的 checkBox 对应的值
-
- ]]--
-
- function RadioManager:ctor()
-
- self.curCheckBox = 0
- self.numCheckBox = 0;
- self.listCheckBox = {}
-
- self.callback = nil
-
- self.colorSwitch = true
- self.normalColor = cc.c4b(21,99,97,255)
- self.selectColor = cc.c4b(213,46,11,255)
- self.disableColor = cc.c4b(128,128,128,255)
- end
-
- function RadioManager:addItem(checkBox, value)
- if not checkBox then
- if cc.Application:getInstance():getTargetPlatform() == 0 then
- showTooltip("节点不存在")
- end
- return
- end
-
- -- 数量自增
- self.numCheckBox = self.numCheckBox + 1
-
- local index = self.numCheckBox;
- self.listCheckBox[index] = {node = checkBox, value = value};
-
- -- 注册点击事件
- checkBox:registerClick(function() self:onClickCheckBos(index, true) end)
-
- -- 子节点响应点击事件
- local children = checkBox:getChildren();
- for i, k in pairs(children) do
- k:setTouchEnabled(true)
- k:registerClick(function()
- playBtnEffect()
- self:onClickCheckBos(index, false)
- end)
- end
- end
-
- -- 设置默认状态
- -- 默认只能选中一个
- function RadioManager:setDefault(value)
- local isValue = false
- for idx, checkbox in pairs(self.listCheckBox) do
- if checkbox.value == value then
- isValue = true
- checkbox.node:setSelectedState(true);
- self:onClickCheckBos(idx, false)
- return;
- end
- end
- --判断默认值存在不,不存在,用默认
- if not isValue then
- for idx, checkbox in pairs(self.listCheckBox) do
- if idx == 1 then
- checkbox.node:setSelectedState(true)
- self:onClickCheckBos(idx, false)
- return;
- end
- end
- end
- end
-
- -- 添加点击时的回调
- function RadioManager:setCallback(callback)
- self.callback = callback;
- end
-
- -- 添加点击时的回调
- function RadioManager:setColorSwitch(colorSwitch)
- self.colorSwitch = colorSwitch;
- end
-
- function RadioManager:setSelectColor(selectColor)
- self.selectColor = selectColor;
- end
-
- function RadioManager:setNormalColor(normalColor)
- self.normalColor = normalColor;
- end
-
- function RadioManager:setDisableColor(disableColor)
- self.disableColor = disableColor;
- end
-
- function RadioManager:onClickCheckBos(index, fromTouch)
- if fromTouch then
- playBtnEffect()
- end
-
- if index == self.curCheckBox then
- if fromTouch then
- self.listCheckBox[self.curCheckBox].node:setSelectedState(false, true)
- end
- -- 回调
- if self.callback then
- self.callback(self.listCheckBox[self.curCheckBox].value,self.listCheckBox[self.curCheckBox].node)
- end
- return
- end
- if self.curCheckBox and self.listCheckBox[self.curCheckBox] and self.listCheckBox[self.curCheckBox].node then
- self.listCheckBox[self.curCheckBox].node:setSelectedState(false)
- local children = self.listCheckBox[self.curCheckBox].node:getChildren();
- if self.colorSwitch then
- for i, k in pairs(children) do
- if k.setTextColor then
- k:setTextColor(self.normalColor)
- end
- end
- end
- self.curCheckBox = nil
- end
-
- self.curCheckBox = index
-
- if self.curCheckBox and self.listCheckBox[self.curCheckBox] and self.listCheckBox[self.curCheckBox].node then
- if not fromTouch then
- self.listCheckBox[self.curCheckBox].node:setSelectedState(true)
- local children = self.listCheckBox[self.curCheckBox].node:getChildren();
- if self.colorSwitch then
- for i, k in pairs(children) do
- if k.setTextColor then
- k:setTextColor(self.selectColor)
- end
- end
- end
- else
- local children = self.listCheckBox[self.curCheckBox].node:getChildren();
- if self.colorSwitch then
- for i, k in pairs(children) do
- if k.setTextColor then
- k:setTextColor(self.selectColor)
- end
- end
- end
- -- 要被逼疯....
- -- 因为手动点击的时候,C++代码中会将选中状态改变,而脚本这边捕获不到改变
- -- 所以这里需要先设为选中,并更新选中状态
- -- 在设为未选中个,不更新选中状态
- -- 再等待C++改变状态
- self.listCheckBox[self.curCheckBox].node:setSelectedState(true)
- self.listCheckBox[self.curCheckBox].node:setSelectedState(false, true)
- end
- end
-
- -- 回调
- if self.callback then
- self.callback(self.listCheckBox[self.curCheckBox].value, self.listCheckBox[self.curCheckBox].node)
- end
- end
-
- function RadioManager:getResult()
- if self.curCheckBox and self.listCheckBox[self.curCheckBox] and self.listCheckBox[self.curCheckBox].value then
- return self.listCheckBox[self.curCheckBox].value
- else
- return nil
- end
- end
-
- function RadioManager:setEnabled(b)
- for k,v in pairs(self.listCheckBox) do
- v.node:setEnabled(b)
- local children = v.node:getChildren()
- for _, c in pairs(children) do
- if c.setTextColor then
- if b then
- if self.curCheckBox==k then
- c:setTextColor(self.selectColor)
- else
- c:setTextColor(self.normalColor)
- end
- else
- c:setTextColor(self.disableColor)
- end
- end
- end
- end
-
- end
-
- return RadioManager
|