You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.0 KiB

  1. local CheckBoxManager = class("CheckBoxManager")
  2. --[[
  3. 复选框管理器
  4. 使用方法:
  5. 使用接口addItem,传入 checkBox 及对应的key,选中值,非选中值,
  6. 不需要关注每个 checkBox 的点击事件
  7. 玩家操作完之后,使用接口 getResult 获取最后的结果
  8. 所有被选中的 checkBox 对应的 value 会被放在一个 table 中返回
  9. ]]--
  10. local normalColor = cc.c4b(21,99,97,255)
  11. local selectColor = cc.c4b(213,46,11,255)
  12. function CheckBoxManager:ctor()
  13. self.numCheckBox = 0;
  14. self.listCheckBox = {}
  15. self.callback = nil
  16. self.colorSwitch = true
  17. end
  18. -- 添加点击时的回调
  19. function CheckBoxManager:setColorSwitch(colorSwitch)
  20. self.colorSwitch = colorSwitch;
  21. end
  22. function CheckBoxManager:addItem(checkBox, key,selectValue,noSelectValue)
  23. if not checkBox then
  24. return
  25. end
  26. -- 数量自增
  27. self.numCheckBox = self.numCheckBox + 1
  28. local index = self.numCheckBox;
  29. self.listCheckBox[index] = {node = checkBox, key = key, selectValue = selectValue ,noSelectValue = noSelectValue};
  30. -- 注册点击事件
  31. checkBox:registerClick(function()
  32. self:onClickCheckBos(index,true)
  33. end)
  34. -- 子节点响应点击事件
  35. local children = checkBox:getChildren();
  36. for i, k in pairs(children) do
  37. k:setTouchEnabled(true)
  38. k:registerClick(function()
  39. self:onClickCheckBos(index,false)
  40. end)
  41. end
  42. end
  43. -- 设置默认状态
  44. function CheckBoxManager:setDefault(keyList)
  45. for idx, key in pairs(keyList) do
  46. for k,v in pairs(self.listCheckBox) do
  47. if key == v.key and v.node then
  48. v.node:setSelectedState(true)
  49. local children = v.node:getChildren();
  50. for i, k in pairs(children) do
  51. if self.colorSwitch then
  52. k:setTextColor(selectColor)
  53. end
  54. end
  55. end
  56. end
  57. end
  58. end
  59. --[[
  60. 返回Key和当前checkbox状态对应的值
  61. ]]
  62. function CheckBoxManager:getResult()
  63. local tt = {}
  64. for k,v in pairs(self.listCheckBox) do
  65. if v.node then
  66. tt[v.key] = {}
  67. if v.node:getSelectedState() then
  68. tt[v.key].value = v.selectValue
  69. tt[v.key].key = v.key
  70. else
  71. tt[v.key].value = v.noSelectValue
  72. tt[v.key].key = nil
  73. end
  74. end
  75. end
  76. return tt;
  77. end
  78. function CheckBoxManager:getResult2()
  79. local tt = {}
  80. for k,v in pairs(self.listCheckBox) do
  81. if v.node then
  82. if v.node:getSelectedState() then
  83. tt[v.key] = v.selectValue
  84. else
  85. tt[v.key] = v.noSelectValue
  86. end
  87. end
  88. end
  89. return tt;
  90. end
  91. -- 添加点击时的回调
  92. function CheckBoxManager:setCallback(callback)
  93. self.callback = callback;
  94. end
  95. --
  96. function CheckBoxManager:onClickCheckBos(index, fromTouch)
  97. --[[if fromTouch then
  98. playBtnEffect()
  99. end--]]
  100. playBtnEffect()
  101. self.curCheckBox = index
  102. if not fromTouch then
  103. local state = self.listCheckBox[self.curCheckBox].node:getSelectedState()
  104. self.listCheckBox[self.curCheckBox].node:setSelectedState(not state)
  105. local children = self.listCheckBox[self.curCheckBox].node:getChildren();
  106. for i, k in pairs(children) do
  107. if not state then
  108. if self.colorSwitch then
  109. k:setTextColor(selectColor)
  110. end
  111. else
  112. if self.colorSwitch then
  113. k:setTextColor(normalColor)
  114. end
  115. end
  116. end
  117. else
  118. local state = self.listCheckBox[self.curCheckBox].node:getSelectedState()
  119. local children = self.listCheckBox[self.curCheckBox].node:getChildren();
  120. for i, k in pairs(children) do
  121. if not state then
  122. if self.colorSwitch then
  123. k:setTextColor(selectColor)
  124. end
  125. else
  126. if self.colorSwitch then
  127. k:setTextColor(normalColor)
  128. end
  129. end
  130. end
  131. end
  132. local state = self.listCheckBox[self.curCheckBox].node:getSelectedState()
  133. -- 回调
  134. runDelay(0.1,function ()
  135. if self.callback then
  136. self.callback()
  137. end
  138. end)
  139. end
  140. function CheckBoxManager:setSelectedState(key,state)
  141. for k,v in pairs(self.listCheckBox) do
  142. if key == v.key and v.node then
  143. v.node:setSelectedState(state)
  144. local children = v.node:getChildren();
  145. for i, k in pairs(children) do
  146. if self.colorSwitch then
  147. k:setTextColor(state and selectColor or normalColor)
  148. end
  149. end
  150. end
  151. end
  152. end
  153. return CheckBoxManager