源战役客户端
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.

269 lines
7.3 KiB

  1. --tabbar按钮(单个按钮)
  2. UICusVerTabBtn = UICusVerTabBtn or BaseClass(BaseComponent)
  3. -- UICusVerTabBtn.BTN_HEIGHT = 66 --tab按钮的高度,不包括下一级tab按钮的容器高度
  4. -- UICusVerTabBtn.SUB_CON_WIDTH = 178 --下一级tab按钮的容器宽度
  5. function UICusVerTabBtn:__init(parent, sub_item, size, sub_offset)
  6. self.sub_btn_list = {}
  7. self.index = 1
  8. self.call_back = nil
  9. self.show = false --是否展开
  10. self.select_anything = false --是否可以在有子类的时候选中大类
  11. self.btn_width = size and size.x or 189 --tab按钮的宽度,不包括下一级tab按钮的容器宽度
  12. self.btn_height = size and size.y or 66 --tab按钮的高度,不包括下一级tab按钮的容器高度
  13. self.sub_con_width = size and size.x - 16 or 178 --下一级tab按钮的容器宽度
  14. self.sub_item = sub_item
  15. self.sub_offset = sub_offset
  16. self:CreateGameObject(UIType.UIVerTabBtn)
  17. end
  18. function UICusVerTabBtn:__delete()
  19. for i,v in ipairs(self.sub_btn_list) do
  20. v:DeleteMe()
  21. v = nil
  22. end
  23. self.sub_btn_list = {}
  24. if self.event_id then
  25. GlobalEventSystem:UnBind(self.event_id)
  26. self.event_id = nil
  27. end
  28. end
  29. function UICusVerTabBtn:LoadSuccess()
  30. self.btnText = self:GetChild("tab/Text"):GetComponent("Text")
  31. self.btn_tran = self:GetChild("tab/Text")
  32. self.icon = self:GetChild("tab/icon"):GetComponent("Image")
  33. self.icon_tran = self:GetChild("tab/icon")
  34. self.imgSelect = self:GetChild("tab/selectBg").gameObject
  35. self.iconObj = self:GetChild("tab/icon").gameObject
  36. self.addBtn = self:GetChild("subCon").gameObject
  37. self.tab = self:GetChild("tab").gameObject
  38. self.tab_img = self:GetChild("tab")
  39. self.subCon = self:GetChild("subCon")
  40. self.img = self:GetChild("tab/img").gameObject
  41. self.red_dot = self:GetChild("tab/dot").gameObject
  42. self:ResetSize()
  43. self:InitEvent()
  44. end
  45. function UICusVerTabBtn:InitEvent()
  46. end
  47. --设置tab数据
  48. function UICusVerTabBtn:SetTabDetail(tab_data, index, call_back, select_anything, force_size)
  49. self.sub_tab_data = {}
  50. self.index = index
  51. self.call_back = call_back
  52. self.select_anything = select_anything
  53. self.force_size = force_size
  54. if tab_data == nil then return end
  55. local tab_index = "_"..self.index
  56. if type(tab_data[tab_index]) == "table" then --主菜单名称
  57. self.btnText.text = tab_data[tab_index].name
  58. elseif type(tab_data[tab_index]) == "string" then
  59. self.btnText.text = tab_data[tab_index]
  60. end
  61. for i = 1, TableSize(tab_data) do
  62. tab_index = "_"..self.index.."_"..i
  63. if not tab_data[tab_index] then break end
  64. table.insert(self.sub_tab_data, tab_data[tab_index])
  65. end
  66. --是否显示展开标志
  67. if #self.sub_tab_data == 0 then
  68. self.iconObj:SetActive(false)
  69. else
  70. self.iconObj:SetActive(true)
  71. end
  72. self:SetSubBtns(self.sub_tab_data, self.index, call_back)
  73. end
  74. --设置下一级的按钮
  75. function UICusVerTabBtn:SetSubBtns(sub_tab_list, parent_index, call_back)
  76. for i,v in ipairs(self.sub_btn_list) do
  77. v:SetVisible(false)
  78. end
  79. for i,v in ipairs(sub_tab_list) do
  80. local sub_tab = self.sub_btn_list[i]
  81. if sub_tab == nil then
  82. sub_tab = self.sub_item.New(self.subCon)
  83. self.sub_btn_list[i] = sub_tab
  84. if self.force_size then
  85. sub_tab:SetTabSubBtnSize(self.sub_con_width)
  86. end
  87. end
  88. sub_tab:SetSubDeail(v, parent_index, i, call_back)
  89. sub_tab:SetVisible(true)
  90. local x = 0
  91. local y = -(i - 1) * self.sub_item.Height
  92. x = self.offset and self.offset.x + x or x
  93. y = self.offset and -(i - 1) * self.offset.y + y or y
  94. sub_tab:SetPosition(x, y)
  95. local function onBtnClickHandler(target)
  96. self:SetSelectSubIndex(0, 0)
  97. self:SetSelectSubIndex(sub_tab.parent_index, sub_tab.sub_index)
  98. end
  99. if sub_tab.Button then
  100. AddClickEvent(sub_tab.Button, onBtnClickHandler,2)
  101. end
  102. end
  103. self.subCon.gameObject:SetActive(false)
  104. end
  105. --选中菜单
  106. function UICusVerTabBtn:SetSelectMode(index, sub_index,force_show)
  107. if self.index == index then
  108. self.show = not self.show
  109. else
  110. self.show = false
  111. end
  112. if force_show and self.index == index then
  113. self.show = true
  114. end
  115. if self.show and index then
  116. SetRotate(self.icon_tran, 0, 0, -90)
  117. self:ShowSubBtnVisible(true)
  118. if sub_index then
  119. self:SetSelectSubIndex(index, sub_index)
  120. end
  121. else
  122. SetLocalRotation(self.icon_tran, 0, 0, 0)
  123. self:ShowSubBtnVisible(false)
  124. end
  125. end
  126. --设置图标,隐藏sub
  127. function UICusVerTabBtn:RefreshBtnState(show)
  128. self.show = show
  129. if self.show then
  130. SetRotate(self.icon_tran, 0, 0, -90)
  131. --lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_minus",true)
  132. --self.icon_tran.localPosition = Vector3(self.btn_width- 26, -self.btn_height / 2,0)
  133. else
  134. SetLocalRotation(self.icon_tran, 0, 0, 0)
  135. --lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_addicon",true)
  136. --self.icon_tran.localPosition = Vector3(self.btn_width- 26, -self.btn_height / 2 + 7,0)
  137. end
  138. if self.subCon and self.sub_btn_list and #self.sub_btn_list ~= 0 then
  139. self.subCon.gameObject:SetActive(show)
  140. end
  141. end
  142. --设置选中的子一级tab
  143. function UICusVerTabBtn:SetSelectSubIndex(main_index, sub_index)
  144. for i,v in ipairs(self.sub_btn_list) do
  145. if v.parent_index == main_index and v.sub_index == sub_index then
  146. v:SetSelected(true, true)
  147. else
  148. v:SetSelected(false)
  149. end
  150. end
  151. end
  152. function UICusVerTabBtn:SetText(str)
  153. self.btnText.text = str
  154. end
  155. function UICusVerTabBtn:ShowSubBtnVisible(bool)
  156. if self.subCon and self.sub_btn_list and #self.sub_btn_list ~= 0 then
  157. self.subCon.gameObject:SetActive(bool)
  158. if self.select_anything then
  159. if self.call_back then self.call_back(self.index,0) end
  160. end
  161. else
  162. if self.call_back then self.call_back(self.index,0) end
  163. end
  164. end
  165. function UICusVerTabBtn:SetSubConSize(height)
  166. self.subCon.sizeDelta = Vector2(UICusVerTabBtn.SUB_CON_WIDTH, height)
  167. end
  168. --设置大小
  169. function UICusVerTabBtn:SetTabBtnSize()
  170. end
  171. function UICusVerTabBtn:SetSelectState(b)
  172. self.imgSelect:SetActive(b)
  173. end
  174. function UICusVerTabBtn:SetImageTexture(asset_path,res_name)
  175. if asset_path and res_name then
  176. self.img:SetActive(true)
  177. lua_resM:setImageSprite(self,self.img:GetComponent("Image"),asset_path,res_name)
  178. else
  179. self.img:SetActive(false)
  180. end
  181. end
  182. function UICusVerTabBtn:ShowRedDot(list)
  183. if list and #list > 0 then
  184. table.sort(list)
  185. end
  186. self.sub_red_list = list
  187. for i, v in ipairs(self.sub_btn_list) do
  188. v:ShowRedDot(false)
  189. end
  190. if not list or #list == 0 then
  191. self.red_dot:SetActive(false)
  192. else
  193. self.red_dot:SetActive(true)
  194. for i, v in ipairs(list) do
  195. if self.sub_btn_list[v] then
  196. self.sub_btn_list[v]:ShowRedDot(true)
  197. end
  198. end
  199. end
  200. if list[0] then
  201. self.red_dot:SetActive(true)
  202. end
  203. end
  204. function UICusVerTabBtn:SetSubOffset(offset)
  205. self.offset = offset
  206. end
  207. --加载完但不显示的不需计算高度
  208. function UICusVerTabBtn:GetVisibleCount(visible)
  209. local count = 0
  210. for _,v in ipairs(self.sub_btn_list) do
  211. if v:GetVisible() == visible or not v.is_loaded then
  212. count = count + 1
  213. end
  214. end
  215. return count
  216. end
  217. function UICusVerTabBtn:ResetSize()
  218. self.imgSelect.transform.sizeDelta = Vector2(self.btn_width + 12, self.btn_height + 12)
  219. self.imgSelect.transform.sizeDelta = Vector2(self.btn_width + 12, self.btn_height + 12)
  220. self.tab_img.sizeDelta = Vector2(self.btn_width, self.btn_height)
  221. --self.icon_tran.localPosition = Vector3(self.btn_width- 26, -self.btn_height / 2 + 7,0)
  222. if self.sub_offset then
  223. self.subCon.localPosition = Vector3(self.sub_offset.x, self.sub_offset.y,0)
  224. else
  225. self.subCon.localPosition = Vector3(self.subCon.localPosition.x, -self.btn_height,0)
  226. end
  227. end