-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternativepower.lua
More file actions
268 lines (209 loc) · 8.11 KB
/
Copy pathalternativepower.lua
File metadata and controls
268 lines (209 loc) · 8.11 KB
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
--[[
# Element: Alternative Power Bar
Handles the visibility and updating of a status bar that displays encounter- or quest-related power information, such as
the number of hour glass charges during the Murozond encounter in the dungeon End Time.
## Widget
AlternativePower - A `StatusBar` used to represent the unit's alternative power.
## Notes
If mouse interactivity is enabled for the widget, `OnEnter` and/or `OnLeave` handlers will be set to display a tooltip.
A default texture will be applied if the widget is a StatusBar and doesn't have a texture set.
## Options
.smoothing - Which status bar smoothing method to use, defaults to `Enum.StatusBarInterpolation.Immediate` (number)
The following options are listed by priority. The first check that returns true decides the color of the bar.
.colorPower - Use `self.colors.power[token]` to color the bar based on the unit's alternative power type
(boolean)
.colorPowerSmooth - Use color curve from `self.colors.power[token]` to color the bar with a smooth gradient based on the
player's current power percentage. Requires `.colorPower` to be enabled (boolean)
## Examples
-- Position and size
local AlternativePower = CreateFrame('StatusBar', nil, self)
AlternativePower:SetHeight(20)
AlternativePower:SetPoint('BOTTOM')
AlternativePower:SetPoint('LEFT')
AlternativePower:SetPoint('RIGHT')
-- Register with oUF
self.AlternativePower = AlternativePower
--]]
local _, ns = ...
local oUF = ns.oUF
local Private = oUF.Private
local unitIsUnit = Private.unitIsUnit
-- sourced from Blizzard_UnitFrame/UnitPowerBarAlt.lua
local ALTERNATE_POWER_INDEX = Enum.PowerType.Alternate or 10
local ALTERNATE_POWER_NAME = 'ALTERNATE'
local function updateTooltip(self)
local name, tooltip = GetUnitPowerBarStringsByID(self.__barID)
GameTooltip:SetText(name or '', 1, 1, 1)
GameTooltip:AddLine(tooltip or '', nil, nil, nil, true)
GameTooltip:Show()
end
local function onEnter(self)
if(GameTooltip:IsForbidden() or not self:IsVisible()) then return end
GameTooltip_SetDefaultAnchor(GameTooltip, self)
self:UpdateTooltip()
end
local function onLeave()
if(GameTooltip:IsForbidden()) then return end
GameTooltip:Hide()
end
local function UpdateColor(self, event, unit, powerType)
if(self.unit ~= unit or powerType ~= ALTERNATE_POWER_NAME) then return end
local element = self.AlternativePower
local color
if(element.colorPower) then
color = self.colors.power[ALTERNATE_POWER_INDEX]
if(element.colorPowerSmooth and color and color:GetCurve()) then
color = UnitPowerPercent(unit, ALTERNATE_POWER_INDEX, true, color:GetCurve())
end
end
if(color) then
element:SetStatusBarColor(color:GetRGB())
end
--[[ Callback: AlternativePower:PostUpdateColor(unit, color)
Called after the element color has been updated.
* self - the AlternativePower element
* unit - the unit for which the update has been triggered (string)
* color - the used ColorMixin-based object (table?)
--]]
if(element.PostUpdateColor) then
element:PostUpdateColor(unit, color)
end
end
local function Update(self, event, unit, powerType)
if(self.unit ~= unit or powerType ~= ALTERNATE_POWER_NAME) then return end
local element = self.AlternativePower
--[[ Callback: AlternativePower:PreUpdate()
Called before the element has been updated.
* self - the AlternativePower element
--]]
if(element.PreUpdate) then
element:PreUpdate()
end
local cur, max, min
local barInfo = element.__barInfo
if(barInfo) then
cur = UnitPower(unit, ALTERNATE_POWER_INDEX)
max = UnitPowerMax(unit, ALTERNATE_POWER_INDEX)
min = barInfo.minPower
element:SetMinMaxValues(min, max)
element:SetValue(cur, element.smoothing)
end
element.cur = cur
element.min = min
element.max = max
--[[ Callback: AlternativePower:PostUpdate(unit, cur, min, max)
Called after the element has been updated.
* self - the AlternativePower element
* unit - the unit for which the update has been triggered (string)
* cur - the current value of the unit's alternative power (number?)
* min - the minimum value of the unit's alternative power (number?)
* max - the maximum value of the unit's alternative power (number?)
--]]
if(element.PostUpdate) then
return element:PostUpdate(unit, cur, min, max)
end
end
local function Path(self, ...)
--[[ Override: AlternativePower.Override(self, event, unit, ...)
Used to completely override the element's update process.
* self - the parent object
* event - the event triggering the update (string)
* unit - the unit accompanying the event (string)
* ... - the arguments accompanying the event
--]]
do
(self.AlternativePower.Override or Update) (self, ...)
end
--[[ Override: AlternativePower.UpdateColor(self, event, unit, ...)
Used to completely override the internal function for updating the widgets' colors.
* self - the parent object
* event - the event triggering the update (string)
* unit - the unit accompanying the event (string)
* ... - the arguments accompanying the event
--]]
(self.AlternativePower.UpdateColor or UpdateColor) (self, ...)
end
local function Visibility(self, event, unit)
if(unit ~= self.unit) then return end
local element = self.AlternativePower
local barID = UnitPowerBarID(unit)
local barInfo = GetUnitPowerBarInfoByID(barID)
element.__barID = barID
element.__barInfo = barInfo
if(barInfo and (barInfo.showOnRaid and (UnitInParty(unit) or UnitInRaid(unit))
or not barInfo.hideFromOthers
or unitIsUnit(unit, 'player')))
then
Private.SmartRegisterUnitEvent(self, 'UNIT_POWER_UPDATE', unit, Path)
Private.SmartRegisterUnitEvent(self, 'UNIT_MAXPOWER', unit, Path)
element:Show()
Path(self, event, unit, ALTERNATE_POWER_NAME)
else
self:UnregisterEvent('UNIT_POWER_UPDATE', Path)
self:UnregisterEvent('UNIT_MAXPOWER', Path)
element:Hide()
Path(self, event, unit, ALTERNATE_POWER_NAME)
end
end
local function VisibilityPath(self, ...)
--[[ Override: AlternativePower.OverrideVisibility(self, event, unit)
Used to completely override the element's visibility update process.
* self - the parent object
* event - the event triggering the update (string)
* unit - the unit accompanying the event (string)
--]]
return (self.AlternativePower.OverrideVisibility or Visibility) (self, ...)
end
local function ForceUpdate(element)
return VisibilityPath(element.__owner, 'ForceUpdate', element.__owner.unit)
end
local function Enable(self, unit)
local element = self.AlternativePower
if(element) then
element.__owner = self
element.ForceUpdate = ForceUpdate
if(not element.smoothing) then
element.smoothing = Enum.StatusBarInterpolation.Immediate
end
self:RegisterUnitEvent('UNIT_POWER_BAR_SHOW', VisibilityPath, unit)
self:RegisterUnitEvent('UNIT_POWER_BAR_HIDE', VisibilityPath, unit)
if(element:IsObjectType('StatusBar') and not element:GetStatusBarTexture()) then
element:SetStatusBarTexture([[Interface\TargetingFrame\UI-StatusBar]])
end
if(element:IsMouseEnabled()) then
if(not element:GetScript('OnEnter')) then
element:SetScript('OnEnter', onEnter)
end
if(not element:GetScript('OnLeave')) then
element:SetScript('OnLeave', onLeave)
end
--[[ Override: AlternativePower:UpdateTooltip()
Called when the mouse is over the widget. Used to populate its tooltip.
* self - the AlternativePower element
--]]
if(not element.UpdateTooltip) then
element.UpdateTooltip = updateTooltip
end
end
if(unit == 'player') then
PlayerPowerBarAlt:UnregisterEvent('UNIT_POWER_BAR_SHOW')
PlayerPowerBarAlt:UnregisterEvent('UNIT_POWER_BAR_HIDE')
PlayerPowerBarAlt:UnregisterEvent('PLAYER_ENTERING_WORLD')
end
return true
end
end
local function Disable(self, unit)
local element = self.AlternativePower
if(element) then
element:Hide()
self:UnregisterEvent('UNIT_POWER_BAR_SHOW', VisibilityPath)
self:UnregisterEvent('UNIT_POWER_BAR_HIDE', VisibilityPath)
if(unit == 'player') then
PlayerPowerBarAlt:RegisterEvent('UNIT_POWER_BAR_SHOW')
PlayerPowerBarAlt:RegisterEvent('UNIT_POWER_BAR_HIDE')
PlayerPowerBarAlt:RegisterEvent('PLAYER_ENTERING_WORLD')
end
end
end
oUF:AddElement('AlternativePower', VisibilityPath, Enable, Disable)