tibia-client/modules/game_cooldown/cooldown.lua

216 lines
5.2 KiB
Lua
Raw Normal View History

local ProgressCallback = {
update = 1,
finish = 2
}
2012-10-13 21:33:04 +02:00
cooldownWindow = nil
cooldownButton = nil
contentsPanel = nil
cooldownPanel = nil
2013-01-31 13:30:36 +01:00
lastPlayer = nil
2014-08-06 20:40:56 +02:00
cooldown = {}
groupCooldown = {}
function init()
2013-01-26 21:40:03 +01:00
connect(g_game, { onGameStart = online,
onSpellGroupCooldown = onSpellGroupCooldown,
onSpellCooldown = onSpellCooldown })
cooldownButton = modules.client_topmenu.addRightGameToggleButton('cooldownButton',
tr('Cooldowns'), '/images/topbuttons/cooldowns', toggle)
2012-10-13 21:33:04 +02:00
cooldownButton:setOn(true)
cooldownButton:hide()
cooldownWindow = g_ui.loadUI('cooldown', modules.game_interface.getRightPanel())
2012-10-13 21:33:04 +02:00
cooldownWindow:disableResize()
cooldownWindow:setup()
2012-10-13 21:33:04 +02:00
contentsPanel = cooldownWindow:getChildById('contentsPanel')
cooldownPanel = contentsPanel:getChildById('cooldownPanel')
-- preload cooldown images
for k,v in pairs(SpelllistSettings) do
g_textures.preload(v.iconFile)
end
2013-01-26 21:40:03 +01:00
if g_game.isOnline() then
2013-01-26 21:40:03 +01:00
online()
end
end
function terminate()
2013-01-26 21:40:03 +01:00
disconnect(g_game, { onGameStart = online,
onSpellGroupCooldown = onSpellGroupCooldown,
onSpellCooldown = onSpellCooldown })
2012-10-13 21:33:04 +02:00
cooldownWindow:destroy()
cooldownButton:destroy()
2012-10-13 21:33:04 +02:00
end
function loadIcon(iconId)
local spell, profile, spellName = Spells.getSpellByIcon(iconId)
if not spellName then return end
if not profile then return end
clientIconId = Spells.getClientId(spellName)
if not clientIconId then return end
local icon = cooldownPanel:getChildById(iconId)
if not icon then
icon = g_ui.createWidget('SpellIcon')
icon:setId(iconId)
end
local spellSettings = SpelllistSettings[profile]
if spellSettings then
icon:setImageSource(spellSettings.iconFile)
icon:setImageClip(Spells.getImageClip(clientIconId, profile))
else
icon = nil
end
return icon
end
2012-10-13 21:33:04 +02:00
function onMiniWindowClose()
cooldownButton:setOn(false)
end
function toggle()
if cooldownButton:isOn() then
cooldownWindow:close()
cooldownButton:setOn(false)
else
cooldownWindow:open()
cooldownButton:setOn(true)
end
end
2013-01-26 21:40:03 +01:00
function online()
if g_game.getFeature(GameSpellList) then
2012-10-13 21:33:04 +02:00
cooldownButton:show()
else
2013-01-26 21:40:03 +01:00
cooldownButton:hide()
cooldownWindow:close()
end
2013-01-31 13:30:36 +01:00
2013-02-01 00:46:44 +01:00
if not lastPlayer or lastPlayer ~= g_game.getCharacterName() then
2013-01-31 13:30:36 +01:00
refresh()
lastPlayer = g_game.getCharacterName()
end
end
function refresh()
cooldownPanel:destroyChildren()
end
function removeCooldown(progressRect)
removeEvent(progressRect.event)
if progressRect.icon then
progressRect.icon:destroy()
progressRect.icon = nil
end
progressRect = nil
end
function turnOffCooldown(progressRect)
removeEvent(progressRect.event)
if progressRect.icon then
progressRect.icon:setOn(false)
progressRect.icon = nil
end
-- create particles
--[[local particle = g_ui.createWidget('GroupCooldownParticles', progressRect)
particle:fill('parent')
scheduleEvent(function() particle:destroy() end, 1000) -- hack until onEffectEnd]]
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
2013-03-05 03:45:19 +01:00
function updateCooldown(progressRect, duration)
progressRect:setPercent(progressRect:getPercent() + 10000/duration)
if progressRect:getPercent() < 100 then
removeEvent(progressRect.event)
progressRect.event = scheduleEvent(function()
progressRect.callback[ProgressCallback.update]()
end, 100)
else
progressRect.callback[ProgressCallback.finish]()
end
end
2014-08-06 20:40:56 +02:00
function isGroupCooldownIconActive(groupId)
return groupCooldown[groupId]
end
function isCooldownIconActive(iconId)
return cooldown[iconId]
end
function onSpellCooldown(iconId, duration)
local icon = loadIcon(iconId)
if not icon then
return
end
icon:setParent(cooldownPanel)
local progressRect = icon:getChildById(iconId)
if not progressRect then
progressRect = g_ui.createWidget('SpellProgressRect', icon)
progressRect:setId(iconId)
progressRect.icon = icon
progressRect:fill('parent')
else
progressRect:setPercent(0)
end
progressRect:setTooltip(spellName)
local updateFunc = function()
2013-03-05 03:45:19 +01:00
updateCooldown(progressRect, duration)
end
local finishFunc = function()
removeCooldown(progressRect)
2014-08-06 20:40:56 +02:00
cooldown[iconId] = false
end
initCooldown(progressRect, updateFunc, finishFunc)
2014-08-06 20:40:56 +02:00
cooldown[iconId] = true
end
function onSpellGroupCooldown(groupId, duration)
if not SpellGroups[groupId] then return end
2012-10-13 21:33:04 +02:00
local icon = contentsPanel:getChildById('groupIcon' .. SpellGroups[groupId])
local progressRect = contentsPanel:getChildById('progressRect' .. SpellGroups[groupId])
if icon then
icon:setOn(true)
removeEvent(icon.event)
end
progressRect.icon = icon
if progressRect then
removeEvent(progressRect.event)
local updateFunc = function()
2013-03-05 03:45:19 +01:00
updateCooldown(progressRect, duration)
end
local finishFunc = function()
turnOffCooldown(progressRect)
2014-08-06 20:40:56 +02:00
groupCooldown[groupId] = false
end
initCooldown(progressRect, updateFunc, finishFunc)
2014-08-06 20:40:56 +02:00
groupCooldown[groupId] = true
end
end