-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPresets.lua
More file actions
355 lines (328 loc) · 13 KB
/
Copy pathPresets.lua
File metadata and controls
355 lines (328 loc) · 13 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
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
---@type string, AddonTable
local _, addonTable = ...
local L = addonTable.L
local ReforgeLite = addonTable.ReforgeLite
local GUI = addonTable.GUI
local StatHit = addonTable.statIds.HIT
local StatCrit = addonTable.statIds.CRIT
local StatHaste = addonTable.statIds.HASTE
local StatExp = addonTable.statIds.EXP
local function CreateIconMarkup(icon)
-- 4.4.2 may not have the texcoord form of CreateTextureMarkup; fall back to the simple one.
if CreateTextureMarkup then
return CreateTextureMarkup(icon, 64, 64, 18, 18, 0.07, 0.93, 0.07, 0.93, 0, 0) .. " "
end
return CreateSimpleTextureMarkup(icon, 18, 18) .. " "
end
addonTable.CreateIconMarkup = CreateIconMarkup
-- Cap presets, haste breakpoints, cap-set constants, preset builders, and the class
-- preset tables (addonTable.classPresets / ReforgeLite.capPresets / addonTable.CAPS)
-- are flavor-specific and live in <Flavor>/Presets.lua, loaded after this file.
local specInfo
local function UpdateSpecInfo(ids)
for _, id in pairs(ids) do
local _, tabName, _, icon = addonTable.compat.GetSpecInfoByID(id)
specInfo[id] = { name = tabName, icon = icon }
end
end
---Initializes class-specific stat weight and cap presets
---Loads presets for all specs of the player's class (or all classes in debug mode)
---@return nil
function ReforgeLite:InitClassPresets()
self.presets = wipe(self.presets or {})
specInfo = wipe(specInfo or {})
if self.db.debug then
for classFile, className in pairs(LOCALIZED_CLASS_NAMES_MALE) do
self.presets[className] = addonTable.classPresets[classFile]
end
TableUtil.Execute(addonTable.SPEC_IDS, UpdateSpecInfo)
else
for specId, preset in pairs(addonTable.classPresets[addonTable.playerClass]) do
self.presets[specId] = preset
end
UpdateSpecInfo(addonTable.SPEC_IDS[addonTable.playerClass])
end
end
local DYNAMIC_PRESETS = tInvert( { "Pawn", CUSTOM } )
---Initializes custom user-created presets from saved variables
---@return nil
function ReforgeLite:InitCustomPresets()
local customPresets = {}
for k, v in pairs(self.cdb.customPresets) do
local preset = CopyTable(v)
preset.name = k
tinsert(customPresets, preset)
end
self.presets[CUSTOM] = customPresets
end
---Initializes all dynamic presets (class and custom)
---@return nil
function ReforgeLite:InitDynamicPresets()
self:InitClassPresets()
self:InitCustomPresets()
end
---Initializes all presets including Pawn integration and preset menu
---Sets up class presets, custom presets, Pawn integration, and menu generator
---@return nil
function ReforgeLite:InitPresets()
self:InitDynamicPresets()
if PawnVersion then
self.presets["Pawn"] = function ()
if not PawnCommon or not PawnCommon.Scales then return {} end
local result = {}
for k, v in pairs (PawnCommon.Scales) do
if v.ClassID == addonTable.playerClassID then
local preset = {name = v.LocalizedName or k}
preset.weights = {}
local raw = v.Values or {}
preset.weights[addonTable.statIds.SPIRIT] = raw["Spirit"] or 0
preset.weights[addonTable.statIds.DODGE] = raw["DodgeRating"] or 0
preset.weights[addonTable.statIds.PARRY] = raw["ParryRating"] or 0
preset.weights[StatHit] = raw["HitRating"] or 0
preset.weights[StatCrit] = raw["CritRating"] or 0
preset.weights[StatHaste] = raw["HasteRating"] or 0
preset.weights[StatExp] = raw["ExpertiseRating"] or 0
preset.weights[addonTable.statIds.MASTERY] = raw["MasteryRating"] or 0
local total = 0
local average = 0
for i = 1, addonTable.itemStatCount do
if preset.weights[i] ~= 0 then
total = total + 1
average = average + preset.weights[i]
end
end
if total > 0 and average > 0 then
local factor = 1
while factor * average / total < 10 do
factor = factor * 100
end
while factor * average / total > 1000 do
factor = factor / 10
end
for i = 1, addonTable.itemStatCount do
preset.weights[i] = preset.weights[i] * factor
end
tinsert(result, preset)
end
end
end
return result
end
end
self.presetMenuGenerator = function(owner, rootDescription)
GUI:ClearEditFocus()
local saveButton = rootDescription:CreateButton(SAVE, function()
GUI.CreateStaticPopup("SAVE_PRESET",
L["Enter the preset name"],
function(popup)
self.cdb.customPresets[popup:GetEditBox():GetText()] = {
caps = CopyTable(self.pdb.caps),
weights = CopyTable(self.pdb.weights)
}
self:InitCustomPresets()
end, { hasEditBox = 1 })
end)
saveButton:SetTitleAndTextTooltip(L["Save current stat weights and caps as a custom preset"], L["Custom presets are shared across all characters of this class"])
rootDescription:CreateDivider()
local function GetCapPreset(presetValue)
return FindValueInTableIf(self.capPresets, function(preset) return preset.value == presetValue end) or {}
end
local function FormatWeightsTooltip(tooltip, element, preset)
if not preset or not preset.weights then return end
local statWeights = {}
for i, weight in ipairs(preset.weights) do
if weight and weight > 0 then
tinsert(statWeights, {stat = addonTable.itemStats[i].long, weight = weight, index = i})
end
end
local rightR, rightG, rightB = addonTable.COLORS.white:GetRGB()
if statWeights[1] then
tooltip:AddLine(element.text, rightR, rightG, rightB)
sort(statWeights, function(a, b)
if a.weight == b.weight then
return a.index < b.index
end
return a.weight > b.weight
end)
for _, entry in ipairs(statWeights) do
tooltip:AddDoubleLine(entry.stat, entry.weight, nil, nil, nil, rightR, rightG, rightB)
end
end
if preset.caps then
local methodNames = {
[addonTable.StatCapMethods.AtLeast] = L["At least"],
[addonTable.StatCapMethods.AtMost] = L["At most"],
[addonTable.StatCapMethods.Exactly] = L["Exactly"],
}
for i, cap in ipairs(preset.caps) do
if cap and cap.stat and cap.stat > 0 and cap.points and cap.points[1] then
local statName = addonTable.itemStats[cap.stat] and addonTable.itemStats[cap.stat].long or ""
local pt = cap.points[1]
local presetInfo = GetCapPreset(pt.preset)
local methodName = methodNames[pt.method] or ""
local capText
if presetInfo and pt.preset ~= addonTable.CAPS.ManualCap then
capText = ("%s %s"):format(methodName, presetInfo.name)
else
capText = ("%s %d"):format(methodName, pt.value or 0)
end
tooltip:AddLine(L["Cap %d - %s"]:format(i, statName))
tooltip:AddLine(" " .. capText, rightR, rightG, rightB)
end
end
end
end
local function AddPresetButton(desc, info)
if info.hasDelete then
local button = desc:CreateButton(info.text, function(mouseButton)
if IsShiftKeyDown() then
GUI.CreateStaticPopup("DELETE_PRESET",
L["Delete preset '%s'?"]:format(info.presetName),
function()
self.cdb.customPresets[info.presetName] = nil
self:InitCustomPresets()
end, { button1 = DELETE })
else
if info.value.targetLevel then
self.pdb.targetLevel = info.value.targetLevel
self.targetLevel:SetValue(info.value.targetLevel)
end
self:SetStatWeights(info.value.weights, info.value.caps or {})
end
end)
button:SetTooltip(function(tooltip, element)
FormatWeightsTooltip(tooltip, element, info.value)
tooltip:AddLine(" ")
GameTooltip_AddColoredLine(tooltip, L["Shift+Click to delete"], addonTable.COLORS.red)
end)
else
local button = desc:CreateButton(info.text, function()
if info.value.targetLevel then
self.pdb.targetLevel = info.value.targetLevel
self.targetLevel:SetValue(info.value.targetLevel)
end
self:SetStatWeights(info.value.weights, info.value.caps or {})
end)
button:SetTooltip(function(tooltip, element)
FormatWeightsTooltip(tooltip, element, info.value)
end)
end
end
local menuList = {}
for k in pairs(self.presets) do
local v = GetValueOrCallFunction(self.presets, k)
local isClassMenu = type(v) == "table" and not v.weights and not v.caps
if isClassMenu then
local classInfo = {
sortKey = specInfo[k] and specInfo[k].name or k,
text = specInfo[k] and specInfo[k].name or k,
prioritySort = DYNAMIC_PRESETS[k] or 0,
key = k,
isSubmenu = true,
submenuItems = {}
}
if specInfo[k] then
classInfo.text = CreateIconMarkup(specInfo[k].icon) .. specInfo[k].name
end
for specId in pairs(v) do
local preset = GetValueOrCallFunction(v, specId)
local hasSubPresets = type(preset) == "table" and not preset.weights and not preset.caps
if hasSubPresets then
local specSubmenu = {
sortKey = (specInfo[specId] and specInfo[specId].name) or tostring(specId),
text = (specInfo[specId] and specInfo[specId].name) or tostring(specId),
prioritySort = 0,
isSubmenu = true,
submenuItems = {}
}
if specInfo[specId] then
specSubmenu.text = CreateIconMarkup(specInfo[specId].icon) .. specInfo[specId].name
end
for subK, subPreset in pairs(preset) do
if type(subPreset) == "table" and (subPreset.weights or subPreset.caps) then
local subSubInfo = {
sortKey = subK,
text = subK,
prioritySort = DYNAMIC_PRESETS[subK] or 0,
value = subPreset,
}
if subPreset.icon then
subSubInfo.text = CreateIconMarkup(subPreset.icon) .. subSubInfo.text
end
tinsert(specSubmenu.submenuItems, subSubInfo)
end
end
if #specSubmenu.submenuItems > 0 then
sort(specSubmenu.submenuItems, function (a, b)
if a.prioritySort ~= b.prioritySort then
return a.prioritySort > b.prioritySort
end
return tostring(a.sortKey) < tostring(b.sortKey)
end)
tinsert(classInfo.submenuItems, specSubmenu)
end
else
local subInfo = {
sortKey = preset.name or (specInfo[specId] and specInfo[specId].name) or tostring(specId),
text = preset.name or (specInfo[specId] and specInfo[specId].name) or tostring(specId),
prioritySort = DYNAMIC_PRESETS[k] or 0,
value = preset,
hasDelete = (k == CUSTOM),
presetName = preset.name,
}
if specInfo[specId] then
subInfo.text = CreateIconMarkup(specInfo[specId].icon) .. specInfo[specId].name
subInfo.sortKey = specInfo[specId].name
end
if preset.icon then
subInfo.text = CreateIconMarkup(preset.icon) .. subInfo.text
end
tinsert(classInfo.submenuItems, subInfo)
end
end
sort(classInfo.submenuItems, function (a, b)
if a.prioritySort ~= b.prioritySort then
return a.prioritySort > b.prioritySort
end
return tostring(a.sortKey) < tostring(b.sortKey)
end)
tinsert(menuList, classInfo)
else
local info = {
sortKey = v.name or k,
text = v.name or k,
prioritySort = DYNAMIC_PRESETS[k] or 0,
value = v,
}
if specInfo[k] then
info.text = CreateIconMarkup(specInfo[k].icon) .. specInfo[k].name
info.sortKey = specInfo[k].name
end
if v.icon then
info.text = CreateIconMarkup(v.icon) .. info.text
end
tinsert(menuList, info)
end
end
sort(menuList, function (a, b)
if a.prioritySort ~= b.prioritySort then
return a.prioritySort > b.prioritySort
end
return tostring(a.sortKey) < tostring(b.sortKey)
end)
local function AddMenuItems(desc, items)
for _, info in ipairs(items) do
if info.isSubmenu then
local submenu = desc:CreateButton(info.text)
if #info.submenuItems == 0 then
submenu:SetEnabled(false)
end
AddMenuItems(submenu, info.submenuItems)
elseif info.value and (info.value.caps or info.value.weights) then
AddPresetButton(desc, info)
end
end
end
AddMenuItems(rootDescription, menuList)
end
end