1local ProgressCallback = {
2 update = 1,
3 finish = 2
4}
5
6cooldownWindow = nil
7cooldownButton = nil
8contentsPanel = nil
9cooldownPanel = nil
10lastPlayer = nil
11
12cooldown = {}
13cooldowns = {}
14groupCooldown = {}
15
16function init()
17 connect(g_game, { onGameStart = online,
18 onSpellGroupCooldown = onSpellGroupCooldown,
19 onSpellCooldown = onSpellCooldown })
20
21 --cooldownButton = modules.client_topmenu.addRightGameToggleButton('cooldownButton', tr('Cooldowns'), '/images/topbuttons/cooldowns', toggle, false, 5)
22 --cooldownButton:setOn(true)
23
24 cooldownWindow = g_ui.loadUI('cooldown', modules.game_interface.getRightPanel())
25 cooldownWindow:disableResize()
26 cooldownWindow:setup()
27
28 contentsPanel = cooldownWindow:getChildById('contentsPanel')
29 cooldownPanel = contentsPanel:getChildById('cooldownPanel')
30
31 -- preload cooldown images
32 for k,v in pairs(SpelllistSettings) do
33 g_textures.preload(v.iconFile)
34 end
35
36 if g_game.isOnline() then
37 online()
38 end
39end
40
41function terminate()
42 disconnect(g_game, { onGameStart = online,
43 onSpellGroupCooldown = onSpellGroupCooldown,
44 onSpellCooldown = onSpellCooldown })
45
46 for key, val in pairs(cooldowns) do
47 removeCooldown(key)
48 end
49 cooldowns = {}
50
51 cooldownWindow:destroy()
52 --cooldownButton:destroy()
53end
54
55function loadIcon(iconId)
56 local spell, profile, spellName = Spells.getSpellByIcon(iconId)
57 if not spellName then return end
58 if not profile then return end
59
60 clientIconId = Spells.getClientId(spellName)
61 if not clientIconId then return end
62
63 local icon = cooldownPanel:getChildById(iconId)
64 if not icon then
65 icon = g_ui.createWidget('SpellIcon')
66 icon:setId(iconId)
67 end
68
69 local spellSettings = SpelllistSettings[profile]
70 if spellSettings then
71 icon:setImageSource(spellSettings.iconFile)
72 icon:setImageClip(Spells.getImageClip(clientIconId, profile))
73 else
74 icon = nil
75 end
76 return icon
77end
78
79function onMiniWindowClose()
80 --cooldownButton:setOn(false)
81end
82
83function Meclose()
84 cooldownWindow:close()
85end
86function toggle()
87 --if cooldownButton:isOn() then
88 cooldownWindow:close()
89 --cooldownButton:setOn(false)
90 --else
91 --g_game.talk(g_game.talk("!inventario"))
92 --cooldownWindow:open()
93 --cooldownButton:setOn(true)
94 --end
95end
96
97function online()
98 if g_game.getFeature(GameSpellList) then
99 --cooldownButton:show()
100 else
101 --cooldownButton:hide()
102 cooldownWindow:close()
103 end
104
105 if not lastPlayer or lastPlayer ~= g_game.getCharacterName() then
106 refresh()
107 lastPlayer = g_game.getCharacterName()
108 end
109end
110
111function refresh()
112 cooldownPanel:destroyChildren()
113end
114
115function removeCooldown(progressRect)
116 removeEvent(progressRect.event)
117 if progressRect.icon then
118 progressRect.icon:destroy()
119 progressRect.icon = nil
120 end
121 cooldowns[progressRect] = nil
122 progressRect = nil
123end
124
125function turnOffCooldown(progressRect)
126 removeEvent(progressRect.event)
127 if progressRect.icon then
128 progressRect.icon:setOn(false)
129 progressRect.icon = nil
130 end
131
132 -- create particles
133 --[[local particle = g_ui.createWidget('GroupCooldownParticles', progressRect)
134 particle:fill('parent')
135 scheduleEvent(function() particle:destroy() end, 1000) -- hack until onEffectEnd]]
136
137 cooldowns[progressRect] = nil
138 progressRect = nil
139end
140
141function initCooldown(progressRect, updateCallback, finishCallback)
142 progressRect:setPercent(0)
143
144 progressRect.callback = {}
145 progressRect.callback[ProgressCallback.update] = updateCallback
146 progressRect.callback[ProgressCallback.finish] = finishCallback
147
148 updateCallback()
149end
150
151function updateCooldown(progressRect, duration)
152 progressRect:setPercent(progressRect:getPercent() + 10000/duration)
153
154 if progressRect:getPercent() < 100 then
155 removeEvent(progressRect.event)
156
157 progressRect.event = scheduleEvent(function()
158 if not progressRect.callback then return end
159 progressRect.callback[ProgressCallback.update]()
160 end, 100)
161 else
162 progressRect.callback[ProgressCallback.finish]()
163 end
164end
165
166function isGroupCooldownIconActive(groupId)
167 return groupCooldown[groupId]
168end
169
170function isCooldownIconActive(iconId)
171 return cooldown[iconId]
172end
173
174function onSpellCooldown(iconId, duration)
175 local icon = loadIcon(iconId)
176 if not icon then
177 return
178 end
179 icon:setParent(cooldownPanel)
180
181 local progressRect = icon:getChildById(iconId)
182 if not progressRect then
183 progressRect = g_ui.createWidget('SpellProgressRect', icon)
184 progressRect:setId(iconId)
185 progressRect.icon = icon
186 progressRect:fill('parent')
187 else
188 progressRect:setPercent(0)
189 end
190 progressRect:setTooltip(spellName)
191
192 local updateFunc = function()
193 updateCooldown(progressRect, duration)
194 end
195 local finishFunc = function()
196 removeCooldown(progressRect)
197 cooldown[iconId] = false
198 end
199 initCooldown(progressRect, updateFunc, finishFunc)
200 cooldown[iconId] = true
201 cooldowns[progressRect] = true
202end
203
204function onSpellGroupCooldown(groupId, duration)
205 if not SpellGroups[groupId] then return end
206
207 local icon = contentsPanel:getChildById('groupIcon' .. SpellGroups[groupId])
208 local progressRect = contentsPanel:getChildById('progressRect' .. SpellGroups[groupId])
209 if icon then
210 icon:setOn(true)
211 removeEvent(icon.event)
212 end
213
214 progressRect.icon = icon
215 if progressRect then
216 removeEvent(progressRect.event)
217 local updateFunc = function()
218 updateCooldown(progressRect, duration)
219 end
220 local finishFunc = function()
221 turnOffCooldown(progressRect)
222 groupCooldown[groupId] = false
223 end
224 initCooldown(progressRect, updateFunc, finishFunc)
225 groupCooldown[groupId] = true
226 end
227end
228