-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoUF_Reputation.lua
377 lines (304 loc) · 11.1 KB
/
oUF_Reputation.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
--[[ Home:header
# Element: Reputation
Adds support for an element that updates and displays the player's reputation and standing with a
tracked faction as a StatusBar widget.
## Widgets
- `Repuration`
A statusbar which displays the player's current reputation with the tracked faction.
- `Reputation.Reward`
An optional widget that is visible if the tracked faction has a pending reward.
## Options
- `inAlpha` - Alpha used when the mouse is over the element (default: `1`)
- `outAlpha` - Alpha used when the mouse is outside of the element (default: `1`)
- `tooltipAnchor` - Anchor for the tooltip (default: `"ANCHOR_BOTTOMRIGHT"`)
## Extras
- [Callbacks](Callbacks)
- [Overrides](Overrides)
- [Tags](Tags)
## Colors
This plug-in adds another color to `oUF.colors.reaction` for paragon support, after exalted.
## Notes
- A default texture will be applied if the element is a StatusBar and doesn't have a texture set.
- A default texture will be applied to the `Reward` sub-widget if it's a Texture and doesn't have a texture set.
- Tooltip and mouse interaction options are only enabled if the element is mouse-enabled.
- Remember to set the plug-in as an optional dependency for the layout if not embedding.
## Example implementation
```lua
-- Position and size
local Reputation = CreateFrame('StatusBar', nil, self)
Reputation:SetPoint('BOTTOM', 0, -50)
Reputation:SetSize(200, 20)
Reputation:EnableMouse(true) -- for tooltip/fading support
-- Position and size the Reward sub-widget
local Reward = Reputation:CreateTexture(nil, 'OVERLAY')
Reward:SetPoint('LEFT')
Reward:SetSize(20, 20)
-- Text display
local Value = Reputation:CreateFontString(nil, 'OVERLAY')
Value:SetAllPoints(Reputation)
Value:SetFontObject(GameFontHighlight)
self:Tag(Value, '[reputation:cur] / [reputation:max]')
-- Add a background
local Background = Reputation:CreateTexture(nil, 'BACKGROUND')
Background:SetAllPoints(Reputation)
Background:SetTexture('Interface\\ChatFrame\\ChatFrameBackground')
-- Register with oUF
self.Reputation = Reputation
self.Reputation.Reward = Reward
```
--]]
local _, addon = ...
local oUF = addon.oUF or oUF
assert(oUF, 'oUF Reputation was unable to locate oUF install')
local PARAGON
do
-- pulled from title of http://www.wowhead.com/faction=2089
local paragonStrings = {
deDE = 'Huldigend',
esES = 'Baluarte',
frFR = 'Parangon',
itIT = 'Eccellenza',
ptBR = 'Parag\195\163o',
ruRU = '\208\152\208\180\208\181\208\176\208\187',
koKR = '\235\182\136\235\169\184\236\157\152 \235\143\153\235\167\185',
zhCN = '\229\183\133\229\179\176',
}
paragonStrings.esMX = paragonStrings.esES
paragonStrings.zhTW = paragonStrings.zhCN
PARAGON = paragonStrings[GetLocale()] or 'Paragon'
end
local function getReputation()
local pendingReward, standingText
local name, standingID, min, max, cur, factionID = GetWatchedFactionInfo()
local friendshipInfo = C_GossipInfo.GetFriendshipReputation(factionID)
if friendshipInfo then
if not friendshipInfo.nextThreshold then
min, max, cur = 0, 1, 1 -- force a full bar when maxed out
end
standingID = 5 -- force friends' color
standingText = friendshipInfo.text
else
local value, nextThreshold, _, hasRewardPending = C_Reputation.GetFactionParagonInfo(factionID)
if value then
cur = value % nextThreshold
min = 0
max = nextThreshold
pendingReward = hasRewardPending
standingID = MAX_REPUTATION_REACTION + 1 -- force paragon's color
standingText = PARAGON
end
end
max = max - min
cur = cur - min
if cur == max then
-- cur and max are both 0 for maxed out factions
cur, max = 1, 1
end
if not standingText then
standingText = GetText('FACTION_STANDING_LABEL' .. standingID, UnitSex('player'))
end
return cur, max, name, factionID, standingID, standingText, pendingReward
end
--[[ Tags:header
A few basic tags are included:
- `[reputation:cur]` - the player's current reputation with the faction
- `[reputation:max]` - the player's maximum reputation with the faction
- `[reputation:per]` - the player's percentage of reputation with the faction
- `[reputation:standing]` - the player's current standing with the faction
- `[reputation:faction]` - the name of the player's currently tracked faction
See the [Examples](./#example-implementation) section on how to use the tags.
--]]
for tag, func in next, {
['reputation:cur'] = function()
return (getReputation())
end,
['reputation:max'] = function()
local _, max = getReputation()
return max
end,
['reputation:per'] = function()
local cur, max = getReputation()
return math.floor(cur / max * 100 + 1/2)
end,
['reputation:standing'] = function()
local _, _, _, _, _, standingText = getReputation()
return standingText
end,
['reputation:faction'] = function()
local _, _, name = getReputation()
return name
end,
} do
oUF.Tags.Methods[tag] = func
oUF.Tags.Events[tag] = 'UPDATE_FACTION'
end
oUF.Tags.SharedEvents.UPDATE_FACTION = true
oUF.colors.reaction[MAX_REPUTATION_REACTION + 1] = oUF:CreateColor(0, 0.5, 0.9) -- paragon color
local function UpdateTooltip(element)
local cur, max, name, factionID, standingID, standingText, pendingReward = getReputation()
local rewardAtlas = pendingReward and '|A:ParagonReputation_Bag:0:0:0:0|a' or ''
local _, desc = GetFactionInfoByID(factionID)
local color = element.__owner.colors.reaction[standingID]
GameTooltip:SetText(name, color[1], color[2], color[3])
GameTooltip:AddLine(desc, nil, nil, nil, true)
if cur ~= max then
GameTooltip:AddLine(string.format('%s (%s / %s) %s', standingText, BreakUpLargeNumbers(cur), BreakUpLargeNumbers(max), rewardAtlas), 1, 1, 1)
else
GameTooltip:AddLine(standingText, 1, 1, 1)
end
GameTooltip:Show()
end
local function OnEnter(element)
element:SetAlpha(element.inAlpha)
GameTooltip:SetOwner(element, element.tooltipAnchor)
--[[ Overrides:header
### element:OverrideUpdateTooltip()
Used to completely override the internal function for updating the tooltip.
- `self` - the Reputation element
--]]
if element.OverrideUpdateTooltip then
element:OverrideUpdateTooltip()
else
UpdateTooltip(element)
end
end
local function OnLeave(element)
GameTooltip:Hide()
element:SetAlpha(element.outAlpha)
end
local function OnMouseUp()
ToggleCharacter('ReputationFrame')
end
local function Update(self, event, unit)
local element = self.Reputation
if element.PreUpdate then
--[[ Callbacks:header
### element:PreUpdate(_unit_)
Called before the element has been updated.
- `self` - the Reputation element
- `unit` - the unit for which the update has been triggered _(string)_
--]]
element:PreUpdate(unit)
end
local cur, max, name, factionID, standingID, standingText, pendingReward = getReputation()
if name then
element:SetMinMaxValues(0, max)
element:SetValue(cur)
if element.colorStanding then
local colors = self.colors.reaction[standingID]
element:SetStatusBarColor(colors[1], colors[2], colors[3])
end
if element.Reward then
-- no idea what this function actually does, but Blizzard uses it as well
C_Reputation.RequestFactionParagonPreloadRewardData(factionID)
element.Reward:SetShown(pendingReward)
end
end
if element.PostUpdate then
--[[ Callbacks:header
### element:PostUpdate(_unit, cur, max, factionName, factionID, standingID, standingText, pendingReward_)
Called after the element has been updated.
- `self` - the Reputation element
- `unit` - the unit for which the update has been triggered _(string)_
- `cur` - the current reputation with the tracked faction _(number)_
- `max` - the maximum reputation with the tracked faction _(number)_
- `factionName` - the name of the tracked faction _(string)_
- `factionID` - the identifier for the tracked faction _(number)_
- `standingID` - the identifier for the standing for the tracked faction _(number)_
- `standingText` - the name of the standing for the tracked faction _(string)_
- `pendingReward` - indicates if there's a pending paragon reward with the faction _(boolean)_
--]]
return element:PostUpdate(unit, cur, max, name, factionID, standingID, standingText, pendingReward)
end
end
local function Path(self, ...)
--[[ Overrides:header
### element.Override(_self, event, unit_)
Used to completely override the internal update function.
Overriding this function also disables the [Callbacks](Callbacks).
- `self` - the parent object
- `event` - the event triggering the update _(string)_
- `unit` - the unit accompanying the event _(variable(s))_
--]]
return (self.Reputation.Override or Update) (self, ...)
end
local function ElementEnable(self)
local element = self.Reputation
self:RegisterEvent('UPDATE_FACTION', Path, true)
element:Show()
element:SetAlpha(element.outAlpha or 1)
Path(self, 'ElementEnable', 'player')
end
local function ElementDisable(self)
self:UnregisterEvent('UPDATE_FACTION', Path)
self.Reputation:Hide()
Path(self, 'ElementDisable', 'player')
end
local function Visibility(self, event, unit, selectedFactionIndex)
local shouldEnable
if selectedFactionIndex ~= nil then
if selectedFactionIndex > 0 then
shouldEnable = true
end
elseif not not (GetWatchedFactionInfo()) then
shouldEnable = true
end
if shouldEnable then
ElementEnable(self)
else
ElementDisable(self)
end
end
local function VisibilityPath(self, ...)
--[[ Overrides:header
### element.OverrideVisibility(_self, event, unit_)
Used to completely override the element's visibility update process.
The internal function is also responsible for (un)registering events related to the updates.
- `self` - the parent object
- `event` - the event triggering the update _(string)_
- `unit` - the unit accompanying the event _(variable(s))_
--]]
return (self.Reputation.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.Reputation
if element then
element.__owner = self
element.ForceUpdate = ForceUpdate
hooksecurefunc('SetWatchedFactionIndex', function(selectedFactionIndex)
if self:IsElementEnabled('Reputation') then
VisibilityPath(self, 'SetWatchedFactionIndex', 'player', selectedFactionIndex or 0)
end
end)
if not element:GetStatusBarTexture() then
element:SetStatusBarTexture([[Interface\TargetingFrame\UI-StatusBar]])
end
if element.Reward and element.Reward:IsObjectType('Texture') and not element.Reward:GetTexture() then
element.Reward:SetAtlas('ParagonReputation_Bag')
end
if element:IsMouseEnabled() then
element.tooltipAnchor = element.tooltipAnchor or 'ANCHOR_BOTTOMRIGHT'
element.inAlpha = element.inAlpha or 1
element.outAlpha = element.outAlpha or 1
if not element:GetScript('OnEnter') then
element:SetScript('OnEnter', OnEnter)
end
if not element:GetScript('OnLeave') then
element:SetScript('OnLeave', OnLeave)
end
if not element:GetScript('OnMouseUp') then
element:SetScript('OnMouseUp', OnMouseUp)
end
end
return true
end
end
local function Disable(self)
if self.Reputation then
ElementDisable(self)
end
end
oUF:AddElement('Reputation', VisibilityPath, Enable, Disable)