源战役客户端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

221 строка
6.3 KiB

4 недель назад
  1. --[[
  2. @description: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 = UIVerTabBar.New(self.scroll_content)
  13. self.verTabBar:SetTabBars(test_tab_data, call_back)
  14. self.verTabBar:SetSelectTab(1, 2)
  15. ]]
  16. UIVerTabBar = UIVerTabBar or BaseClass()
  17. local UIVerTabBar = UIVerTabBar
  18. function UIVerTabBar:__init(parent_wnd, sizeDelta, quick_select,type_id)
  19. self.parent_wnd = parent_wnd
  20. self.btn_list = {}
  21. self.cur_sub_index = nil
  22. self.new_source = false
  23. self.type_mode = type_id or 1
  24. if quick_select == nil then
  25. quick_select = true
  26. end
  27. self.quick_select = quick_select
  28. if not sizeDelta and parent_wnd then
  29. sizeDelta = {}
  30. sizeDelta.x, sizeDelta.y = GetSizeDeltaXY(parent_wnd)
  31. end
  32. self:Load(sizeDelta)
  33. end
  34. function UIVerTabBar:__delete()
  35. self:DestroyBtnList()
  36. end
  37. function UIVerTabBar:DestroyBtnList()
  38. for i,v in ipairs(self.btn_list) do
  39. v:UnBindEvent()
  40. v:DeleteMe()
  41. v = nil
  42. end
  43. self.btn_list = {}
  44. end
  45. function UIVerTabBar:Load(sizeDelta)
  46. self.scroll_view = UiFactory.createChild(self.parent_wnd, UIType.ScrollView, "bar_scroll")
  47. self.scroll_view.transform.sizeDelta = sizeDelta and sizeDelta or Vector2(202, 561)
  48. self.scroll_view_content = self.scroll_view.transform:Find("Viewport/Content")
  49. self.scroll_view_viewport = self.scroll_view.transform:Find("Viewport")
  50. self:InitEvent()
  51. end
  52. function UIVerTabBar:InitEvent()
  53. end
  54. --设置选项卡数据 is_btn_need_select_img:大类按钮是否需要设置选中的背景图
  55. function UIVerTabBar:SetTabBars(bar_list, call_back, select_anything, is_btn_need_select_img)
  56. if bar_list == nil or call_back == nil then return end
  57. for i,v in ipairs(self.btn_list) do
  58. v:SetVisible(false)
  59. end
  60. -- print("Nice:UIVerTabBar [67] self.new_source: ",self.new_source)
  61. for i,v in ipairs(bar_list) do
  62. local tab_btn = self.btn_list[i]
  63. if tab_btn == nil then
  64. tab_btn = UIVerTabBtn.New(self.scroll_view_content, nil, nil, self,self.type_mode,is_btn_need_select_img)
  65. self.btn_list[i] = tab_btn
  66. end
  67. --print("Nice:UIVerTabBar [74] self.new_source: ",self.new_source)
  68. tab_btn:SetTabDetail(v, i, call_back, select_anything)
  69. tab_btn:SetVisible(true)
  70. local x = 0
  71. local y = -(i - 1) * UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT -5
  72. -- local y = -i * UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT -5
  73. tab_btn:SetPosition(x, y)
  74. local function onBtnClickHandler(target)
  75. self:SetSelectTab(tab_btn.index,tab_btn.sub_index,self.force_show)
  76. end
  77. if tab_btn.tab then
  78. AddClickEvent(tab_btn.tab, onBtnClickHandler,2)
  79. end
  80. end
  81. end
  82. --设置选中的选项卡
  83. function UIVerTabBar:SetSelectTab(index,sub_index,force_show)
  84. self.force_show = force_show
  85. for i,v in ipairs(self.btn_list) do
  86. -- if self.new_source then
  87. -- v:SetNewSource()
  88. -- end
  89. if v.index and v.index == index then
  90. if not v.show then
  91. self:CancelSetSelectTab(index)
  92. end
  93. v:SetSelectMode(index, sub_index, force_show, self.quick_select)
  94. end
  95. end
  96. self:RefreshTabPos()
  97. end
  98. --取消所有的选中项
  99. function UIVerTabBar:CancelSetSelectTab(index)
  100. for i,v in ipairs(self.btn_list) do
  101. v:RefreshBtnState(false)
  102. v:SetSelectSubIndex(0, 0)
  103. end
  104. self:RefreshTabPos()
  105. if index and index * UIVerTabSubBtn.Height > self.scroll_view.transform.sizeDelta.y / 2 then
  106. self.scroll_view_content.localPosition = Vector2(self.scroll_view_content.localPosition.x, (index - 1) * UIVerTabSubBtn.Height)
  107. else
  108. self.scroll_view_content.localPosition = Vector2(self.scroll_view_content.localPosition.x, 0)
  109. end
  110. end
  111. --更新tabBar的位置
  112. function UIVerTabBar:RefreshTabPos()
  113. local gap = 2
  114. for i,v in ipairs(self.btn_list) do
  115. if v.show == true then
  116. local h = #v.sub_btn_list * (UIVerTabBtn.Parms[self.type_mode].SUB_CON_HEIGHT+(UIVerTabBtn.Parms[self.type_mode].SUB_BTN_Y_SPACE or 10))
  117. v:SetPosition(0, -gap)
  118. v:SetSubConSize(h)
  119. if h <= 10 then
  120. h = UIVerTabBtn.Parms[self.type_mode].BTN_Y_SPACE
  121. end
  122. gap = gap + UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT + h
  123. else
  124. v:SetPosition(0, -gap)
  125. gap = gap + UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT + UIVerTabBtn.Parms[self.type_mode].BTN_Y_SPACE
  126. end
  127. end
  128. self.scroll_view_content.sizeDelta = Vector2(202, gap)
  129. -- print("UIVerTabBar:RefreshTabPos()",(gap + 40))
  130. end
  131. function UIVerTabBar:SetScrollViewSize(width,heigh)
  132. self.scroll_view.transform.sizeDelta = Vector2(width,heigh)
  133. end
  134. function UIVerTabBar:RefreshTabBtnState(index)
  135. for i,v in ipairs(self.btn_list) do
  136. if v.index == index then
  137. v:RefreshBtnState(v.show)
  138. end
  139. end
  140. self:RefreshTabPos()
  141. end
  142. function UIVerTabBar:GetTabBtnShowState(index)
  143. for i,v in ipairs(self.btn_list) do
  144. if v.index == index then
  145. return v.show
  146. end
  147. end
  148. end
  149. function UIVerTabBar:SetNewSource( )
  150. self.new_source = true
  151. UIVerTabBtn.BTN_HEIGHT = 50
  152. end
  153. --在回调函数中调用该函数,刷新选中状态
  154. function UIVerTabBar:UpdateSelectState(index, sub_index)
  155. for i,v in ipairs(self.btn_list) do
  156. v:SetSelectState(false)
  157. for key,value in ipairs(v.sub_btn_list) do
  158. value:SetSelectState(false)
  159. end
  160. end
  161. if sub_index == 0 then
  162. self.btn_list[index]:SetSelectState(true)
  163. else
  164. self.btn_list[index].sub_btn_list[sub_index]:SetSelectState(true)
  165. end
  166. end
  167. function UIVerTabBar:SetImageTexture(asset_path,res_name,index_list)
  168. for i,v in ipairs(self.btn_list) do
  169. v:SetImageTexture()
  170. for key,value in ipairs(v.sub_btn_list) do
  171. value:SetImageTexture()
  172. end
  173. end
  174. -- PrintTable(index_list)
  175. if asset_path and res_name then
  176. for k,v in pairs(index_list) do
  177. self.btn_list[v.main_index]:SetImageTexture(asset_path,res_name)
  178. if v.sub_index ~= 0 then
  179. self.btn_list[v.main_index].sub_btn_list[v.sub_index]:SetImageTexture(asset_path,res_name)
  180. end
  181. end
  182. end
  183. end
  184. --传入主索引和所有要显示的子索引列表,取消红点只传入主索引
  185. function UIVerTabBar:ShowRedDot(index, index_list)
  186. if self.btn_list[index] then
  187. self.btn_list[index]:ShowRedDot(index_list)
  188. end
  189. end
  190. function UIVerTabBar:CancelAllRedDot()
  191. for i, v in pairs(self.btn_list) do
  192. v:ShowRedDot()
  193. end
  194. end