forked from Schaka/ArenaLive-TBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCooldown.lua
268 lines (211 loc) · 6.68 KB
/
Cooldown.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
--[[ ArenaLive Core Functions: Cooldown
Created by: Vadrak
Creation Date: 09.06.2013
Last Update: "
This file contains handling of cooldowns and cooldown text.
]]--
-- AddOnName to get SavedVariables.
local addonName = "ArenaLiveCore";
-- Local version is said to be faster.
local ArenaLiveCore = ArenaLiveCore;
-- Set up a new handler.
local Cooldown = ArenaLiveCore:AddHandler("Cooldown");
-- Get the global UnitFrame handler.
local UnitFrame = ArenaLiveCore:GetHandler("UnitFrame");
-- Set abbreviations for hours and minutes
local minute_abbreviation = "m";
local hour_abbreviation = "h";
-- Set up a table to store references for all registered cooldowns. This way it will be easier to enable/disable OmniCC support.
local cooldownFrames = {};
-- *** FRAME FUNCTIONS ***
local function Set (self, startTime, duration)
local remainingDuration = duration - (GetTime() - startTime);
self.duration = remainingDuration;
self.elapsedTillNow = 0;
self:SetCooldown(startTime, duration);
self:Update();
self:Show();
end
--[[ Function: Update
This function Updates the cooldown text, if there is one.
Arguments:
cooldown: The cooldown frame that will be changed.
]]--
local function Update(self)
if ( not self.text ) then
return;
end
if ( self.duration and self.duration > 0 and self.showText ) then
self.text:Show();
self.text:SetText(Cooldown:FormatText(self.duration));
else
self.text:Hide();
self:Hide()
end
end
local function Reset (self)
self:Hide();
self.duration = 0;
self.elapsedTillNow = 0;
if ( self.text ) then
self.text:SetText();
end
end
--[[ Function: OnUpdate
OnUpdate handler for the cooldown.
Arguments:
cooldown: The cooldown frame that will be changed.
]]--
local function OnUpdate (self, elapsed)
-- I try to reduce needed performance here by updating the text only when more than 0.1 second has passed, because we only show 1 decimal digit max anyway.
self.elapsedTillNow = self.elapsedTillNow + elapsed;
if ( self.elapsedTillNow > 0.1 ) then
self.duration = self.duration - self.elapsedTillNow;
self.elapsedTillNow = 0;
-- If duration is 0 the cooldown has finished, if the frame is not visible it means that the cooldown isn't needed anymore (e.g. wenn CC is dispelled and PortraitOverlay therefore hidden).
if ( ( self.duration <= 0 ) ) then
self:Reset();
else
if (self.text and self.showText) then
self:Update()
end
end
end
end
-- *** HANDLER FUNCTIONS ***
--[[ Function: AddFrame
Sets up a cooldown frame for specified parent frame.
Arguments:
cooldown: The cooldown frame that will be registered.
cooldownText: A FontString that is shown, when the cooldown is active (optional).
parent: The cooldown frame's parent.
staticSize: If true the cooldown font size won't be changed.
]]--
function Cooldown:AddFrame (cooldown, cooldownText, parent, staticSize)
if ( not cooldown or not parent ) then
ArenaLiveCore:Message(ArenaLiveCore:GetLocalisation(addonName, "ERR_ADD_COOLDOWN_NOT_GIVEN"), "error");
return;
end
-- Create a reference for the healthbar inside the unit frame and vice versa.
parent.cooldown = cooldown;
cooldown.parent = parent;
-- Create a reference for the cooldown text.
cooldown.text = cooldownText;
-- Set up the standard variables.
cooldown.duration = 0;
cooldown.elapsedTillNow = 0;
cooldown.staticSize = staticSize;
-- Set basic functions for the cooldown.
cooldown.Set = Set;
cooldown.Update = Update;
cooldown.Reset = Reset;
if ( cooldown.text ) then
cooldown.OnUpdate = OnUpdate;
cooldown:SetScript("OnUpdate", cooldown.OnUpdate);
end
cooldownFrames[cooldown] = true;
-- Set up wether font is shown or not, if there is a cooldown text. And update the text size.
Cooldown:SetTextMode(cooldown);
Cooldown:UpdateTextSize(cooldown);
end
--[[ Function: SetTextMode
This function sets the cooldown up according to saved variables to show a cooldown text or hide it.
Arguments:
cooldown: The cooldown frame that will be changed.
]]--
function Cooldown:SetTextMode(cooldown)
if ( cooldown.text ) then
cooldown.showText = ArenaLiveCore:GetDBEntry(addonName, "Cooldown/ShowText");
if ( cooldown.showText ) then
cooldown.noCooldownCount = 1;
else
cooldown.noCooldownCount = nil;
cooldown:Reset();
end
else
cooldown.showText = nil;
cooldown.noCooldownCount = nil;
end
end
function Cooldown:UpdateAll()
for cooldown, value in pairs(cooldownFrames) do
Cooldown:SetTextMode(cooldown);
end
end
--[[ Function: SetTextMode
This function sets the cooldown up according to saved variables to show a cooldown text or hide it.
Arguments:
cooldown: The cooldown frame that will be changed.
]]--
function Cooldown:UpdateTextSize(cooldown)
if ( cooldown.text and not cooldown.staticSize ) then
local size;
local sizeX = cooldown.parent:GetWidth();
local sizeY = cooldown.parent:GetHeight();
if ( sizeX < sizeY ) then
size = sizeX;
else
size = sizeY;
end
sizeY = math.floor(sizeY / 2 );
local filename, fontHeight, flags = cooldown.text:GetFont()
fontHeight = sizeY;
cooldown.text:SetFont(filename, fontHeight, flags)
end
end
--[[ Function: SetTextMode
This function sets the cooldown up according to saved variables to show a cooldown text or hide it.
Arguments:
cooldown: The cooldown frame that will be changed.
]]--
function Cooldown:FormatText(cooldownTime)
if ( not cooldownTime ) then
return;
end
local cooldownTimeText;
local timeType = "seconds";
local decimal;
-- Minutes
if ( cooldownTime > 59 ) then
cooldownTime = cooldownTime / 60;
timeType = "minutes"
-- Hours
if ( cooldownTime > 60 ) then
cooldownTime = cooldownTime / 60;
timeType = "hours"
end
-- We need to round up or down correctly on this one.
decimal = math.floor(cooldownTime * 10);
decimal = tonumber(string.sub(decimal, -1));
if ( decimal < 5 ) then
cooldownTime = math.floor(cooldownTime);
else
cooldownTime = math.ceil(cooldownTime);
end
else
if (cooldownTime < 10 and math.floor(cooldownTime) > 0 ) then
decimal = (math.floor(cooldownTime*10));
cooldownTime = string.sub(decimal, 1, -2);
cooldownTime = cooldownTime..".";
cooldownTime = cooldownTime..string.sub(decimal, -1);
return cooldownTime;
end
if (math.floor(cooldownTime) == 0 ) then
cooldownTime = string.sub(cooldownTime, 1, 3);
else
cooldownTime = math.floor(cooldownTime)
end
end
if ( timeType == "hours" ) then
cooldownTimeText = cooldownTime..hour_abbreviation;
elseif ( timeType == "minutes" ) then
cooldownTimeText = cooldownTime..minute_abbreviation;
else
if ( tonumber(cooldownTime) <= 0 ) then
cooldownTimeText = "";
else
cooldownTimeText = cooldownTime;
end
end
return cooldownTimeText;
end