This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oUF_ArtifactPower.lua
407 lines (348 loc) · 14.9 KB
/
oUF_ArtifactPower.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
--[[
# Element: ArtifactPower
Handles updating and visibility of a status bar that displays the player's artifact power.
## Widget
ArtifactPower - a `StatusBar` used to display the player's artifact power
## Options
.color - the RGB values for the widget. Defaults to `ARTIFACT_POWER_BAR:GetRGB()` (table)
.onAlpha - alpha value of the widget when it is mouse-enabled and hovered. Defaults to 1 (number)[0-1]
.offAlpha - alpha value of the widget when it is mouse-enabled and not hovered. Defaults to 1 (number)[0-1]
.tooltipAnchor - anchor point for the tooltip. Defaults to 'ANCHOR_BOTTOMRIGHT' (string)
.unusableColor - the RGB values for the widget when the equipped artifact is unusable. Defaults to {1, 0, 0} (table)
## Attributes
.current - the amount of artifact power gained towards the next artifact level/trait (number?)
.max - the total amount of artifact power needed for the next artifact level/trait (number?)
.level - the current artifact level or the sum of learned and learnable traits (number?)
.unspentPower - the amount of unspent artifact power (number?)
.numTraitsLearnable - the number of learnable traits based on the amount of unspent artifact power (number?)
## Notes
A default texture will be applied if the widget is a `StatusBar` and doesn't have a texture or color set.
`OnEnter` and `OnLeave` handlers to display a tooltip will be set on the widget if it is mouse-enabled and the scripts
are not set by the layout.
`OnMouseUp` handler to show the artifact UI will be set on the widget if it is mouse-enabled and the script is not set
by the layout.
## Examples
-- Position and size
local ArtifactPower = CreateFrame('StatusBar', nil, self)
ArtifactPower:SetSize(200, 5)
ArtifactPower:SetPoint('TOP', self, 'BOTTOM')
-- Enable the tooltip
ArtifactPower:EnableMouse(true)
-- Register with oUF
self.ArtifactPower = ArtifactPower
--]]
local _, ns = ...
local oUF = ns.oUF or oUF
local ARTIFACT_BAR_COLOR = ARTIFACT_BAR_COLOR or CreateColor(0.901, 0.8, 0.601, 1)
local ItemDataLoadedCancelFunc
local function GetNumTraitsLearnable(numTraitsLearned, power, tier)
local numPoints = 0;
local powerForNextTrait = C_ArtifactUI.GetCostForPointAtRank(numTraitsLearned, tier)
while power >= powerForNextTrait and powerForNextTrait > 0 do
power = power - powerForNextTrait
numTraitsLearned = numTraitsLearned + 1
numPoints = numPoints + 1
powerForNextTrait = C_ArtifactUI.GetCostForPointAtRank(numTraitsLearned, tier)
end
return numPoints, power, powerForNextTrait
end
local function GetHeartOfAzeroth()
local azeriteItemLocation = C_AzeriteItem and C_AzeriteItem.FindActiveAzeriteItem()
local item = Item:CreateFromItemLocation(azeriteItemLocation)
if not item:IsItemEmpty() then
return azeriteItemLocation, item
end
end
for tag, func in next, {
['artifactpower:name'] = function()
if (not UnitHasVehicleUI('player')) then
local azeriteItemLocation = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, name = C_ArtifactUI.GetEquippedArtifactInfo()
return name
elseif (azeriteItemLocation) then
local link = GetInventoryItemLink('player', azeriteItemLocation.equipmentSlotIndex)
return link and link:match('%[(.+)%]')
end
end
end,
['artifactpower:power'] = function()
if (not UnitHasVehicleUI('player')) then
local azeriteItemLocation = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, _, _, unspentPower, numTraitsLearned, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
local _, power = GetNumTraitsLearnable(numTraitsLearned, unspentPower, tier)
return power
elseif (azeriteItemLocation) then
local power = C_AzeriteItem.GetAzeriteItemXPInfo(azeriteItemLocation)
return power
end
end
end,
['artifactpower:until_next'] = function()
if (not UnitHasVehicleUI('player')) then
local azeriteItemLocation = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, _, _, unspentPower, numTraitsLearned, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
local _, power, powerForNextTrait = GetNumTraitsLearnable(numTraitsLearned, unspentPower, tier)
return powerForNextTrait - power
elseif (azeriteItemLocation) then
local power, max = C_AzeriteItem.GetAzeriteItemXPInfo(azeriteItemLocation)
return max - power
end
end
end,
['artifactpower:until_next_per'] = function()
if (not UnitHasVehicleUI('player')) then
local azeriteItemLocation = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, _, _, unspentPower, numTraitsLearned, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
local _, power, powerForNextTrait = GetNumTraitsLearnable(numTraitsLearned, unspentPower, tier)
return math.floor(power / powerForNextTrait * 100 + 0.5)
elseif (azeriteItemLocation) then
local power, max = C_AzeriteItem.GetAzeriteItemXPInfo(azeriteItemLocation)
return math.floor(power / max * 100 + 0.5)
end
end
end,
['artifactpower:total_until_next'] = function() -- was next_trait_cost
if (not UnitHasVehicleUI('player')) then
local azeriteItemLocation = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, _, _, unspentPower, numTraitsLearned, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
local _, _, powerForNextTrait = GetNumTraitsLearnable(numTraitsLearned, unspentPower, tier)
return powerForNextTrait
elseif (azeriteItemLocation) then
local _, max = C_AzeriteItem.GetAzeriteItemXPInfo(azeriteItemLocation)
return max
end
end
end,
['artifactpower:unspent_power'] = function() -- was total
if (not HasArtifactEquipped() or UnitHasVehicleUI('player') or C_ArtifactUI.IsEquippedArtifactDisabled()) then return end
local _, _, _, _, unspentPower = C_ArtifactUI.GetEquippedArtifactInfo()
return unspentPower
end,
['artifactpower:traits_learnable'] = function()
if (not HasArtifactEquipped() or UnitHasVehicleUI('player') or C_ArtifactUI.IsEquippedArtifactDisabled()) then return end
local _, _, _, _, unspentPower, numTraitsLearned, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
local numTraitsLearnable = GetNumTraitsLearnable(numTraitsLearned, unspentPower, tier)
return numTraitsLearnable
end,
['artifactpower:traits_learned'] = function()
if (not HasArtifactEquipped() or UnitHasVehicleUI('player') or C_ArtifactUI.IsEquippedArtifactDisabled()) then return end
local _, _, _, _, _, numTraitsLearned = C_ArtifactUI.GetEquippedArtifactInfo()
return numTraitsLearned
end,
['artifactpower:tier'] = function()
if (not HasArtifactEquipped() or UnitHasVehicleUI('player') or C_ArtifactUI.IsEquippedArtifactDisabled()) then return end
local _, _, _, _, _, _, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
return tier
end,
['artifactpower:level'] = function()
if (not UnitHasVehicleUI('player')) then
local azeriteItemLocation = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, _, _, unspentPower, numTraitsLearned, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
local numTraitsLearnable = GetNumTraitsLearnable(numTraitsLearned, unspentPower, tier)
return numTraitsLearnable + numTraitsLearned
elseif (azeriteItemLocation) then
return C_AzeriteItem.GetPowerLevel(azeriteItemLocation)
end
end
end,
} do
oUF.Tags.Methods[tag] = func
oUF.Tags.Events[tag] = 'AZERITE_ITEM_EXPERIENCE_CHANGED ARTIFACT_XP_UPDATE UNIT_INVENTORY_CHANGED'
end
oUF.Tags.SharedEvents.AZERITE_ITEM_EXPERIENCE_CHANGED = true
oUF.Tags.SharedEvents.ARTIFACT_XP_UPDATE = true
--[[ Override: ArtifactPower:OnEnter()
Called when the mouse cursor enters the widget's interactive area.
* self - the ArtifactPower widget
--]]
local function OnEnter(element)
element:SetAlpha(element.onAlpha)
local _, azeriteItem = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, name = C_ArtifactUI.GetEquippedArtifactInfo()
GameTooltip:SetOwner(element, element.tooltipAnchor)
GameTooltip:SetText(name, HIGHLIGHT_FONT_COLOR:GetRGB())
GameTooltip:AddLine(
ARTIFACT_POWER_TOOLTIP_TITLE:format(
AbbreviateLargeNumbers(element.unspentPower),
AbbreviateLargeNumbers(element.current),
AbbreviateLargeNumbers(element.max)
),
nil, nil, nil, true
)
GameTooltip:AddLine(ARTIFACT_POWER_TOOLTIP_BODY:format(element.numTraitsLearnable), nil, nil, nil, true)
GameTooltip:Show()
elseif (azeriteItem) then
ItemDataLoadedCancelFunc = azeriteItem:ContinueWithCancelOnItemLoad(function()
GameTooltip:SetOwner(element, element.tooltipAnchor)
GameTooltip:SetText(
AZERITE_POWER_TOOLTIP_TITLE:format(element.level, element.max - element.current),
HIGHLIGHT_FONT_COLOR:GetRGB()
)
GameTooltip:AddLine(AZERITE_POWER_TOOLTIP_BODY:format(azeriteItem:GetItemName()))
GameTooltip:Show()
end)
else
element:Hide()
end
end
--[[ Override: ArtifactPower:OnLeave()
Called when the mouse cursor leaves the widget's interactive area.
* self - the ArtifactPower widget
--]]
local function OnLeave(element)
if (ItemDataLoadedCancelFunc) then
ItemDataLoadedCancelFunc()
ItemDataLoadedCancelFunc = nil
end
GameTooltip:Hide()
element:SetAlpha(element.offAlpha)
end
--[[ Override: ArtifactPower:OnMouseUp()
Used to show the artifact or azerite essence UI if the widget is mouse-enabled
and has been clicked.
* self - the ArtifactPower widget
--]]
local function OnMouseUp()
local azeriteItemLocation = C_AzeriteItem.FindActiveAzeriteItem();
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
SocketInventoryItem(INVSLOT_MAINHAND)
elseif (azeriteItemLocation) then
OpenAzeriteEssenceUIFromItemLocation(azeriteItemLocation)
end
end
--[[ Override: ArtifactPower:UpdateColor(isUsable)
Used to update the widget's color based on whether the equipped artifact is usable.
* self - the ArtifactPower widget
* isUsable - indicates whether the equipped artifact is usable (boolean)
--]]
local function UpdateColor(element, isUsable)
local color = isUsable and element.color or element.unusableColor
element:SetStatusBarColor(unpack(color))
end
local function Update(self, event, arg)
local unit = type(arg) == 'string' and arg
if (unit and unit ~= self.unit) then return end
local element = self.ArtifactPower
--[[ Callback: ArtifactPower:PreUpdate(event)
Called before the element has been updated.
* self - the ArtifactPower widget
* event - the event that triggered the update (string)
--]]
if (element.PreUpdate) then
element:PreUpdate(event)
end
local current, max, level, show
local isUsable = true
if (not UnitHasVehicleUI('player')) then
local azeriteItemLocation = GetHeartOfAzeroth()
if (HasArtifactEquipped() and not C_ArtifactUI.IsEquippedArtifactDisabled()) then
local _, _, _, _, unspentPower, numTraitsLearned, _, _, _, _, _, _, tier = C_ArtifactUI.GetEquippedArtifactInfo()
local numTraitsLearnable, power, powerForNextTrait = GetNumTraitsLearnable(numTraitsLearned, unspentPower, tier)
current = power
max = powerForNextTrait
level = numTraitsLearnable + numTraitsLearned
isUsable = not GetInventoryItemEquippedUnusable('player', INVSLOT_MAINHAND)
element.numTraitsLearnable = numTraitsLearnable
element.unspentPower = unspentPower
show = true
elseif (azeriteItemLocation) then
element.numTraitsLearnable = nil
element.unspentPower = nil
current, max = C_AzeriteItem.GetAzeriteItemXPInfo(azeriteItemLocation)
level = C_AzeriteItem.GetPowerLevel(azeriteItemLocation)
show = true
end
end
element.current = current
element.max = max
element.level = level
if (show) then
element:UpdateColor(isUsable)
if (element.SetAnimatedValues) then
element:SetAnimatedValues(current, 0, max, level)
else
element:SetMinMaxValues(0, max)
element:SetValue(current)
end
element:Show()
else
element:Hide()
end
--[[ Callback: ArtifactPower:PostUpdate(event, current, max, level, isUsable)
Called after the element has been updated.
* self - the ArtifactPower widget
* event - the event that triggered the update (string)
* current - the amount of artifact power gained towards the next artifact level/trait (number)
* max - the total amount of artifact power needed for the next artifact level/trait (number)
* level - the current artifact level or the sum of learned and learnable traits (number)
* isUsable - indicates whether the equipped artifact is usable (boolean)
--]]
if (element.PostUpdate) then
return element:PostUpdate(event, current, max, level, isUsable)
end
end
local function Path(self, ...)
--[[ Override: ArtifactPower.Override(event, ...)
Used to override the element's update process.
* self - the parent of the ArtifactPower widget
* event - the event that triggered the update (string)
* ... - the arguments accompanying the event
--]]
return (self.ArtifactPower.Override or Update)(self, ...)
end
local function ForceUpdate(element)
return Path(element.__owner, 'ForceUpdate', element.__owner.unit)
end
local function Enable(self, unit)
local element = self.ArtifactPower
if (not element or unit ~= 'player') then return end
element.__owner = self
element.ForceUpdate = ForceUpdate
if (element:IsObjectType('StatusBar')) then
if (not element:GetStatusBarTexture()) then
element:SetStatusBarTexture([[Interface\TargetingFrame\UI-StatusBar]])
end
end
element.color = element.color or { ARTIFACT_BAR_COLOR:GetRGB() }
element.unusableColor = element.unusableColor or { 1, 0, 0 }
element.UpdateColor = element.UpdateColor or UpdateColor
if (element:IsMouseEnabled()) then
element.tooltipAnchor = element.tooltipAnchor or 'ANCHOR_BOTTOMRIGHT'
element.onAlpha = element.onAlpha or 1
element.offAlpha = element.offAlpha or 1
element:SetAlpha(element.offAlpha)
if (not element:GetScript('OnEnter')) then
element:SetScript('OnEnter', element.OnEnter or OnEnter)
end
if (not element:GetScript('OnLeave')) then
element:SetScript('OnLeave', element.OnLeave or OnLeave)
end
if (not element:GetScript('OnMouseUp')) then
element:SetScript('OnMouseUp', element.OnMouseUp or OnMouseUp)
end
end
self:RegisterEvent('ARTIFACT_XP_UPDATE', Path, true)
self:RegisterEvent('AZERITE_ITEM_EXPERIENCE_CHANGED', Path, true)
self:RegisterEvent('UNIT_INVENTORY_CHANGED', Path)
return true
end
local function Disable(self)
local element = self.ArtifactPower
if (not element) then return end
if (ItemDataLoadedCancelFunc) then
ItemDataLoadedCancelFunc()
ItemDataLoadedCancelFunc = nil
end
self:UnregisterEvent('ARTIFACT_XP_UPDATE', Path)
self:UnregisterEvent('AZERITE_ITEM_EXPERIENCE_CHANGED', Path)
self:UnregisterEvent('UNIT_INVENTORY_CHANGED', Path)
element:Hide()
end
oUF:AddElement('ArtifactPower', Path, Enable, Disable)