c1_lua/lua/app/ui/common/toast.lua
2023-04-03 10:59:13 +08:00

150 lines
4.7 KiB
Lua

local ToastManager = class("ToastManager", BaseModule)
local TOAST_DURATION = 0.5
local TOAST_DEFAULT_SIZE_Y = 54
local TOAST_DEFAULT_FONT_SIZE = 30
local TOAST_BATTLE_SIZE_Y = 124
local TOAST_BATTLE_FONT_SIZE = 30
function ToastManager:ctor()
self.curIndex = 0
self.toastList = {}
end
function ToastManager:clear()
self.curIndex = 0
self.toastList = {}
end
function ToastManager:showToast(params)
if self.curIndex >= #self.toastList then
self.toastList = {}
self.curIndex = 0
end
table.insert(self.toastList, params)
if not self.toastSid then
self:_doImmediately()
self.toastSid = self:scheduleGlobal(function()
self:_doImmediately()
if self.curIndex >= #self.toastList then
self:pauseScheduleGlobal(self.toastSid)
self.toastSidPaused = true
end
end, TOAST_DURATION)
else
if self.toastSidPaused then
self:_doImmediately()
end
self.toastSidPaused = false
self:resumeScheduleGlobal(self.toastSid)
end
end
function ToastManager:_doImmediately()
local curIndex = self.curIndex + 1
if not self.toastList[curIndex] then
return
end
self.curIndex = curIndex
local curParams = self.toastList[self.curIndex]
self:_showToast(curParams)
end
function ToastManager:_showToast(params)
UIManager:getToast(function(prefabObject)
AudioManager:playEffect(AudioManager.EFFECT_ID.SFX_TOAST)
prefabObject._using = true
if prefabObject._toastSequence then
prefabObject._toastSequence:Kill()
prefabObject._toastSequence = nil
end
local childMap = prefabObject:genAllChildren()
local content = childMap["toast.content"]
local icon = childMap["toast.icon"]
local gotTx = childMap["toast.got_tx"]
local numTx = childMap["toast.num_tx"]
local cellWidth = 46
if type(params) == "table" and params.id then
content:setVisible(false)
gotTx:setVisible(true)
numTx:setVisible(true)
icon:setVisible(true)
gotTx:setText(I18N:getGlobalText(I18N.GlobalConst.GET))
local cfg = ConfigManager:getConfig("item")[params.id]
numTx:setText(params.str or "")
if params.type == GConst.REWARD_TYPE.ITEM then
icon:setSprite(GFunc.getIconRes(params.id))
elseif params.type == GConst.REWARD_TYPE.EQUIP then
icon:setSprite(GFunc.getEquipIconRes(params.id))
elseif params.type == GConst.REWARD_TYPE.JEWELRY then
icon:setSprite(GFunc.getJewelryIconRes(params.id))
end
local meshProComp = numTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
local gotMeshProComp = gotTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
local contentWidth = meshProComp.preferredWidth
local gotWidth = gotMeshProComp.preferredWidth
gotTx:setAnchoredPositionX(-(contentWidth + cellWidth)*0.5)
icon:setAnchoredPositionX(-(contentWidth - gotWidth)*0.5)
numTx:setAnchoredPositionX((gotWidth + cellWidth)*0.5)
elseif type(params) == "table" and params.battleToast then
content:setVisible(true)
gotTx:setVisible(false)
numTx:setVisible(false)
icon:setVisible(false)
content:setText(params.content)
local meshProComp = content:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
meshProComp.fontSize = TOAST_BATTLE_FONT_SIZE
if meshProComp.preferredHeight > 100 then
prefabObject:setSizeDeltaY(meshProComp.preferredHeight + 22)
else
prefabObject:setSizeDeltaY(TOAST_BATTLE_SIZE_Y)
end
else
content:setVisible(true)
gotTx:setVisible(false)
numTx:setVisible(false)
icon:setVisible(false)
content:setText(params)
local meshProComp = content:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
meshProComp.fontSize = TOAST_DEFAULT_FONT_SIZE
if meshProComp.preferredHeight > 40 then
prefabObject:setSizeDeltaY(meshProComp.preferredHeight + 22)
else
prefabObject:setSizeDeltaY(TOAST_DEFAULT_SIZE_Y)
end
end
prefabObject:setLocalPosition(0, 100, 0)
prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = true
local canvasGroup = prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
canvasGroup.alpha = 1
prefabObject._toastSequence = prefabObject:createBindTweenSequence()
local moveTween = prefabObject:getTransform():DOLocalMove(CS.UnityEngine.Vector3(0, 180, 0), 0.6)
prefabObject._toastSequence:Append(moveTween)
local fadeOutTween = canvasGroup:DOFade(0, 0.5)
prefabObject._toastSequence:Append(fadeOutTween)
prefabObject._toastSequence:OnComplete(function()
prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
prefabObject._using = false
end)
end)
end
function ToastManager:refreshText()
local list = UIManager:getAllToast()
if list == nil then
return
end
for _, toastObj in ipairs(list) do
local childMap = toastObj:genAllChildren()
childMap["toast.content"]:setText("")
childMap["toast.got_tx"]:setText("")
end
end
return ToastManager