-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_bar.lua
231 lines (211 loc) · 8.26 KB
/
power_bar.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
local addonName, addon = ...
setfenv(1, addon)
function createPowerBar(unit, mirror)
local powerBar = _G.CreateFrame("Frame")
local frameLevel = powerBar:GetFrameLevel()
powerBar.powerStatusBar = _G.CreateFrame("StatusBar", nil, powerBar)
do
local statusBar = powerBar.powerStatusBar
statusBar:SetAllPoints()
if mirror then
statusBar:SetReverseFill(true)
end
statusBar:SetStatusBarTexture(settings.barTexture)
local color = settings.colors.noPower
statusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(0)
end
powerBar.powerMissingStatusBar = _G.CreateFrame("StatusBar", nil, powerBar)
do
local statusBar = powerBar.powerMissingStatusBar
statusBar:SetAllPoints()
if not mirror then
statusBar:SetReverseFill(true)
end
statusBar:SetStatusBarTexture(settings.barTexture)
local color = settings.colors.powerBarBackground
statusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(1)
end
--------------------------------------------------------------------------------------------------
powerBar:SetScript("OnEvent", function(self, event, ...)
return self[event](self, ...)
end)
function powerBar:UNIT_DISPLAYPOWER(unit)
local powerMax = _G.UnitPowerMax(unit)
if powerMax and powerMax > 0 then
local _, powerToken, altR, altG, altB = _G.UnitPowerType(unit)
local color = powerToken and settings.powerColors[powerToken] or nil
if color then
self.powerStatusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
else
self.powerStatusBar:SetStatusBarColor(altR, altG, altB, settings.powerAlpha)
end
-- See http://wowprogramming.com/docs/api/UnitPowerType
else
local color = settings.colors.noPower
self.powerStatusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
end
self:UNIT_MAXPOWER(unit)
--self:UNIT_POWER_FREQUENT(unit)
end
powerBar:RegisterUnitEvent("UNIT_DISPLAYPOWER", unit)
-- When Berserk expires the current energy might change (as the maximum energy is lower), but only UNIT_MAXPOWER is
-- fired. That's why we also update the current power.
function powerBar:UNIT_MAXPOWER(unit)
local powerMax = _G.UnitPowerMax(unit)
if powerMax and powerMax > 0 then
self.powerStatusBar:SetMinMaxValues(0, powerMax)
self.powerMissingStatusBar:SetMinMaxValues(0, powerMax)
if _G.UnitIsDeadOrGhost(unit) then
self.powerStatusBar:SetValue(0)
self.powerMissingStatusBar:SetValue(powerMax)
else
local power = _G.UnitPower(unit)
self.powerStatusBar:SetValue(power)
self.powerMissingStatusBar:SetValue(powerMax - power)
end
else
self.powerStatusBar:SetMinMaxValues(0, 1)
self.powerStatusBar:SetValue(1)
self.powerMissingStatusBar:SetMinMaxValues(0, 1)
self.powerMissingStatusBar:SetValue(0)
end
end
powerBar:RegisterUnitEvent("UNIT_MAXPOWER", unit)
function powerBar:UNIT_POWER_FREQUENT(unit)
if _G.UnitPowerType(unit) then
local powerMax = _G.UnitPowerMax(unit)
if powerMax and powerMax > 0 then
if _G.UnitIsDeadOrGhost(unit) then
self.powerStatusBar:SetValue(0)
self.powerMissingStatusBar:SetValue(powerMax)
else
local maxValue = _G.select(2, self.powerStatusBar:GetMinMaxValues())
local power = _G.UnitPower(unit)
self.powerStatusBar:SetValue(power)
self.powerMissingStatusBar:SetValue(maxValue - power)
end
end
end
end
powerBar.UNIT_POWER = powerBar.UNIT_POWER_FREQUENT
powerBar:RegisterUnitEvent("UNIT_POWER", unit)
powerBar:RegisterUnitEvent("UNIT_POWER_FREQUENT", unit)
function powerBar:update(unit)
if _G.UnitExists(unit) and _G.UnitIsConnected(unit) then
self:UNIT_DISPLAYPOWER(unit)
self:UNIT_POWER_FREQUENT(unit)
elseif (_G.select(2, _G.IsInInstance())) == "arena" then
self.powerStatusBar:SetMinMaxValues(0, 1)
self.powerMissingStatusBar:SetMinMaxValues(0, 1)
local specID
for i = 1, _G.MAX_ARENA_ENEMIES do
if unit == "arena" .. i then
specID = _G.GetArenaOpponentSpec(i)
break
end
end
if specID and specID > 0 then
local powerToken = powerTokens[specID]
local color = powerToken and settings.powerColors[powerToken] or nil
if not color then
color = settings.colors.noPower
end
self.powerStatusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
if powerToken then
if powerToken == "MANA" or powerToken == "FOCUS" or powerToken == "ENERGY" then
self.powerStatusBar:SetValue(1)
self.powerMissingStatusBar:SetValue(0)
elseif powerToken == "RAGE" or powerToken == "RUNIC_POWER" then
self.powerStatusBar:SetValue(0)
self.powerMissingStatusBar:SetValue(1)
end
end
end
else
local color = settings.colors.noPower
self.powerStatusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
self.powerStatusBar:SetMinMaxValues(0, 1)
self.powerMissingStatusBar:SetMinMaxValues(0, 1)
self.powerStatusBar:SetValue(0)
self.powerMissingStatusBar:SetValue(1)
end
end
function powerBar:UNIT_CONNECTION(unit, hasConnected)
self:update(unit)
end
powerBar:RegisterUnitEvent("UNIT_CONNECTION", unit)
--------------------------------------------------------------------------------------------------
return powerBar
end
-- Only use for player.
function createManaBar(unit, mirror)
local manaBar = _G.CreateFrame("Frame")
local frameLevel = manaBar:GetFrameLevel()
manaBar.manaStatusBar = _G.CreateFrame("StatusBar", nil, manaBar)
do
local statusBar = manaBar.manaStatusBar
statusBar:SetAllPoints()
statusBar:SetStatusBarTexture(settings.barTexture)
local manaColor = settings.powerColors["MANA"]
statusBar:SetStatusBarColor(manaColor.r, manaColor.g, manaColor.b, manaColor.a)
end
manaBar.manaMissingStatusBar = _G.CreateFrame("StatusBar", nil, manaBar)
do
local statusBar = manaBar.manaMissingStatusBar
statusBar:SetAllPoints()
statusBar:SetReverseFill(true)
statusBar:SetStatusBarTexture(settings.barTexture)
local color = settings.colors.powerBarBackground
statusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
end
----------------------------------------------------------------------------------------------------------------------
manaBar:SetScript("OnEvent", function(self, event, ...)
return self[event](self, ...)
end)
function manaBar:UNIT_MAXPOWER(unit)
local manaMax = _G.UnitPowerMax(unit, _G.SPELL_POWER_MANA)
if manaMax and manaMax ~= 0 then
self.manaStatusBar:SetMinMaxValues(0, manaMax)
self.manaMissingStatusBar:SetMinMaxValues(0, manaMax)
if _G.UnitIsDeadOrGhost(unit) then
self.manaStatusBar:SetValue(0)
self.manaMissingStatusBar:SetValue(manaMax)
else
local mana = _G.UnitPower(unit, _G.SPELL_POWER_MANA)
self.manaStatusBar:SetValue(mana)
self.manaMissingStatusBar:SetValue(manaMax - mana)
end
else
self.manaStatusBar:SetMinMaxValues(0, 1)
self.manaStatusBar:SetValue(0)
self.manaMissingStatusBar:SetMinMaxValues(0, 1)
self.manaMissingStatusBar:SetValue(1)
end
end
manaBar:RegisterUnitEvent("UNIT_MAXPOWER", unit)
function manaBar:UNIT_POWER_FREQUENT(unit)
local manaMax = _G.UnitPowerMax(unit, _G.SPELL_POWER_MANA)
if manaMax and manaMax ~= 0 then
if _G.UnitIsDeadOrGhost(unit) then
self.manaStatusBar:SetValue(0)
self.manaMissingStatusBar:SetValue(manaMax)
else
local maxValue = _G.select(2, self.manaStatusBar:GetMinMaxValues())
local mana = _G.UnitPower(unit, _G.SPELL_POWER_MANA)
self.manaStatusBar:SetValue(mana)
self.manaMissingStatusBar:SetValue(maxValue - mana)
end
end
end
manaBar:RegisterUnitEvent("UNIT_POWER_FREQUENT", unit)
----------------------------------------------------------------------------------------------------------------------
function manaBar:update(unit)
self:UNIT_MAXPOWER(unit)
end
return manaBar
end
-- vim: tw=120 sts=2 sw=2 et