c1_lua/lua/app/ui/main_city/main_city_ui.lua
2023-05-12 21:07:05 +08:00

560 lines
18 KiB
Lua

local UIPrefabObject = require "app/bf/unity/uiprefab_object"
local MainCityUI = class("MainCityUI", BaseUI)
local BF_UI_HELPER = GConst.TYPEOF_UNITY_CLASS.BF_UI_HELPER
local HERO_COMP = "app/ui/hero/hero_comp"
local MAIN_COMP = "app/ui/main_city/component/main_comp"
local SHOP_COMP = "app/ui/shop/shop_comp"
local BOTTOM_BTN_CELL = "app/ui/main_city/cell/bottom_btn_cell"
MainCityUI.CLICK_BTN_TYPE = {
[1] = "HOME",
[2] = "HERO",
[3] = "SHOP",
}
local MAIN_COMP_INDEX = 1
function MainCityUI:getUIType()
return UIManager.UI_TYPE.MAIN
end
function MainCityUI:getBGMId()
return AudioManager.BGM_ID.MAINCITY
end
function MainCityUI:getCurrencyParams()
if self.currencyParams == nil then
self.currencyParams = {
itemIds = {}
}
end
self.selectedIndex = self.selectedIndex or MAIN_COMP_INDEX
self.currencyParams.showType = GConst.CURRENCY_TYPE.HORIZONTAL
for k, v in ipairs(self.currencyParams.itemIds) do
table.remove(self.currencyParams.itemIds)
end
if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_VIT
self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_GOLD
elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.HERO then
self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_VIT
self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_GOLD
elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.SHOP then
self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_VIT
self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_GOLD
self.currencyParams.itemIds[3] = GConst.ItemConst.ITEM_ID_GEM
end
return self.currencyParams
end
function MainCityUI:getPrefabPath()
return "assets/prefabs/ui/main_city/main_ui.prefab"
end
function MainCityUI:ctor(params)
self.isFirstEnter = params and params.isFirstEnter
self.targetIndex = params and params.targetIndex
end
function MainCityUI:onLoadRootComplete()
local uiMap = self.root:genAllChildren()
self.uiMap = uiMap
self.bottomBgSelected = uiMap["main_ui.bottom_node.bottom_bg_selected"]
self.bottomBgSelected:setLocalPositionX(0)
self:_display()
self:_addListeners()
self:_bind()
self:updateTime()
self:refreshBottomRp()
self:scheduleGlobal(function()
self:updateTime()
end, 1)
end
function MainCityUI:onRefresh()
self:refreshLeftBtns()
self:refreshRightBtns()
end
function MainCityUI:onReshow()
self:checkMainPop()
end
function MainCityUI:onSetUIOrder()
if self.subComps then
for index, comp in pairs(self.subComps) do
local order = self._baseRootCanvas.sortingOrder
local uiHelper = comp:getGameObject():GetComponent(BF_UI_HELPER)
if uiHelper then
uiHelper:SetSortingOrder(order + 1)
end
end
end
end
function MainCityUI:_display()
self:initBottomUI()
self:initComp()
self:initLeftRightBtns()
self:initPlayerInfo()
self:initTopNode()
self:refreshBottom()
end
function MainCityUI:_addListeners()
self:addEventListener(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP, function()
self:checkMainPop()
end)
end
function MainCityUI:_bind()
self:bind(DataManager.ChapterData, "isDirty", function()
if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
if self.subComps[self.selectedIndex] then
self.subComps[self.selectedIndex]:refreshChapter(true)
end
end
end, true)
self:bind(DataManager.PlayerData, "dirty", function(binder, value)
self:refreshPlayerInfo()
end, true)
self:bind(DataManager.PlayerData, "lvUpDirty", function(binder, value)
self:checkMainPop()
end)
self:bind(DataManager.FormationData, "dirty", function(binder, value)
if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.HERO then
self.subComps[self.selectedIndex]:refresh()
end
end)
self:bind(DataManager.HeroData, "isDirty", function(binder, value)
if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.HERO then
if self.subComps[self.selectedIndex] then
self.subComps[self.selectedIndex]:refresh()
end
self:refreshBottomRp()
elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
if self.subComps[self.selectedIndex] then
self.subComps[self.selectedIndex]:refreshStageFormaion()
end
end
end)
self:bind(DataManager.BagData.ItemData, "dirty", function(binder, value)
if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN and self.subComps[self.selectedIndex] then
if self.subComps[self.selectedIndex] then
self.subComps[self.selectedIndex]:refreshStageFormaion()
end
end
end)
self:bind(DataManager.DailyTaskData, "redPointFlag", function()
self:refreshTask()
end)
end
function MainCityUI:initBottomUI()
local uiMap = self.root:genAllChildren()
self.bottomBtnCells = {}
self.bottomBtnSpines = {}
for i = 1, 3 do
local cellCom = CellManager:addCellComp(uiMap["main_ui.bottom_node.bottom_btn_cell_" .. i], BOTTOM_BTN_CELL)
cellCom:addClickListener(function()
if self.selectedIndex == i then
return
end
if i <= #MainCityUI.CLICK_BTN_TYPE then
BIReport:postHomeBtnCilck(BIReport.CLICK_BTN_TYPE[MainCityUI.CLICK_BTN_TYPE[i]])
end
self.bottomBtnSpines[i]:playAnimComplete("born", false, false, function()
self.bottomBtnSpines[i]:playAnim("idle", false, false)
end)
self:refreshBottom(i, true)
end)
table.insert(self.bottomBtnCells, cellCom)
self.bottomBtnSpines[i] = uiMap["main_ui.bottom_node.icons.ui_spine_obj_" .. i]
self.bottomBtnSpines[i]:playAnim("idle", false, false)
end
end
function MainCityUI:initComp()
if not self.subComps then
local uiMap = self.root:genAllChildren()
self.subComps = {}
-- 主城
local mainComp = uiMap["main_ui.sub_ui_node.main_comp"]
mainComp:initPrefabHelper()
mainComp:genAllChildren()
self.mainComp = mainComp:addLuaComponent(MAIN_COMP)
self.subComps[GConst.MainCityConst.BOTTOM_PAGE.MAIN] = self.mainComp
-- 英雄
local heroComp = uiMap["main_ui.sub_ui_node.hero_ui"]
heroComp:initPrefabHelper()
heroComp:genAllChildren()
self.subComps[GConst.MainCityConst.BOTTOM_PAGE.HERO] = heroComp:addLuaComponent(HERO_COMP)
self:onSetUIOrder()
-- 商城
local shopComp = uiMap["main_ui.sub_ui_node.shop_comp"]
shopComp:initPrefabHelper()
shopComp:genAllChildren()
self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP] = shopComp:addLuaComponent(SHOP_COMP)
end
end
function MainCityUI:refreshBottom(selectedIndex, playAnim)
if self.targetIndex then
selectedIndex = self.targetIndex
playAnim = true
self.targetIndex = nil
end
if selectedIndex and (not self.subComps[selectedIndex]) then
return
end
local oldIndex = self.selectedIndex
self.selectedIndex = selectedIndex and selectedIndex or MAIN_COMP_INDEX
self:switchComp()
-- 动效
if playAnim and (oldIndex ~= selectedIndex) then
local targetX = self.bottomBtnCells[self.selectedIndex]:getCurLocalPosX()
local isLeft = self.selectedIndex < oldIndex
local offset = isLeft and -20 or 20
GFunc.goTargetPosXShake(self.bottomBgSelected, nil, targetX, offset)
self:refreshBottomCell(true)
else
self:refreshBottomCell(false)
end
end
function MainCityUI:refreshBottomCell(showAni)
for i, cell in ipairs(self.bottomBtnCells) do
cell:refresh(I18N:getGlobalText("MAIN_BTN_" .. i), i == self.selectedIndex, showAni)
end
end
function MainCityUI:initLeftRightBtns()
self.leftNode = self.uiMap["main_ui.left_node"]
self.leftSideBar = self.uiMap["main_ui.left_node.side_bar"]
self.leftArrowNode = self.uiMap["main_ui.left_node.arrow_node"]
self.leftArrowBtn = self.uiMap["main_ui.left_node.arrow_node.arrow"]
self.leftArrowBtn:addClickListener(function()
self:openOrCloseLeftSideBar()
end)
self.rightNode = self.uiMap["main_ui.right_node"]
self.rightSideBar = self.uiMap["main_ui.right_node.side_bar"]
self.rightArrowNode = self.uiMap["main_ui.right_node.arrow_node"]
self.rightArrowBtn = self.uiMap["main_ui.right_node.arrow_node.arrow"]
self.rightArrowBtn:addClickListener(function()
self:openOrCloseRightSideBar()
end)
self.sideBarCellObject = self.uiMap["main_ui.cache_node.side_bar_cell"]
local w, h = self.sideBarCellObject:fastGetSizeDelta()
self.sideBarHeight = h
if self.leftBarList == nil then
self.leftBarList = {}
end
if self.rightBarList == nil then
self.rightBarList = {}
end
if self.sideBarClassMap == nil then
self.sideBarClassMap = {}
end
if self.sideBarCellMap == nil then
self.sideBarCellMap = {}
end
self:recycleSideBarCells(self.leftSideBar)
self:recycleSideBarCells(self.rightSideBar)
end
function MainCityUI:recycleSideBarCells(list)
local childList = list:getChildList()
if childList and #childList > 0 then
local count = #childList
for i = count, 1, -1 do
local child = childList[i]
local name = child:getName()
if name and name ~= "" then
child:setVisible(false)
self.sideBarCellMap[name] = child:getLuaComponent(name)
else
child:destroy()
table.remove(childList, i)
end
end
end
end
function MainCityUI:addSideBarCellComp(cellClassPath)
local prefab = CS.UnityEngine.Object.Instantiate(self.sideBarCellObject:getGameObject())
local prefabObject = UIPrefabObject:create()
prefabObject:initWithPrefab(GConst.EMPTY_STRING, prefab)
prefabObject:initPrefabHelper()
prefabObject:genAllChildren()
prefabObject:setName(cellClassPath)
return prefabObject:addLuaComponent(cellClassPath)
end
function MainCityUI:openOrCloseLeftSideBar()
ModuleManager.MaincityManager:changeMainCityLeftSideBarOpenOrClose()
self:refreshLeftBtns()
end
function MainCityUI:refreshLeftBtns()
self:clearSideBarList(self.leftBarList)
local list = GConst.MainCityConst.LEFT_SIDE_BARS
if #list <= 0 then
return
end
local isClose = ModuleManager.MaincityManager:getIsMainCityLeftSideBarClose()
for k, v in ipairs(list) do
local CellClass = self.sideBarClassMap[v]
if CellClass == nil then
CellClass = require(v)
self.sideBarClassMap[v] = CellClass
end
if CellClass:getIsOpen() then
local cell = self.sideBarCellMap[v]
if cell == nil then
cell = self:addSideBarCellComp(v)
cell:getBaseObject():setParent(self.leftSideBar, false)
end
cell:setCellPath(v)
table.insert(self.leftBarList, cell)
end
end
local y = - 2 - self.sideBarHeight/2
if isClose then -- 只显示一个
local first = self.leftBarList[1]
first:setAnchoredPositionY(y)
first:setVisible(true)
first:refresh()
y = y - self.sideBarHeight - 2
for i = 2, #self.leftBarList do
self.leftBarList[i]:setVisible(false)
self.leftBarList[i]:refresh()
end
else
for k, v in ipairs(self.leftBarList) do
v:setAnchoredPositionY(y)
v:setVisible(true)
v:refresh()
y = y - self.sideBarHeight - 2
end
end
self.leftSideBar:setSizeDeltaY(-y)
self.leftArrowNode:setLocalScale(1, isClose and -1 or 1, 1)
self.leftArrowNode:setAnchoredPositionY(self.leftSideBar:fastGetAnchoredPositionY() + y + 20)
end
function MainCityUI:openOrCloseRightSideBar()
ModuleManager.MaincityManager:changeMainCityRightSideBarOpenOrClose()
self:refreshRightBtns()
end
function MainCityUI:refreshRightBtns()
self:clearSideBarList(self.rightBarList)
local list = GConst.MainCityConst.RIGHT_SIDE_BARS
if #list <= 0 then
return
end
local isClose = ModuleManager.MaincityManager:getIsMainCityRightSideBarClose()
for k, v in ipairs(list) do
local CellClass = self.sideBarClassMap[v]
if CellClass == nil then
CellClass = require(v)
self.sideBarClassMap[v] = CellClass
end
if CellClass:getIsOpen() then
local cell = self.sideBarCellMap[v]
if cell == nil then
cell = self:addSideBarCellComp(v)
cell:getBaseObject():setParent(self.rightSideBar, false)
end
cell:setCellPath(v)
table.insert(self.rightBarList, cell)
end
end
local y = -2 - self.sideBarHeight/2
if isClose then -- 只显示一个
local first = self.rightBarList[1]
first:setAnchoredPositionY(y)
first:setVisible(true)
first:refresh()
y = y - self.sideBarHeight - 2
for i = 2, #self.rightBarList do
self.rightBarList[i]:setVisible(false)
self.rightBarList[i]:refresh()
end
else
for k, v in ipairs(self.rightBarList) do
v:setAnchoredPositionY(y)
v:setVisible(true)
v:refresh()
y = y - self.sideBarHeight - 2
end
end
self.rightSideBar:setSizeDeltaY(-y)
self.rightArrowNode:setLocalScale(1, isClose and -1 or 1, 1)
self.rightArrowNode:setAnchoredPositionY(self.rightSideBar:fastGetAnchoredPositionY() + y + 20)
end
function MainCityUI:clearSideBarList(sideBarList)
local count = #sideBarList
for i = 1, count do
local cell = table.remove(sideBarList)
self.sideBarCellMap[cell:getCellPath()] = cell
end
end
function MainCityUI:setSideBarVisible(visible)
self.leftNode:setVisible(visible)
self.rightNode:setVisible(visible)
end
function MainCityUI:initPlayerInfo()
self.playerSlider = self.uiMap["main_ui.player_node.slider"]
self.playerLvTx = self.uiMap["main_ui.player_node.lv"]
end
function MainCityUI:refreshPlayerInfo()
local lv = DataManager.PlayerData:getLv()
self.playerLvTx:setText(GConst.INT_TO_STRING[lv] or tostring(lv))
self.playerSlider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = DataManager.PlayerData:getExpPercent()
end
function MainCityUI:initTopNode()
self.topNode = self.uiMap["main_ui.top_node"]
self:initTask()
self:initBounty()
self:initSetting()
end
function MainCityUI:initTask()
self.taskBtn = self.uiMap["main_ui.top_node.task_btn"]
self.taskBtn:addClickListener(function()
ModuleManager.TaskManager:showTaskMainUI()
end)
end
function MainCityUI:initBounty()
self.bountyNode = self.uiMap["main_ui.top_node.bounty_node"]
self.bountyBanner = self.uiMap["main_ui.top_node.bounty_node.banner"]
self.bountyBanner:addClickListener(function()
ModuleManager.BountyManager:showBountyMainUI()
end)
end
function MainCityUI:initSetting()
self.settingbtn = self.uiMap["main_ui.top_node.setting_btn"]
self.settingbtn:addClickListener(function()
ModuleManager.GameSettingManager:showSelectOtherBtnUI()
end)
end
function MainCityUI:setTopNodeVisible(visible)
self.topNode:setVisible(visible)
end
function MainCityUI:refreshTopNode()
self:refreshBounty()
self:refreshTask()
end
function MainCityUI:refreshBounty()
local isOpen = DataManager.BountyData:getIsOpen()
if not isOpen then
self.bountyNode:setVisible(false)
return
end
self.bountyNode:setVisible(true)
local bannerName = DataManager.BountyData:getBannerName()
self.bountyBanner:setSprite(GConst.ATLAS_PATH.BOUNTY, bannerName)
end
function MainCityUI:refreshTask()
local isOpen = DataManager.DailyTaskData:getIsOpen()
if not isOpen then
self.taskBtn:setVisible(false)
return
end
self.taskBtn:setVisible(true)
local showRedPoint = DataManager.DailyTaskData:getIsShowRedPoint()
if showRedPoint then
self.taskBtn:addRedPoint(42, 42, 0.8)
else
self.taskBtn:removeRedPoint()
end
end
function MainCityUI:switchComp(index)
index = index or self.selectedIndex
for i, comp in pairs(self.subComps) do
comp:getBaseObject():setActive(i == index)
if i == index then
comp:refresh()
self:updateTime()
end
end
self:updateCurrencyBar()
if self.selectedIndex == MAIN_COMP_INDEX then
self:checkMainPop()
self:setTopNodeVisible(true)
self:refreshTopNode()
self:setSideBarVisible(true)
else
self:setTopNodeVisible(false)
self:setSideBarVisible(false)
end
end
function MainCityUI:updateTime()
end
function MainCityUI:refreshBottomRp()
local uiMap = self.root:genAllChildren()
local heroRpObj = uiMap["main_ui.bottom_node.icons.ui_spine_obj_2.rp_node"]
if DataManager.HeroData:getRp() then
heroRpObj:addRedPoint(0, 0, 1)
else
heroRpObj:removeRedPoint()
end
end
function MainCityUI:checkMainPop()
-- 引导
if self:checkTutorial() then
return
end
end
-- 检查引导
function MainCityUI:checkTutorial()
if DataManager.ChapterData:getMaxChapterId() == 1 then
if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.PASS_ONE_CHAPTER) then
return
end
end
if DataManager.ChapterData:boxCanGet(2, 1) then
if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.TWO_CHAPTER_BOX_CAN_GOT) then
return
end
end
if DataManager.ChapterData:getMaxChapterId() == 2 then
if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.PASS_THREE_CHAPTER) then
return
end
end
end
return MainCityUI