c1_lua/lua/app/ui/common/common_box_ui.lua
2025-09-25 20:12:22 +08:00

177 lines
6.1 KiB
Lua
Executable File

local CommonBoxUI = class("CommonBoxUI", BaseUI)
function CommonBoxUI:isFullScreen()
return false
end
function CommonBoxUI:showCommonBG()
return false
end
function CommonBoxUI:getPrefabPath()
return "assets/prefabs/ui/common/common_box_ui.prefab"
end
function CommonBoxUI:onPressBackspace()
self:closeUI()
end
function CommonBoxUI:onClose()
end
function CommonBoxUI:ctor(params)
self.id = params.id
self.showTips = params.showTips
self.maxCount = DataManager.BagData.ItemData:getItemNumById(self.id)
self.itemType = ModuleManager.ItemManager:getItemType(self.id)
local cfg = ConfigManager:getConfig("item")[self.id]
if self.itemType == GConst.ItemConst.ITEM_TYPE.BOX_RANDOM then
-- 随机宝箱
self.curCount = self.maxCount
self.rewards = cfg and cfg.box_drop
elseif self.itemType == GConst.ItemConst.ITEM_TYPE.BOX_SELECT then
-- 自选宝箱
self.curCount = 1
self.rewards = cfg and cfg.box_select
else
self.curCount = 1
self.rewards = cfg and cfg.box_drop
end
if self.rewards == nil then
self:closeUI()
end
end
function CommonBoxUI:onLoadRootComplete()
local uiMap = self.root:genAllChildren()
self.mask = uiMap["common_box_ui.mask"]
self.contentNode = uiMap["common_box_ui.content"]
self.btnClose = uiMap["common_box_ui.content.btn_close"]
self.rewardCell = uiMap["common_box_ui.content.info_node.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
self.txName = uiMap["common_box_ui.content.info_node.tx_name"]
self.txType = uiMap["common_box_ui.content.info_node.tx_type"]
self.txCount = uiMap["common_box_ui.content.info_node.tx_count"]
self.txDesc = uiMap["common_box_ui.content.desc.tx_desc"]
self.scrollrect = uiMap["common_box_ui.content.scrollrect"]
self.selectNode = uiMap["common_box_ui.content.select"]
self.txSelect = uiMap["common_box_ui.content.select.tx_select"]
self.btnSub = uiMap["common_box_ui.content.select.btn_sub"]
self.btnAdd = uiMap["common_box_ui.content.select.btn_add"]
self.btnMax = uiMap["common_box_ui.content.select.btn_max"]
self.txMax = uiMap["common_box_ui.content.select.btn_max.tx_max"]
self.btnUse = uiMap["common_box_ui.content.btn_use"]
self.txUse = uiMap["common_box_ui.content.btn_use.tx_desc"]
self.txMax:setText(I18N:getGlobalText(I18N.GlobalConst.ITEM_DESC_2))
self.txUse:setText(I18N:getGlobalText(I18N.GlobalConst.USING_DESC))
self.btnSub:addClickListener(function()
if self.curCount <= 1 then
return
end
self.curCount = self.curCount - 1
self:onRefresh()
end)
self.btnAdd:addClickListener(function()
if self.curCount >= self.maxCount then
return
end
self.curCount = self.curCount + 1
self:onRefresh()
end)
self.btnMax:addClickListener(function()
if self.curCount >= self.maxCount then
return
end
self.curCount = self.maxCount
self:onRefresh()
end)
self.btnUse:addClickListener(function()
if self.itemType == GConst.ItemConst.ITEM_TYPE.BOX_SELECT and (self.curIndex == nil or self.curIndex <= 0) then
GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ITEM_DESC_3))
return
end
ModuleManager.ItemManager:reqOpenBoxReward(self.id, self.curCount, self.curIndex)
self:closeUI()
end)
self.mask:addClickListener(function()
self:closeUI()
end)
self.btnClose:addClickListener(function()
self:closeUI()
end)
end
function CommonBoxUI:onRefresh()
self.rewardCell:refreshById(self.id)
self.txName:setText(ModuleManager.ItemManager:getItemName(self.id))
self.txType:setText(I18N:getGlobalText(I18N.GlobalConst["ITEM_TYPE_" .. ModuleManager.ItemManager:getItemType(self.id)]))
self.txCount:setText(I18N:getGlobalText(I18N.GlobalConst.ITEM_DESC_1, self.maxCount))
self.txDesc:setText(ModuleManager.ItemManager:getItemDesc(self.id))
self.txSelect:setText(self.curCount)
if self.rewards then
if self.scrollRect == nil then
self.scrollRect = self.scrollrect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
self.scrollRect:addInitCallback(function()
return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
end)
self.scrollRect:addRefreshCallback(function(index, cell)
cell:refreshByConfig(self.rewards[index])
cell:setShowSelect(index == self.curIndex)
cell:setShowProbTag(self.itemType == GConst.ItemConst.ITEM_TYPE.BOX_RANDOM, self:getRewardProb(index))
if index ~= self.curIndex and self.itemType == GConst.ItemConst.ITEM_TYPE.BOX_SELECT then
cell:addClickListener(function()
self:onClickReward(index)
end)
else
-- cell:setClickShowTips()
end
end)
end
if self.scrollRect:getTotalCount() == nil or self.scrollRect:getTotalCount() <= 0 then
self.scrollRect:refillCells(#self.rewards)
elseif self.scrollRect:getTotalCount() ~= #self.rewards then
self.scrollRect:clearCells()
self.scrollRect:refillCells(#self.rewards)
else
self.scrollRect:updateAllCell()
end
end
if self.showTips then
-- 展示宝箱tips
self.selectNode:setActive(false)
self.btnUse:setActive(false)
self.contentNode:setSizeDeltaY(500)
else
-- 展示宝箱开奖励
self.selectNode:setActive(true)
self.btnUse:setActive(true)
self.contentNode:setSizeDeltaY(648)
end
end
function CommonBoxUI:onClickReward(index)
self.curIndex = index
self:onRefresh()
end
function CommonBoxUI:getRewardProb(index)
if self.itemType ~= GConst.ItemConst.ITEM_TYPE.BOX_RANDOM then
return
end
local total = 0
local weight = self.rewards[index].weight or 0
for i, reward in ipairs(self.rewards) do
total = total + (reward.weight or 0)
end
return math.floor((weight / total) * 1000) / 10
end
return CommonBoxUI