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.

199 lines
5.1 KiB

  1. local RadioManager = class("RadioManager")
  2. --[[
  3. 单选框管理器
  4. 使用方法:
  5. 使用接口addItem,传入 checkBox 及对应的值,
  6. 不需要关注每个 checkBox 的点击事件
  7. 玩家操作完之后,使用接口 getResult 获取最后的结果
  8. 返回玩家最后选择的 checkBox 对应的值
  9. ]]--
  10. function RadioManager:ctor()
  11. self.curCheckBox = 0
  12. self.numCheckBox = 0;
  13. self.listCheckBox = {}
  14. self.callback = nil
  15. self.colorSwitch = true
  16. self.normalColor = cc.c4b(21,99,97,255)
  17. self.selectColor = cc.c4b(213,46,11,255)
  18. self.disableColor = cc.c4b(128,128,128,255)
  19. end
  20. function RadioManager:addItem(checkBox, value)
  21. if not checkBox then
  22. if cc.Application:getInstance():getTargetPlatform() == 0 then
  23. showTooltip("节点不存在")
  24. end
  25. return
  26. end
  27. -- 数量自增
  28. self.numCheckBox = self.numCheckBox + 1
  29. local index = self.numCheckBox;
  30. self.listCheckBox[index] = {node = checkBox, value = value};
  31. -- 注册点击事件
  32. checkBox:registerClick(function() self:onClickCheckBos(index, true) end)
  33. -- 子节点响应点击事件
  34. local children = checkBox:getChildren();
  35. for i, k in pairs(children) do
  36. k:setTouchEnabled(true)
  37. k:registerClick(function()
  38. playBtnEffect()
  39. self:onClickCheckBos(index, false)
  40. end)
  41. end
  42. end
  43. -- 设置默认状态
  44. -- 默认只能选中一个
  45. function RadioManager:setDefault(value)
  46. local isValue = false
  47. for idx, checkbox in pairs(self.listCheckBox) do
  48. if checkbox.value == value then
  49. isValue = true
  50. checkbox.node:setSelectedState(true);
  51. self:onClickCheckBos(idx, false)
  52. return;
  53. end
  54. end
  55. --判断默认值存在不,不存在,用默认
  56. if not isValue then
  57. for idx, checkbox in pairs(self.listCheckBox) do
  58. if idx == 1 then
  59. checkbox.node:setSelectedState(true)
  60. self:onClickCheckBos(idx, false)
  61. return;
  62. end
  63. end
  64. end
  65. end
  66. -- 添加点击时的回调
  67. function RadioManager:setCallback(callback)
  68. self.callback = callback;
  69. end
  70. -- 添加点击时的回调
  71. function RadioManager:setColorSwitch(colorSwitch)
  72. self.colorSwitch = colorSwitch;
  73. end
  74. function RadioManager:setSelectColor(selectColor)
  75. self.selectColor = selectColor;
  76. end
  77. function RadioManager:setNormalColor(normalColor)
  78. self.normalColor = normalColor;
  79. end
  80. function RadioManager:setDisableColor(disableColor)
  81. self.disableColor = disableColor;
  82. end
  83. function RadioManager:onClickCheckBos(index, fromTouch)
  84. if fromTouch then
  85. playBtnEffect()
  86. end
  87. if index == self.curCheckBox then
  88. if fromTouch then
  89. self.listCheckBox[self.curCheckBox].node:setSelectedState(false, true)
  90. end
  91. -- 回调
  92. if self.callback then
  93. self.callback(self.listCheckBox[self.curCheckBox].value,self.listCheckBox[self.curCheckBox].node)
  94. end
  95. return
  96. end
  97. if self.curCheckBox and self.listCheckBox[self.curCheckBox] and self.listCheckBox[self.curCheckBox].node then
  98. self.listCheckBox[self.curCheckBox].node:setSelectedState(false)
  99. local children = self.listCheckBox[self.curCheckBox].node:getChildren();
  100. if self.colorSwitch then
  101. for i, k in pairs(children) do
  102. if k.setTextColor then
  103. k:setTextColor(self.normalColor)
  104. end
  105. end
  106. end
  107. self.curCheckBox = nil
  108. end
  109. self.curCheckBox = index
  110. if self.curCheckBox and self.listCheckBox[self.curCheckBox] and self.listCheckBox[self.curCheckBox].node then
  111. if not fromTouch then
  112. self.listCheckBox[self.curCheckBox].node:setSelectedState(true)
  113. local children = self.listCheckBox[self.curCheckBox].node:getChildren();
  114. if self.colorSwitch then
  115. for i, k in pairs(children) do
  116. if k.setTextColor then
  117. k:setTextColor(self.selectColor)
  118. end
  119. end
  120. end
  121. else
  122. local children = self.listCheckBox[self.curCheckBox].node:getChildren();
  123. if self.colorSwitch then
  124. for i, k in pairs(children) do
  125. if k.setTextColor then
  126. k:setTextColor(self.selectColor)
  127. end
  128. end
  129. end
  130. -- 要被逼疯....
  131. -- 因为手动点击的时候,C++代码中会将选中状态改变,而脚本这边捕获不到改变
  132. -- 所以这里需要先设为选中,并更新选中状态
  133. -- 在设为未选中个,不更新选中状态
  134. -- 再等待C++改变状态
  135. self.listCheckBox[self.curCheckBox].node:setSelectedState(true)
  136. self.listCheckBox[self.curCheckBox].node:setSelectedState(false, true)
  137. end
  138. end
  139. -- 回调
  140. if self.callback then
  141. self.callback(self.listCheckBox[self.curCheckBox].value, self.listCheckBox[self.curCheckBox].node)
  142. end
  143. end
  144. function RadioManager:getResult()
  145. if self.curCheckBox and self.listCheckBox[self.curCheckBox] and self.listCheckBox[self.curCheckBox].value then
  146. return self.listCheckBox[self.curCheckBox].value
  147. else
  148. return nil
  149. end
  150. end
  151. function RadioManager:setEnabled(b)
  152. for k,v in pairs(self.listCheckBox) do
  153. v.node:setEnabled(b)
  154. local children = v.node:getChildren()
  155. for _, c in pairs(children) do
  156. if c.setTextColor then
  157. if b then
  158. if self.curCheckBox==k then
  159. c:setTextColor(self.selectColor)
  160. else
  161. c:setTextColor(self.normalColor)
  162. end
  163. else
  164. c:setTextColor(self.disableColor)
  165. end
  166. end
  167. end
  168. end
  169. end
  170. return RadioManager