源战役客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

284 rindas
7.5 KiB

pirms 4 nedēļām
  1. UITabBarView = UITabBarView or BaseClass(BaseComponent)
  2. local UITabBarView = UITabBarView
  3. local math_abs = math.abs
  4. function UITabBarView:__init(parent_wnd,prefab_asset,layer_name,view_style,is_scroll)
  5. self.view_style = view_style
  6. self.is_scroll = is_scroll
  7. end
  8. --[[@
  9. :
  10. :
  11. tabbar_list ({name, ...})
  12. switch_callback
  13. tabbar_style 1
  14. extra_arg_table
  15. ]]
  16. function UITabBarView:SetData(tabbar_list, switch_callback, tabbar_style, linebg_size_callback,extra_arg_table)
  17. self.curr_tabBtn_index = 0
  18. self.tabbar_item_list = {}
  19. self.linebg_size_callback = linebg_size_callback
  20. self.tabbar_list = tabbar_list
  21. self.switch_callback = switch_callback
  22. self.tabbar_style = tabbar_style
  23. self.extra_arg_table = extra_arg_table
  24. self.parms = UITabButton.Style[self.tabbar_style or 1] or UITabButton.Style[1]
  25. self:CreateGameObject(UIType.Image3, "tab_line_bg")
  26. end
  27. function UITabBarView:LoadSuccess()
  28. self.line_bg = self.gameObject:GetComponent("Image")
  29. self:LoadTabBarPrefab()
  30. self:UpdateLineHeight()
  31. end
  32. function UITabBarView:UpdateLineHeight( )
  33. self.gameObject.transform.anchorMin = Vector2(0.5, 1)
  34. self.gameObject.transform.anchorMax = Vector2(0.5, 1)
  35. self.gameObject.transform.pivot = Vector2(0.5, 1)
  36. local len = #self.tabbar_list
  37. local height = math_abs(self.parms.offsetY) + len * self.parms.spacing + 30
  38. -- SetAnchoredPosition(self.gameObject.transform, 0,0)--0.5,1的轴点的local不准确 还是用anchored
  39. if self.linebg_size_callback then
  40. new_height = height > 580 and 580 or height
  41. self.linebg_size_callback(self.parms.line_width,new_height,height)
  42. else
  43. SetLocalPositionX(self.gameObject.transform, self.parms.line_offsetX)
  44. self.gameObject.transform.sizeDelta = Vector2(self.parms.line_width, height)
  45. end
  46. end
  47. function UITabBarView:LoadTabBarPrefab()
  48. local vo = nil
  49. local item = nil
  50. local click_callback = function(index)
  51. self:SetTabBarIndex(index)
  52. end
  53. local item_len = #self.tabbar_list
  54. for i = 1,#self.tabbar_list do
  55. vo = self.tabbar_list[i]
  56. item = self.tabbar_item_list[i]
  57. if item == nil then
  58. item = UITabButton.New(self.parent_transform, nil, self.layer_name , self.view_style, self.tabbar_style, self.is_scroll)
  59. self.tabbar_item_list[i] = item
  60. end
  61. item:SetData(i, vo, click_callback, item_len, self.extra_arg_table)
  62. end
  63. for i=#self.tabbar_list + 1,#self.tabbar_item_list do
  64. self.tabbar_item_list[i]:SetVisible(false)
  65. end
  66. --延迟等所有的页签设置完再合批
  67. -- local function on_delay( )
  68. -- self:DoBatch()
  69. -- end
  70. -- setTimeout(on_delay, 0.5)
  71. end
  72. function UITabBarView:DoBatch()
  73. -- if self.tabbar_style == 2 then
  74. -- do return end
  75. -- end
  76. if IsNull(self.parent_transform) then
  77. return
  78. end
  79. if not self.batch_cont then
  80. self.batch_cont = UiFactory.createChild(self.parent_transform, UIType.EmptyObject, "batch_cont")
  81. end
  82. if not self.batch_cont then return end
  83. for k,v in pairs(self.tabbar_item_list) do
  84. v:DoBatch(self.batch_cont.transform)
  85. end
  86. for k,v in pairs(self.tabbar_item_list) do
  87. if v.text_transform then
  88. v.text_transform:SetAsLastSibling()
  89. end
  90. end
  91. self.batch_cont.transform:SetAsLastSibling()
  92. end
  93. function UITabBarView:SetTabBarIndex(index,is_force,ignore_call_back)
  94. if self.curr_tabBtn_index == index and not is_force then
  95. return
  96. end
  97. local tab_btn
  98. for k,v in pairs(self.tabbar_item_list) do
  99. if v.tab_id == self.curr_tabBtn_index then
  100. tab_btn = v
  101. end
  102. end
  103. if tab_btn then
  104. tab_btn:SetSelect(false)
  105. end
  106. self.curr_tabBtn_index = index
  107. local tab_btn
  108. local pos_count = 0
  109. for k,v in pairsByKeys(self.tabbar_item_list) do
  110. pos_count = pos_count + 1
  111. if v.tab_id == self.curr_tabBtn_index then
  112. tab_btn = v
  113. break
  114. end
  115. end
  116. if tab_btn then
  117. tab_btn:SetSelect(true)
  118. end
  119. if self.gameObject then
  120. self.gameObject.transform:SetAsFirstSibling()
  121. end
  122. if not ignore_call_back and self.switch_callback then
  123. if not self.first_open_loaded and self.view_style == UITabWindow.SizeSmall then--防止页签过多 一开始看不到页签
  124. self.first_open_loaded = true
  125. local max_num = #self.tabbar_item_list
  126. if pos_count <= 6 then
  127. --前六个
  128. SetAnchoredPositionY(self.parent_transform, -48)
  129. else
  130. local show_left_pos = (pos_count-6)
  131. if pos_count == max_num then
  132. --最后一个
  133. show_left_pos = (pos_count-1)
  134. end
  135. show_left_pos = (show_left_pos+6) > max_num and (max_num-6) or show_left_pos
  136. SetAnchoredPositionY(self.parent_transform,show_left_pos * (self.parms.spacing+30))
  137. end
  138. end
  139. self.switch_callback(index)
  140. end
  141. end
  142. function UITabBarView:ClickTabItemHandler( id )
  143. if self.curr_tabBtn_index == id then return end
  144. local tab_btn
  145. for k,v in pairs(self.tabbar_item_list) do
  146. if v.tab_id == self.curr_tabBtn_index then
  147. tab_btn = v
  148. end
  149. end
  150. if tab_btn and tab_btn.transform then
  151. tab_btn:SetSelect(false)
  152. end
  153. self.curr_tabBtn_index = id
  154. item = self:GetTabItem(id)
  155. if item ~= nil then
  156. item:SetSelected(true)
  157. end
  158. local tab_btn
  159. for k,v in pairs(self.tabbar_item_list) do
  160. if v.tab_id == self.curr_tabBtn_index then
  161. tab_btn = v
  162. end
  163. end
  164. if tab_btn and tab_btn.transform then
  165. -- print("HWR:UITabbarView [97]self.curr_tabBtn_index:,index ",self.curr_tabBtn_index,index)
  166. tab_btn:SetSelect(true)
  167. end
  168. if self.gameObject then
  169. self.gameObject.transform:SetAsFirstSibling()
  170. end
  171. end
  172. function UITabBarView:GetCurrentSelectIndex()
  173. return self.curr_tabBtn_index
  174. end
  175. function UITabBarView:ShowRedPoint(index,bool)
  176. for k,v in pairs(self.tabbar_item_list) do
  177. if index == v.tab_id then
  178. v:ShowRedPoint(bool)
  179. break
  180. end
  181. end
  182. -- local tab_btn = self.tabbar_item_list[index]
  183. -- if tab_btn then
  184. -- tab_btn:ShowRedPoint(bool)
  185. -- end
  186. end
  187. function UITabBarView:ShowRedPointWithNum(index, num, force_show)
  188. for k,v in pairs(self.tabbar_item_list) do
  189. if index == v.tab_id then
  190. v:ShowRedPointWithNum(num, force_show)
  191. break
  192. end
  193. end
  194. end
  195. function UITabBarView:SetSpeacialImg(index, state)
  196. for k,v in pairs(self.tabbar_item_list) do
  197. if index == v.tab_id then
  198. v:SetSpeacialImg(state)
  199. break
  200. end
  201. end
  202. end
  203. function UITabBarView:SetTextSize(font_size)
  204. for k,v in pairs(self.tabbar_item_list) do
  205. v:SetTextSize(font_size)
  206. end
  207. end
  208. function UITabBarView:GetTabbarBtn( index )
  209. local tab_btn = self.tabbar_item_list[index]
  210. return tab_btn
  211. end
  212. function UITabBarView:__delete()
  213. self.curr_tabBtn_index = 0
  214. for i,item in pairs(self.tabbar_item_list) do
  215. item:DeleteMe()
  216. end
  217. self.tabbar_item_list = {}
  218. end
  219. function UITabBarView:RefeshTabBar( )
  220. self:LoadTabBarPrefab()
  221. end
  222. function UITabBarView:RefreshTabData( tabbar_list, switch_callback, tabbar_style )
  223. self.tabbar_list = tabbar_list or self.tabbar_list
  224. self.switch_callback = switch_callback or self.switch_callback
  225. self.tabbar_style = tabbar_style
  226. self.parms = UITabButton.Style[self.tabbar_style or 1] or UITabButton.Style[1]
  227. local vo = nil
  228. local item = nil
  229. local click_callback = function(index)
  230. self:SetTabBarIndex(index)
  231. end
  232. for k,v in pairs(self.tabbar_item_list) do
  233. v:SetVisible(false)
  234. -- 重置tab_id,避免隐藏掉的页签按钮跟某个存在的页签按钮id相同导致不能正常点亮
  235. v.tab_id = -1
  236. end
  237. local item_len = #self.tabbar_list
  238. for i = 1,#self.tabbar_list do
  239. vo = self.tabbar_list[i]
  240. item = self.tabbar_item_list[i]
  241. if item == nil then
  242. item = UITabButton.New(self.parent_transform, nil, self.layer_name , self.view_style, self.tabbar_style, self.is_scroll)
  243. self.tabbar_item_list[i] = item
  244. end
  245. item:SetVisible(true)
  246. item:SetData(i, vo, click_callback, item_len)
  247. end
  248. self:UpdateLineHeight()
  249. self:DoBatch()
  250. end