--[[
|
|
@description: 一级主选项卡模板
|
|
--]]
|
|
|
|
CusVerMainTabsTemplate = CusVerMainTabsTemplate or BaseClass(BaseItem)
|
|
local CusVerMainTabsTemplate = CusVerMainTabsTemplate
|
|
local SetSizeDelta = SetSizeDelta
|
|
local SetAnchoredPosition = SetAnchoredPosition
|
|
|
|
function CusVerMainTabsTemplate:__init(parent_transform, main_tabs_sizebtn, subCon_offset, sub_tabs_class, sub_tabs_spacing, arrow_pos)
|
|
|
|
self.btn_width = main_tabs_sizebtn.x --tab按钮的宽度
|
|
self.btn_height = main_tabs_sizebtn.y --tab按钮的高度
|
|
self.sub_tabs_width = main_tabs_sizebtn.x - 8 --下一级tab按钮的宽度
|
|
self.subCon_offset = subCon_offset
|
|
self.sub_tabs_class = sub_tabs_class
|
|
self.sub_tabs_spacing = sub_tabs_spacing
|
|
self.arrow_pos = arrow_pos or -30
|
|
self.have_sub_tabs = sub_tabs_class and true or false
|
|
|
|
self.index = nil
|
|
self.show = false -- 是否展开
|
|
self.select_anything = false -- 是否可以在有子类的时候选中大类
|
|
self.force_size = false -- 是否强制设置子选项卡大小
|
|
self.sub_tabs_data = nil
|
|
self.sub_tabs_count = 0
|
|
self.sub_btn_list = {}
|
|
self.select_sub_index = nil
|
|
self.sub_tabs_load_complete = false
|
|
|
|
self.sub_red_list = nil
|
|
|
|
self:Initialization()
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:__delete()
|
|
for i,v in ipairs(self.sub_btn_list) do
|
|
v:DeleteMe()
|
|
v = nil
|
|
end
|
|
self.sub_btn_list = {}
|
|
end
|
|
|
|
-- 子类中重写,自定义预设
|
|
function CusVerMainTabsTemplate:Initialization()
|
|
self.layout_file = "UIVerTabBtn"
|
|
--self.base_file = ""
|
|
self.prefab_asset = UiFactory.getPrefab(UIType.UIVerTabBtn)
|
|
self:Load()
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:Load_callback()
|
|
|
|
self.button,
|
|
self.iconObj,
|
|
self.red_dot
|
|
= GetChildGameObjects(self.transform,
|
|
{
|
|
"tab",
|
|
"tab/icon",
|
|
"tab/dot"
|
|
})
|
|
|
|
self.btnText = GetChildTexts(self.transform,
|
|
{
|
|
"tab/Text",
|
|
})
|
|
|
|
self.icon_tran,
|
|
self.subCon,
|
|
self.tab_btn_tra
|
|
= GetChildTransforms(self.transform,
|
|
{
|
|
"tab/icon",
|
|
"subCon",
|
|
"tab"
|
|
})
|
|
|
|
self.imgSelect,
|
|
self.tab_btn_img,
|
|
self.arrow_img
|
|
= GetChildImages(self.transform,
|
|
{
|
|
"tab/selectBg",
|
|
"tab",
|
|
"tab/icon",
|
|
})
|
|
self.select_go = self.imgSelect.gameObject
|
|
|
|
SetAnchoredPosition(self.icon_tran, self.arrow_pos, 0)
|
|
|
|
self:LoadCallbackAfter()
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:LoadCallbackAfter()
|
|
|
|
lua_resM:setImageSprite(self, self.tab_btn_img,"common_asset","tyui_btn2_2")
|
|
lua_resM:setImageSprite(self, self.imgSelect,"common_asset","tyui_btn2_1")
|
|
lua_resM:setImageSprite(self, self.arrow_img, "common_asset", "tyui_btn3_4", true)
|
|
self.btnText.fontSize = 26
|
|
|
|
SetSizeDelta(self.select_go.transform, self.btn_width, self.btn_height)
|
|
SetSizeDelta(self.tab_btn_tra, self.btn_width, self.btn_height)
|
|
if self.subCon_offset then
|
|
SetAnchoredPosition(self.subCon, self.subCon_offset.x, - self.btn_height- self.subCon_offset.y)
|
|
else
|
|
SetAnchoredPosition(self.subCon, 0, -self.btn_height)
|
|
end
|
|
self.btnText.color = Color(167/255, 119/255, 85/255, 1)
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:SetTabsDetail(tab_data, index, call_back, select_anything, force_size)
|
|
|
|
self.sub_tabs_data = {}
|
|
self.index = index
|
|
self.call_back = call_back
|
|
self.select_anything = select_anything
|
|
self.force_size = force_size
|
|
self.select_sub_index = 0
|
|
|
|
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_tabs_data, tab_data[tab_index])
|
|
end
|
|
self.sub_tabs_count = #self.sub_tabs_data
|
|
|
|
--是否显示展开标志
|
|
if self.sub_tabs_count == 0 then
|
|
self.iconObj:SetActive(false)
|
|
else
|
|
self.iconObj:SetActive(true)
|
|
end
|
|
self.need_refresh_tabs = true
|
|
self.sub_tabs_load_complete = false
|
|
--self:SetSubTabs(self.sub_tabs_data, self.index, call_back)
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:SetSubTabs(sub_tab_list, parent_index, call_back)
|
|
|
|
if self.force_size then
|
|
self.sub_tabs_class.real_height = self.btn_height
|
|
else
|
|
self.sub_tabs_class.real_height = self.sub_tabs_class.Height or 76
|
|
end
|
|
local sub_tab = nil
|
|
for i,v in ipairs(sub_tab_list) do
|
|
sub_tab = self.sub_btn_list[i]
|
|
if sub_tab == nil then
|
|
sub_tab = self.sub_tabs_class.New(self.subCon)
|
|
self.sub_btn_list[i] = sub_tab
|
|
if self.force_size then
|
|
sub_tab:SetTabSubBtnSize(self.sub_tabs_width, self.btn_height)
|
|
end
|
|
end
|
|
sub_tab:SetSubDeail(v, parent_index, i, call_back)
|
|
sub_tab:SetVisible(true)
|
|
|
|
local x = 0
|
|
local y = -(i - 1) * self.sub_tabs_class.real_height
|
|
x = self.sub_tabs_spacing and self.sub_tabs_spacing.x + x or x
|
|
y = self.sub_tabs_spacing and -(i - 1) * self.sub_tabs_spacing.y + y or y
|
|
sub_tab:SetPosition(x, y)
|
|
|
|
local function onBtnClickHandler(target)
|
|
self:SetSelectSubIndex(parent_index, i)
|
|
end
|
|
if sub_tab.Button then
|
|
AddClickEvent(sub_tab.Button, onBtnClickHandler, 2)
|
|
end
|
|
|
|
end
|
|
for i = #sub_tab_list + 1, #self.sub_btn_list do
|
|
self.sub_btn_list[i]:SetVisible(false)
|
|
end
|
|
self.subCon.gameObject:SetActive(false)
|
|
end
|
|
|
|
-- 处理选中tabs
|
|
function CusVerMainTabsTemplate:SetSelectMode(index, sub_index, force_show, quick_select)
|
|
--print("CusVerMainTabsTemplate:SetSelectMode() main = "..tostring(index)..", sub = "..tostring(sub_index))
|
|
if self.need_refresh_tabs then
|
|
self:SetSubTabs(self.sub_tabs_data, self.index, self.call_back)
|
|
self.need_refresh_tabs = false
|
|
self.sub_tabs_load_complete = true
|
|
|
|
if self.cache_redDot_list then
|
|
self:ShowRedDot(self.cache_redDot_list)
|
|
self.cache_redDot_list = nil
|
|
end
|
|
end
|
|
|
|
if self.index == index then
|
|
self.show = not self.show
|
|
else
|
|
self.show = false
|
|
end
|
|
|
|
if self.sub_tabs_count == 0 then
|
|
self.show = true
|
|
end
|
|
|
|
if force_show and self.index == index then
|
|
self.show = true
|
|
end
|
|
|
|
if self.show and index and self.select_sub_index == 0 then
|
|
sub_index = sub_index or self:HandleDefaultSelectSubIndex(quick_select)
|
|
if sub_index > 0 then
|
|
self:SetSelectSubIndex(index, sub_index)
|
|
else
|
|
self:HandleCallback()
|
|
end
|
|
elseif not self.show and index and self.select_anything then
|
|
self:HandleCallback()
|
|
if self.select_sub_index ~= 0 then
|
|
self:SetSelectSubIndex(0, 0)
|
|
end
|
|
end
|
|
|
|
self:RefreshTabsSelectState(self.show, self.index == index)
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:HandleDefaultSelectSubIndex(quick_select)
|
|
|
|
local default_index = 0
|
|
if quick_select and self.sub_red_list and #self.sub_red_list > 0 then
|
|
default_index = self.sub_red_list[1]
|
|
elseif quick_select and self.sub_tabs_count > 0 and not self.select_anything then
|
|
default_index = 1
|
|
end
|
|
|
|
return default_index
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:HandleCallback()
|
|
if self.sub_tabs_count > 0 then
|
|
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 CusVerMainTabsTemplate:RefreshTabsSelectState(show, is_select)
|
|
if not self.have_sub_tabs then
|
|
return
|
|
end
|
|
|
|
self.show = show
|
|
|
|
if self.select_anything and is_select then
|
|
self:SetSelectState(true)
|
|
else
|
|
self:SetSelectState(self.show)
|
|
end
|
|
|
|
if self.show then
|
|
lua_resM:setImageSprite(self, self.arrow_img, "common_asset", "tyui_btn3_4a", true)
|
|
--SetRotate(self.icon_tran, 0, 0, -90)
|
|
else
|
|
lua_resM:setImageSprite(self, self.arrow_img, "common_asset", "tyui_btn3_4", true)
|
|
--SetLocalRotation(self.icon_tran, 0, 0, 0)
|
|
end
|
|
|
|
if self.subCon and self.sub_btn_list and #self.sub_btn_list ~= 0 then
|
|
self.subCon.gameObject:SetActive(show)
|
|
end
|
|
end
|
|
|
|
-- 设置选中的子一级选项卡
|
|
function CusVerMainTabsTemplate:SetSelectSubIndex(main_index, sub_index)
|
|
if not self.have_sub_tabs then
|
|
return
|
|
end
|
|
--print("CusVerMainTabsTemplate:SetSelectSubIndex() main = "..main_index..", sub = "..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)
|
|
else
|
|
v:SetSelected(false)
|
|
end
|
|
end
|
|
self.select_sub_index = sub_index
|
|
end
|
|
|
|
-- 子选项卡个数(加载完但不显示的不需计算高度)
|
|
function CusVerMainTabsTemplate:GetVisibleCount(visible)
|
|
local count = 0
|
|
if self.have_sub_tabs then
|
|
for _,v in ipairs(self.sub_btn_list) do
|
|
if v:GetVisible() == visible or not v.is_loaded then
|
|
count = count + 1
|
|
end
|
|
end
|
|
end
|
|
return count
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:SetSelectState(bool)
|
|
self.select_go:SetActive(bool)
|
|
|
|
if self.btnText then
|
|
self.btnText.color = bool and Color(131/255, 87/255, 65/255, 1) or Color(167/255, 119/255, 85/255, 1)
|
|
end
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:ShowRedDot(list)
|
|
if not self.sub_tabs_load_complete then
|
|
self.cache_redDot_list = list
|
|
end
|
|
|
|
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 and list[0] then
|
|
self.red_dot:SetActive(true)
|
|
end
|
|
end
|
|
|
|
function CusVerMainTabsTemplate:SetPosition(x, y)
|
|
BaseItem.SetAnchoredPosition(self, x, y)
|
|
end
|