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

246 lines
6.5 KiB

пре 4 недеља
  1. --[[
  2. @description:item样式的垂直TabBar
  3. @example:
  4. code:
  5. local test_tab_data = { --选项卡配置, main_tab 代表一级选项卡,sub_tab 代表二级选项卡
  6. [1] = {main_tab = "主Tab1", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}},
  7. [2] = {main_tab = "主Tab2", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}},
  8. [3] = {main_tab = "主Tab3", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}},
  9. }
  10. local call_back = function (index, sub_index) --回调函数
  11. end
  12. self.verTabBar = NewUICusVerTabBar.New(self.scroll_content, sizeDelta, item)
  13. self.verTabBar:SetTabBars(test_tab_data)
  14. self.verTabBar:SetSelectTab(1, 2)
  15. ]]
  16. NewUICusVerTabBar = NewUICusVerTabBar or BaseClass()
  17. --[[
  18. item:item
  19. tab_btn_size:
  20. call_back
  21. ]]
  22. function NewUICusVerTabBar:__init(parent_wnd, sizeDelta, item, tab_btn_size,child_offset)
  23. self.parent_wnd = parent_wnd
  24. self.btn_list = {}
  25. self.item = item
  26. self.tab_btn_size = tab_btn_size
  27. self.child_offset = child_offset
  28. self.index = 0
  29. self.sub_index = 0
  30. self.last_index = 0
  31. self.last_sub_index = 0
  32. self:Load(sizeDelta)
  33. end
  34. function NewUICusVerTabBar:__delete()
  35. for i,v in ipairs(self.btn_list) do
  36. v:DeleteMe()
  37. v = nil
  38. end
  39. self.btn_list = {}
  40. destroy(self.scroll_view)
  41. end
  42. function NewUICusVerTabBar:Load(sizeDelta)
  43. self.scroll_view = UiFactory.createChild(self.parent_wnd, UIType.ScrollView, "bar_scroll")
  44. self.scroll_view.transform.pivot = Vector2(0.5, 0.5)
  45. self.scroll_view.transform.anchoredPosition = Vector2(0, 0)
  46. self.scroll_view.transform.sizeDelta = sizeDelta and sizeDelta or Vector2(202, 561)
  47. self.scroll_view_content = self.scroll_view.transform:Find("Viewport/Content")
  48. self.scroll_view_viewport = self.scroll_view.transform:Find("Viewport")
  49. self:InitEvent()
  50. end
  51. function NewUICusVerTabBar:InitEvent()
  52. end
  53. --设置选项卡数据
  54. function NewUICusVerTabBar:SetTabBars(bar_list, call_back, select_anything, force_size)
  55. if bar_list == nil or TableSize(bar_list) == 0 then return end
  56. --先隐藏所有菜单
  57. for i,v in ipairs(self.btn_list) do
  58. v:SetVisible(false)
  59. end
  60. for i,v in ipairs(bar_list) do
  61. local tab_btn = self.btn_list[i]
  62. if tab_btn == nil then
  63. tab_btn = NewUICusVerTabBtn.New(self.scroll_view_content, self.item, self.tab_btn_size,self,self.child_offset)
  64. self.btn_list[i] = tab_btn
  65. end
  66. tab_btn:SetTabDetail(v, i, call_back, select_anything, force_size)
  67. tab_btn:SetSubOffset(self.offset)
  68. tab_btn:SetVisible(true)
  69. local function onBtnClickHandler(target)
  70. if call_back then
  71. call_back(tab_btn.index,0)
  72. end
  73. self:SetSelectTab(tab_btn.index)
  74. end
  75. if tab_btn.tab then
  76. AddClickEvent(tab_btn.tab, onBtnClickHandler,2)
  77. end
  78. end
  79. self:RefreshTabPos()
  80. end
  81. --更新tabBar的位置
  82. function NewUICusVerTabBar:RefreshTabPos()
  83. local gap = 0
  84. local btn_height
  85. for i,v in ipairs(self.btn_list) do
  86. btn_height = v.btn_height
  87. if v.show == true then
  88. local off = self.offset and (v:GetVisibleCount(true) - 1) * self.offset.y or 0
  89. local h = v:GetVisibleCount(true) * self.item.Height + off
  90. v:SetPosition(0, -gap)
  91. --v:SetSubConSize(h)
  92. gap = gap + v.btn_height + h + 4
  93. else
  94. v:SetPosition(0, -gap)
  95. gap = gap + v.btn_height + 4
  96. end
  97. end
  98. self.scroll_view_content.sizeDelta = Vector2(self.scroll_view_content.sizeDelta.x, gap) -- +40?
  99. if self.sub_index and self.index and self.sub_index >= 5 then
  100. local all_height = self.item.Height * 4 + (self.index - 1) * btn_height
  101. self.scroll_view_content.localPosition = Vector3(0,all_height,0)
  102. else
  103. self.scroll_view_content.localPosition = Vector3(0,0,0)
  104. end
  105. end
  106. --设置选中的选项卡
  107. function NewUICusVerTabBar:SetSelectTab(index, sub_index,force_show)
  108. self.sub_index = sub_index
  109. self.index = index
  110. for i,v in ipairs(self.btn_list) do
  111. -- if v.index and v.index == index then
  112. v:SetSelectMode(index, sub_index, force_show)
  113. -- end
  114. end
  115. self:RefreshTabPos()
  116. end
  117. function NewUICusVerTabBar:SetTabBarIndex(index,sub_index)
  118. self.sub_index = sub_index or 0
  119. self.index = index or 0
  120. local item,sub_item
  121. if sub_index and sub_index > 0 then
  122. item = self.btn_list[self.last_index]
  123. if item then
  124. sub_item = item.sub_btn_list[self.last_sub_index]
  125. if sub_item then
  126. sub_item:SetSelected(false)
  127. end
  128. end
  129. self.last_index = index
  130. self.last_sub_index = sub_index
  131. end
  132. item = self.btn_list[self.index]
  133. if item then
  134. sub_item = item.sub_btn_list[self.sub_index]
  135. if sub_item then
  136. sub_item:SetSelected(true)
  137. end
  138. end
  139. end
  140. function NewUICusVerTabBar:GetCurrentSelectIndex()
  141. return self.index,self.sub_index
  142. end
  143. --设置选中的选项卡
  144. -- function NewUICusVerTabBar:SetSelectTab(index, sub_index,force_show)
  145. -- for i,v in ipairs(self.btn_list) do
  146. -- if v.index and v.index == index then
  147. -- if not v.show then
  148. -- self:CancelSetSelectTab(index)
  149. -- end
  150. -- v:SetSelectMode(index, sub_index, force_show, self.quick_select)
  151. -- break
  152. -- end
  153. -- end
  154. -- self:RefreshTabPos(index, sub_index)
  155. -- end
  156. --取消当前的选中项
  157. function NewUICusVerTabBar:CancelCurrSelect()
  158. if self.last_sub_index > 0 then
  159. local item,sub_item
  160. item = self.btn_list[self.last_index]
  161. if item then
  162. sub_item = item.sub_btn_list[self.last_sub_index]
  163. if sub_item then
  164. sub_item:SetSelected(false)
  165. end
  166. end
  167. self.last_index = 0
  168. self.last_sub_index = 0
  169. end
  170. end
  171. function NewUICusVerTabBar:SetScrollViewSize(width,heigh)
  172. self.scroll_view.transform.sizeDelta = Vector2(width,heigh)
  173. end
  174. function NewUICusVerTabBar:RefreshTabBtnState(index)
  175. for i,v in ipairs(self.btn_list) do
  176. if v.index == index then
  177. v:RefreshBtnState(v.show)
  178. end
  179. end
  180. self:RefreshTabPos()
  181. end
  182. function NewUICusVerTabBar:GetTabBtnShowState(index)
  183. for i,v in ipairs(self.btn_list) do
  184. if v.index == index then
  185. return v.show
  186. end
  187. end
  188. end
  189. --在回调函数中调用该函数,刷新选中状态
  190. function NewUICusVerTabBar:UpdateSelectState(index, sub_index)
  191. for i,v in ipairs(self.btn_list) do
  192. v:SetSelectState(false)
  193. for key,value in ipairs(v.sub_btn_list) do
  194. value:SetSelectState(false)
  195. end
  196. end
  197. if sub_index == 0 then
  198. self.btn_list[index]:SetSelectState(true)
  199. else
  200. self.btn_list[index].sub_btn_list[sub_index]:SetSelectState(true)
  201. end
  202. end
  203. --传入主索引和所有要显示的子索引列表,取消红点只传入主索引
  204. function NewUICusVerTabBar:ShowRedDot(index, index_list)
  205. if self.btn_list[index] and self.btn_list[index].ShowRedDot then
  206. self.btn_list[index]:ShowRedDot(index_list)
  207. end
  208. end
  209. function NewUICusVerTabBar:SetSubOffset(offset)
  210. self.offset = offset
  211. end