|
-
- local CheckBoxManager = class("CheckBoxManager")
-
- --[[
-
- 复选框管理器
- 使用方法:
- 使用接口addItem,传入 checkBox 及对应的key,选中值,非选中值,
- 不需要关注每个 checkBox 的点击事件
- 玩家操作完之后,使用接口 getResult 获取最后的结果
-
- 所有被选中的 checkBox 对应的 value 会被放在一个 table 中返回
-
- ]]--
-
- local normalColor = cc.c4b(21,99,97,255)
- local selectColor = cc.c4b(213,46,11,255)
-
- function CheckBoxManager:ctor()
- self.numCheckBox = 0;
- self.listCheckBox = {}
- self.callback = nil
-
- self.colorSwitch = true
- end
-
-
- -- 添加点击时的回调
- function CheckBoxManager:setColorSwitch(colorSwitch)
- self.colorSwitch = colorSwitch;
- end
-
- function CheckBoxManager:addItem(checkBox, key,selectValue,noSelectValue)
- if not checkBox then
- return
- end
-
- -- 数量自增
- self.numCheckBox = self.numCheckBox + 1
-
- local index = self.numCheckBox;
- self.listCheckBox[index] = {node = checkBox, key = key, selectValue = selectValue ,noSelectValue = noSelectValue};
-
- -- 注册点击事件
- 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()
- self:onClickCheckBos(index,false)
- end)
- end
- end
-
- -- 设置默认状态
- function CheckBoxManager:setDefault(keyList)
- for idx, key in pairs(keyList) do
- for k,v in pairs(self.listCheckBox) do
- if key == v.key and v.node then
- v.node:setSelectedState(true)
- local children = v.node:getChildren();
- for i, k in pairs(children) do
- if self.colorSwitch then
- k:setTextColor(selectColor)
- end
- end
- end
- end
-
- end
- end
-
- --[[
- 返回Key和当前checkbox状态对应的值
- ]]
- function CheckBoxManager:getResult()
- local tt = {}
- for k,v in pairs(self.listCheckBox) do
- if v.node then
- tt[v.key] = {}
- if v.node:getSelectedState() then
- tt[v.key].value = v.selectValue
- tt[v.key].key = v.key
- else
- tt[v.key].value = v.noSelectValue
- tt[v.key].key = nil
- end
- end
- end
- return tt;
- end
-
- function CheckBoxManager:getResult2()
- local tt = {}
- for k,v in pairs(self.listCheckBox) do
- if v.node then
- if v.node:getSelectedState() then
- tt[v.key] = v.selectValue
- else
- tt[v.key] = v.noSelectValue
- end
- end
- end
- return tt;
- end
-
- -- 添加点击时的回调
- function CheckBoxManager:setCallback(callback)
- self.callback = callback;
- end
-
- --
- function CheckBoxManager:onClickCheckBos(index, fromTouch)
- --[[if fromTouch then
- playBtnEffect()
- end--]]
- playBtnEffect()
-
- self.curCheckBox = index
-
- if not fromTouch then
- local state = self.listCheckBox[self.curCheckBox].node:getSelectedState()
- self.listCheckBox[self.curCheckBox].node:setSelectedState(not state)
- local children = self.listCheckBox[self.curCheckBox].node:getChildren();
- for i, k in pairs(children) do
- if not state then
- if self.colorSwitch then
- k:setTextColor(selectColor)
- end
- else
- if self.colorSwitch then
- k:setTextColor(normalColor)
- end
- end
- end
- else
- local state = self.listCheckBox[self.curCheckBox].node:getSelectedState()
- local children = self.listCheckBox[self.curCheckBox].node:getChildren();
- for i, k in pairs(children) do
- if not state then
- if self.colorSwitch then
- k:setTextColor(selectColor)
- end
- else
- if self.colorSwitch then
- k:setTextColor(normalColor)
- end
- end
- end
- end
-
- local state = self.listCheckBox[self.curCheckBox].node:getSelectedState()
-
- -- 回调
- runDelay(0.1,function ()
- if self.callback then
- self.callback()
- end
- end)
- end
-
- function CheckBoxManager:setSelectedState(key,state)
- for k,v in pairs(self.listCheckBox) do
- if key == v.key and v.node then
- v.node:setSelectedState(state)
- local children = v.node:getChildren();
- for i, k in pairs(children) do
- if self.colorSwitch then
- k:setTextColor(state and selectColor or normalColor)
- end
- end
- end
- end
- end
-
- return CheckBoxManager
|