/
anachronismStats.lua
303 lines (257 loc) · 9.87 KB
/
anachronismStats.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
local addonName, AS = ...; -- Get addon name and shared table.
AS.ContainerFrame = nil; -- Gets set in OnLoad.
-- Keys are integer position, value is string name of panel.
-- Persisted between sessions via SavedVariablesPerCharacter
AnachronismStats_PanelPositions = nil;
local _isOpen = false;
-- Keys are names, values are references to panels
local _panelReferences = nil;
-- //// PANEL POSITION HANDLING ////
local function GetOrLoadPanelPositions()
if AnachronismStats_PanelPositions then
return AnachronismStats_PanelPositions;
end
-- No saved variables? Generate inital positions
local attrPanel = AS.GetAttributesPanel();
local meleePanel = AS.GetMeleePanel();
local spellPanel = AS.GetSpellPanel();
local defensesPanel = AS.GetDefensePanel();
local rangedPanel = AS.GetRangedPanel();
AnachronismStats_PanelPositions = {};
AnachronismStats_PanelPositions[1] = attrPanel:GetName();
AnachronismStats_PanelPositions[2] = meleePanel:GetName();
AnachronismStats_PanelPositions[3] = spellPanel:GetName();
AnachronismStats_PanelPositions[4] = defensesPanel:GetName();
AnachronismStats_PanelPositions[5] = rangedPanel:GetName();
return AnachronismStats_PanelPositions;
end
local function ArrangePanels(panelPositions)
for i, v in ipairs(panelPositions) do
local currPanel = _panelReferences[v];
if i == 1 then
-- Special case for the first panel, as it gets positioned relative to the root frame
currPanel:SetPoint("TOPLEFT", AnachronismStatsContent, "TOPLEFT", 5, 0);
else
-- Everything else will be relative to the frame above itself
local prevPanel = _panelReferences[panelPositions[i - 1]];
local prevPanelHeight = prevPanel:GetHeight();
currPanel:SetPoint("TOPLEFT", prevPanel, "TOPLEFT", 0, -prevPanelHeight);
end
end
end
local function GetPanelPosition(panelPositions, panelName)
for i, v in ipairs(panelPositions) do
if v == panelName then
return i;
end
end
return -1;
end
local function SwapPanelPositions(panelPositions, i1, i2)
local panelAt1 = panelPositions[i1];
panelPositions[i1] = panelPositions[i2];
panelPositions[i2] = panelAt1;
end
-- //// END PANEL POSITION HANDLING ////
-- //// GLOBAL FUNCTIONS ////
local equipmentSlots = {
1, -- head
2, -- neck
3, -- shoulders
15, -- cloak
5, -- chest
9, -- bracers
10, -- gloves
6, -- belt
7, -- pants
8, -- boots
11, -- ring 1
12, -- ring 2
13, -- trinket 1
14, -- trinket 2
16, -- main hand
17, -- off hand
18, -- ranged slot
}
function AS.GetMp5FromEquippedItems()
local summedMp5 = 0;
for _, slotId in ipairs(equipmentSlots) do
local itemId = GetInventoryItemID("player", slotId);
if (itemId ~= nil) then
local statsTable = {}; -- need an empty table here, because GetItemStats() won't clear it for us
GetItemStats("item:" .. itemId, statsTable); -- this fills the stats table
local mp5Value = statsTable["ITEM_MOD_POWER_REGEN0_SHORT"]; -- aka MP5
if (mp5Value ~= nil) then
summedMp5 = summedMp5 + mp5Value + 1; -- for some reason, MP5 values are returned as 1 less than actual, so add 1
end
end
end
return summedMp5;
end
function AS.GetStatValue(base, posBuff, negBuff)
local effective = max(0, base + posBuff + negBuff);
if ((posBuff == 0) and (negBuff == 0)) then
return effective;
else
-- if there is a negative buff then show the main number in red, even if there are
-- positive buffs. Otherwise show the number in green
if (negBuff < 0) then
return RED_FONT_COLOR_CODE .. effective .. FONT_COLOR_CODE_CLOSE;
else
return GREEN_FONT_COLOR_CODE .. effective .. FONT_COLOR_CODE_CLOSE;
end
end
end
function AS.GetStatTooltipText(name, base, posBuff, negBuff)
local effective = max(0, base + posBuff + negBuff);
local tooltipRow1 = HIGHLIGHT_FONT_COLOR_CODE .. name .. " " .. effective;
if ((posBuff == 0) and (negBuff == 0)) then
tooltipRow1 = tooltipRow1 .. FONT_COLOR_CODE_CLOSE;
else
if (posBuff > 0 or negBuff < 0) then
tooltipRow1 = tooltipRow1 .. " (" .. base .. FONT_COLOR_CODE_CLOSE;
end
if (posBuff > 0) then
tooltipRow1 = tooltipRow1 .. FONT_COLOR_CODE_CLOSE .. GREEN_FONT_COLOR_CODE .. "+" .. posBuff ..
FONT_COLOR_CODE_CLOSE;
end
if (negBuff < 0) then
tooltipRow1 = tooltipRow1 .. RED_FONT_COLOR_CODE .. " " .. negBuff .. FONT_COLOR_CODE_CLOSE;
end
if (posBuff > 0 or negBuff < 0) then
tooltipRow1 = tooltipRow1 .. HIGHLIGHT_FONT_COLOR_CODE .. ")" .. FONT_COLOR_CODE_CLOSE;
end
end
return tooltipRow1;
end
function AS.ShowStatTooltip(self)
if (not (self.tooltipRow1) and not (self.tooltipSpecialCase)) then
return;
end
-- If the tooltip is super special, it can handle its own nonsense. Let it take over and bail out.
if (self.tooltipSpecialCase) then
self.tooltipSpecialCase(self);
return;
end
GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
GameTooltip:SetText(self.tooltipRow1, 1.0, 1.0, 1.0);
if (self.tooltipRow2) then
GameTooltip:AddLine(self.tooltipRow2, NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, true);
end
GameTooltip:Show();
end
function AS.StatPanel_UpArrow_OnClick(panel)
local panelPositions = GetOrLoadPanelPositions();
local panelPosition = GetPanelPosition(panelPositions, panel:GetName());
if (panelPosition == 1 or panelPosition == -1) then
-- We're either at the top, or we got an invalid position
return;
end
SwapPanelPositions(panelPositions, panelPosition, panelPosition - 1);
ArrangePanels(panelPositions);
end
function AS.StatPanel_DownArrow_OnClick(panel)
local panelPositions = GetOrLoadPanelPositions();
local panelPosition = GetPanelPosition(panelPositions, panel:GetName());
if (panelPosition == -1 or panelPosition == 5) then
-- We're either at the bottom, or got an invalid position
return;
end
SwapPanelPositions(panelPositions, panelPosition, panelPosition + 1);
ArrangePanels(panelPositions);
end
local function SetMainFrameVisible(visible)
if (visible) then
AnachronismStatsFrame:Show();
SquareButton_SetIcon(AS_OpenStats, "LEFT");
_isOpen = true;
else
AnachronismStatsFrame:Hide();
SquareButton_SetIcon(AS_OpenStats, "RIGHT");
_isOpen = false;
end
end
-- //// END GLOBAL FUNCTIONS ////
-- //// EVENT HANDLERS ////
function AnachronismStats_OpenStats_OnClick()
if (_isOpen) then
SetMainFrameVisible(false);
else
SetMainFrameVisible(true);
end
end
function AnachronismStats_OpenStats_OnLoad(self)
SquareButton_SetIcon(self, "RIGHT");
end
function AnachronismStats_OpenStats_OnHide()
-- Make sure we're not sitting in the background if this tab has focus
-- when the character frame is closed.
SetMainFrameVisible(false);
end
function AnachronismStats_Frame_OnMouseWheel(self, delta)
local current = AnachronismStatsScrollFrame_VSlider:GetValue();
local _, maxValue = AnachronismStatsScrollFrame_VSlider:GetMinMaxValues();
if (delta < 0) and (current < maxValue) then
AnachronismStatsScrollFrame_VSlider:SetValue(current + 5);
elseif (delta > 0) and (current > 1) then
AnachronismStatsScrollFrame_VSlider:SetValue(current - 5);
end
end
function AnachronismStats_Frame_OnLoad(self)
AS.ContainerFrame = AnachronismStatsContent;
self:RegisterEvent("ADDON_LOADED");
self:RegisterEvent("UNIT_RESISTANCES");
self:RegisterEvent("UNIT_STATS");
self:RegisterEvent("UNIT_DAMAGE");
self:RegisterEvent("UNIT_RANGEDDAMAGE");
self:RegisterEvent("PLAYER_DAMAGE_DONE_MODS");
self:RegisterEvent("UNIT_ATTACK_SPEED");
self:RegisterEvent("UNIT_ATTACK_POWER");
self:RegisterEvent("UNIT_RANGED_ATTACK_POWER");
self:RegisterEvent("UNIT_ATTACK");
self:RegisterEvent("SKILL_LINES_CHANGED");
self:RegisterEvent("UNIT_AURA");
self:RegisterEvent("UNIT_MAXHEALTH");
self:RegisterEvent("COMBAT_RATING_UPDATE");
-- Queue one initial update
self:SetScript("OnUpdate", AnachronismStats_Frame_QueuedUpdate);
end
local function LoadCompleted()
SetMainFrameVisible(false);
local attrPanel = AS.GetAttributesPanel();
local meleePanel = AS.GetMeleePanel();
local spellPanel = AS.GetSpellPanel();
local defensesPanel = AS.GetDefensePanel();
local rangedPanel = AS.GetRangedPanel();
_panelReferences = {};
_panelReferences[attrPanel:GetName()] = attrPanel;
_panelReferences[meleePanel:GetName()] = meleePanel;
_panelReferences[spellPanel:GetName()] = spellPanel;
_panelReferences[defensesPanel:GetName()] = defensesPanel;
_panelReferences[rangedPanel:GetName()] = rangedPanel;
local panelPositions = GetOrLoadPanelPositions();
ArrangePanels(panelPositions);
print("AnachronismStats loaded!");
end
function AnachronismStats_Frame_OnEvent(self, event, ...)
if (event == "ADDON_LOADED" and ... == "AnachronismStats") then
LoadCompleted();
else
-- Make sure we batch event updates to only happen once-per-frame.
self:SetScript("OnUpdate", AnachronismStats_Frame_QueuedUpdate);
end
end
local function UpdateStatFrames()
local playerLevel = UnitLevel("player");
AS.Frame_SetAttributes(playerLevel);
AS.Frame_SetMelee(playerLevel);
AS.Frame_SetRanged(playerLevel);
AS.Frame_SetSpell(playerLevel);
AS.Frame_SetDefenses(playerLevel);
end
function AnachronismStats_Frame_QueuedUpdate(self)
-- Clear the queued update.
self:SetScript("OnUpdate", nil);
UpdateStatFrames();
end
-- //// END EVENT HANDLERS ////