-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibEditModeOverride.lua
More file actions
298 lines (238 loc) · 8.44 KB
/
LibEditModeOverride.lua
File metadata and controls
298 lines (238 loc) · 8.44 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
-- Copyright 2022-2023 plusmouse. Licensed under terms found in LICENSE file.
local lib = LibStub:NewLibrary("LibEditModeOverride-1.0", 10)
if not lib then return end
local activeLayoutPending = false
local pointGetter = CreateFrame("Frame", nil, UIParent)
local FRAME_ERROR = "This frame isn't used by edit mode"
local LOAD_ERROR = "You need to call LibEditModeOverride:LoadLayouts first"
local EDIT_ERROR = "Active layout is not editable"
local READY_ERROR = "You need to wait for EDIT_MODE_LAYOUTS_UPDATED"
local layoutInfo
local reconciledLayouts = false
local function GetSystemByID(systemID, systemIndex)
-- Get the system by checking each one for the right system id
for _, system in pairs(layoutInfo.layouts[layoutInfo.activeLayout].systems) do
if system.system == systemID and system.systemIndex == systemIndex then
return system
end
end
end
local function GetSystemByFrame(frame)
assert(frame and type(frame) == "table" and frame.IsObjectType and frame:IsObjectType("Frame"), "Frame required")
local systemID = frame.system
local systemIndex = frame.systemIndex
return GetSystemByID(systemID, systemIndex)
end
local function GetParameterRestrictions(frame, setting)
local systemRestrictions = EditModeSettingDisplayInfoManager.systemSettingDisplayInfo[frame.system]
for _, setup in ipairs(systemRestrictions) do
if setup.setting == setting then
return setup
end
end
return nil
end
local function GetLayoutIndex(layoutName)
for index, layout in ipairs(layoutInfo.layouts) do
if layout.layoutName == layoutName then
return index
end
end
end
local function GetHighestIndex()
local highestLayoutIndexByType = {};
for index, layoutInfo in ipairs(layoutInfo.layouts) do
if not highestLayoutIndexByType[layoutInfo.layoutType] or highestLayoutIndexByType[layoutInfo.layoutType] < index then
highestLayoutIndexByType[layoutInfo.layoutType] = index;
end
end
return highestLayoutIndexByType
end
function lib:SetGlobalSetting(setting, value)
C_EditMode.SetAccountSetting(setting, value)
end
function lib:GetGlobalSetting(setting)
local currentSettings = C_EditMode.GetAccountSettings()
for _, s in ipairs(currentSettings) do
if s.setting == setting then
return s.value
end
end
end
function lib:HasEditModeSettings(frame)
return GetSystemByFrame(frame) ~= nil
end
-- Set an option found in the Enum.EditMode enumerations
function lib:SetFrameSetting(frame, setting, value)
assert(lib:CanEditActiveLayout(), EDIT_ERROR)
local system = GetSystemByFrame(frame)
assert(system, FRAME_ERROR)
assert(value == math.floor(value), "Non-negative integer values only")
local restrictions = GetParameterRestrictions(frame, setting)
if restrictions then
local min, max
if restrictions.type == Enum.EditModeSettingDisplayType.Dropdown then
for _, option in pairs(restrictions.options) do
if min == nil or min > option.value then
min = option.value
end
if max == nil or max < option.value then
max = option.value
end
end
elseif restrictions.type == Enum.EditModeSettingDisplayType.Checkbox then
min = 0
max = 1
elseif restrictions.type == Enum.EditModeSettingDisplayType.Slider then
if restrictions.stepSize then
if frame:GetName():match("CooldownViewer") then
min = restrictions.minValue
max = restrictions.maxValue
else
min = 0
max = restrictions.maxValue - restrictions.minValue
end
else
min = restrictions.minValue
max = restrictions.maxValue
end
else
error("Internal Error: Unknown setting restrictions")
end
assert(min <= value and value <= max, string.format("Value %s invalid for this setting: min %s, max %s", value, min, max))
end
for _, item in pairs(system.settings) do
if item.setting == setting then
item.value = value
end
end
end
function lib:GetFrameSetting(frame, setting)
local system = GetSystemByFrame(frame)
assert(system, FRAME_ERROR)
for _, item in pairs(system.settings) do
if item.setting == setting then
return item.value
end
end
return nil
end
function lib:ReanchorFrame(frame, ...)
assert(lib:CanEditActiveLayout(), EDIT_ERROR)
local system = GetSystemByFrame(frame)
assert(system, FRAME_ERROR)
system.isInDefaultPosition = false
pointGetter:ClearAllPoints()
pointGetter:SetPoint(...)
local anchorInfo = system.anchorInfo
anchorInfo.point, anchorInfo.relativeTo, anchorInfo.relativePoint, anchorInfo.offsetX, anchorInfo.offsetY = pointGetter:GetPoint(1)
anchorInfo.relativeTo = anchorInfo.relativeTo:GetName()
end
function lib:AreLayoutsLoaded()
return layoutInfo ~= nil
end
function lib:IsReady()
return EditModeManagerFrame.accountSettings ~= nil
end
function lib:LoadLayouts()
assert(lib:IsReady(), READY_ERROR)
layoutInfo = C_EditMode.GetLayouts()
if not reconciledLayouts then
local anyChanged = false
for _, layout in ipairs(layoutInfo.layouts) do
anyChanged = anyChanged or EditModeManagerFrame:ReconcileWithModern(layout)
end
if not anyChanged then
reconciledLayouts = true
end
end
local tmp = EditModePresetLayoutManager:GetCopyOfPresetLayouts()
tAppendAll(tmp, layoutInfo.layouts);
layoutInfo.layouts = tmp
end
function lib:SaveOnly()
assert(layoutInfo, LOAD_ERROR)
C_EditMode.SaveLayouts(layoutInfo)
if activeLayoutPending then
C_EditMode.SetActiveLayout(layoutInfo.activeLayout)
activeLayoutPending = false
end
reconciledLayouts = true -- Would have updated for new/old systems in LoadLayouts
end
function lib:ApplyChanges()
assert(not InCombatLockdown(), "Cannot move frames in combat")
assert(lib:IsReady(), READY_ERROR)
lib:SaveOnly()
if not issecurevariable(DropDownList1, "numButtons") then
ShowUIPanel(AddonList)
HideUIPanel(AddonList)
end
ShowUIPanel(EditModeManagerFrame)
HideUIPanel(EditModeManagerFrame)
end
function lib:DoesLayoutExist(layoutName)
assert(layoutInfo, LOAD_ERROR)
return GetLayoutIndex(layoutName) ~= nil
end
function lib:AddLayout(layoutType, layoutName)
assert(layoutInfo, LOAD_ERROR)
assert(layoutName and layoutName ~= "", "Non-empty string required")
assert(not lib:DoesLayoutExist(layoutName), "Layout should not already exist")
local newLayout = CopyTable(layoutInfo.layouts[1]) -- Modern layout
newLayout.layoutType = layoutType
newLayout.layoutName = layoutName
local highestLayoutIndexByType = GetHighestIndex()
local newLayoutIndex;
if highestLayoutIndexByType[layoutType] then
newLayoutIndex = highestLayoutIndexByType[layoutType] + 1;
elseif (layoutType == Enum.EditModeLayoutType.Character) and highestLayoutIndexByType[Enum.EditModeLayoutType.Account] then
newLayoutIndex = highestLayoutIndexByType[Enum.EditModeLayoutType.Account] + 1;
else
newLayoutIndex = Enum.EditModePresetLayoutsMeta.NumValues + 1;
end
table.insert(layoutInfo.layouts, newLayoutIndex, newLayout)
self:SetActiveLayout(layoutName)
end
function lib:DeleteLayout(layoutName)
assert(layoutInfo, LOAD_ERROR)
local index = GetLayoutIndex(layoutName)
assert(index ~= nil, "Can't delete layout as it doesn't exist")
assert(layoutInfo.layouts[index].layoutType ~= Enum.EditModeLayoutType.Preset, "Cannot delete preset layouts")
table.remove(layoutInfo.layouts, index)
C_EditMode.OnLayoutDeleted(index)
end
function lib:GetEditableLayoutNames()
assert(layoutInfo, LOAD_ERROR)
local names = {}
for _, layout in ipairs(layoutInfo.layouts) do
if layout.layoutType ~= Enum.EditModeLayoutType.Preset then
table.insert(names, layout.layoutName)
end
end
return names
end
function lib:GetPresetLayoutNames()
assert(layoutInfo, LOAD_ERROR)
local names = {}
for _, layout in ipairs(layoutInfo.layouts) do
if layout.layoutType == Enum.EditModeLayoutType.Preset then
table.insert(names, layout.layoutName)
end
end
return names
end
function lib:CanEditActiveLayout()
assert(layoutInfo, LOAD_ERROR)
return layoutInfo.layouts[layoutInfo.activeLayout].layoutType ~= Enum.EditModeLayoutType.Preset
end
function lib:SetActiveLayout(layoutName)
assert(layoutInfo, LOAD_ERROR)
assert(lib:DoesLayoutExist(layoutName), "Layout must exist")
local index = GetLayoutIndex(layoutName)
layoutInfo.activeLayout = index
activeLayoutPending = true
end
function lib:GetActiveLayout()
assert(layoutInfo, LOAD_ERROR)
return layoutInfo.layouts[layoutInfo.activeLayout].layoutName
end