Repository navigation
Expand file tree
/
Copy pathActionLabels.lua
More file actions
373 lines (341 loc) · 11.5 KB
/
Copy pathActionLabels.lua
File metadata and controls
373 lines (341 loc) · 11.5 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
local _, addon = ...
local hotkeyRecords = {}
local macroRecords = {}
local warned = {}
local initialized = false
local refreshQueued = false
local QueueRefresh
local function IsReadable(value)
return type(issecretvalue) ~= "function" or not issecretvalue(value)
end
local function IsForbidden(object)
return type(object.IsForbidden) == "function" and object:IsForbidden()
end
local function Warn(record, message)
message = message or addon.L.LABEL_ACCESS_BLOCKED
local warnings = warned[record.button]
if not warnings then
warnings = {}
warned[record.button] = warnings
end
if not warnings[message] then
warnings[message] = true
addon.Print(message:format(addon.GetBarName(record.bar), record.index))
end
end
local function WriteRegion(record, region, method, ...)
record.writing = record.writing or {}
local previous = record.writing[method]
record.writing[method] = true
local success, reason = pcall(region[method], region, ...)
record.writing[method] = previous
if not success then
error(reason, 0)
end
end
local function ApplyHotkeyText(record)
if IsForbidden(record.button) or IsForbidden(record.hotkey) or not record.readable then
if addon.GetLabel(record.bar, record.index) then
Warn(record)
end
return
end
local _, keys = addon.GetBindingInfo(record.bar, record.index)
local label = addon.IsEnabled() and keys[1] and addon.GetLabel(record.bar, record.index)
local blankNativeText = record.nativeText == nil or record.nativeText == ""
if blankNativeText and not record.allowBlank then
label = nil
end
if label then
local text = addon.LiteralLabel(label)
-- The native range updater treats this exact string as an unbound range dot.
if text == RANGE_INDICATOR then
text = text .. "|r"
end
if record.appliedText ~= text then
WriteRegion(record, record.hotkey, "SetText", text)
record.appliedText = text
end
elseif record.appliedText then
WriteRegion(record, record.hotkey, "SetText", record.nativeText)
record.appliedText = nil
end
end
local function IsFontReadable(font, size, flags)
return IsReadable(font) and IsReadable(size) and IsReadable(flags)
and type(font) == "string" and font ~= ""
and type(size) == "number" and size > 0
and (flags == nil or type(flags) == "string")
end
local function NativeFont(record)
if record.fontObject then
return record.fontObject:GetFont()
end
return record.nativeFont[1], record.nativeFont[2], record.nativeFont[3]
end
local function CaptureFont(record, fontObject)
record.nativeFont = { record.hotkey:GetFont() }
record.fontObject = nil
if IsReadable(fontObject) and fontObject and type(fontObject.GetFont) == "function" then
local font, size, flags = fontObject:GetFont()
if IsFontReadable(font, size, flags) then
record.fontObject = fontObject
record.nativeFont = { font, size, flags }
end
end
end
local function ApplyHotkeyFont(record)
local size = addon.GetKeybindFontSize()
local _, keys = addon.GetBindingInfo(record.bar, record.index)
if not keys[1] then
size = nil
end
if not size and not record.fontManaged then
return
end
if IsForbidden(record.button) or IsForbidden(record.hotkey) then
Warn(record, addon.L.FONT_ACCESS_BLOCKED)
return
end
local font, nativeSize, flags = NativeFont(record)
local currentFont, currentSize, currentFlags = record.hotkey:GetFont()
if not IsFontReadable(font, nativeSize, flags) or not IsFontReadable(currentFont, currentSize, currentFlags) then
Warn(record, addon.L.FONT_ACCESS_BLOCKED)
return
end
local desiredSize = size or nativeSize
if currentFont ~= font or not ApproximatelyEqual(currentSize, desiredSize) or currentFlags ~= flags then
WriteRegion(record, record.hotkey, "SetFont", font, desiredSize, flags)
end
record.fontManaged = true
end
local function ApplyHotkey(record)
ApplyHotkeyFont(record)
ApplyHotkeyText(record)
end
local function IsWritingFont(record)
local writing = record.writing
return writing and (writing.SetFont or writing.SetFontObject)
end
local function TrackFont(record)
local hotkey = record.hotkey
CaptureFont(record)
local object = hotkey:GetFontObject()
if IsReadable(object) and object then
local font, size, flags = object:GetFont()
if IsFontReadable(record.nativeFont[1], record.nativeFont[2], record.nativeFont[3])
and IsFontReadable(font, size, flags) and font == record.nativeFont[1]
and ApproximatelyEqual(size, record.nativeFont[2]) and flags == record.nativeFont[3] then
record.fontObject = object
end
end
hooksecurefunc(hotkey, "SetFont", function(_, font, size, flags)
if not IsWritingFont(record) then
record.nativeFont = { font, size, flags }
record.fontObject = nil
ApplyHotkeyFont(record)
QueueRefresh()
end
end)
hooksecurefunc(hotkey, "SetFontObject", function()
if not IsWritingFont(record) then
CaptureFont(record, hotkey:GetFontObject())
ApplyHotkeyFont(record)
QueueRefresh()
end
end)
hooksecurefunc(hotkey, "SetHeight", QueueRefresh)
hooksecurefunc(hotkey, "SetSize", QueueRefresh)
end
function addon.GetPreviewKeybindFont(button, useNativeFont)
local hotkey = button and button.HotKey
if button and IsForbidden(button) or hotkey and IsForbidden(hotkey) then
return nil, addon.L.FONT_REFERENCE_UNAVAILABLE
end
local font, size, flags
local record = useNativeFont and button and hotkeyRecords[button]
if record then
font, size, flags = NativeFont(record)
else
font, size, flags = (hotkey or NumberFontNormalSmallGray):GetFont()
end
if not IsFontReadable(font, size, flags) then
return nil, addon.L.FONT_REFERENCE_UNAVAILABLE
end
if not useNativeFont then
size = addon.GetKeybindFontSize() or size
end
return font, size, flags
end
local function ApplyMacroName(record)
local hidden = addon.ShouldHideMacroNames()
if IsForbidden(record.button) or IsForbidden(record.name) or not IsReadable(record.nativeAlpha) then
if hidden or record.hidden then
Warn(record, addon.L.MACRO_NAME_ACCESS_BLOCKED)
end
return
end
local isMacro = false
if hidden then
local action = record.button.action
if not IsReadable(action) then
Warn(record, addon.L.MACRO_NAME_ACCESS_BLOCKED)
elseif type(action) == "number" and action > 0 then
local actionType = GetActionInfo(action)
if IsReadable(actionType) then
isMacro = actionType == "macro"
else
Warn(record, addon.L.MACRO_NAME_ACCESS_BLOCKED)
end
end
end
if isMacro then
if not record.hidden then
WriteRegion(record, record.name, "SetAlpha", 0)
record.hidden = true
end
elseif record.hidden then
WriteRegion(record, record.name, "SetAlpha", record.nativeAlpha)
record.hidden = false
end
end
local function TrackMacroName(button, bar, index)
local record = macroRecords[button]
if record then
ApplyMacroName(record)
return
end
local name = button.Name
if not name then
return
end
if IsForbidden(button) or IsForbidden(name) then
if addon.ShouldHideMacroNames() then
Warn({ button = button, bar = bar, index = index }, addon.L.MACRO_NAME_ACCESS_BLOCKED)
end
return
end
record = {
button = button,
name = name,
bar = bar,
index = index,
nativeAlpha = name:GetAlpha(),
}
macroRecords[button] = record
hooksecurefunc(name, "SetAlpha", function(_, alpha)
if record.writing and record.writing.SetAlpha then
return
end
record.nativeAlpha = alpha
record.hidden = false
ApplyMacroName(record)
end)
hooksecurefunc(name, "SetText", function()
ApplyMacroName(record)
end)
button:HookScript("OnShow", function()
ApplyMacroName(record)
end)
ApplyMacroName(record)
end
local function TrackButton(button, bar, index)
if not button then
return
end
if bar.id:match("^actionbar") then
TrackMacroName(button, bar, index)
end
local record = hotkeyRecords[button]
if record then
ApplyHotkey(record)
return
end
if IsForbidden(button) then
if addon.GetLabel(bar, index) then
Warn({ button = button, bar = bar, index = index })
end
if addon.GetKeybindFontSize() then
Warn({ button = button, bar = bar, index = index }, addon.L.FONT_ACCESS_BLOCKED)
end
return
end
local hotkey = button.HotKey
if not hotkey or IsForbidden(hotkey) then
if addon.GetLabel(bar, index) then
Warn({ button = button, bar = bar, index = index })
end
if hotkey and addon.GetKeybindFontSize() then
Warn({ button = button, bar = bar, index = index }, addon.L.FONT_ACCESS_BLOCKED)
end
return
end
local nativeText = hotkey:GetText()
record = {
button = button,
hotkey = hotkey,
bar = bar,
index = index,
nativeText = nativeText,
readable = IsReadable(nativeText),
allowBlank = bar.id == "stance",
}
hotkeyRecords[button] = record
TrackFont(record)
hooksecurefunc(hotkey, "SetText", function(_, text)
if record.writing and record.writing.SetText then
return
end
record.nativeText = text
record.readable = IsReadable(text)
record.allowBlank = false
record.appliedText = nil
ApplyHotkey(record)
end)
button:HookScript("OnShow", function()
ApplyHotkey(record)
end)
ApplyHotkey(record)
end
function addon.RefreshActionLabels()
if not initialized then
return
end
for _, bar in ipairs(addon.Bars) do
for index = 1, bar.buttonCount do
TrackButton(addon.GetBarButton(bar, index), bar, index)
end
for _, mirror in ipairs(bar.mirrors or {}) do
for index = 1, mirror.buttonCount do
TrackButton(addon.GetBarButton(mirror, index), bar, index)
end
end
end
end
QueueRefresh = function()
if not refreshQueued then
refreshQueued = true
C_Timer.After(0, function()
refreshQueued = false
addon.RefreshActionLabels()
if addon.RefreshSettings then
addon.RefreshSettings()
end
end)
end
end
function addon.InitializeActionLabels()
initialized = true
addon.RefreshActionLabels()
local events = CreateFrame("Frame")
for _, event in ipairs({
"ADDON_LOADED", "PLAYER_ENTERING_WORLD", "UPDATE_BINDINGS",
"GAME_PAD_ACTIVE_CHANGED", "UPDATE_SHAPESHIFT_FORM", "PET_BAR_UPDATE",
"UPDATE_VEHICLE_ACTIONBAR", "PLAYER_REGEN_ENABLED",
"UI_SCALE_CHANGED",
"ACTIONBAR_SLOT_CHANGED", "ACTIONBAR_PAGE_CHANGED", "UPDATE_BONUS_ACTIONBAR", "UPDATE_MACROS",
}) do
events:RegisterEvent(event)
end
events:SetScript("OnEvent", QueueRefresh)
end