-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Advanced.lua
496 lines (451 loc) · 13.5 KB
/
Advanced.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
local addonName, core = ...
local L = core.L
local selectedValue = {}
local reverseList = {}
local module = core:NewModule("Advanced", core.config:AddSubCategory("Advanced"))
local editbox
do
local function update(key, value, oldKey)
local list = module:GetSelectedList()
if selectedValue.isNumeric and value then
reverseList[list[key]] = nil
reverseList[value] = true
end
if oldKey then
value = list[oldKey]
list[oldKey] = nil
end
list[key] = value
local onAccept = selectedValue.onAccept
if onAccept then
onAccept(key, value)
end
core:UpdateSpells()
module:Update()
end
local function validateKey(self, id)
local isValid, reason = true
if id ~= self.data and module:GetSelectedList()[id] then
isValid = false
reason = L["This spell ID is already mapped."]
end
if reverseList[id] then
-- the value of a mapping equals the specified ID, which we won't allow since it won't work very well
isValid = false
reason = L["Conflicting mapping."]
end
return isValid, reason
end
local function onTextChanged(self)
if self.validate:IsShown() then
local isValid, reason = self:Validate(self:GetValue())
if isValid then
if not C_Spell.GetSpellLink(self:GetValue()) then
self.validate.tex:SetTexture([[Interface\RaidFrame\ReadyCheck-Waiting]])
reason = L["Invalid spell ID."]
else
self.validate.tex:SetTexture([[Interface\RaidFrame\ReadyCheck-Ready]])
end
else
self.validate.tex:SetTexture([[Interface\RaidFrame\ReadyCheck-NotReady]])
end
self.validate.reason = reason
end
end
local function onShow(self)
local isNumeric = self.type == "key" or selectedValue.isNumeric
self:SetNumeric(isNumeric)
self.GetValue = isNumeric and self.GetNumber or self.GetText
self:ClearAllPoints()
self:SetPoint("TOPLEFT", self.parent, 5, 0)
if not isNumeric then
-- non-spell ID exclusive editboxes will be wider
self:SetPoint("TOPRIGHT", self.parent)
end
self.Validate = self.type == "key" and validateKey or selectedValue.validate
self.validate:SetShown(self.Validate)
self:SetText(self.data)
onTextChanged(self)
self:HighlightText()
self:SetFocus()
end
local function onHide(self)
self.parent:Show()
end
local function onEnterPressed(self)
local text = self:GetValue()
if self.Validate then
local isValid, reason = self:Validate(text)
if not isValid then
core:Message(reason)
return
end
end
if self:GetText():trim() == "" then
return
end
if text ~= self.data then
if self.type == "key" then
update(text, nil, self.data)
else
update(self.key, text)
end
end
self:Hide()
end
editbox = core:CreateEditbox(module)
editbox:Hide()
-- editbox:SetFrameStrata("HIGH")
editbox:SetWidth(56)
editbox:SetFontObject("ChatFontNormal")
editbox:SetScript("OnShow", onShow)
editbox:SetScript("OnHide", onHide)
editbox:SetScript("OnEscapePressed", editbox.Hide)
editbox:SetScript("OnEditFocusLost", editbox.Hide)
editbox:SetScript("OnEnterPressed", onEnterPressed)
editbox:SetScript("OnTextChanged", onTextChanged)
local validate = CreateFrame("Frame", nil, editbox)
validate:SetSize(20, 20)
validate:SetPoint("LEFT", editbox, "RIGHT")
validate:SetScript("OnEnter", function(self)
if self.reason then
GameTooltip:SetOwner(self, "ANCHOR_LEFT")
GameTooltip:SetText(self.reason)
end
end)
validate:SetScript("OnLeave", GameTooltip_Hide)
validate:Hide()
editbox.validate = validate
local validTex = validate:CreateTexture()
validTex:SetAllPoints()
validate.tex = validTex
end
local scrollFrame
do
local deleteButton = CreateFrame("Button", nil, module, "UIPanelCloseButton")
deleteButton:SetScript("OnLeave", deleteButton.Hide)
deleteButton:SetScript("OnClick", function(self)
module:GetSelectedList()[self.value] = nil
if selectedValue.onDelete then
selectedValue.onDelete(self.value)
end
core:UpdateSpells()
module:Update()
self:Hide()
end)
local function showDelete(anchor)
deleteButton.value = anchor.key.data
deleteButton:SetPoint("RIGHT", anchor, "LEFT")
deleteButton:Show()
end
local function onEnter(self)
if type(self.data) == "number" then
GameTooltip:SetOwner(self, "ANCHOR_LEFT")
if C_Spell.GetSpellLink(self.data) then
GameTooltip:SetSpellByID(self.data)
else
GameTooltip:SetText(L["Invalid spell ID."])
end
end
showDelete(self.parent)
end
local function onLeave(self)
GameTooltip_Hide()
if not deleteButton:IsMouseOver() then
deleteButton:Hide()
end
end
local function onClick(self)
self:Hide()
if editbox:IsShown() then
editbox.parent:Show()
editbox:Hide()
end
local parent = self.parent
editbox.parent = self
editbox.data = self.data
editbox.type = self.type
editbox.key = parent.key.data
editbox.value = parent.value.data
editbox:SetParent(self.parent)
editbox:Show()
end
local function createCell(frame, valueType)
local cell = CreateFrame("Button", nil, frame)
cell:SetNormalFontObject("ChatFontNormal")
cell:SetPushedTextOffset(0, 0)
cell:SetScript("OnClick", onClick)
cell:SetScript("OnEnter", onEnter)
cell:SetScript("OnLeave", onLeave)
cell.type = valueType
frame[valueType] = cell
local text = cell:CreateFontString()
text:SetJustifyH("LEFT")
text:SetPoint("TOPLEFT", 5, 0)
text:SetPoint("BOTTOMRIGHT")
cell:SetFontString(text)
local spellName = cell:CreateFontString(nil, nil, "ChatFontNormal")
spellName:SetJustifyH("LEFT")
spellName:SetPoint("TOPLEFT", 64, 0)
spellName:SetPoint("BOTTOMRIGHT")
cell.name = spellName
cell.parent = frame
return cell
end
local function createButton()
local frame = CreateFrame("Frame", nil, module)
frame:SetScript("OnEnter", showDelete)
frame:SetScript("OnLeave", onLeave)
local key = createCell(frame, "key")
key:SetPoint("TOPLEFT", 4, 0)
key:SetPoint("BOTTOMLEFT")
key:SetPoint("RIGHT", frame, "CENTER", -8, 0)
local equals = frame:CreateFontString(nil, nil, "ChatFontNormal")
equals:SetPoint("CENTER")
equals:SetText("=")
local value = createCell(frame, "value")
value:SetPoint("LEFT", frame, "CENTER", 8, 0)
value:SetPoint("TOPRIGHT", -4, 0)
value:SetPoint("BOTTOMRIGHT")
return frame
end
local sortedList = {}
local function sortList(a, b)
local nameA, nameB = core:GetSpellName(a, true), core:GetSpellName(b, true)
if not (nameA and nameB) then
return a < b
else
return nameA < nameB
end
end
local function preUpdate(self)
editbox:Hide()
return module:GetSelectedList(), selectedValue.isNumeric
end
local function getList()
wipe(sortedList)
for k, v in pairs(module:GetSelectedList()) do
tinsert(sortedList, k)
end
sort(sortedList)
return sortedList
end
local function onButtonShow(self, frame, value, list, isNumeric)
frame.key.data = value
if C_Spell.GetSpellLink(value) then
frame.key:SetText(value)
else
frame.key:SetFormattedText("|cffff0000%d", value)
end
frame.key.name:SetText(core:GetSpellName(value, true))
local text = list[value]
frame.value.data = text
if not isNumeric or C_Spell.GetSpellLink(text) then
frame.value:SetText(text)
else
frame.value:SetFormattedText("|cffff0000%s", text)
end
if isNumeric then
frame.value.name:SetText(core:GetSpellName(text, true))
else
frame.value.name:SetText(nil)
end
end
local NUM_BUTTONS = 20
local BUTTON_HEIGHT = 20
scrollFrame = core:CreateScrollframe(module, NUM_BUTTONS, BUTTON_HEIGHT, createButton, 0, -4)
scrollFrame:SetSize(480, NUM_BUTTONS * BUTTON_HEIGHT + 8)
scrollFrame:SetPoint("CENTER")
scrollFrame.PreUpdate = preUpdate
scrollFrame.GetList = getList
scrollFrame.OnButtonShow = onButtonShow
end
local bg = module:CreateTexture(nil, "BORDER")
bg:SetTexture(0.3, 0.3, 0.3)
bg:SetBlendMode("MOD")
bg:SetAllPoints(scrollFrame)
local border = module:CreateTexture(nil, "BORDER", nil, -1)
border:SetTexture(0.5, 0.5, 0.5, 0.3)
border:SetPoint("TOPLEFT", bg, -1, 1)
border:SetPoint("BOTTOMRIGHT", bg, 1, -1)
local menu = {
"spellMappings",
"tooltipMappings",
"spellNameOverrides",
"spellIconOverrides",
spellMappings = L["Spell mappings"],
tooltipMappings = L["Tooltip mappings"],
spellNameOverrides = L["Spell name overrides"],
spellIconOverrides = L["Spell icon overrides"],
isNumeric = {
spellMappings = true,
tooltipMappings = true,
},
onAccept = {
spellMappings = function(key, value)
for tree in pairs(core.trees) do
local spells = core.percharDB.profile.spells[tree]
local spell = spells[key]
if spell then
local map = spells[value] or spell
for i = 1, 2 do
map[i] = map[i] or spell[i]
end
spells[key] = nil
end
core:BuildSpellArray(tree)
core:UpdateTopRecords(tree)
end
end,
spellNameOverrides = function(key, value)
for tree in pairs(core.trees) do
for i, spell in ipairs(core:GetSpellArray(tree, false)) do
if spell.id == key then
spell.name = value
end
end
end
end,
},
onDelete = {
spellNameOverrides = function(key)
for tree in pairs(core.trees) do
for i, spell in ipairs(core:GetSpellArray(tree, false)) do
if spell.id == key then
spell.name = core:GetSpellName(key)
end
end
end
end,
},
validate = {
spellMappings = function(self, value)
local isValid, reason = true
if value ~= self.data and module:GetSelectedList()[value] then
isValid = false
reason = L["Conflicting mapping."]
end
if not C_Spell.GetSpellLink(value) then
isValid = false
reason = L["Invalid spell ID."]
end
return isValid, reason
end,
},
getDefault = {
spellMappings = function(id)
return id
end,
tooltipMappings = function(id)
return id
end,
spellNameOverrides = function(id)
return core:GetSpellName(id, true)
end,
spellIconOverrides = function(id)
return C_Spell.GetSpellTexture(id)
end,
},
info = {
spellMappings =
L["Spell mappings causes the source spell to be registered as if it were the target spell. It will be stored with the ID, name and icon of"..
" the target spell. Useful for merging different spell IDs of the same spell into a single record. Spells will immediately be converted"..
" into their target ID after a mapping has been created."],
tooltipMappings =
L["Tooltip mappings causes the tooltip of the source spell to display the records of the target spell. "..
"Useful for when several tooltips refer to the same spell, or you want to display some side effect of a spell. "..
"Any tooltip can only display the records of one database entry at a time."],
spellNameOverrides =
L["Name overrides will cause the source spell to be listed as "..
"the target name in tree tooltips, new record messages and the spell list."],
spellIconOverrides =
L["Icon overrides will cause the source spell to use the target texture in the spell list."],
},
}
local function onClick(self, value)
module:SelectList(value)
end
local selectedList
local dropdown = core:CreateDropdown("Frame", module)
dropdown:SetWidth(140)
dropdown:SetPoint("BOTTOMLEFT", scrollFrame, "TOPLEFT", -16, 0)
dropdown:JustifyText("LEFT")
dropdown.initialize = function(self)
for _, v in ipairs(menu) do
local info = UIDropDownMenu_CreateInfo()
info.text = menu[v]
info.func = onClick
info.arg1 = v
info.checked = (v == selectedList)
self:AddButton(info)
end
end
local add = core:CreateEditbox(module)
add:SetNumeric(true)
add:SetWidth(59)
add:SetFontObject("ChatFontNormal")
add:SetPoint("TOPLEFT", scrollFrame, "BOTTOMLEFT", 5, -16)
add:SetScript("OnEnterPressed", function(self)
local list = module:GetSelectedList()
local spellID = self:GetNumber()
if list[spellID] then
core:Message(L["This spell ID is already mapped."])
elseif reverseList[spellID] then
core:Message(L["Conflicting mapping."])
else
local default = selectedValue.getDefault(spellID)
if default then
list[spellID] = default
self:SetText("")
self:ClearFocus()
module:Update()
else
core:Message(L["Invalid spell ID."])
end
end
end)
add:SetScript("OnEscapePressed", function(self)
self:SetText("")
self:ClearFocus()
end)
local label = add:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
label:SetPoint("BOTTOMLEFT", add, "TOPLEFT", -5, -2)
label:SetPoint("BOTTOMRIGHT", add, "TOPRIGHT", 0, -2)
label:SetJustifyH("LEFT")
label:SetHeight(18)
label:SetText("Add")
local info = module:CreateFontString(nil, nil, "GameFontNormalSmallLeft")
info:SetPoint("LEFT", add, "RIGHT", 16, 0)
info:SetPoint("TOPRIGHT", scrollFrame, "BOTTOMRIGHT", -4, -8)
info:SetPoint("BOTTOM")
info:SetJustifyV("TOP")
setmetatable(selectedValue, {
__index = function(tbl, key)
return menu[key][selectedList]
end
})
function module:OnInitialize()
self:SelectList(menu[1])
end
function module:Update()
scrollFrame:Update()
end
function module:SelectList(value)
selectedList = value
dropdown:SetText(menu[value])
module:BuildReverseList()
module:Update()
info:SetText(selectedValue.info)
end
function module:GetSelectedList()
return core.db.global[selectedList]
end
function module:BuildReverseList()
wipe(reverseList)
-- reversed mappings are only interesting for lists where the values are spell IDs
if selectedValue.isNumeric then
for k, v in pairs(self:GetSelectedList()) do
reverseList[v] = true
end
end
end