forked from Schaka/ArenaLive-TBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthBar.lua
263 lines (208 loc) · 6.62 KB
/
HealthBar.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
--[[ ArenaLive Core Functions: HealthBar Handler
Created by: Vadrak
Creation Date: 02.06.2013
Last Update: 06.06.2013
This file contains all relevant functions for health bars and their behaviour.
]]--
local addonName = "ArenaLiveCore";
-- Local version is said to be faster.
local ArenaLiveCore = ArenaLiveCore;
-- Set up a new handler.
local HealthBar = ArenaLiveCore:AddHandler("HealthBar", "EventCore");
-- Get the global UnitFrame handler.
local UnitFrame = ArenaLiveCore:GetHandler("UnitFrame");
-- Register the handler for all needed events.
HealthBar:RegisterEvent("UNIT_HEALTH");
HealthBar:RegisterEvent("UNIT_MAXHEALTH");
-- *** FRAME FUNCTIONS ***
--[[ Function: SetColour
Sets the colour of the healthbar.
]]--
local function SetColour (self)
local unit = self.unitFrame.unit;
if ( not unit or self.lockColour or self.disconnected ) then
self:SetStatusBarColor(0.5, 0.5, 0.5);
return;
end
local red, green, blue = 0, 1, 0;
local isPlayer = UnitIsPlayer(unit);
local colourMode = ArenaLiveCore:GetDBEntry(self.unitFrame.addonName, self.unitFrame.frameType.."/HealthBar/ColourMode");
if ( colourMode == "class" and isPlayer ) then
local _, class = UnitClass(unit);
if ( class ) then
red, green, blue = RAID_CLASS_COLORS[class]["r"], RAID_CLASS_COLORS[class]["g"], RAID_CLASS_COLORS[class]["b"];
end
elseif ( colourMode == "reaction" ) then
local reaction = UnitReaction(unit, "player");
if ( reaction ) then
red = UnitReactionColor[reaction].r;
green = UnitReactionColor[reaction].g;
blue = UnitReactionColor[reaction].b;
else
red = 1
green = 1
blue = 1
end
elseif ( colourMode == "smooth" ) then
-- Luckily Blizzard has already written a perfect function for smooth health bar colour. I just adjust this to the addon's needs.
local value = UnitHealth(unit);
local minValue = 0;
local maxValue = UnitHealthMax(unit);
if ( (maxValue - minValue) > 0 ) then
value = (value - minValue) / (maxValue - minValue);
else
value = 0;
end
if( value > 0.5 ) then
red = (1.0 - value) * 2;
green = 1.0;
else
red = 1.0;
green = value * 2;
end
blue = 0;
end
self:SetStatusBarColor(red, green, blue);
end
--[[ Function: OnUpdate
OnUpdate function for the healthbar.
]]--
local function OnUpdate (self, elapsed)
if ( not self.disconnected and not self.lockValues) then
local unit = self.unitFrame.unit;
if ( not unit ) then
return;
end
local currValue = UnitHealth(unit);
if ( currValue ~= self.currValue ) then
if ( not self.ignoreNoUnit or UnitGUID(unit) ) then
self:SetValue(currValue);
self.currValue = currValue;
end
end
end
end
--[[ Function: OnValueChanged
OnValueChanged function for the healthbar.
]]--
local function OnValueChanged(self)
local colourMode = ArenaLiveCore:GetDBEntry(self.unitFrame.addonName, self.unitFrame.frameType.."/HealthBar/ColourMode");
if ( colourMode == "smooth" and self.frequentUpdates ) then
self:SetColour();
end
if ( self.unitFrame.handlerList.absorbBar ) then
self.unitFrame.absorbBar:Update();
end
if ( self.unitFrame.handlerList.healPredictionBar ) then
self.unitFrame.healPredictionBar:Update();
end
if ( self.unitFrame.healthBarText ) then
self.unitFrame.healthBarText:Update();
end
end
--[[ Function: Update
General update function for the healthbar.
]]--
local function Update(self)
local unit = self.unitFrame.unit;
if ( not unit or self.lockValues ) then
return;
end
local maxValue = UnitHealthMax(unit);
local currValue = UnitHealth(unit);
self.forceHideText = false;
if ( maxValue == 0 ) then
maxValue = 1;
self.forceHideText = true;
end
self:SetMinMaxValues(0, maxValue);
self.disconnected = not UnitIsConnected(unit);
if ( self.disconnected ) then
currValue = maxValue;
end
self:SetValue(currValue);
self:SetColour();
self.currValue = currValue;
end
--[[ Function: Reset
Reset the healthbar's values.
]]--
local function Reset(self)
self:SetMinMaxValues(0, 1);
self:SetValue(1);
self:SetStatusBarColor(0.5, 0.5, 0.5);
end
-- *** HANDLER FUNCTIONS ***
--[[ Function: AddFrame
Sets up a statusbar to be the healthbar of a unit frame.
Arguments:
healthBar: The statusBar that will be registered as a healthbar.
unitFrame: the unit frame the healthbar belongs to.
frequentUpdates: If true, the healthbar will update itself via an OnUpdate script.
]]--
function HealthBar:AddFrame (healthBar, unitFrame, frequentUpdates)
-- Create a reference for the healthbar inside the unit frame and vice versa.
unitFrame.healthBar = healthBar;
healthBar.unitFrame = unitFrame;
-- Register the healthbar in the unit frame's handler list.
unitFrame.handlerList.healthBar = true;
-- Set the colour type for the unit frame.
healthBar.lockColour = false;
healthBar.frequentUpdates = frequentUpdates;
healthBar.SetColour = SetColour;
healthBar.OnUpdate = OnUpdate;
healthBar.OnValueChanged = OnValueChanged;
healthBar.Update = Update;
healthBar.Reset = Reset;
-- Set reverse fill
--HealthBar:SetReverseFill(unitFrame);
if ( frequentUpdates ) then
healthBar:SetScript("OnUpdate", healthBar.OnUpdate);
end
healthBar:SetScript("OnValueChanged", healthBar.OnValueChanged);
end
--[[ Function: SetReverseFill
Sets the colour mode for the healthbar of the specified unit frame.
Arguments:
unitFrame: the unit frame the healthbar belongs to.
]]--
function HealthBar:SetReverseFill (unitFrame)
if ( unitFrame.healthBar ) then
local DBKey = unitFrame.frameType.."/StatusBars/ReverseFill";
local reverseFill = ArenaLiveCore:GetDBEntry(unitFrame.addonName, DBKey);
unitFrame.healthBar:SetReverseFill(reverseFill);
end
end
--[[ Function: OnEvent
OnEvent function for the HealthBar handler.
Arguments:
event: The event that fired.
...: A list of arguments that accompany the event.
]]--
local affectedFrame;
function HealthBar:OnEvent (event, ...)
local unit = ...;
if ( event == "UNIT_HEALTH" ) then
if ( UnitFrame.UnitIDTable[unit] ) then
for key, value in pairs(UnitFrame.UnitIDTable[unit]) do
if ( value and UnitFrame.UnitFrameTable[key] ) then
affectedFrame = UnitFrame.UnitFrameTable[key];
if ( affectedFrame.healthBar and not affectedFrame.healthBar.frequentUpdates ) then
affectedFrame.healthBar:Update();
end
end
end
end
elseif ( event == "UNIT_MAXHEALTH" ) then
if ( UnitFrame.UnitIDTable[unit] ) then
for key, value in pairs(UnitFrame.UnitIDTable[unit]) do
if ( value and UnitFrame.UnitFrameTable[key] ) then
affectedFrame = UnitFrame.UnitFrameTable[key];
if ( affectedFrame.healthBar ) then
affectedFrame.healthBar:Update();
end
end
end
end
end
end