c1_lua/lua/app/ui/tips/reward_box.lua
2023-06-01 17:42:25 +08:00

70 lines
2.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
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])
end)
self.scrollRect:clearCells()
self.root:addClickListener(function()
self:closeUI()
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))
self:_refreshScrollRect()
uiMap["reward_box.effect_node.ui_spine_obj"]:playAnim("idle", false, false)
end
function RewardBox:_refreshScrollRect()
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
return RewardBox