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

359 lines
12 KiB

преди 4 седмици
  1. --tabbar按钮(单个按钮)
  2. UIVerTabBtn = UIVerTabBtn or BaseClass(BaseComponent)
  3. local UIVerTabBtn = UIVerTabBtn
  4. UIVerTabBtn.BTN_HEIGHT = 50 --tab按钮的高度,不包括下一级tab按钮的容器高度
  5. UIVerTabBtn.SUB_CON_WIDTH = 171 --下一级tab按钮的容器宽度
  6. UIVerTabBtn.TYPE = {
  7. normal = 1,
  8. designation = 2,
  9. }
  10. UIVerTabBtn.Parms = {
  11. [1] = {
  12. BTN_HEIGHT = 56,
  13. BTN_WIDTH = 186,
  14. BTN_Y_SPACE = 10,
  15. SUB_CON_WIDTH = 186,
  16. SUB_CON_HEIGHT = 48,
  17. SUB_BTN_Y_SPACE = 2,
  18. SubConPosOffset = {5, -20},
  19. normalsource = "ui_tab_bg",
  20. selectsource = "ui_tab_bg_select2",
  21. abname = "uicomponent_asset",
  22. -- text_color_nor = Color(1,1,1,1),
  23. -- text_color_sel = Color(1,1,1,1),
  24. -- sub_btn_text_color_nor = Color(1,1,1,1),
  25. -- sub_btn_text_color_sel = Color(1,1,1,1),
  26. need_newsouce = false,--如果是false 就不需要填后面的参数了
  27. need_show_arrow = true,--是否显示上下箭头
  28. },
  29. -- [2] = {
  30. -- BTN_HEIGHT = 51,
  31. -- BTN_WIDTH = 206,
  32. -- BTN_Y_SPACE = 8,
  33. -- SUB_CON_WIDTH = 210,
  34. -- SUB_CON_HEIGHT = 68,
  35. -- SUB_BTN_Y_SPACE = 0,
  36. -- SubConPosOffset = {3, -20},
  37. -- normalsource = "design_blue",
  38. -- selectsource = "design_orange",
  39. -- abname = "designation_asset",
  40. -- need_newsouce = true,
  41. -- sub_normal = "design_sub_blue",
  42. -- sub_select = "design_sub_orange",
  43. -- subabname = "designation_asset",
  44. -- text_color_nor = Color(255/255,255/255,255/255,1),
  45. -- text_color_sel = Color(155/255,87/255,47/255,1),
  46. -- },
  47. }
  48. -- is_btn_need_select_img:大类按钮是否需要设置选中的背景图
  49. function UIVerTabBtn:__init(parent, call_back, desc, tabbar,type_id,is_btn_need_select_img)
  50. self.tabbar = tabbar
  51. self.sub_btn_list = {}
  52. self.tab_data = nil
  53. self.index = 1
  54. self.call_back = nil
  55. self.type_id = type_id or 1
  56. self.is_btn_need_select_img = is_btn_need_select_img or false
  57. self.style_data = UIVerTabBtn.Parms[self.type_id]
  58. self.show = false --是否展开
  59. self.select_anything = false --是否可以在有子类的时候选中大类
  60. self.new_source = false
  61. self:CreateGameObject(UIType.UIVerTabBtn)
  62. self:InitData(call_back, desc)
  63. end
  64. function UIVerTabBtn:__delete()
  65. self:UnBindEvent()
  66. end
  67. function UIVerTabBtn:UnBindEvent()
  68. for i,v in ipairs(self.sub_btn_list) do
  69. v:DeleteMe()
  70. v = nil
  71. end
  72. self.sub_btn_list = {}
  73. if self.event_id then
  74. GlobalEventSystem:UnBind(self.event_id)
  75. self.event_id = nil
  76. end
  77. end
  78. function UIVerTabBtn:InitData(call_back, text)
  79. self.call_back = call_back
  80. self.btnText.text = text
  81. end
  82. function UIVerTabBtn:LoadSuccess()
  83. self.nodes = {
  84. "tab/normalText:tmp:obj",
  85. }
  86. self:GetChildren(self.nodes)
  87. self.btnText = self:GetChild("tab/Text"):GetComponent("TextMeshProUGUI")
  88. self.btnText_obj = self:GetChild("tab/Text").gameObject
  89. self.icon = self:GetChild("tab/icon"):GetComponent("Image")
  90. self.icon_tran = self:GetChild("tab/icon")
  91. self.icon_pos = self.icon_tran.localPosition
  92. self.imgSelect = self:GetChild("tab/selectBg").gameObject
  93. self.imgSelect_img = self:GetChild("tab/selectBg"):GetComponent("Image")
  94. self.iconObj = self:GetChild("tab/icon").gameObject
  95. self.addBtn = self:GetChild("subCon").gameObject
  96. self.tab = self:GetChild("tab").gameObject
  97. self.tab_image = self:GetChild("tab"):GetComponent("Image")
  98. self.tab_con = self:GetChild("tab")
  99. self.subCon = self:GetChild("subCon")
  100. self.img = self:GetChild("tab/img").gameObject
  101. self.img:SetActive(false)
  102. self.dot = self:GetChild("tab/dot").gameObject
  103. self.dot:SetActive(false)
  104. if self.is_btn_need_select_img then
  105. lua_resM:setImageSprite(self,self.imgSelect_img,"uiComponent_asset","ui_subtab_selected")
  106. else
  107. lua_resM:setImageSprite(self,self.imgSelect_img,"uiComponent_asset","com_empty")
  108. end
  109. self:InitEvent()
  110. end
  111. function UIVerTabBtn:InitEvent()
  112. local select_fun = function (tabbar, parent_idnex, sub_index)
  113. if self.tabbar == tabbar then
  114. if self.index == parent_idnex then
  115. self:SetSelectSubIndex(parent_idnex, sub_index)
  116. else
  117. self:SetSelectSubIndex(0, 0)
  118. end
  119. end
  120. end
  121. self.event_id = GlobalEventSystem:Bind(EventName.SUB_TAB_BTN_BE_SELECTED, select_fun)
  122. end
  123. function UIVerTabBtn:SetSelectMode(index,sub_index,force_show, quick_select)
  124. self.show = not self.show
  125. if force_show then
  126. self.show = true
  127. end
  128. if self.show and index then
  129. lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_minus",true)
  130. lua_resM:setImageSprite(self,self.tab_image,UIVerTabBtn.Parms[self.type_id].abname,UIVerTabBtn.Parms[self.type_id].selectsource)
  131. -- if self.new_source then
  132. -- lua_resM:setImageSprite(self, self.tab_image, "designation_asset", "design_orange")
  133. --self.btnText.color = UIVerTabBtn.Parms[self.type_id].text_color_sel
  134. -- end
  135. -- self.icon_tran.localPosition = Vector3(self.icon_pos.x, self.icon_pos.y - 7,0)
  136. self:ShowSubBtnVisible(true)
  137. if sub_index then
  138. -- self:SetSelectSubIndex(index, sub_index)
  139. GlobalEventSystem:Fire(EventName.SUB_TAB_BTN_BE_SELECTED, self.tabbar, index, sub_index)
  140. elseif quick_select then
  141. local select_index
  142. if self.sub_red_list and #self.sub_red_list > 0 then
  143. select_index = self.sub_red_list[1]
  144. else
  145. select_index = self.sub_btn_list[1] and self.sub_btn_list[1].sub_index
  146. end
  147. if select_index then
  148. -- self:SetSelectSubIndex(index, select_index)
  149. GlobalEventSystem:Fire(EventName.SUB_TAB_BTN_BE_SELECTED, self.tabbar, index, select_index)
  150. end
  151. end
  152. else
  153. lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_addicon",true)
  154. lua_resM:setImageSprite(self,self.tab_image,UIVerTabBtn.Parms[self.type_id].abname,UIVerTabBtn.Parms[self.type_id].normalsource)
  155. -- if self.new_source then
  156. -- lua_resM:setImageSprite(self,self.tab_image,"designation_asset","design_blue")
  157. --self.btnText.color = UIVerTabBtn.Parms[self.type_id].text_color_nor
  158. -- end
  159. -- self.icon_tran.localPosition = Vector3(self.icon_pos.x, self.icon_pos.y,0)
  160. self:ShowSubBtnVisible(false)
  161. end
  162. end
  163. function UIVerTabBtn:RefreshBtnState(show)
  164. self.show = show
  165. if self.show then
  166. lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_minus",true)
  167. lua_resM:setImageSprite(self,self.tab_image,UIVerTabBtn.Parms[self.type_id].abname,UIVerTabBtn.Parms[self.type_id].selectsource)
  168. -- if self.new_source then
  169. -- lua_resM:setImageSprite(self,self.tab_image,"designation_asset","design_orange")
  170. --self.btnText.color = UIVerTabBtn.Parms[self.type_id].text_color_sel
  171. -- end
  172. -- self.icon_tran.localPosition = Vector3(self.icon_pos.x, self.icon_pos.y - 7,0)
  173. else
  174. lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_addicon",true)
  175. -- lua_resM:setImageSprite(self,self.tab_image,"uicomponent_asset","ui_tab_bg",true)
  176. lua_resM:setImageSprite(self,self.tab_image,UIVerTabBtn.Parms[self.type_id].abname,UIVerTabBtn.Parms[self.type_id].normalsource)
  177. -- if self.new_source then
  178. -- lua_resM:setImageSprite(self,self.tab_image,"designation_asset","design_blue")
  179. --self.btnText.color = UIVerTabBtn.Parms[self.type_id].text_color_nor
  180. -- end
  181. -- self.icon_tran.localPosition = Vector3(self.icon_pos.x, self.icon_pos.y,0)
  182. end
  183. if self.subCon and self.sub_btn_list and #self.sub_btn_list ~= 0 then
  184. self.subCon.gameObject:SetActive(show)
  185. end
  186. end
  187. --设置选中的子一级tab
  188. function UIVerTabBtn:SetSelectSubIndex(index, sub_index)
  189. for i,v in ipairs(self.sub_btn_list) do
  190. if v.parent_idnex == index and v.sub_index == sub_index then
  191. v:SetSelected(true)
  192. else
  193. v:SetSelected(false)
  194. end
  195. end
  196. end
  197. --设置tab数据
  198. function UIVerTabBtn:SetTabDetail(data, index, call_back, select_anything)
  199. self.tab_data = data
  200. self.index = index
  201. self.call_back = call_back
  202. self.select_anything = select_anything
  203. if self.tab_data == nil or self.tab_data.main_tab == nil then return end
  204. self:SetText(self.tab_data.main_tab)
  205. if self.tab_data == nil or #self.tab_data.sub_tab == 0 then
  206. self.iconObj:SetActive(false)
  207. SetAnchoredPositionX(self.btnText.transform,-0.55)
  208. SetSizeDeltaX(self.btnText.transform,176.1)
  209. elseif self.style_data and self.style_data.need_show_arrow then
  210. self.iconObj:SetActive(true)
  211. SetAnchoredPositionX(self.btnText.transform,-7.61)
  212. SetSizeDeltaX(self.btnText.transform,161.97)
  213. end
  214. self:SetTabBtnSize()
  215. self:SetSubBtns(self.tab_data.sub_tab, self.index, call_back)
  216. end
  217. function UIVerTabBtn:SetText(str)
  218. self.btnText.text = str
  219. self.normalText_tmp.text = str
  220. end
  221. --设置下一级的按钮
  222. function UIVerTabBtn:SetSubBtns(sub_tab_list, parent_index, call_back)
  223. for i,v in ipairs(self.sub_btn_list) do
  224. v:SetVisible(false)
  225. end
  226. for i,v in ipairs(sub_tab_list) do
  227. local sub_tab = self.sub_btn_list[i]
  228. if sub_tab == nil then
  229. sub_tab = UIVerTabSubBtn.New(self.subCon)
  230. self.sub_btn_list[i] = sub_tab
  231. end
  232. sub_tab:SetSubDeail(v, parent_index, i, call_back)
  233. sub_tab:SetVisible(true)
  234. sub_tab:SetStyleData(UIVerTabBtn.Parms[self.type_id])
  235. local x = -5
  236. local y = -(i - 1) * (UIVerTabBtn.Parms[self.type_id].SUB_CON_HEIGHT + (UIVerTabBtn.Parms[self.type_id].SUB_BTN_Y_SPACE or 10)) + 20
  237. if UIVerTabBtn.Parms[self.type_id].need_newsouce then
  238. sub_tab:SetNewSource(UIVerTabBtn.Parms[self.type_id])
  239. sub_tab:ResetSubDeail(self.tab_data.info[i])
  240. end
  241. self.iconObj:SetActive(self.style_data and self.style_data.need_show_arrow or false)
  242. local is_text_middle = self.style_data and self.style_data.need_show_arrow or false
  243. if not is_text_middle then
  244. SetAnchoredPositionX(self.btnText.transform,-0.55)
  245. SetSizeDeltaX(self.btnText.transform,176.1)
  246. else
  247. SetAnchoredPositionX(self.btnText.transform,-7.61)
  248. SetSizeDeltaX(self.btnText.transform,161.97)
  249. end
  250. sub_tab:SetPosition(x, y)
  251. local function onBtnClickHandler(target)
  252. GlobalEventSystem:Fire(EventName.SUB_TAB_BTN_BE_SELECTED, self.tabbar, sub_tab.parent_idnex, sub_tab.sub_index)
  253. end
  254. if sub_tab.Button then
  255. AddClickEvent(sub_tab.Button, onBtnClickHandler,LuaSoundManager.SOUND_UI.SWITCH)
  256. end
  257. end
  258. self.subCon.gameObject:SetActive(false)
  259. end
  260. function UIVerTabBtn:ShowSubBtnVisible(bool)
  261. if self.subCon and self.sub_btn_list and #self.sub_btn_list ~= 0 then
  262. self.subCon.gameObject:SetActive(bool)
  263. if self.select_anything then
  264. self.call_back(self.index,0)
  265. end
  266. else
  267. self.call_back(self.index,0)
  268. end
  269. end
  270. function UIVerTabBtn:SetSubConSize(height)
  271. self.subCon.sizeDelta = Vector2(UIVerTabBtn.Parms[self.type_id].SUB_CON_WIDTH, height)
  272. end
  273. --设置大小
  274. function UIVerTabBtn:SetTabBtnSize()
  275. if not self.type_id or not UIVerTabBtn.Parms[self.type_id] then return end
  276. -- print("Nice:UIVerTabBtn [243]222 : ")
  277. self.tab_con.sizeDelta = Vector2(UIVerTabBtn.Parms[self.type_id].BTN_WIDTH,UIVerTabBtn.Parms[self.type_id].BTN_HEIGHT)
  278. local pos_offset = UIVerTabBtn.Parms[self.type_id].SubConPosOffset
  279. if pos_offset then
  280. SetLocalPosition(self.subCon, pos_offset[1], -self.tab_con.sizeDelta.y + pos_offset[2])
  281. end
  282. end
  283. function UIVerTabBtn:SetSelectState(b)
  284. self.normalText_obj:SetActive(not b)
  285. self.btnText_obj:SetActive(b)
  286. self.imgSelect:SetActive(b)
  287. end
  288. function UIVerTabBtn:SetImageTexture(asset_path,res_name)
  289. if asset_path and res_name then
  290. self.img:SetActive(true)
  291. lua_resM:setImageSprite(self,self.img:GetComponent("Image"),asset_path,res_name,true)
  292. else
  293. self.img:SetActive(false)
  294. end
  295. end
  296. function UIVerTabBtn:ShowRedDot(list)
  297. self.sub_red_list = list
  298. for i, v in ipairs(self.sub_btn_list) do
  299. v:ShowRedDot(false)
  300. end
  301. if not list or #list == 0 then
  302. self.dot:SetActive(false)
  303. else
  304. self.dot:SetActive(true)
  305. for i, v in pairs(list) do
  306. if self.sub_btn_list[v] then
  307. self.sub_btn_list[v]:ShowRedDot(true)
  308. end
  309. end
  310. end
  311. end
  312. function UIVerTabBtn:SetNewSource( )
  313. self.new_source = true
  314. self.iconObj:SetActive(false)
  315. SetAnchoredPositionX(self.btnText.transform,-0.55)
  316. SetSizeDeltaX(self.btnText.transform,176.1)
  317. self:SetTabBtnSize()
  318. for i, v in ipairs(self.sub_btn_list) do
  319. v:SetNewSource()
  320. v:ResetSubDeail(self.tab_data.info[i])
  321. end
  322. SetLocalPosition(self.subCon.transform, 1, -42, 0)
  323. end