From f492d291ebacbd043e1f520974e9137df77c594b Mon Sep 17 00:00:00 2001 From: BeniS Date: Wed, 30 Jan 2013 02:01:06 +1300 Subject: [PATCH] More fixes to spell cooldown module: * Fixed duration issues. * Fixed referencing after destroyed issue. * Using callbacks for progress rect now. --- modules/game_cooldown/cooldown.lua | 78 ++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/modules/game_cooldown/cooldown.lua b/modules/game_cooldown/cooldown.lua index 2fe009ab..2e4c704b 100644 --- a/modules/game_cooldown/cooldown.lua +++ b/modules/game_cooldown/cooldown.lua @@ -1,3 +1,8 @@ +local ProgressCallback = { + update = 1, + finish = 2 +} + cooldownWindow = nil cooldownButton = nil contentsPanel = nil @@ -5,7 +10,6 @@ spellCooldownPanel = nil function init() connect(g_game, { onGameStart = online, - onGameEnd = offline, onSpellGroupCooldown = onSpellGroupCooldown, onSpellCooldown = onSpellCooldown }) @@ -27,7 +31,6 @@ end function terminate() disconnect(g_game, { onGameStart = online, - onGameEnd = offline, onSpellGroupCooldown = onSpellGroupCooldown, onSpellCooldown = onSpellCooldown }) @@ -58,19 +61,42 @@ function online() end end -function offline() +function removeCooldown(progressRect) + removeEvent(progressRect.event) + if progressRect.icon then + progressRect.icon:destroy() + progressRect.icon = nil + end + progressRect = nil end -function updateProgressRect(progressRect, interval, init) - if init then - progressRect:setPercent(0) - else - progressRect:setPercent(progressRect:getPercent() + 5) +function turnOffCooldown(progressRect) + removeEvent(progressRect.event) + if progressRect.icon then + progressRect.icon:setOn(false) + progressRect.icon = nil end + progressRect = nil +end + +function initCooldown(progressRect, updateCallback, finishCallback) + progressRect:setPercent(0) + + progressRect.callback = {} + progressRect.callback[ProgressCallback.update] = updateCallback + progressRect.callback[ProgressCallback.finish] = finishCallback + + updateCallback() +end + +function updateCooldown(progressRect, interval) + progressRect:setPercent(progressRect:getPercent() + 5) if progressRect:getPercent() < 100 then - removeEvent(progressRect.event) - progressRect.event = scheduleEvent(function() updateProgressRect(progressRect, interval) end, interval) + removeEvent(progressRect.event) + progressRect.event = scheduleEvent(function() progressRect.callback[ProgressCallback.update]() end, interval) + else + progressRect.callback[ProgressCallback.finish]() end end @@ -78,8 +104,8 @@ function onSpellCooldown(iconId, duration) local spell, profile, spellName = Spells.getSpellByIcon(iconId) if not spellName then return end - local ping = g_game.getPing() - if ping > 0 then duration = duration - (ping/2) end + --local ping = g_game.getPing() + --if ping > 0 then duration = duration + ping end clientIconId = Spells.getClientId(spellName) if not clientIconId then return end @@ -90,30 +116,44 @@ function onSpellCooldown(iconId, duration) icon:setId(spellName) icon:setImageSource('/images/game/spells/' .. SpelllistSettings[profile].iconFile) icon:setImageClip(Spells.getImageClip(clientIconId, profile)) - icon.event = scheduleEvent(function() icon:destroy() icon = nil end, duration) local progressRect = g_ui.createWidget('SpellProgressRect', icon) + progressRect.icon = icon progressRect:fill('parent') progressRect:setTooltip(spellName) - updateProgressRect(progressRect, duration/25, true) + + local updateFunc = function() + updateCooldown(progressRect, duration/25) + end + local finishFunc = function() + removeCooldown(progressRect) + end + initCooldown(progressRect, updateFunc, finishFunc) end end function onSpellGroupCooldown(groupId, duration) if not SpellGroups[groupId] then return end - local ping = g_game.getPing() - if ping > 0 then duration = duration - (ping/2) end + --local ping = g_game.getPing() + --if ping > 0 then duration = duration + ping end + local icon = contentsPanel:getChildById('groupIcon' .. SpellGroups[groupId]) local progressRect = contentsPanel:getChildById('progressRect' .. SpellGroups[groupId]) if icon then icon:setOn(true) removeEvent(icon.event) - icon.event = scheduleEvent(function() icon:setOn(false) end, duration) end - + + progressRect.icon = icon if progressRect then removeEvent(progressRect.event) - updateProgressRect(progressRect, duration/25, true) + local updateFunc = function() + updateCooldown(progressRect, duration/25) + end + local finishFunc = function() + turnOffCooldown(progressRect) + end + initCooldown(progressRect, updateFunc, finishFunc) end end