|
|
-
- --[[
- @description:可以自定义item样式的垂直TabBar
-
- @example:
- code:
- local test_tab_data = { --选项卡配置, main_tab 代表一级选项卡,sub_tab 代表二级选项卡
- [1] = {main_tab = "主Tab1", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}},
- [2] = {main_tab = "主Tab2", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}},
- [3] = {main_tab = "主Tab3", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}},
- }
- local call_back = function (index, sub_index) --回调函数
-
- end
- self.verTabBar = NewUICusVerTabBar.New(self.scroll_content, sizeDelta, item)
-
- self.verTabBar:SetTabBars(test_tab_data)
- self.verTabBar:SetSelectTab(1, 2)
- ]]
-
- NewUICusVerTabBar = NewUICusVerTabBar or BaseClass()
-
- --[[
- item:二级选项卡item
- tab_btn_size:一级选项卡的大小
- call_back 是点击主标签的回调函数
- ]]
- function NewUICusVerTabBar:__init(parent_wnd, sizeDelta, item, tab_btn_size,child_offset)
- self.parent_wnd = parent_wnd
- self.btn_list = {}
- self.item = item
- self.tab_btn_size = tab_btn_size
- self.child_offset = child_offset
- self.index = 0
- self.sub_index = 0
- self.last_index = 0
- self.last_sub_index = 0
- self:Load(sizeDelta)
- end
-
- function NewUICusVerTabBar:__delete()
- for i,v in ipairs(self.btn_list) do
- v:DeleteMe()
- v = nil
- end
- self.btn_list = {}
-
- destroy(self.scroll_view)
- end
-
-
- function NewUICusVerTabBar:Load(sizeDelta)
- self.scroll_view = UiFactory.createChild(self.parent_wnd, UIType.ScrollView, "bar_scroll")
- self.scroll_view.transform.pivot = Vector2(0.5, 0.5)
- self.scroll_view.transform.anchoredPosition = Vector2(0, 0)
- self.scroll_view.transform.sizeDelta = sizeDelta and sizeDelta or Vector2(202, 561)
- self.scroll_view_content = self.scroll_view.transform:Find("Viewport/Content")
- self.scroll_view_viewport = self.scroll_view.transform:Find("Viewport")
- self:InitEvent()
- end
-
- function NewUICusVerTabBar:InitEvent()
-
- end
-
- --设置选项卡数据
- function NewUICusVerTabBar:SetTabBars(bar_list, call_back, select_anything, force_size)
- if bar_list == nil or TableSize(bar_list) == 0 then return end
-
- --先隐藏所有菜单
- for i,v in ipairs(self.btn_list) do
- v:SetVisible(false)
- end
-
- for i,v in ipairs(bar_list) do
- local tab_btn = self.btn_list[i]
- if tab_btn == nil then
- tab_btn = NewUICusVerTabBtn.New(self.scroll_view_content, self.item, self.tab_btn_size,self,self.child_offset)
- self.btn_list[i] = tab_btn
- end
- tab_btn:SetTabDetail(v, i, call_back, select_anything, force_size)
- tab_btn:SetSubOffset(self.offset)
- tab_btn:SetVisible(true)
-
- local function onBtnClickHandler(target)
- if call_back then
- call_back(tab_btn.index,0)
- end
- self:SetSelectTab(tab_btn.index)
- end
- if tab_btn.tab then
- AddClickEvent(tab_btn.tab, onBtnClickHandler,2)
- end
- end
- self:RefreshTabPos()
- end
-
- --更新tabBar的位置
- function NewUICusVerTabBar:RefreshTabPos()
- local gap = 0
- local btn_height
- for i,v in ipairs(self.btn_list) do
- btn_height = v.btn_height
- if v.show == true then
- local off = self.offset and (v:GetVisibleCount(true) - 1) * self.offset.y or 0
- local h = v:GetVisibleCount(true) * self.item.Height + off
- v:SetPosition(0, -gap)
- --v:SetSubConSize(h)
- gap = gap + v.btn_height + h + 4
- else
- v:SetPosition(0, -gap)
- gap = gap + v.btn_height + 4
- end
- end
- self.scroll_view_content.sizeDelta = Vector2(self.scroll_view_content.sizeDelta.x, gap) -- +40?
- if self.sub_index and self.index and self.sub_index >= 5 then
- local all_height = self.item.Height * 4 + (self.index - 1) * btn_height
- self.scroll_view_content.localPosition = Vector3(0,all_height,0)
- else
- self.scroll_view_content.localPosition = Vector3(0,0,0)
- end
- end
-
- --设置选中的选项卡
- function NewUICusVerTabBar:SetSelectTab(index, sub_index,force_show)
- self.sub_index = sub_index
- self.index = index
- for i,v in ipairs(self.btn_list) do
- -- if v.index and v.index == index then
- v:SetSelectMode(index, sub_index, force_show)
- -- end
- end
- self:RefreshTabPos()
- end
-
- function NewUICusVerTabBar:SetTabBarIndex(index,sub_index)
- self.sub_index = sub_index or 0
- self.index = index or 0
-
- local item,sub_item
- if sub_index and sub_index > 0 then
- item = self.btn_list[self.last_index]
- if item then
- sub_item = item.sub_btn_list[self.last_sub_index]
- if sub_item then
- sub_item:SetSelected(false)
- end
- end
-
- self.last_index = index
- self.last_sub_index = sub_index
- end
-
- item = self.btn_list[self.index]
- if item then
- sub_item = item.sub_btn_list[self.sub_index]
- if sub_item then
- sub_item:SetSelected(true)
- end
- end
- end
-
- function NewUICusVerTabBar:GetCurrentSelectIndex()
- return self.index,self.sub_index
- end
-
- --设置选中的选项卡
- -- function NewUICusVerTabBar:SetSelectTab(index, sub_index,force_show)
- -- for i,v in ipairs(self.btn_list) do
- -- if v.index and v.index == index then
- -- if not v.show then
- -- self:CancelSetSelectTab(index)
- -- end
- -- v:SetSelectMode(index, sub_index, force_show, self.quick_select)
- -- break
- -- end
- -- end
- -- self:RefreshTabPos(index, sub_index)
- -- end
-
- --取消当前的选中项
- function NewUICusVerTabBar:CancelCurrSelect()
- if self.last_sub_index > 0 then
- local item,sub_item
- item = self.btn_list[self.last_index]
- if item then
- sub_item = item.sub_btn_list[self.last_sub_index]
- if sub_item then
- sub_item:SetSelected(false)
- end
- end
-
- self.last_index = 0
- self.last_sub_index = 0
- end
- end
-
- function NewUICusVerTabBar:SetScrollViewSize(width,heigh)
- self.scroll_view.transform.sizeDelta = Vector2(width,heigh)
- end
-
- function NewUICusVerTabBar:RefreshTabBtnState(index)
- for i,v in ipairs(self.btn_list) do
- if v.index == index then
- v:RefreshBtnState(v.show)
- end
- end
-
- self:RefreshTabPos()
- end
-
- function NewUICusVerTabBar:GetTabBtnShowState(index)
- for i,v in ipairs(self.btn_list) do
- if v.index == index then
- return v.show
- end
- end
- end
-
-
- --在回调函数中调用该函数,刷新选中状态
- function NewUICusVerTabBar:UpdateSelectState(index, sub_index)
- for i,v in ipairs(self.btn_list) do
- v:SetSelectState(false)
- for key,value in ipairs(v.sub_btn_list) do
- value:SetSelectState(false)
- end
- end
-
- if sub_index == 0 then
- self.btn_list[index]:SetSelectState(true)
- else
- self.btn_list[index].sub_btn_list[sub_index]:SetSelectState(true)
- end
- end
-
- --传入主索引和所有要显示的子索引列表,取消红点只传入主索引
- function NewUICusVerTabBar:ShowRedDot(index, index_list)
- if self.btn_list[index] and self.btn_list[index].ShowRedDot then
- self.btn_list[index]:ShowRedDot(index_list)
- end
- end
-
- function NewUICusVerTabBar:SetSubOffset(offset)
- self.offset = offset
- end
|