c1_lua/lua/app/ui/common/common_exchange_ui.lua
2023-08-31 20:18:14 +08:00

102 lines
3.2 KiB
Lua

local CommonExchangeUI = class("CommonExchangeUI", BaseUI)
function CommonExchangeUI:ctor(params)
self.defaultNum = params.defaultNum
self.remainNum = params.remainNum
self.reward = params.reward
self.cost = params.cost
self.callback = params.callback
end
function CommonExchangeUI:getPrefabPath()
return "assets/prefabs/ui/common/exchange_ui.prefab"
end
function CommonExchangeUI:onLoadRootComplete()
self:_display()
self:_addListeners()
end
function CommonExchangeUI:onClose()
if self.inputComp then
self.inputComp.onValueChanged:RemoveAllListeners()
end
end
function CommonExchangeUI:_display()
local uiMap = self.root:genAllChildren()
if not self.rewardCell then
self.rewardCell = CellManager:addCellComp(uiMap["exchange_ui.bg.reward_cell"], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
end
self.rewardCell:refreshByConfig(self.reward)
uiMap["exchange_ui.bg.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_18))
uiMap["exchange_ui.bg.tx_remain"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_19, self.remainNum))
uiMap["exchange_ui.bg.ok_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_18))
self.inputComp = uiMap["exchange_ui.bg.input"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TMP_INPUT_FIELD)
self.inputComp.onValueChanged:AddListener(function(str)
local count = self.defaultNum
if str and tonumber(str) then
count = math.floor(tonumber(str) + 0.0001)
end
if count < 1 then
count = 1
end
if count > self.remainNum then
count = self.remainNum
end
self.defaultNum = count
self:refreshCountInfo()
end)
self:refreshCountInfo()
end
function CommonExchangeUI:_addListeners()
local uiMap = self.root:genAllChildren()
uiMap["exchange_ui.bg.ok_btn"]:addClickListener(function()
if self.callback then
self.callback(self.defaultNum)
end
self:closeUI()
end)
uiMap["exchange_ui.bg.btn_reduce"]:addClickListener(function()
self.defaultNum = self.defaultNum - 1
self:refreshCountInfo()
end)
uiMap["exchange_ui.bg.btn_min"]:addClickListener(function()
self.defaultNum = 1
self:refreshCountInfo()
end)
uiMap["exchange_ui.bg.btn_add"]:addClickListener(function()
self.defaultNum = self.defaultNum + 1
self:refreshCountInfo()
end)
uiMap["exchange_ui.bg.btn_max"]:addClickListener(function()
self.defaultNum = self.remainNum
self:refreshCountInfo()
end)
end
function CommonExchangeUI:refreshCountInfo()
if self.defaultNum < 1 then
self.defaultNum = 1
end
if self.defaultNum > self.remainNum then
self.defaultNum = self.remainNum
end
local uiMap = self.root:genAllChildren()
local costIcon = uiMap["exchange_ui.bg.ok_btn.icon"]
costIcon:setSprite(ModuleManager.ItemManager:getItemIcon(GFunc.getRewardId(self.cost)))
local singlePrice = GFunc.getRewardNum(self.reward)
uiMap["exchange_ui.bg.ok_btn.text"]:setText(singlePrice * self.defaultNum)
end
return CommonExchangeUI