46 lines
1.2 KiB
Lua
46 lines
1.2 KiB
Lua
local HeroComp = class("HeroComp", LuaComponent)
|
|
|
|
local HERO_LIST_CELL = "app/ui/hero/cell/hero_list_cell"
|
|
|
|
function HeroComp:init()
|
|
self.uiMap = self:getBaseObject():genAllChildren()
|
|
self.scrollRect = self.uiMap["hero_ui.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
|
|
self.scrollRect:addInitCallback(function()
|
|
return HERO_LIST_CELL
|
|
end)
|
|
self.scrollRect:addRefreshCallback(function(index, cell)
|
|
cell:refresh()
|
|
end)
|
|
self.heroList = {}
|
|
end
|
|
|
|
function HeroComp:refresh()
|
|
if #self.heroList <= 0 then
|
|
local heroes = DataManager.HeroData:getAllHeroes()
|
|
for k, v in pairs(heroes) do
|
|
table.insert(self.heroList, v)
|
|
end
|
|
end
|
|
self:refreshScrollRect()
|
|
end
|
|
|
|
function HeroComp:refreshScrollRect()
|
|
local activeCount = DataManager.HeroData:getActiveHeroCount()
|
|
local lockCount = #self.heroList - activeCount
|
|
local cellCount = 0
|
|
if activeCount > 0 then
|
|
cellCount = cellCount + math.ceil(activeCount / 5)
|
|
end
|
|
if lockCount > 0 then
|
|
cellCount = cellCount + math.ceil(lockCount / 5)
|
|
end
|
|
local currCount = self.scrollRect:getTotalCount()
|
|
if cellCount == currCount then
|
|
self.scrollRect:updateAllCell()
|
|
else
|
|
self.scrollRect:clearCells()
|
|
self.scrollRect:refillCells(cellCount)
|
|
end
|
|
end
|
|
|
|
return HeroComp |