c1_lua/lua/app/ui/tips/reward_box.lua

145 lines
4.2 KiB
Lua

local BaseTips = require "app/ui/tips/base_tips"
local RewardBox = class("RewardBox", BaseTips)
local CELL_WIDTH = 151
local CELL_NUM = 5
local MIN_SIZE_Y = 426
local MAX_SIZE_Y = 966
local MAX_SIZE_X = 690
local MIN_TITLE_POS_Y = 264
local MAX_TITLE_POS_Y = 540
function RewardBox:ctor(params)
self.params = params or {}
self.params.rewards = params.rewards or {}
self.customTitleStr = params.customTitleStr
self.callback = params.callback
self.flyPos = {}
self.showFly = false
self.actionStatus = {}
if #self.params.rewards > 35 then
self.showAction = true
self.turnIdx = 7
self.maxIdx = math.ceil(#self.params.rewards / CELL_NUM)
end
end
function RewardBox:getPrefabPath()
return "assets/prefabs/ui/common/reward_box.prefab"
end
function RewardBox:onLoadRootComplete()
local uiMap = self.root:genAllChildren()
self.scrollView = uiMap["reward_box.scroll_rect"]
self.scrollRect = self.scrollView: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:refresh(self.params.rewards[index])
self:playCellAction(cell, index)
end)
self.scrollRect:clearCells()
self.root:addClickListener(function()
self:closeUI()
if self.showFly then
local pos = clone(self.flyPos)
-- self:showCurrencyAction(pos)
end
if self.callback then
self.callback()
end
end)
self.titleTx = uiMap["reward_box.reward_title"]
self.titleTx:setText(self.customTitleStr or I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
uiMap["reward_box.continue_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_CLOSE_DESC))
-- AudioManager:playEffect(AudioManager.EFFECT_ID.REWARD)
self:_refreshScrollRect()
end
function RewardBox:_refreshScrollRect(onlyRefresh)
for i,v in ipairs(self.params.rewards) do
if self.params.rewards[i].type == GConst.REWARD_TYPE.ITEM then
local id = self.params.rewards[i].item.id or self.params.rewards[i].item.cfg_id
if id == GConst.ItemConst.ITEM_ID_GOLD or
id == GConst.ItemConst.ITEM_ID_GEM or
id == GConst.ItemConst.ITEM_ID_VIT
then
self.showFly = true
local allPos = {}
for i = 1, 4 do
local posX, posY = GFunc.randomPos(i, {x = 0, y = 0})
allPos[i] = {x = posX, y = posY}
end
self.flyPos[id] = allPos
end
end
end
if #self.params.rewards <= 5 then
self.scrollView:setSizeDelta(#self.params.rewards*CELL_WIDTH, MIN_SIZE_Y)
self.scrollRect:setPerLineNum(#self.params.rewards)
self.scrollRect:refillCells(#self.params.rewards, true)
self.titleTx:setAnchoredPositionY(MIN_TITLE_POS_Y)
elseif #self.params.rewards <= 15 then
self.scrollView:setSizeDelta(MAX_SIZE_X, MIN_SIZE_Y)
self.scrollRect:setPerLineNum(CELL_NUM)
self.scrollRect:refillCells(#self.params.rewards, true)
self.titleTx:setAnchoredPositionY(MIN_TITLE_POS_Y)
else
self.scrollView:setSizeDelta(MAX_SIZE_X, MAX_SIZE_Y)
self.scrollRect:setPerLineNum(CELL_NUM)
self.scrollRect:refillCells(#self.params.rewards)
self.titleTx:setAnchoredPositionY(MAX_TITLE_POS_Y)
end
end
function RewardBox:getCellDelayTime(idx)
if idx <= 35 then
return idx*0.05
else
local re = (idx - 1)%5 + 1
return re*0.025
end
end
function RewardBox:playCellAction(cell, idx)
if self.actionStatus[idx] then
return
end
local delayTime = self:getCellDelayTime(idx)
local canvasGroup = cell.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
if not canvasGroup then
canvasGroup = cell.baseObject:addComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
end
canvasGroup.alpha = 0
local seq = cell.baseObject:createBindTweenSequence()
seq:AppendInterval(delayTime)
local tween = canvasGroup:DOFade(1, 0.1)
tween:SetEase(CS.DG.Tweening.Ease.InOutSine)
seq:Append(tween)
seq:AppendCallback(function()
self.actionStatus[idx] = true
self:turnToNext(idx)
end)
end
function RewardBox:turnToNext(idx)
if not self.showAction or idx ~= self.turnIdx * CELL_NUM then
return
end
self:performWithDelayGlobal(function()
self.scrollRect:moveToIndex((self.turnIdx - 6)*CELL_NUM + 1)
self.turnIdx = self.turnIdx + 1
if self.turnIdx >= self.maxIdx then
self.showAction = false
end
end, 0.025)
end
return RewardBox