Files
Endfield-Data/LuaScripts/UI/Panels/ShopPackage/ShopPackageCtrl.lua
2025-12-02 20:37:18 +07:00

389 lines
11 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local uiCtrl = require_ex('UI/Panels/Base/UICtrl')
local PANEL_ID = PanelId.ShopPackage
ShopPackageCtrl = HL.Class('ShopPackageCtrl', uiCtrl.UICtrl)
ShopPackageCtrl.m_shopGoodsInfos = HL.Field(HL.Table)
ShopPackageCtrl.m_getCellFunc = HL.Field(HL.Function)
ShopPackageCtrl.m_isControllerTarget = HL.Field(HL.Boolean) << false
ShopPackageCtrl.m_currNaviIndex = HL.Field(HL.Int) << 1
ShopPackageCtrl.m_needResetCurrNavi = HL.Field(HL.Boolean) << false
ShopPackageCtrl.s_messages = HL.StaticField(HL.Table) << {
[MessageConst.ON_READ_CASH_SHOP_GOODS] = '_UpdateContent',
}
ShopPackageCtrl.OnCreate = HL.Override(HL.Any) << function(self, arg)
self.m_shopGoodsInfos = arg.tabData.cashGoodsInfos
self.m_phase = arg.phase
table.sort(self.m_shopGoodsInfos, Utils.genSortFunction({ "soldOutSortValue", "cashShopPriority", "priority" }, true))
self.view.buyillustrateBtn.onClick:AddListener(function()
UIManager:Open(PanelId.InstructionBook, "ShopPackage_All")
end)
self:_InitShortCut()
if DeviceInfo.isPCorConsole then
self.view.contentScroll:SetSpace(Vector2(25, 40))
else
self.view.contentScroll:SetSpace(Vector2(40, 40))
end
self.m_getCellFunc = UIUtils.genCachedCellFunction(self.view.contentScroll)
self.view.contentScroll.onUpdateCell:AddListener(function(obj, index)
local cell = self.m_getCellFunc(obj)
local shopGoodsInfo = self.m_shopGoodsInfos[LuaIndex(index)]
self:_SetupCellView(cell, shopGoodsInfo)
if self.m_needResetCurrNavi and LuaIndex(index) == self.m_currNaviIndex then
UIUtils.setAsNaviTarget(cell.inputBindingGroupNaviDecorator)
self.m_needResetCurrNavi = false
end
end)
self:SetCurrNaviByGoodsId(arg.naviGoodsId)
if arg.naviGoodsId ~= nil and not string.isEmpty(arg.naviGoodsId) then
self.m_needResetCurrNavi = true
InputManagerInst:ToggleGroup(self.view.scrollGroupTarget.groupId, true)
end
self.view.contentScroll:UpdateCount(#self.m_shopGoodsInfos)
local cashShopCtrl = self.m_phase.cashShopCtrl
if cashShopCtrl == nil then
cashShopCtrl = self.m_phase.m_panel2Item[PanelId.CashShop].uiCtrl
end
self.view.controllerHintPlaceholder:InitControllerHintPlaceholder({
self.view.inputGroup.groupId,
arg.emptyCtrl.view.inputGroup.groupId,
cashShopCtrl.view.inputGroup.groupId,
})
end
ShopPackageCtrl.OnShow = HL.Override() << function(self)
end
ShopPackageCtrl._InitShortCut = HL.Method() << function(self)
if not DeviceInfo.usingController then
return
end
self:BindInputPlayerAction("cashshop_giftpack_goto_left", function()
InputManagerInst:ToggleGroup(self.view.scrollGroupTarget.groupId, false)
local leftCtrl = self.m_phase.m_panel2Item[PanelId.ShopGiftPackEmpty].uiCtrl
leftCtrl:NaviTargetCurrTab()
end, self.view.scrollGroupTarget.groupId)
self:BindInputPlayerAction("cashshop_navigation_4_dir_left", function()
self:_OnGoLeft()
end, self.view.scrollGroupTarget.groupId)
self:BindInputPlayerAction("cashshop_navigation_4_dir_up", function()
self:_OnGoUp()
end, self.view.scrollGroupTarget.groupId)
self:BindInputPlayerAction("cashshop_navigation_4_dir_right", function()
self:_OnGoRight()
end, self.view.scrollGroupTarget.groupId)
self:BindInputPlayerAction("cashshop_navigation_4_dir_down", function()
self:_OnGoDown()
end, self.view.scrollGroupTarget.groupId)
InputManagerInst:ToggleGroup(self.view.scrollGroupTarget.groupId, false)
end
ShopPackageCtrl._SetupCellView = HL.Method(HL.Table, HL.Table) << function(self, cell, shopGoodsInfo)
local shopGoodsId = shopGoodsInfo.goodsId
local succ, shopGoodsData = Tables.CashShopGoodsTable:TryGetValue(shopGoodsId)
if not succ then
logger.error("找不到商品配置:" .. shopGoodsId)
return
end
local stateCtrl = cell.stateController
local canBuy = CashShopUtils.CheckCanBuyCashShopGoods(shopGoodsId)
if canBuy then
stateCtrl:SetState("Sell")
else
stateCtrl:SetState("SellOut")
end
if shopGoodsData.goodsType == GEnums.CashGoodsType.MonthlyCard then
stateCtrl:SetState("MonthlyPass")
self:_SetupViewMonthlyPass(cell, shopGoodsId)
else
stateCtrl:SetState("Other")
end
local tagWidget = cell.cashShopItemTag
tagWidget:InitCashShopItemTag({
isCashShop = true,
shopGoodsInfo = shopGoodsInfo,
})
local name = CashShopUtils.GetCashGoodsName(shopGoodsId)
cell.groupBagNameTxt.text = name
cell.groupBagNameShadownTxt.text = name
cell.sellNumberTxt.text = CashShopUtils.getGoodsPriceText(shopGoodsId)
local path, imageName = CashShopUtils.GetCashGoodsImage(shopGoodsId)
cell.pattern:LoadSprite(path, imageName)
local _, cfg = Tables.GiftpackCashShopGoodsDataTable:TryGetValue(shopGoodsId)
if cfg and not string.isEmpty(cfg.bg) then
cell.bg:LoadSprite(UIConst.UI_SPRITE_SHOP_GROUP_BAG, cfg.bg)
end
if cfg and not string.isEmpty(cfg.deco) then
cell.bgDeco:LoadSprite(UIConst.UI_SPRITE_SHOP_GROUP_BAG, cfg.deco)
end
cell.contentBtn.onClick:RemoveAllListeners()
cell.contentBtn.onClick:AddListener(function()
GameInstance.player.cashShopSystem:ReadCashGoods(shopGoodsId)
EventLogManagerInst:GameEvent_GoodsViewClick(
"1",
shopGoodsInfo.cashShopId,
CashShopConst.CashShopCategoryType.Pack,
shopGoodsId
)
if shopGoodsData.goodsType == GEnums.CashGoodsType.MonthlyCard then
UIManager:Open(PanelId.ShopMonthlyDetail, {
goodsId = shopGoodsId,
goodsInfo = shopGoodsInfo,
})
else
UIManager:Open(PanelId.ShopGiftPackDetails, {
goodsId = shopGoodsId,
goodsInfo = shopGoodsInfo,
})
end
end)
cell.contentBtn.customBindingViewLabelText = Language.LUA_CASH_SHOP_PACKAGE_PANEL_BUTTON_BUTTON_KEY_HINT
end
ShopPackageCtrl._SetupCellViewTag = HL.Method(HL.Table, HL.Table) << function(self, cell, shopGoodsInfo)
local tagWidget = cell.cashShopItemTag
tagWidget:InitCashShopItemTag({
isCashShop = true,
shopGoodsInfo = shopGoodsInfo,
})
end
ShopPackageCtrl._SetupViewMonthlyPass = HL.Method(HL.Table, HL.String) << function(self, cell, goodsId)
local remainValidDays = GameInstance.player.monthlyPassSystem:GetRemainValidDays()
cell.monthlyPassNode.gameObject:SetActiveIfNecessary(remainValidDays > 0)
cell.dayNumberTxt.text = remainValidDays
end
ShopPackageCtrl._UpdateContent = HL.Method() << function(self)
logger.info("ShopPackageCtrl: 收到msg刷新content")
self.view.contentScroll:UpdateShowingCells(function(index, obj)
local cell = self.m_getCellFunc(obj)
local shopGoodsInfo = self.m_shopGoodsInfos[LuaIndex(index)]
self:_SetupCellViewTag(cell, shopGoodsInfo)
end)
end
ShopPackageCtrl.TargetFirstCell = HL.Method() << function(self)
logger.info("ShopPackageCtrl: TargetFirstCell")
InputManagerInst:ToggleGroup(self.view.scrollGroupTarget.groupId, true)
local range = self.view.contentScroll:GetShowRange()
local firstCell = self.m_getCellFunc(self.view.contentScroll:Get(range.x))
UIUtils.setAsNaviTarget(firstCell.inputBindingGroupNaviDecorator)
self.m_currNaviIndex = LuaIndex(range.x)
self:_SetSingleGoodsReadByIndex(self.m_currNaviIndex)
end
ShopPackageCtrl._OnGoLeft = HL.Method() << function(self)
logger.info("ShopPackageCtrl: _OnGoLeft")
local currIndex = self.m_currNaviIndex
local countPerLine = self.view.contentScroll.countPerLine
if currIndex and currIndex % countPerLine == 1 then
InputManagerInst:ToggleGroup(self.view.scrollGroupTarget.groupId, false)
local leftCtrl = self.m_phase.m_panel2Item[PanelId.ShopGiftPackEmpty].uiCtrl
leftCtrl:NaviTargetCurrTab()
logger.info("ShopPackageCtrl: _OnGoLeft: NaviTargetCurrTab, currIndex is: " .. currIndex)
else
local targetCell = self.m_getCellFunc(self.view.contentScroll:Get(CSIndex(currIndex - 1)))
UIUtils.setAsNaviTarget(targetCell.inputBindingGroupNaviDecorator)
self.m_currNaviIndex = self.m_currNaviIndex - 1
self:_SetSingleGoodsReadByIndex(self.m_currNaviIndex)
end
end
ShopPackageCtrl._OnGoUp = HL.Method() << function(self)
local countPerLine = self.view.contentScroll.countPerLine
local targetIndex = self.m_currNaviIndex - countPerLine
if targetIndex <= 0 then
return
end
local targetCell = self.m_getCellFunc(self.view.contentScroll:Get(CSIndex(targetIndex)))
UIUtils.setAsNaviTarget(targetCell.inputBindingGroupNaviDecorator)
self.m_currNaviIndex = targetIndex
self:_SetSingleGoodsReadByIndex(self.m_currNaviIndex)
end
ShopPackageCtrl._OnGoRight = HL.Method() << function(self)
local targetIndex = self.m_currNaviIndex + 1
if targetIndex > #self.m_shopGoodsInfos then
return
end
local targetCell = self.m_getCellFunc(self.view.contentScroll:Get(CSIndex(targetIndex)))
UIUtils.setAsNaviTarget(targetCell.inputBindingGroupNaviDecorator)
self.m_currNaviIndex = targetIndex
self:_SetSingleGoodsReadByIndex(self.m_currNaviIndex)
end
ShopPackageCtrl._OnGoDown = HL.Method() << function(self)
local countPerLine = self.view.contentScroll.countPerLine
local targetIndex = self.m_currNaviIndex + countPerLine
if targetIndex > #self.m_shopGoodsInfos then
local lineNumber = (#self.m_shopGoodsInfos // countPerLine) +
((#self.m_shopGoodsInfos % countPerLine > 0) and 1 or 0)
if self.m_currNaviIndex <= countPerLine * (lineNumber - 1) then
targetIndex = #self.m_shopGoodsInfos
else
return
end
end
local targetCell = self.m_getCellFunc(self.view.contentScroll:Get(CSIndex(targetIndex)))
UIUtils.setAsNaviTarget(targetCell.inputBindingGroupNaviDecorator)
self.m_currNaviIndex = targetIndex
self:_SetSingleGoodsReadByIndex(self.m_currNaviIndex)
end
ShopPackageCtrl.GetCurrNaviGoodsId = HL.Method().Return(HL.String) << function(self)
if self.m_currNaviIndex > #self.m_shopGoodsInfos then
return ""
else
return self.m_shopGoodsInfos[self.m_currNaviIndex].goodsId
end
end
ShopPackageCtrl.SetCurrNaviByGoodsId = HL.Method(HL.String) << function(self, goodsId)
if goodsId ~= nil then
for index, info in ipairs(self.m_shopGoodsInfos) do
if info.goodsId == goodsId then
self.m_currNaviIndex = index
return
end
end
end
self.m_currNaviIndex = 1
self:_SetSingleGoodsReadByIndex(self.m_currNaviIndex)
end
ShopPackageCtrl._SetSingleGoodsReadByIndex = HL.Method(HL.Number) << function(self, luaIndex)
local info = self.m_shopGoodsInfos[luaIndex]
if info ~= nil then
local goodsId = info.goodsId
GameInstance.player.cashShopSystem:ReadCashGoods(goodsId)
end
end
HL.Commit(ShopPackageCtrl)