Repository navigation
Expand file tree
/
Copy pathPanels.lua
More file actions
494 lines (435 loc) · 17.8 KB
/
Copy pathPanels.lua
File metadata and controls
494 lines (435 loc) · 17.8 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
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
-- The four paddle panels: creation, position, visibility, focus visuals,
-- appearance, and Edit Mode.
local _, ns = ...
local PANEL_COUNT = ns.PANEL_COUNT
local PADDLE_COUNT = ns.PADDLE_COUNT
local LAYOUT = ns.LAYOUT
local ATLAS = ns.ATLAS
local NATIVE_CVAR_SCALING = ns.NATIVE_CVAR_SCALING
local NATIVE_CVAR_HIGHLIGHT = ns.NATIVE_CVAR_HIGHLIGHT
local PANELS = ns.PANELS
local addon = ns.addon
local panelFrames = ns.panelFrames
local buttons = ns.buttons
local focusRouting = ns.focusRouting
local Print = ns.Print
local SafeCall = ns.SafeCall
local ApplyArt = ns.ApplyArt
local GetNativeCVarBool = ns.GetNativeCVarBool
local NATIVE_CROSSBAR_FRAME = ns.NATIVE_CROSSBAR_FRAME
local DefaultPanelPosition = ns.DefaultPanelPosition
local ResolvePanelAnchor = ns.ResolvePanelAnchor
local IsEditable = ns.IsEditable
local UpdatePromptVisibility = ns.UpdatePromptVisibility
local GetButtonCenter = ns.GetButtonCenter
local LayoutButtonVisual = ns.LayoutButtonVisual
local CreateActionButton = ns.CreateActionButton
local GetVisualPanelFromGamepadState = ns.GetVisualPanelFromGamepadState
local lastVisualPanel
local lastCrossbarFocused
-- Mirrors GamepadMainActionBarFrameMixin:UpdateActionBarFocusedState: the
-- native crossbar collapses every bar while a binding set other than the core
-- gameplay one (game menu, bags, settings) owns the controller. Polled rather
-- than registered through BindToCoreBindingActive so no addon code runs inside
-- Blizzard's listener loop. Unknown counts as focused, as before.
local function IsNativeCrossbarFocused()
local manager = GamepadSharedUtility and GamepadSharedUtility.InputBindingManager
if type(manager) ~= "table" or type(manager.IsOnlyCoreBindingSetActive) ~= "function" then
return true
end
return SafeCall(manager.IsOnlyCoreBindingSetActive, manager) ~= false
end
local function SetPanelPosition(panelIndex)
local panel = panelFrames[panelIndex]
if not panel then
return
end
local pos = BackhandDB.panelPositions[panelIndex] or DefaultPanelPosition(panelIndex)
panel:ClearAllPoints()
panel:SetPoint(ResolvePanelAnchor(pos))
end
local function SavePanelPosition(panelIndex)
local panel = panelFrames[panelIndex]
if not panel then
return
end
local point, relativeTo, relativePoint, x, y = panel:GetPoint(1)
if not point then
return
end
-- Dragging re-anchors the panel to UIParent, so only a position that is
-- still attached to the native crossbar keeps its frame reference.
local relativeName = relativeTo and relativeTo ~= UIParent and type(relativeTo.GetName) == "function" and relativeTo:GetName() or nil
BackhandDB.panelPositions[panelIndex] = {
point = point,
relativeTo = relativeName == NATIVE_CROSSBAR_FRAME and relativeName or nil,
relativePoint = relativePoint or point,
x = x or 0,
y = y or 0,
}
end
local function IsGamepadInterfaceActive()
if C_InputInterfaceStyle and type(C_InputInterfaceStyle.GetCurrentStyle) == "function" then
local style = SafeCall(C_InputInterfaceStyle.GetCurrentStyle)
if style ~= nil then
local gamepad = Enum and Enum.InputDeviceInterfaceType and Enum.InputDeviceInterfaceType.Gamepad or 1
return style == gamepad
end
end
if type(IsGamePadEnabled) == "function" then
return SafeCall(IsGamePadEnabled) == true
end
return true
end
-- Hides the panels outside gamepad mode unless they are being positioned.
-- The panels parent secure action buttons, so showing or hiding them is a
-- protected action; in combat it waits for PLAYER_REGEN_ENABLED.
local function UpdatePanelVisibility()
if InCombatLockdown() then
ns.pendingAppearanceRefresh = true
return
end
local shown = BackhandDB.gamepadOnly == false or IsGamepadInterfaceActive() or IsEditable()
for panelIndex = 1, PANEL_COUNT do
local panel = panelFrames[panelIndex]
if panel then
panel:SetShown(shown)
end
end
end
local function UpdateEditOverlays()
local editable = IsEditable()
for panelIndex = 1, PANEL_COUNT do
local panel = panelFrames[panelIndex]
if panel and panel.editOverlay then
panel.editOverlay:SetShown(editable)
end
end
UpdatePanelVisibility()
end
local function SetModifierIconFocused(panel, focused)
local icon = panel.modifierIcon
if not icon then
return
end
if focused then
if type(icon.SetFocused) == "function" then
pcall(icon.SetFocused, icon)
end
elseif type(icon.SetPressable) == "function" then
pcall(icon.SetPressable, icon)
end
end
-- Uses Blizzard's InputIconTextureFrameTemplate so the LT / RT prompts follow
-- the connected controller's glyph style exactly like the native crossbar.
local function CreateModifierIcon(panel, panelInfo)
if not panelInfo.lt and not panelInfo.rt then
return nil
end
local leftKey = GAMEPAD_TRIGGER_LEFT or "PADLTRIGGER"
local rightKey = GAMEPAD_TRIGGER_RIGHT or "PADRTRIGGER"
local frame
if panelInfo.lt and panelInfo.rt then
local ok, created = pcall(CreateFrame, "Frame", nil, panel, "InputPromptTwoIconTemplate")
if ok and created and type(created.SetPromptInputIconKey) == "function" then
local configured = pcall(function()
created:SetPromptInputIconKey(1, leftKey)
created:SetPromptInputIconKey(2, rightKey)
if type(created.SetUseDropShadow) == "function" then
created:SetUseDropShadow(true)
end
end)
if configured then
frame = created
else
created:Hide()
end
elseif ok and created then
created:Hide()
end
else
local ok, created = pcall(CreateFrame, "Frame", nil, panel, "InputIconTextureFrameTemplate")
if ok and created and type(created.SetInputKey) == "function" then
local configured = pcall(function()
created:SetInputKey(panelInfo.lt and leftKey or rightKey)
if type(created.EnableDropShadow) == "function" then
created:EnableDropShadow()
end
end)
if configured then
frame = created
else
created:Hide()
end
elseif ok and created then
created:Hide()
end
end
if not frame then
-- Text fallback for clients without the gamepad prompt templates.
frame = CreateFrame("Frame", nil, panel)
frame:SetSize(48, 16)
frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
frame.text:SetPoint("CENTER")
frame.text:SetText(panelInfo.label)
frame.SetFocused = function(self)
self.text:SetTextColor(1, 0.82, 0)
end
frame.SetPressable = function(self)
self.text:SetTextColor(0.62, 0.62, 0.62)
end
frame:SetPressable()
end
frame:ClearAllPoints()
frame:SetPoint("CENTER", panel, "CENTER", 0, LAYOUT.MODIFIER_ICON_Y)
return frame
end
-- isActive: the panel's layer is selected by the triggers. isFocused: it is
-- selected and the native crossbar has gamepad focus, so it draws expanded.
local function SetPanelActiveVisual(panelIndex, isActive, crossbarFocused)
local panel = panelFrames[panelIndex]
if not panel then
return
end
local scalingEnabled = GetNativeCVarBool(NATIVE_CVAR_SCALING, true)
local highlightEnabled = BackhandDB.highlightActivePanel ~= false and GetNativeCVarBool(NATIVE_CVAR_HIGHLIGHT, true)
local isFocused = isActive and crossbarFocused
local expanded = isFocused and scalingEnabled
local wasExpanded = panel.expanded == true
panel.isActive = isActive
panel.isFocused = isFocused
panel.expanded = expanded
local inactiveOpacity = BackhandDB.inactiveOpacity or 1.0
panel:SetAlpha(IsEditable() and 1 or (isActive and 1 or inactiveOpacity))
panel.focusBackground:SetAlpha(LAYOUT.FOCUS_BACKGROUND_ALPHA * (BackhandDB.highlightStrength or 1.0))
panel.focusBackground:SetShown(isFocused and highlightEnabled and panel.focusBackground.artAvailable)
if panel.modifierIcon then
-- The native crossbar hides its LT / RT prompts while a menu has focus.
panel.modifierIcon:SetShown(BackhandDB.showPanelLabels ~= false and (crossbarFocused or IsEditable()))
SetModifierIconFocused(panel, isFocused)
end
if expanded and not wasExpanded then
-- Native focus: the focus shadow starts fully opaque and settles to 0.4.
panel.focusFadeStart = GetTime()
elseif not expanded then
panel.focusFadeStart = nil
end
for paddleIndex = 1, PADDLE_COUNT do
local button = buttons[panelIndex][paddleIndex]
if button then
LayoutButtonVisual(button)
if expanded and not wasExpanded then
button.visual.shadowFocus:SetAlpha(1)
end
UpdatePromptVisibility(button)
end
end
end
local function UpdateFocusFades()
local now = GetTime()
for panelIndex = 1, PANEL_COUNT do
local panel = panelFrames[panelIndex]
if panel and panel.focusFadeStart then
local progress = (now - panel.focusFadeStart) / LAYOUT.FOCUS_FADE_DURATION
local alpha
if progress >= 1 then
alpha = LAYOUT.FOCUS_SHADOW_MIN_ALPHA
panel.focusFadeStart = nil
else
alpha = 1 - ((1 - LAYOUT.FOCUS_SHADOW_MIN_ALPHA) * progress)
end
for paddleIndex = 1, PADDLE_COUNT do
local button = buttons[panelIndex][paddleIndex]
if button then
button.visual.shadowFocus:SetAlpha(alpha)
end
end
end
end
end
local function UpdatePanelVisualState(panelIndex, force)
local crossbarFocused = IsNativeCrossbarFocused()
if not force and lastVisualPanel == panelIndex and lastCrossbarFocused == crossbarFocused then
return
end
lastVisualPanel = panelIndex
lastCrossbarFocused = crossbarFocused
for i = 1, PANEL_COUNT do
SetPanelActiveVisual(i, i == panelIndex, crossbarFocused)
end
end
local function CreatePanelFrame(panelIndex)
local panelInfo = PANELS[panelIndex]
local panel = CreateFrame("Frame", "BackhandPanel" .. panelIndex, UIParent)
panel.panelIndex = panelIndex
panel.expanded = false
panel:SetSize(LAYOUT.PANEL_WIDTH, LAYOUT.PANEL_HEIGHT)
panel:SetFrameStrata("LOW")
-- The panels nest inside the native crossbar; keep them above its slots so
-- an expanded paddle grid never disappears behind a native ring.
panel:SetFrameLevel(40)
panel:SetClampedToScreen(true)
panel:SetMovable(true)
-- Registered before the slots are created so their first layout pass works.
panelFrames[panelIndex] = panel
-- Soft highlight the native crossbar draws behind the focused bar.
panel.focusBackground = panel:CreateTexture(nil, "BACKGROUND", nil, -2)
panel.focusBackground:SetSize(LAYOUT.FOCUS_BACKGROUND_WIDTH, LAYOUT.FOCUS_BACKGROUND_HEIGHT)
panel.focusBackground:SetPoint("CENTER", panel, "CENTER", 0, LAYOUT.GRID_CENTER_Y)
if not ApplyArt(panel.focusBackground, ATLAS.focusBackground, nil) then
panel.focusBackground:SetColorTexture(0.68, 0.45, 0.08, 0.6)
panel.focusBackground:SetSize(LAYOUT.PANEL_WIDTH - 20, LAYOUT.PANEL_WIDTH - 20)
panel.focusBackground.artAvailable = true
end
panel.focusBackground:Hide()
for paddleIndex = 1, PADDLE_COUNT do
local button = CreateActionButton(panelIndex, paddleIndex, panel)
local x, y = GetButtonCenter(paddleIndex, true)
button:SetPoint("CENTER", panel, "CENTER", x, y)
end
panel.modifierIcon = CreateModifierIcon(panel, panelInfo)
panel.editOverlay = CreateFrame("Button", nil, panel)
panel.editOverlay:SetAllPoints()
panel.editOverlay:SetFrameLevel(panel:GetFrameLevel() + 50)
panel.editOverlay:RegisterForDrag("LeftButton", "RightButton")
panel.editOverlay:Hide()
panel.editOverlay.fill = panel.editOverlay:CreateTexture(nil, "BACKGROUND")
if ApplyArt(panel.editOverlay.fill, ATLAS.editGlow, nil) then
panel.editOverlay.fill:SetSize(LAYOUT.PANEL_WIDTH + 44, LAYOUT.PANEL_WIDTH + 44)
panel.editOverlay.fill:SetPoint("CENTER", panel, "CENTER", 0, LAYOUT.GRID_CENTER_Y)
panel.editOverlay.fill:SetAlpha(0.5)
else
panel.editOverlay.fill:SetAllPoints()
panel.editOverlay.fill:SetColorTexture(0.95, 0.72, 0.16, 0.10)
end
local function CreateEdge(point1, point2, isHorizontal)
local edge = panel.editOverlay:CreateTexture(nil, "OVERLAY")
edge:SetPoint(point1.point, point1.x, point1.y)
edge:SetPoint(point2.point, point2.x, point2.y)
if isHorizontal then
edge:SetHeight(2)
else
edge:SetWidth(2)
end
edge:SetColorTexture(1.0, 0.82, 0.28, 0.95)
return edge
end
panel.editOverlay.top = CreateEdge({ point = "TOPLEFT", x = -2, y = 2 }, { point = "TOPRIGHT", x = 2, y = 2 }, true)
panel.editOverlay.bottom = CreateEdge({ point = "BOTTOMLEFT", x = -2, y = -2 }, { point = "BOTTOMRIGHT", x = 2, y = -2 }, true)
panel.editOverlay.left = CreateEdge({ point = "TOPLEFT", x = -2, y = 2 }, { point = "BOTTOMLEFT", x = -2, y = -2 }, false)
panel.editOverlay.right = CreateEdge({ point = "TOPRIGHT", x = 2, y = 2 }, { point = "BOTTOMRIGHT", x = 2, y = -2 }, false)
panel.editOverlay.text = panel.editOverlay:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
panel.editOverlay.text:SetPoint("CENTER", panel, "CENTER", 0, LAYOUT.GRID_CENTER_Y)
panel.editOverlay.text:SetText(panelInfo.label .. "\nDRAG TO MOVE")
panel.editOverlay:SetScript("OnDragStart", function()
if not IsEditable() then
return
end
if InCombatLockdown() then
Print("Paddle panels cannot be moved during combat.")
return
end
panel:StartMoving()
end)
panel.editOverlay:SetScript("OnDragStop", function()
panel:StopMovingOrSizing()
SavePanelPosition(panelIndex)
end)
SetPanelPosition(panelIndex)
return panel
end
local function CreateUI()
focusRouting.CreateRouters()
for panelIndex = 1, PANEL_COUNT do
buttons[panelIndex] = {}
CreatePanelFrame(panelIndex)
end
UpdatePanelVisualState(1, true)
UpdateEditOverlays()
end
local function ApplyAppearance()
if not panelFrames[1] then
return
end
if InCombatLockdown() then
ns.pendingAppearanceRefresh = true
return
end
local scale = BackhandDB.hudScale or 1.0
for panelIndex = 1, PANEL_COUNT do
local panel = panelFrames[panelIndex]
if panel then
panel:SetScale(scale)
end
end
UpdatePanelVisualState(GetVisualPanelFromGamepadState(), true)
UpdateEditOverlays()
ns.pendingAppearanceRefresh = false
end
local function SetUnlocked(unlocked)
BackhandDB.unlocked = unlocked and true or false
if InCombatLockdown() then
ns.pendingAppearanceRefresh = true
Print("Layout lock changes will apply after combat.")
return
end
UpdateEditOverlays()
UpdatePanelVisualState(GetVisualPanelFromGamepadState(), true)
Print(unlocked and "Unlocked. Drag each paddle panel independently." or "Locked. Edit Mode will still unlock the panels while it is open.")
end
local function ResetPosition(panelIndex)
if InCombatLockdown() then
Print("Paddle panels cannot be moved during combat.")
return
end
if panelIndex then
BackhandDB.panelPositions[panelIndex] = DefaultPanelPosition(panelIndex)
SetPanelPosition(panelIndex)
Print(PANELS[panelIndex].label .. " position reset.")
return
end
for i = 1, PANEL_COUNT do
BackhandDB.panelPositions[i] = DefaultPanelPosition(i)
SetPanelPosition(i)
end
Print("All paddle panel positions reset.")
end
local function RegisterEditModeIntegration()
if ns.editModeCallbacksRegistered or not EventRegistry or type(EventRegistry.RegisterCallback) ~= "function" then
return
end
ns.editModeCallbacksRegistered = true
EventRegistry:RegisterCallback("EditMode.Enter", function()
ns.editModeActive = true
UpdateEditOverlays()
UpdatePanelVisualState(GetVisualPanelFromGamepadState(), true)
end, addon)
-- Dragged panels save on OnDragStop. Re-saving every panel here would turn
-- a fallback UIParent anchor into a stored position and lose the crossbar
-- attachment for panels that were never moved.
EventRegistry:RegisterCallback("EditMode.Exit", function()
ns.editModeActive = false
UpdateEditOverlays()
UpdatePanelVisualState(GetVisualPanelFromGamepadState(), true)
end, addon)
-- Reloading the UI while Edit Mode is already open does not emit another
-- EditMode.Enter event for us, so mirror the manager's current state once.
if EditModeManagerFrame and type(EditModeManagerFrame.IsEditModeActive) == "function" then
local ok, active = pcall(EditModeManagerFrame.IsEditModeActive, EditModeManagerFrame)
if ok and active then
ns.editModeActive = true
end
end
end
ns.SetPanelPosition = SetPanelPosition
ns.IsGamepadInterfaceActive = IsGamepadInterfaceActive
ns.UpdatePanelVisibility = UpdatePanelVisibility
ns.UpdateEditOverlays = UpdateEditOverlays
ns.UpdateFocusFades = UpdateFocusFades
ns.UpdatePanelVisualState = UpdatePanelVisualState
ns.IsNativeCrossbarFocused = IsNativeCrossbarFocused
ns.CreateUI = CreateUI
ns.ApplyAppearance = ApplyAppearance
ns.SetUnlocked = SetUnlocked
ns.ResetPosition = ResetPosition
ns.RegisterEditModeIntegration = RegisterEditModeIntegration