--tabbar按钮(单个按钮) UICusVerTabBtn = UICusVerTabBtn or BaseClass(BaseComponent) -- UICusVerTabBtn.BTN_HEIGHT = 66 --tab按钮的高度,不包括下一级tab按钮的容器高度 -- UICusVerTabBtn.SUB_CON_WIDTH = 178 --下一级tab按钮的容器宽度 function UICusVerTabBtn:__init(parent, sub_item, size, sub_offset) self.sub_btn_list = {} self.index = 1 self.call_back = nil self.show = false --是否展开 self.select_anything = false --是否可以在有子类的时候选中大类 self.btn_width = size and size.x or 189 --tab按钮的宽度,不包括下一级tab按钮的容器宽度 self.btn_height = size and size.y or 66 --tab按钮的高度,不包括下一级tab按钮的容器高度 self.sub_con_width = size and size.x - 16 or 178 --下一级tab按钮的容器宽度 self.sub_item = sub_item self.sub_offset = sub_offset self:CreateGameObject(UIType.UIVerTabBtn) end function UICusVerTabBtn:__delete() for i,v in ipairs(self.sub_btn_list) do v:DeleteMe() v = nil end self.sub_btn_list = {} if self.event_id then GlobalEventSystem:UnBind(self.event_id) self.event_id = nil end end function UICusVerTabBtn:LoadSuccess() self.btnText = self:GetChild("tab/Text"):GetComponent("Text") self.btn_tran = self:GetChild("tab/Text") self.icon = self:GetChild("tab/icon"):GetComponent("Image") self.icon_tran = self:GetChild("tab/icon") self.imgSelect = self:GetChild("tab/selectBg").gameObject self.iconObj = self:GetChild("tab/icon").gameObject self.addBtn = self:GetChild("subCon").gameObject self.tab = self:GetChild("tab").gameObject self.tab_img = self:GetChild("tab") self.subCon = self:GetChild("subCon") self.img = self:GetChild("tab/img").gameObject self.red_dot = self:GetChild("tab/dot").gameObject self:ResetSize() self:InitEvent() end function UICusVerTabBtn:InitEvent() end --设置tab数据 function UICusVerTabBtn:SetTabDetail(tab_data, index, call_back, select_anything, force_size) self.sub_tab_data = {} self.index = index self.call_back = call_back self.select_anything = select_anything self.force_size = force_size if tab_data == nil then return end local tab_index = "_"..self.index if type(tab_data[tab_index]) == "table" then --主菜单名称 self.btnText.text = tab_data[tab_index].name elseif type(tab_data[tab_index]) == "string" then self.btnText.text = tab_data[tab_index] end for i = 1, TableSize(tab_data) do tab_index = "_"..self.index.."_"..i if not tab_data[tab_index] then break end table.insert(self.sub_tab_data, tab_data[tab_index]) end --是否显示展开标志 if #self.sub_tab_data == 0 then self.iconObj:SetActive(false) else self.iconObj:SetActive(true) end self:SetSubBtns(self.sub_tab_data, self.index, call_back) end --设置下一级的按钮 function UICusVerTabBtn:SetSubBtns(sub_tab_list, parent_index, call_back) for i,v in ipairs(self.sub_btn_list) do v:SetVisible(false) end for i,v in ipairs(sub_tab_list) do local sub_tab = self.sub_btn_list[i] if sub_tab == nil then sub_tab = self.sub_item.New(self.subCon) self.sub_btn_list[i] = sub_tab if self.force_size then sub_tab:SetTabSubBtnSize(self.sub_con_width) end end sub_tab:SetSubDeail(v, parent_index, i, call_back) sub_tab:SetVisible(true) local x = 0 local y = -(i - 1) * self.sub_item.Height x = self.offset and self.offset.x + x or x y = self.offset and -(i - 1) * self.offset.y + y or y sub_tab:SetPosition(x, y) local function onBtnClickHandler(target) self:SetSelectSubIndex(0, 0) self:SetSelectSubIndex(sub_tab.parent_index, sub_tab.sub_index) end if sub_tab.Button then AddClickEvent(sub_tab.Button, onBtnClickHandler,2) end end self.subCon.gameObject:SetActive(false) end --选中菜单 function UICusVerTabBtn:SetSelectMode(index, sub_index,force_show) if self.index == index then self.show = not self.show else self.show = false end if force_show and self.index == index then self.show = true end if self.show and index then SetRotate(self.icon_tran, 0, 0, -90) self:ShowSubBtnVisible(true) if sub_index then self:SetSelectSubIndex(index, sub_index) end else SetLocalRotation(self.icon_tran, 0, 0, 0) self:ShowSubBtnVisible(false) end end --设置图标,隐藏sub function UICusVerTabBtn:RefreshBtnState(show) self.show = show if self.show then SetRotate(self.icon_tran, 0, 0, -90) --lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_minus",true) --self.icon_tran.localPosition = Vector3(self.btn_width- 26, -self.btn_height / 2,0) else SetLocalRotation(self.icon_tran, 0, 0, 0) --lua_resM:setImageSprite(self,self.icon,"uicomponent_asset","ui_addicon",true) --self.icon_tran.localPosition = Vector3(self.btn_width- 26, -self.btn_height / 2 + 7,0) end if self.subCon and self.sub_btn_list and #self.sub_btn_list ~= 0 then self.subCon.gameObject:SetActive(show) end end --设置选中的子一级tab function UICusVerTabBtn:SetSelectSubIndex(main_index, sub_index) for i,v in ipairs(self.sub_btn_list) do if v.parent_index == main_index and v.sub_index == sub_index then v:SetSelected(true, true) else v:SetSelected(false) end end end function UICusVerTabBtn:SetText(str) self.btnText.text = str end function UICusVerTabBtn:ShowSubBtnVisible(bool) if self.subCon and self.sub_btn_list and #self.sub_btn_list ~= 0 then self.subCon.gameObject:SetActive(bool) if self.select_anything then if self.call_back then self.call_back(self.index,0) end end else if self.call_back then self.call_back(self.index,0) end end end function UICusVerTabBtn:SetSubConSize(height) self.subCon.sizeDelta = Vector2(UICusVerTabBtn.SUB_CON_WIDTH, height) end --设置大小 function UICusVerTabBtn:SetTabBtnSize() end function UICusVerTabBtn:SetSelectState(b) self.imgSelect:SetActive(b) end function UICusVerTabBtn:SetImageTexture(asset_path,res_name) if asset_path and res_name then self.img:SetActive(true) lua_resM:setImageSprite(self,self.img:GetComponent("Image"),asset_path,res_name) else self.img:SetActive(false) end end function UICusVerTabBtn:ShowRedDot(list) if list and #list > 0 then table.sort(list) end self.sub_red_list = list for i, v in ipairs(self.sub_btn_list) do v:ShowRedDot(false) end if not list or #list == 0 then self.red_dot:SetActive(false) else self.red_dot:SetActive(true) for i, v in ipairs(list) do if self.sub_btn_list[v] then self.sub_btn_list[v]:ShowRedDot(true) end end end if list[0] then self.red_dot:SetActive(true) end end function UICusVerTabBtn:SetSubOffset(offset) self.offset = offset end --加载完但不显示的不需计算高度 function UICusVerTabBtn:GetVisibleCount(visible) local count = 0 for _,v in ipairs(self.sub_btn_list) do if v:GetVisible() == visible or not v.is_loaded then count = count + 1 end end return count end function UICusVerTabBtn:ResetSize() self.imgSelect.transform.sizeDelta = Vector2(self.btn_width + 12, self.btn_height + 12) self.imgSelect.transform.sizeDelta = Vector2(self.btn_width + 12, self.btn_height + 12) self.tab_img.sizeDelta = Vector2(self.btn_width, self.btn_height) --self.icon_tran.localPosition = Vector3(self.btn_width- 26, -self.btn_height / 2 + 7,0) if self.sub_offset then self.subCon.localPosition = Vector3(self.sub_offset.x, self.sub_offset.y,0) else self.subCon.localPosition = Vector3(self.subCon.localPosition.x, -self.btn_height,0) end end