-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPowerBar.lua
231 lines (185 loc) · 5.73 KB
/
PowerBar.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
--[[ ArenaLive Core Functions: PowerBar Handler
Created by: Vadrak
Creation Date: 07.06.2013
Last Update: "
This file contains all relevant functions for power bars and their behaviour.
]]--
-- Local version is said to be faster.
local ArenaLiveCore = ArenaLiveCore;
-- Set up a new handler.
local PowerBar = ArenaLiveCore:AddHandler("PowerBar", "EventCore");
-- Get the global UnitFrame handler.
local UnitFrame = ArenaLiveCore:GetHandler("UnitFrame");
-- Register the handler for all needed events.
PowerBar:RegisterEvent("UNIT_DISPLAYPOWER");
PowerBar:RegisterEvent("UNIT_MANA");
PowerBar:RegisterEvent("UNIT_MAXMANA");
local powerTypeTable = {
[0] = "MANA",
[1] = "RAGE",
[2] = "FOCUS",
[3] = "ENERGY",
}
PowerBarColor = {};
PowerBarColor["MANA"] = { r = 0.00, g = 0.00, b = 1.00 };
PowerBarColor["RAGE"] = { r = 1.00, g = 0.00, b = 0.00 };
PowerBarColor["FOCUS"] = { r = 1.00, g = 0.50, b = 0.25 };
PowerBarColor["ENERGY"] = { r = 1.00, g = 1.00, b = 0.00 };
-- *** FRAME FUNCTIONS ***
--[[ Function: SetPowerType
Sets the power type and the colour of the powerbar.
]]--
local function SetPowerType(self)
if ( not self or self.lockColour or self.disconnected ) then
self:SetStatusBarColor(0.5, 0.5, 0.5);
return;
end
local unit = self.unitFrame.unit;
local powerType, powerToken, red, green, blue = UnitPowerType(unit)
powerToken = powerTypeTable[powerType]
local info = PowerBarColor[powerToken];
if ( info ) then
red = info.r
green = info.g
blue = info.b
else
if ( not red ) then
info = PowerBarColor[powerType] or PowerBarColor["MANA"];
red = info.r
green = info.g
blue = info.b
end
end
self.powerType = powerType;
self:SetStatusBarColor(red, green, blue);
end
--[[ Function: OnUpdate
OnUpdate function for the powerbar.
]]--
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 = UnitMana(unit, self.powerType);
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 powerbar.
]]--
local function OnValueChanged(self)
if ( self.unitFrame.powerBarText ) then
self.unitFrame.powerBarText:Update();
end
end
--[[ Function: Update
General update function for the powerbar.
]]--
local function Update (self)
local unit = self.unitFrame.unit;
if ( not unit or self.lockValues ) then
return;
end
self.disconnected = not UnitIsConnected(unit);
self:SetPowerType(self);
local maxValue = UnitManaMax(unit, self.powerType);
self.forceHideText = false;
if (maxValue == 0 ) then
maxValue = 1;
self.forceHideText = true;
end
self:SetMinMaxValues(0, maxValue);
self:SetValue(maxValue);
self.currValue = maxValue;
local currValue = UnitMana(unit, self.powerType);
self:SetValue(currValue);
self.currValue = currValue;
end
--[[ Function: Reset
Reset the powerbar'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 powerbar of a unit frame.
Arguments:
powerBar: The statusBar that will be registered as a powerbar.
unitFrame: the unit frame the powerbar belongs to.
frequentUpdates: If true, the powerbar will update itself via an OnUpdate script.
]]--
function PowerBar:AddFrame (powerBar, unitFrame, frequentUpdates)
-- Create a reference for the powerbar inside the unit frame and vice versa.
unitFrame.powerBar = powerBar;
powerBar.unitFrame = unitFrame;
-- Register the powerBar in the unit frame's handler list.
unitFrame.handlerList.powerBar = true;
powerBar.lockColour = false;
powerBar.frequentUpdates = frequentUpdates;
powerBar.SetPowerType = SetPowerType;
powerBar.OnUpdate = OnUpdate;
powerBar.OnValueChanged = OnValueChanged;
powerBar.Update = Update;
powerBar.Reset = Reset;
-- Set reverse fill
--PowerBar:SetReverseFill(unitFrame);
if ( frequentUpdates ) then
powerBar:SetScript("OnUpdate", powerBar.OnUpdate);
end
powerBar:SetScript("OnValueChanged", powerBar.OnValueChanged);
end
--[[ Function: SetReverseFill
Sets reverse fill mode for the powerbar of the specified unit frame.
Arguments:
unitFrame: the unit frame the healthbar belongs to.
]]--
function PowerBar:SetReverseFill (unitFrame)
if ( unitFrame.powerBar ) then
local DBKey = unitFrame.frameType.."/StatusBars/ReverseFill";
local reverseFill = ArenaLiveCore:GetDBEntry(unitFrame.addonName, DBKey);
unitFrame.powerBar:SetReverseFill(reverseFill);
end
end
--[[ Function: OnEvent
OnEvent function for the PowerBar handler.
Arguments:
event: The event that fired.
...: A list of arguments that accompany the event.
]]--
local affectedFrame;
function PowerBar:OnEvent (event, ...)
local unit = ...;
if ( event == "UNIT_POWER" ) 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.powerBar and not affectedFrame.powerBar.frequentUpdates ) then
affectedFrame.powerBar:Update();
end
end
end
end
elseif ( event == "UNIT_DISPLAYPOWER" or event == "UNIT_MAXPOWER" ) 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.powerBar ) then
affectedFrame.powerBar:Update();
end
end
end
end
end
end