-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibEditMode.lua
392 lines (311 loc) · 12.7 KB
/
LibEditMode.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
local MINOR = 8
local lib = LibStub:NewLibrary('LibEditMode', MINOR)
if not lib then
-- this or a newer version is already loaded
return
end
lib.internal = {} -- internal methods, do not use directly
local internal = lib.internal
local layoutNames = setmetatable({'Modern', 'Classic'}, {
__index = function(t, key)
if key > 2 then
-- the first 2 indices are reserved for 'Modern' and 'Classic' layouts, and anything
-- else are custom ones, although GetLayouts() doesn't return data for the 'Modern'
-- and 'Classic' layouts, so we'll have to substract and check
local layouts = C_EditMode.GetLayouts().layouts
if (key - 2) > #layouts then
error('index is out of bounds')
else
return layouts[key - 2].layoutName
end
else
-- also work for 'Modern' and 'Classic'
rawget(t, key)
end
end
})
lib.frameSelections = lib.frameSelections or {}
lib.frameCallbacks = lib.frameCallbacks or {}
lib.frameDefaults = lib.frameDefaults or {}
lib.frameSettings = lib.frameSettings or {}
lib.frameButtons = lib.frameButtons or {}
lib.anonCallbacksEnter = lib.anonCallbacksEnter or {}
lib.anonCallbacksExit = lib.anonCallbacksExit or {}
lib.anonCallbacksLayout = lib.anonCallbacksLayout or {}
local function resetSelection()
internal.dialog:Hide()
for frame, selection in next, lib.frameSelections do
if selection.isSelected then
frame:SetMovable(false)
end
if not lib.isEditing then
selection:Hide()
selection.isSelected = false
else
selection:ShowHighlighted()
end
end
end
local function onDragStart(self)
self.parent:StartMoving()
end
local function normalizePosition(frame)
-- ripped out of LibWindow-1.1, which is Public Domain
local parent = frame:GetParent()
if not parent then
return
end
local scale = frame:GetScale()
if not scale then
return
end
local left = frame:GetLeft() * scale
local top = frame:GetTop() * scale
local right = frame:GetRight() * scale
local bottom = frame:GetBottom() * scale
local parentWidth, parentHeight = parent:GetSize()
local x, y, point
if left < (parentWidth - right) and left < math.abs((left + right) / 2 - parentWidth / 2) then
x = left
point = 'LEFT'
elseif (parentWidth - right) < math.abs((left + right) / 2 - parentWidth / 2) then
x = right - parentWidth
point = 'RIGHT'
else
x = (left + right) / 2 - parentWidth / 2
point = ''
end
if bottom < (parentHeight - top) and bottom < math.abs((bottom + top) / 2 - parentHeight / 2) then
y = bottom
point = 'BOTTOM' .. point
elseif (parentHeight - top) < math.abs((bottom + top) / 2 - parentHeight / 2) then
y = top - parentHeight
point = 'TOP' .. point
else
y = (bottom + top) / 2 - parentHeight / 2
point = '' .. point
end
if point == '' then
point = 'CENTER'
end
return point, x / scale, y / scale
end
local function onDragStop(self)
local parent = self.parent
parent:StopMovingOrSizing()
-- TODO: snap position to grid
-- FrameXML/EditModeUtil.lua
local point, x, y = normalizePosition(parent)
parent:ClearAllPoints()
parent:SetPoint(point, x, y)
internal:TriggerCallback(parent, point, x, y)
end
local function onMouseDown(self) -- replacement for EditModeSystemMixin:SelectSystem()
resetSelection()
EditModeManagerFrame:ClearSelectedSystem() -- possible taint
if not self.isSelected then
self.parent:SetMovable(true)
self:ShowSelected(true)
internal.dialog:Update(self)
end
end
local function onEditModeEnter()
lib.isEditing = true
resetSelection()
for _, callback in next, lib.anonCallbacksEnter do
securecallfunction(callback)
end
end
local function onEditModeExit()
lib.isEditing = false
resetSelection()
for _, callback in next, lib.anonCallbacksExit do
securecallfunction(callback)
end
end
local function onEditModeChanged(_, layoutInfo)
local layoutName = layoutNames[layoutInfo.activeLayout]
if layoutName ~= lib.activeLayoutName then
lib.activeLayoutName = layoutName
for _, callback in next, lib.anonCallbacksLayout do
securecallfunction(callback, layoutName)
end
-- TODO: we should update the position of the button here, let the user not deal with that
end
end
--[[ LibEditMode:AddFrame(_frame, callback, default_)
Register a frame to be controlled by the Edit Mode.
* `frame`: frame widget to be controlled
* `callback`: callback that triggers whenever the frame has been repositioned
* `default`: table containing the default position of the frame
The `default` table must contain the following entries:
* `point`: relative anchor point, e.g. `"CENTER"` _(string)_
* `x`: horizontal offset from the anchor point _(number)_
* `y`: vertical offset from the anchor point _(number)_
--]]
function lib:AddFrame(frame, callback, default)
local selection = CreateFrame('Frame', nil, frame, 'EditModeSystemSelectionTemplate')
selection:SetAllPoints()
selection:SetScript('OnMouseDown', onMouseDown)
selection:SetScript('OnDragStart', onDragStart)
selection:SetScript('OnDragStop', onDragStop)
selection.Label:SetText(frame.editModeName or frame:GetName())
selection:Hide()
lib.frameSelections[frame] = selection
lib.frameCallbacks[frame] = callback
lib.frameDefaults[frame] = default
if not internal.dialog then
internal.dialog = internal:CreateDialog()
internal.dialog:HookScript('OnHide', function()
resetSelection()
end)
-- listen for layout changes
EventRegistry:RegisterFrameEventAndCallback('EDIT_MODE_LAYOUTS_UPDATED', onEditModeChanged)
-- hook EditMode shown state, since QuickKeybindMode will hide/show EditMode
EditModeManagerFrame:HookScript('OnShow', onEditModeEnter)
EditModeManagerFrame:HookScript('OnHide', onEditModeExit)
-- unselect our selections whenever a system is selected
hooksecurefunc(EditModeManagerFrame, 'SelectSystem', function()
resetSelection()
end)
end
end
--[[ LibEditMode:AddFrameSettings(_frame, settings_)
Register extra settings that will be displayed in a dialog attached to the frame in the Edit Mode.
* `frame`: frame widget already registered with [AddFrame](#libeditmodeaddframeframe-callback-default)
* `settings`: table containing [SettingObject](Types#settingobject) entries _(table, number indexed)_
--]]
function lib:AddFrameSettings(frame, settings)
if not lib.frameSelections[frame] then
error('frame must be registered')
end
lib.frameSettings[frame] = settings
end
--[[ LibEditMode:AddFrameSettingsButton(_frame, data_)
Register extra buttons that will be displayed in a dialog attached to the frame in the Edit Mode.
* `frame`: frame widget already registered with [AddFrame](#libeditmodeaddframeframe-callback-default)
* `data`: table containing [ButtonObject](Types#buttonobject) entries _(table, number indexed)_
--]]
function lib:AddFrameSettingsButton(frame, data)
if not lib.frameButtons[frame] then
lib.frameButtons[frame] = {}
end
table.insert(lib.frameButtons[frame], data)
end
--[[ LibEditMode:RegisterCallback(_event, callback_)
Register extra callbacks whenever an event within the Edit Mode triggers.
* `event`: event name _(string)_
* `callback`: function that will be triggered with the event _(function)_
Possible events:
* `enter`: triggered when the Edit Mode is entered
* `exit`: triggered when the Edit Mode is exited
* `layout`: triggered when the Edit Mode layout is changed (which also occurs at login)
* signature:
* `layoutName`: name of the new layout
--]]
function lib:RegisterCallback(event, callback)
assert(event and type(event) == 'string', 'event must be a string')
assert(callback and type(callback) == 'function', 'callback must be a function')
if event == 'enter' then
table.insert(lib.anonCallbacksEnter, callback)
elseif event == 'exit' then
table.insert(lib.anonCallbacksExit, callback)
elseif event == 'layout' then
table.insert(lib.anonCallbacksLayout, callback)
else
error('invalid callback event "' .. event .. '"')
end
end
--[[ LibEditMode:GetActiveLayoutName()
Returns the active Edit Mode layout name.
This will not return valid data until after the layout has been loaded from the server.
Data will be available for the ["layout" callback](#libeditmoderegistercallbackevent-callback).
--]]
function lib:GetActiveLayoutName()
return lib.activeLayoutName
end
--[[ LibEditMode:IsInEditMode()
Returns whether the Edit Mode is currently active.
--]]
function lib:IsInEditMode()
return not not lib.isEditing
end
--[[ LibEditMode:GetFrameDefaultPosition(_frame_)
Returns the default position table registered with the frame.
* `frame`: registered frame to return positions for
Returns:
* `defaultPosition`: table registered with the frame in [AddFrame](#libeditmodeaddframeframe-callback-default) _(table)_
--]]
function lib:GetFrameDefaultPosition(frame)
return lib.frameDefaults[frame]
end
function internal:TriggerCallback(frame, ...)
if lib.frameCallbacks[frame] then
securecallfunction(lib.frameCallbacks[frame], frame, lib.activeLayoutName, ...)
end
end
function internal:GetFrameSettings(frame)
if lib.frameSettings[frame] then
return lib.frameSettings[frame], #lib.frameSettings[frame]
else
return nil, 0
end
end
function internal:GetFrameButtons(frame)
if lib.frameButtons[frame] then
return lib.frameButtons[frame], #lib.frameButtons[frame]
else
return nil, 0
end
end
--[[ Types:header
## SettingObject
Table containing the following entries:
| key | value | type | required |
|:--------|:------------------------------|:----------------------------|:---------|
| kind | setting type | [SettingType](#settingtype) | yes |
| name | label for the setting | string | yes |
| default | default value for the setting | any | yes |
| get | getter for the current value | function | yes |
| set | setter for the new value | function | yes |
- The getter passes `layoutName` as the sole argument and expects a value in return.
- The setter passes (`layoutName`, `newValue`) and expects no returns.
Depending on the setting type there are additional required and optional entries:
### Dropdown
| key | value | type | required |
|:----------|:----------------------------------------------------------------------------------------------------------------------|:---------|:---------|
| values | indexed table containing [DropdownOption](#dropdownoption)s | table | no |
| generator | [Dropdown `SetupMenu` "generator" (callback)](https://warcraft.wiki.gg/wiki/Patch_11.0.0/API_changes#New_menu_system) | function | no |
| height | max height of the menu | integer | no |
- Either `values` or `generator` is required, the former for simple menues and the latter for complex ones.
- They are not exclusive, but `generator` takes precedence (e.g. `values` will be available but not used).
- `generator` signature is `(dropdown, rootDescription, settingObject)` - `settingObject` being the addition to the default arguments.
- getters and setters are not handled using `generator`, and must be handled by the layout
### Slider
| key | value | type | required | default |
|:----------|:----------------------------------|:---------|:---------|:--------|
| minValue | lower bound for the slider | number | no | 0 |
| maxValue | upper bound for the slider | number | no | 1 |
| valueStep | step increment between each value | number | no | 1 |
| formatter | formatter for the display value | function | no | |
- The formatter passes `value` as the sole argument and expects a number value in return.
## ButtonObject
Table containing the following entries:
| key | value | type | required |
|:------|:--------------------------------|----------|:---------|
| text | text rendered on the button | string | yes |
| click | callback when button is clicked | function | yes |
## DropdownOption
Table containing the following entries:
| key | value | type | required |
|:--------|:-------------------------------------------------------------------|---------|:---------|
| text | text rendered in the dropdown | string | yes |
| isRadio | turns the dropdown entry into a Radio button, otherwise a Checkbox | boolean | no |
## SettingType
Convenient shorthand for `Enum.EditModeSettingDisplayType`.
One of:
- `Dropdown`
- `Checkbox`
- `Slider`
--]]
lib.SettingType = CopyTable(Enum.EditModeSettingDisplayType)