源战役客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

272 lignes
8.3 KiB

il y a 4 semaines
  1. --[[
  2. @description:
  3. @example:
  4. local data = {["_1"] = {name = "一级选项卡1", ...}, ["_1_1"] = {name = "二级选项卡1", ...}}
  5. or
  6. local data = {["_1"] = "一级选项卡1", ["_1_1"] = "二级选项卡1"}
  7. self.tabs = CustomVerTabs.New(self.tab_parent_tra, co.TableXY(265, 599),
  8. CusVerMainTabsTemplate, co.TableXY(256, 70), co.TableXY(0, 6), co.TableXY(0, 3),
  9. co.TableXY(0, 3), UIVerTabSubBtn, co.TableXY(0, 3))
  10. local function call_back(main_index, sub_index)
  11. end
  12. self.tabs:SetTabs(data, call_back, true, true)
  13. --]]
  14. CustomVerTabs = CustomVerTabs or BaseClass(BaseComponent)
  15. local CustomVerTabs = CustomVerTabs
  16. local SetSizeDelta = SetSizeDelta
  17. local SetAnchoredPosition = SetAnchoredPosition
  18. --[[
  19. @scroll_rect:
  20. @main_tabs_class:
  21. @main_tabs_btnSize
  22. @main_tabs_padding
  23. @main_tabs_spacing:
  24. @subCon_offset
  25. @sub_tabs_class:
  26. @sub_tabs_spacing:
  27. --]]
  28. function CustomVerTabs:__init(parent_transform, scroll_rect, main_tabs_class, main_tabs_btnSize, main_tabs_padding, main_tabs_spacing, subCon_offset, sub_tabs_class, sub_tabs_spacing, arrow_pos)
  29. self.scroll_rect = scroll_rect or co.TableXY(202, 561)
  30. self.main_tabs_class = main_tabs_class
  31. self.main_tabs_btnSize = main_tabs_btnSize or co.TableXY(189, 66)
  32. self.main_tabs_padding = main_tabs_padding or co.TableXY(0, 0)
  33. self.main_tabs_spacing = main_tabs_spacing or co.TableXY(0, 0)
  34. self.subCon_offset = subCon_offset
  35. self.arrow_pos = arrow_pos
  36. self.sub_tabs_class = sub_tabs_class
  37. self.sub_tabs_spacing = sub_tabs_spacing
  38. self.quick_select = true
  39. self.select_main_index = nil
  40. self.btn_list = {}
  41. self.btn_load_complete = false
  42. self:CreateGameObject(UIType.ScrollView, "ScrollView")
  43. end
  44. function CustomVerTabs:__delete()
  45. for i,v in ipairs(self.btn_list) do
  46. v:DeleteMe()
  47. v = nil
  48. end
  49. self.btn_list = {}
  50. self:CancelTimer()
  51. end
  52. function CustomVerTabs:LoadSuccess()
  53. local vec2 = Vector2(0, 1)
  54. self.transform.anchorMax = vec2
  55. self.transform.anchorMin = vec2
  56. self.transform.pivot = vec2
  57. SetAnchoredPosition(self.transform, 0, 0)
  58. SetSizeDelta(self.transform, self.scroll_rect.x, self.scroll_rect.y)
  59. self.scroll_view_content = self:GetChild("Viewport/Content")
  60. self.viewport_mask = self:GetChild("Viewport"):GetComponent("Mask")
  61. self.viewport_image = self:GetChild("Viewport"):GetComponent("Image")
  62. end
  63. function CustomVerTabs:SetTabs(bar_list, call_back, select_anything, force_size, quick_select)
  64. if bar_list == nil or TableSize(bar_list) == 0 then return end
  65. self.quick_select = quick_select == nil and true or quick_select
  66. self:ResetTabsIndex()
  67. self:RefreshTabsList(bar_list, call_back, select_anything, force_size)
  68. --self:RefreshTabsPos()
  69. self:ResetTabsConPos()
  70. end
  71. function CustomVerTabs:ResetTabsIndex()
  72. if self.select_main_index then
  73. self:CancelSelectTabs(self.select_main_index)
  74. self:RefreshTabsPos()
  75. end
  76. self.select_main_index = nil
  77. end
  78. function CustomVerTabs:RefreshTabsList(bar_list, call_back, select_anything, force_size)
  79. self:CancelTimer()
  80. local count = 0
  81. local gap_y = self.main_tabs_padding.y
  82. local gap_x = self.main_tabs_padding.x + self.main_tabs_spacing.x
  83. local function refresh_view_func()
  84. if self._use_delete_method then
  85. return
  86. end
  87. count = count + 1
  88. local index = count
  89. local tabs_index = "_" .. index
  90. if bar_list[tabs_index] then
  91. local tabs_btn = self.btn_list[index]
  92. if tabs_btn == nil then
  93. tabs_btn = self.main_tabs_class.New(self.scroll_view_content, self.main_tabs_btnSize, self.subCon_offset, self.sub_tabs_class, self.sub_tabs_spacing, self.arrow_pos)
  94. self.btn_list[index] = tabs_btn
  95. end
  96. tabs_btn:SetTabsDetail(bar_list, index, call_back, select_anything, force_size)
  97. tabs_btn:SetVisible(true)
  98. local function onBtnClickHandler(target)
  99. self:SetSelectTabs(index)
  100. end
  101. if tabs_btn.button then
  102. AddClickEvent(tabs_btn.button, onBtnClickHandler, 2)
  103. end
  104. tabs_btn:SetPosition(gap_x, -gap_y)
  105. gap_y = gap_y + tabs_btn.btn_height + self.main_tabs_spacing.y
  106. else
  107. for i = count, #self.btn_list do
  108. self.btn_list[i]:SetVisible(false)
  109. end
  110. self:RefreshTabsPos()
  111. self:CancelTimer()
  112. self.btn_load_complete = true
  113. if self.redDot_list then
  114. for k, v in pairs(self.redDot_list) do
  115. self:ShowRedDot(k, v)
  116. end
  117. self.redDot_list = nil
  118. end
  119. if self.cache_select then
  120. self.cache_select = false
  121. self:SetSelectTabs(self.cache_main_index,self.cache_sub_index,self.cache_force_show)
  122. end
  123. end
  124. end
  125. self.view_timer_id = GlobalTimerQuest:AddPeriodQuest(refresh_view_func,0.04)
  126. refresh_view_func()
  127. end
  128. function CustomVerTabs:CancelTimer()
  129. if self.view_timer_id then
  130. GlobalTimerQuest:CancelQuest(self.view_timer_id)
  131. self.view_timer_id = nil
  132. end
  133. end
  134. function CustomVerTabs:SetSelectTabs(main_index, sub_index, force_show)
  135. if not self.btn_load_complete then
  136. self.cache_select = true
  137. self.cache_main_index = main_index
  138. self.cache_sub_index = sub_index
  139. self.cache_force_show = force_show
  140. return
  141. end
  142. --print("CustomVerTabs:SetSelectTabs() main = "..tostring(main_index)..", sub = "..tostring(sub_index))
  143. for i,v in ipairs(self.btn_list) do
  144. if v.index and v.index == main_index then
  145. if self.select_main_index ~= main_index and self.select_main_index ~= nil then
  146. self:CancelSelectTabs(self.select_main_index)
  147. end
  148. self.select_main_index = main_index
  149. v:SetSelectMode(main_index, sub_index, force_show, self.quick_select)
  150. sub_index = v.select_sub_index
  151. break
  152. end
  153. end
  154. self:RefreshTabsPos(main_index, sub_index)
  155. end
  156. function CustomVerTabs:CancelSelectTabs(index)
  157. if self.btn_list[index] then
  158. self.btn_list[index]:RefreshTabsSelectState(false)
  159. self.btn_list[index]:SetSelectSubIndex(0, 0)
  160. end
  161. --[[if index and index * self.main_tabs_btnSize.y > self.transform.sizeDelta.y / 2 then
  162. SetAnchoredPosition(self.scroll_view_content, 0, (index - 1) * self.main_tabs_btnSize.y)
  163. else
  164. SetAnchoredPosition(self.scroll_view_content, 0, 0)
  165. end--]]
  166. end
  167. --更新tabBar的位置
  168. function CustomVerTabs:RefreshTabsPos(main_index, sub_index)
  169. local gap_y = self.main_tabs_padding.y
  170. local gap_x = self.main_tabs_padding.x + self.main_tabs_spacing.x
  171. local sub_tabs_height = self.sub_tabs_class and self.sub_tabs_class.real_height or 0
  172. local subCon_offset_y = self.subCon_offset and self.subCon_offset.y or 0
  173. local btn_height, off, h, sub_tabs_count
  174. for i,v in ipairs(self.btn_list) do
  175. if v:GetVisible() then
  176. btn_height = v.btn_height
  177. if v.show == true then
  178. sub_tabs_count = v:GetVisibleCount(true)
  179. off = self.sub_tabs_spacing and (sub_tabs_count - 1) * self.sub_tabs_spacing.y or 0
  180. off = off < 0 and 0 or off + subCon_offset_y
  181. h = sub_tabs_count * sub_tabs_height + off
  182. v:SetPosition(gap_x, -gap_y)
  183. gap_y = gap_y + btn_height + self.main_tabs_spacing.y + h
  184. else
  185. v:SetPosition(gap_x, -gap_y)
  186. gap_y = gap_y + btn_height + self.main_tabs_spacing.y
  187. end
  188. end
  189. end
  190. SetSizeDelta(self.scroll_view_content, 0, gap_y + 40)
  191. if main_index and sub_index then
  192. sub_index = sub_index > 0 and sub_index - 1 or 0
  193. --print("CustomVerTabs:RefreshTabsPos()",main_index,sub_index)
  194. local y_1 = (main_index - 1) * self.main_tabs_btnSize.y + sub_index * self.sub_tabs_class.Height
  195. local y_2 = self.scroll_view_content.sizeDelta.y - self.transform.sizeDelta.y
  196. if y_2 < self.main_tabs_btnSize.y then
  197. y_2 = 0
  198. end
  199. SetAnchoredPosition(self.scroll_view_content, 0, y_1 > y_2 and y_2 or y_1)
  200. end
  201. end
  202. function CustomVerTabs:ResetTabsConPos()
  203. SetAnchoredPosition(self.scroll_view_content, 0, 0)
  204. end
  205. function CustomVerTabs:CancelAllRedDot()
  206. for i, v in pairs(self.btn_list) do
  207. v:ShowRedDot()
  208. end
  209. end
  210. function CustomVerTabs:ShowRedDot(index, index_list)
  211. if not self.btn_load_complete then
  212. self.redDot_list = self.redDot_list or {}
  213. self.redDot_list[index] = index_list
  214. return
  215. end
  216. if self.btn_list[index] and self.btn_list[index].ShowRedDot then
  217. self.btn_list[index]:ShowRedDot(index_list)
  218. end
  219. end
  220. -- 设置不规则遮罩显示
  221. function CustomVerTabs:SetMaskImage(res)
  222. lua_resM:setOutsideImageSprite(self,self.viewport_image,res,false,function ()
  223. self.viewport_mask.showMaskGraphic = true
  224. end)
  225. end
  226. function CustomVerTabs:SetDefaultSelected(index)
  227. self:SetSelectTabs(index)
  228. end