SUIHelper.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. local SUIHelper = {}
  2. local SUIDefine = requireSUI("SUIDefine")
  3. local ssplit = string.split
  4. function SUIHelper:getInputTable(fliter)
  5. local submitTable = {}
  6. if fliter == nil or fliter == "" or fliter == "0" then
  7. elseif fliter == "*" then
  8. for k, v in pairs(self._inputWidgets) do
  9. local input = v
  10. local inputID = k
  11. local inputValue = input.widget and input.widget:getString() or ""
  12. table.insert(submitTable, {id = inputID, value = inputValue, type = input.inputFlag})
  13. end
  14. else
  15. local slices = ssplit(fliter, ",")
  16. for i, v in ipairs(slices) do
  17. local inputID = v
  18. local input = self._inputWidgets[inputID]
  19. local inputValue = input.widget and input.widget:getString() or ""
  20. table.insert(submitTable, {id = inputID, value = inputValue, type = input.inputFlag})
  21. end
  22. end
  23. return submitTable
  24. end
  25. function SUIHelper:getCheckBoxTable(fliter)
  26. local submitTable = {}
  27. if fliter == nil or fliter == "" or fliter == "0" then
  28. submitTable = {}
  29. else
  30. for k, v in pairs(self._checkboxWidgets) do
  31. local widget = v
  32. local widgetID = k
  33. local value = (widget:isSelected() and 1 or 0)
  34. table.insert(submitTable, {id = widgetID, value = value})
  35. end
  36. end
  37. return submitTable
  38. end
  39. function SUIHelper:getCheckBoxTableByID(widgetID)
  40. local submitTable = {}
  41. local widget = self._checkboxWidgets[widgetID]
  42. if not widget then
  43. submitTable = {}
  44. else
  45. local value = (widget:isSelected() and 1 or 0)
  46. table.insert(submitTable, {id = widgetID, value = value})
  47. end
  48. return submitTable
  49. end
  50. function SUIHelper:getITEMBOXTable(fliter)
  51. local submitTable = {}
  52. if fliter == nil or fliter == "" or fliter == "0" then
  53. submitTable = {}
  54. else
  55. for k, v in pairs(self._checkboxWidgets) do
  56. local widget = v
  57. local widgetID = k
  58. local value = (widget:isSelected() and 1 or 0)
  59. table.insert(submitTable, {id = widgetID, value = value})
  60. end
  61. end
  62. return submitTable
  63. end
  64. -- 转换成世界坐标系
  65. SUIHelper.convertToWorldPos = function(sender, input_pos)
  66. local parent = sender:getParent()
  67. local visibleSize = global.Director:getVisibleSize()
  68. local parentSize = parent and parent:getContentSize() or visibleSize
  69. local x = tonumber(input_pos.x) or 0
  70. local y = tonumber(input_pos.y) or 0
  71. local fixX = x
  72. local fixY = parentSize.height - y
  73. return cc.p(fixX, fixY)
  74. end
  75. -- 文本换行显示
  76. SUIHelper.fixText = function(text)
  77. -- trim && convert some special char
  78. text = string.gsub(text, "\\", "\r\n")
  79. text = string.trim(text)
  80. return text
  81. end
  82. -- 根据 colorID 设置文本颜色
  83. SUIHelper.convertToColorText = function(text, colorID)
  84. return string.format("<color=%s>%s</color>", GET_COLOR_BYID(colorID), text)
  85. end
  86. SUIHelper.modifySizeAble = function(type)
  87. return (type == "Text" or type == "TextAtlas" or type == "RichText") and false or true
  88. end
  89. -- 窗口是否可用
  90. SUIHelper.modifyWndEnable = function(attr)
  91. return attr.enable or true
  92. end
  93. -- 设置位置
  94. SUIHelper.setPosition = function(object, attr)
  95. object:setPosition(cc.p(tonumber(attr.x) or SUIDefine.defaultCUIUnitPosX, tonumber(attr.y) or SUIDefine.defaultCUIUnitPosY))
  96. end
  97. -- 设置大小
  98. SUIHelper.setSize = function(object, attr)
  99. local width = tonumber(attr.w) or 0
  100. local height = tonumber(attr.h) or 0
  101. if width > 0 and height > 0 then
  102. object:setContentSize({width = width, height = height})
  103. end
  104. end
  105. -- 设置描点
  106. SUIHelper.setAnchor = function(object, attr)
  107. object:setAnchorPoint(cc.p(tonumber(attr.ax) or SUIDefine.anchorPoints[2].x, tonumber(attr.ay) or SUIDefine.anchorPoints[2].y))
  108. end
  109. -- 旋转角度
  110. SUIHelper.setRotate = function(object, attr)
  111. if attr.rotate then
  112. local rotate = tonumber(attr.rotate) or 0
  113. object:setRotation(rotate)
  114. end
  115. end
  116. SUIHelper.GetStatus = function(status, default_status)
  117. local _status = status or default_status
  118. if type(_status) == "string" then
  119. local states = {
  120. ["true"] = true, ["false"] = false
  121. }
  122. _status = states[_status]
  123. end
  124. return _status
  125. end
  126. -- 是否Esc关闭
  127. SUIHelper.IsEscClose = function(esc_close)
  128. return SUIHelper.GetStatus(esc_close, true)
  129. end
  130. -- 设置窗口可见
  131. SUIHelper.IsVisible = function(visible)
  132. return SUIHelper.GetStatus(visible, true)
  133. end
  134. -- 是否是弹窗
  135. SUIHelper.IsWidghtPop = function (type)
  136. local _Pops = {
  137. ["dialog"] = 1
  138. }
  139. return false--_Pops[type] and true or false
  140. end
  141. -- 是否可拖动
  142. SUIHelper.IsDrag = function (tpye, drag)
  143. return SUIHelper.GetStatus(drag, SUIHelper.IsWidghtPop(tpye))
  144. end
  145. -- 是否可触摸(wnd、Button、ListView、CheckBox默认为true,其它窗口默认为false)
  146. SUIHelper.IsRevmsg = function (tpye, revmsg)
  147. local default = (tpye == "ScrollView" or tpye == "Button" or tpye == "ListView" or tpye == "CheckBox") and true or false
  148. return SUIHelper.GetStatus(revmsg, default)
  149. end
  150. -- 获取字体大小
  151. SUIHelper.getCFontSize = function (size)
  152. return tonumber(size) or SUIDefine.defaultFontSize
  153. end
  154. -- 获取颜色
  155. SUIHelper.getCColor = function (color, NDefault)
  156. if NDefault and not color then
  157. return nil
  158. end
  159. if tonumber(color) then
  160. return GET_COLOR_BYID_C3B(tonumber(color))
  161. end
  162. local slices = ssplit(color, ",")
  163. if #slices > 2 then
  164. return cc.c3b(tonumber(slices[1]), tonumber(slices[2]), tonumber(slices[3]))
  165. end
  166. if #slices > 0 then
  167. return GetColorFromHexString(slices[1])
  168. end
  169. return GET_COLOR_BYID_C3B(SUIDefine.defaultColorID)
  170. end
  171. -- 加载通用属性
  172. SUIHelper.loadCUIBaseAttr = function(unit)
  173. local render = unit._render
  174. if not render then
  175. return
  176. end
  177. local attr = unit:GetAttr()
  178. local type = unit:GetType()
  179. -- size
  180. if SUIHelper.modifySizeAble(type) then
  181. SUIHelper.setSize(render, attr)
  182. end
  183. -- anr
  184. SUIHelper.setAnchor(render, attr)
  185. -- rotate
  186. SUIHelper.setRotate(render, attr)
  187. -- x y
  188. SUIHelper.setPosition(render, attr)
  189. -- visible
  190. if SUIHelper.modifyWndEnable(attr) then
  191. render:setVisible(SUIHelper.IsVisible(attr.visible))
  192. end
  193. end
  194. -- 设置窗体是否可触摸(Image、Text、RichText默认为false,其它窗口默认为true)
  195. SUIHelper.setWndTouched = function(unit)
  196. local render = unit._render
  197. if render then
  198. local type = unit:GetType()
  199. local isTouch = (type == "Image" or type == "Text" or type == "RichText") and false or true
  200. render:setTouchEnabled(isTouch)
  201. end
  202. end
  203. -- 获取资源路径
  204. SUIHelper.getResFile = function(renderer)
  205. if not renderer then
  206. return "", 0
  207. end
  208. local sprite = renderer:getSprite()
  209. if not sprite then
  210. return "", 0
  211. end
  212. return sprite:getResourceName(), sprite:getResourceType()
  213. end
  214. ---------------------------Event-----------------------
  215. SUIHelper.addMouseOverTips = function(sender, attr)
  216. local tips = attr.tips
  217. local tipsx = tonumber(attr.tipsx) or 0
  218. local tipsy = tonumber(attr.tipsy) or 0
  219. if tips and string.len(tips) > 0 then
  220. -- 监听事件;
  221. tips = string.gsub(tips, "%^", "\\")
  222. sender:addMouseOverTips(tips, cc.p(tipsx, -tipsy), cc.p(0.5, 1))
  223. end
  224. end
  225. return SUIHelper