forked from EllesmereGaming/EllesmereUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEUI_UnlockMode.lua
More file actions
5047 lines (4645 loc) · 208 KB
/
EUI_UnlockMode.lua
File metadata and controls
5047 lines (4645 loc) · 208 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-------------------------------------------------------------------------------
-- EUI_UnlockMode.lua
-- Full-featured Unlock Mode for EllesmereUI
-- Animated transition, grid overlay, draggable bar movers, snap guides,
-- position memory, and a polished return-to-options flow.
-- Supports elements from any addon via EllesmereUI:RegisterUnlockElements().
-------------------------------------------------------------------------------
local ADDON_NAME, ns = ...
local EAB = ns.EAB -- may be nil if loaded by a non-ActionBars addon
-------------------------------------------------------------------------------
-- Registration API – lives on the EllesmereUI global so ALL addons share
-- the same table regardless of which copy of this file runs.
-------------------------------------------------------------------------------
if not EllesmereUI._unlockRegisteredElements then
EllesmereUI._unlockRegisteredElements = {}
EllesmereUI._unlockRegisteredOrder = {}
EllesmereUI._unlockRegistrationDirty = true
end
if not EllesmereUI.RegisterUnlockElements then
function EllesmereUI:RegisterUnlockElements(elements)
for _, elem in ipairs(elements) do
self._unlockRegisteredElements[elem.key] = elem
end
self._unlockRegistrationDirty = true
end
end
if not EllesmereUI.UnregisterUnlockElement then
function EllesmereUI:UnregisterUnlockElement(key)
self._unlockRegisteredElements[key] = nil
self._unlockRegistrationDirty = true
end
end
-- If this file was already fully loaded by another addon, bail out.
-- The registration API above is safe to re-run (idempotent), but the
-- rest of the file (state, frames, animations) must only exist once.
if EllesmereUI._unlockModeLoaded then return end
EllesmereUI._unlockModeLoaded = true
-- DEFERRED: heavy body (4900+ lines) runs on first EnsureLoaded() call.
EllesmereUI._deferredInits[#EllesmereUI._deferredInits + 1] = function()
local floor = math.floor
local abs = math.abs
local min = math.min
local max = math.max
local sqrt = math.sqrt
local sin = math.sin
-------------------------------------------------------------------------------
-- Constants
-------------------------------------------------------------------------------
local FONT_PATH = (EllesmereUI and EllesmereUI.GetFontPath and EllesmereUI.GetFontPath("extras"))
or "Interface\\AddOns\\EllesmereUI\\media\\fonts\\Expressway.TTF"
local LOCK_INNER = "Interface\\AddOns\\EllesmereUI\\media\\eui-unlocked-inner-2.png"
local LOCK_OUTER = "Interface\\AddOns\\EllesmereUI\\media\\eui-unlocked-outer-2.png"
local LOCK_TOP = "Interface\\AddOns\\EllesmereUI\\media\\eui-unlocked-top-2.png"
local GRID_SPACING = 32 -- pixels between grid lines
local SNAP_THRESH = 6 -- px distance to trigger snap-to-element
local MOVER_ALPHA = 0.55 -- resting alpha for mover overlays
local MOVER_HOVER = 0.85 -- hover alpha
local MOVER_DRAG = 0.95 -- dragging alpha
local TRANSITION_DUR = 0.35 -- seconds for the open/close fade-in
local GEAR_ROTATION = math.pi / 4 -- 45° rotation for gear effect
-- Bar keys that can be moved (action bars + stance + micro + bag)
-- These are populated by EAB if it's loaded; otherwise empty.
local BAR_LOOKUP = ns.BAR_LOOKUP or {}
local ALL_BAR_ORDER = ns.BAR_DROPDOWN_ORDER or {}
local VISIBILITY_ONLY = ns.VISIBILITY_ONLY or {}
local function GetVisibilityOnly()
-- Read lazily so child addons have time to populate ns.VISIBILITY_ONLY
return ns.VISIBILITY_ONLY or VISIBILITY_ONLY
end
-- Blizzard-owned frames we can move but cannot scale (SetScale causes taint)
local NO_SCALE_BARS = {
MicroBar = true,
BagBar = true,
ExtraActionButton = true,
EncounterBar = true,
}
local function IsNoScaleBar(barKey)
local stripped = barKey:match("^EAB_(.+)$") or barKey
return NO_SCALE_BARS[stripped] == true
end
-- Local aliases for the shared registration tables
local registeredElements = EllesmereUI._unlockRegisteredElements
local registeredOrder = EllesmereUI._unlockRegisteredOrder
local function RebuildRegisteredOrder()
if not EllesmereUI._unlockRegistrationDirty then return end
wipe(registeredOrder)
for key, _ in pairs(registeredElements) do
registeredOrder[#registeredOrder + 1] = key
end
-- Sort by order field (lower first), then alphabetically
table.sort(registeredOrder, function(a, b)
local oa = registeredElements[a].order or 1000
local ob = registeredElements[b].order or 1000
if oa ~= ob then return oa < ob end
return a < b
end)
EllesmereUI._unlockRegistrationDirty = false
end
-------------------------------------------------------------------------------
-- State
-------------------------------------------------------------------------------
local unlockFrame -- the full-screen overlay
local gridFrame -- grid line container
local guidePool = {} -- reusable alignment guide lines
local movers = {} -- { [barKey] = moverFrame }
local isUnlocked = false
local gridMode = "dimmed" -- "disabled", "dimmed", "bright"
local snapEnabled = true -- magnet/snap state (runtime) — must be before SnapPosition
local lockAnimFrame -- lock assembly animation (close)
local openAnimFrame -- lock animation frame (open)
local logoFadeFrame -- the 2s logo+title fade-out timer frame
local pendingPositions = {} -- { [barKey] = {point,relPoint,x,y} } — unsaved changes
local snapshotPositions = {} -- original positions captured when unlock mode opens
local hasChanges = false -- true if user dragged anything this session
local snapHighlightKey = nil -- barKey of mover currently showing snap highlight border
local snapHighlightAnim = nil -- OnUpdate frame for the pulsing border
local combatSuspended = false -- true if unlock mode was auto-closed by combat
local objTrackerWasVisible = false -- track objective tracker state for restore
-- Grid mode helpers
local GRID_ALPHA_DIMMED = 0.15
local GRID_ALPHA_BRIGHT = 0.30
local GRID_CENTER_DIMMED = 0.25
local GRID_CENTER_BRIGHT = 0.50
local GRID_HUD_BRIGHT = 0.60 -- matches HUD_ON_ALPHA
local GRID_HUD_DIMMED = 0.45
local GRID_HUD_OFF = 0.30 -- matches HUD_OFF_ALPHA
local function GridBaseAlpha()
return gridMode == "bright" and GRID_ALPHA_BRIGHT or GRID_ALPHA_DIMMED
end
local function GridCenterAlpha()
return gridMode == "bright" and GRID_CENTER_BRIGHT or GRID_CENTER_DIMMED
end
local function GridHudAlpha()
if gridMode == "bright" then return GRID_HUD_BRIGHT end
if gridMode == "dimmed" then return GRID_HUD_DIMMED end
return GRID_HUD_OFF
end
local function GridLabelText()
if gridMode == "bright" then return "Grid Lines\nBright" end
if gridMode == "dimmed" then return "Grid Lines\nDimmed" end
return "Grid Lines\nDisabled"
end
local function CycleGridMode()
if gridMode == "dimmed" then gridMode = "bright"
elseif gridMode == "bright" then gridMode = "disabled"
else gridMode = "dimmed" end
end
local flashlightEnabled = true -- cursor flashlight toggle
local hoverBarEnabled = false -- show-bar-on-hover toggle
local darkOverlaysEnabled = true -- dark overlay backgrounds on movers
local unlockTipFrame -- one-time "how to use" tip frame
local pendingAfterClose -- callback to run after DoClose completes
local selectedMover -- currently selected mover frame (for arrow key nudging)
local arrowKeyFrame -- invisible frame that captures arrow key input
local selectElementPicker -- mover currently in "Select Element" pick mode (nil = off)
local _overlayFadeFrame -- tiny OnUpdate driver for select-element dimmer fade
local SELECT_ELEMENT_ALPHA = 0.50 -- overlay alpha during select-element pick mode
local SELECT_ELEMENT_FADE = 0.50 -- seconds for the fade transition
-- Smoothly fade the background overlay between normal and select-element alpha
local function FadeOverlayForSelectElement(entering)
if not unlockFrame or not unlockFrame._overlay then return end
local startA = entering and (unlockFrame._overlayMaxAlpha or 0.20) or SELECT_ELEMENT_ALPHA
local endA = entering and SELECT_ELEMENT_ALPHA or (unlockFrame._overlayMaxAlpha or 0.20)
if not _overlayFadeFrame then
_overlayFadeFrame = CreateFrame("Frame")
end
local elapsed = 0
_overlayFadeFrame:SetScript("OnUpdate", function(self, dt)
elapsed = elapsed + dt
local t = math.min(elapsed / SELECT_ELEMENT_FADE, 1)
local a = startA + (endA - startA) * t
unlockFrame._overlay:SetColorTexture(0.02, 0.03, 0.04, a)
if t >= 1 then self:SetScript("OnUpdate", nil) end
end)
end
-------------------------------------------------------------------------------
-- Saved position helpers (action bars — legacy path)
-------------------------------------------------------------------------------
local function GetPositionDB()
if not EAB or not EAB.db then return nil end
if not EAB.db.profile.barPositions then
EAB.db.profile.barPositions = {}
end
return EAB.db.profile.barPositions
end
local function SaveBarPosition(barKey, point, relPoint, x, y, scale)
-- Registered element?
local elem = registeredElements[barKey]
if elem and elem.savePosition then
elem.savePosition(barKey, point, relPoint, x, y, scale)
return
end
-- Legacy action bar path
local db = GetPositionDB()
if not db then return end
db[barKey] = { point = point, relPoint = relPoint, x = x, y = y, scale = scale }
end
local function LoadBarPosition(barKey)
-- Registered element?
local elem = registeredElements[barKey]
if elem and elem.loadPosition then
return elem.loadPosition(barKey)
end
-- Legacy action bar path
local db = GetPositionDB()
if not db or not db[barKey] then return nil end
return db[barKey]
end
local function ClearBarPosition(barKey)
-- Registered element?
local elem = registeredElements[barKey]
if elem and elem.clearPosition then
elem.clearPosition(barKey)
return
end
-- Legacy action bar path
local db = GetPositionDB()
if db then db[barKey] = nil end
end
-------------------------------------------------------------------------------
-- Bar frame resolution (works for both action bars and registered elements)
-------------------------------------------------------------------------------
local function GetBarFrame(barKey)
-- Registered element?
local elem = registeredElements[barKey]
if elem and elem.getFrame then
return elem.getFrame(barKey)
end
-- Action bars (BAR_LOOKUP has frameName + fallbackFrame)
local info = BAR_LOOKUP[barKey]
if info then
local f = _G[info.frameName]
if not f and info.fallbackFrame then f = _G[info.fallbackFrame] end
return f
end
-- Extra bars (MicroBar, BagBar — not in BAR_LOOKUP)
if barKey == "MicroBar" then return _G["MicroMenuContainer"] or _G["MicroMenu"] end
if barKey == "BagBar" then return _G["BagsBar"] end
return nil
end
local function GetBarLabel(barKey)
-- Registered element?
local elem = registeredElements[barKey]
if elem and elem.label then
return elem.label
end
local vals = ns.BAR_DROPDOWN_VALUES
return vals and vals[barKey] or barKey
end
-------------------------------------------------------------------------------
-- Apply saved positions on login / reload
-------------------------------------------------------------------------------
local function ApplySavedPositions()
if InCombatLockdown() then return end
-- Action bars: apply from barPositions DB
local db = GetPositionDB()
if db then
for barKey, pos in pairs(db) do
local bar = GetBarFrame(barKey)
if bar and pos.point then
pcall(function()
bar:ClearAllPoints()
bar:SetPoint(pos.point, UIParent, pos.relPoint, pos.x, pos.y)
if pos.scale and pos.scale ~= 1 then bar:SetScale(pos.scale) end
end)
end
end
end
-- Registered elements: each addon applies its own positions
RebuildRegisteredOrder()
for _, key in ipairs(registeredOrder) do
local elem = registeredElements[key]
if elem and elem.applyPosition then
pcall(elem.applyPosition, key)
end
end
end
-------------------------------------------------------------------------------
-- Edit Mode anchor guard — hooks ApplySystemAnchor on each Blizzard bar
-- frame so that when Blizzard's Edit Mode tries to reposition a bar we
-- have a custom position for, the original method is skipped entirely.
-- This prevents the visual "jump" because the bar never moves to the
-- wrong position in the first place.
--
-- IMPORTANT: We use hooksecurefunc (post-hook) instead of replacing the
-- method outright. Replacing ApplySystemAnchor taints the bar frame,
-- which propagates to child action buttons and causes
-- ADDON_ACTION_BLOCKED on SetShown(). A post-hook lets Blizzard's
-- secure code run first, then we re-position the bar in a deferred
-- timer so our addon code never executes inside the secure call chain.
-------------------------------------------------------------------------------
local anchorGuardedBars = {} -- { [barFrame] = true }
local function InstallAnchorGuard(bar, barKey)
if anchorGuardedBars[bar] then return end
if not bar.ApplySystemAnchor then return end
anchorGuardedBars[bar] = true
hooksecurefunc(bar, "ApplySystemAnchor", function(self)
local db = GetPositionDB()
if db and db[barKey] and db[barKey].point then
-- Defer so we don't taint the secure execution context
C_Timer.After(0, function()
if InCombatLockdown() then return end
pcall(function()
self:ClearAllPoints()
self:SetPoint(db[barKey].point, UIParent, db[barKey].relPoint,
db[barKey].x, db[barKey].y)
if db[barKey].scale and db[barKey].scale ~= 1 then self:SetScale(db[barKey].scale) end
end)
end)
end
end)
end
local function InstallAllAnchorGuards()
local db = GetPositionDB()
if not db then return end
for barKey, _ in pairs(db) do
local bar = GetBarFrame(barKey)
if bar then
InstallAnchorGuard(bar, barKey)
end
end
end
-- Hook into the addon's ApplyAll chain (action bars only)
if EAB then
local _origApplyAll = EAB.ApplyAll
if _origApplyAll then
function EAB:ApplyAll()
_origApplyAll(self)
-- Install anchor guards on first ApplyAll (bars exist by now)
InstallAllAnchorGuards()
C_Timer.After(0.6, ApplySavedPositions)
end
end
-- Called by EllesmereUIActionBars when Blizzard's Edit Mode saves or exits.
function EAB:OnEditModeLayoutReapply()
InstallAllAnchorGuards()
ApplySavedPositions()
C_Timer.After(0.3, function() self:ApplyAll() end)
end
-- Install anchor guards as early as possible — right after the DB is
-- initialized — so Blizzard's very first layout pass can't move bars
-- we have custom positions for.
local _origOnInit = EAB.OnInitialize
if _origOnInit then
function EAB:OnInitialize()
_origOnInit(self)
InstallAllAnchorGuards()
ApplySavedPositions()
end
end
end
-------------------------------------------------------------------------------
-- Accent color helper (reads live from EllesmereUI)
-------------------------------------------------------------------------------
local function GetAccent()
local eg = EllesmereUI and EllesmereUI.ELLESMERE_GREEN
if eg then return eg.r, eg.g, eg.b end
return 12/255, 210/255, 157/255
end
-------------------------------------------------------------------------------
-- Grid overlay
-------------------------------------------------------------------------------
local function CreateGrid(parent)
if gridFrame then return gridFrame end
-- Grid lives on its own BACKGROUND-strata frame so it renders
-- BEHIND the actual game UI elements (action bars, unit frames, etc.)
gridFrame = CreateFrame("Frame", nil, UIParent)
gridFrame:SetFrameStrata("BACKGROUND")
gridFrame:SetAllPoints(UIParent)
gridFrame:SetFrameLevel(1)
gridFrame._lines = {}
function gridFrame:Rebuild()
for _, tex in ipairs(self._lines) do tex:Hide() end
local idx = 0
local w, h = UIParent:GetWidth(), UIParent:GetHeight()
local ar, ag, ab = GetAccent()
local baseA = GridBaseAlpha()
local centerA = GridCenterAlpha()
-- Vertical lines (centered on screen center, extending outward)
local centerX = floor(w / 2)
local centerY = floor(h / 2)
-- Lines left of center
local x = centerX - GRID_SPACING
while x > 0 do
idx = idx + 1
local tex = self._lines[idx]
if not tex then
tex = self:CreateTexture(nil, "BACKGROUND", nil, -7)
self._lines[idx] = tex
end
tex:SetColorTexture(ar, ag, ab, baseA)
tex._baseAlpha = baseA
tex._isVert = true
tex._pos = x
tex:ClearAllPoints()
tex:SetSize(1, h)
tex:SetPoint("TOPLEFT", UIParent, "TOPLEFT", x, 0)
tex:Show()
x = x - GRID_SPACING
end
-- Lines right of center
x = centerX + GRID_SPACING
while x < w do
idx = idx + 1
local tex = self._lines[idx]
if not tex then
tex = self:CreateTexture(nil, "BACKGROUND", nil, -7)
self._lines[idx] = tex
end
tex:SetColorTexture(ar, ag, ab, baseA)
tex._baseAlpha = baseA
tex._isVert = true
tex._pos = x
tex:ClearAllPoints()
tex:SetSize(1, h)
tex:SetPoint("TOPLEFT", UIParent, "TOPLEFT", x, 0)
tex:Show()
x = x + GRID_SPACING
end
-- Horizontal lines (centered on screen center, extending outward)
-- Note: y is distance from top
local y = centerY - GRID_SPACING
while y > 0 do
idx = idx + 1
local tex = self._lines[idx]
if not tex then
tex = self:CreateTexture(nil, "BACKGROUND", nil, -7)
self._lines[idx] = tex
end
tex:SetColorTexture(ar, ag, ab, baseA)
tex._baseAlpha = baseA
tex._isVert = false
tex._pos = y
tex:ClearAllPoints()
tex:SetSize(w, 1)
tex:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 0, -y)
tex:Show()
y = y - GRID_SPACING
end
y = centerY + GRID_SPACING
while y < h do
idx = idx + 1
local tex = self._lines[idx]
if not tex then
tex = self:CreateTexture(nil, "BACKGROUND", nil, -7)
self._lines[idx] = tex
end
tex:SetColorTexture(ar, ag, ab, baseA)
tex._baseAlpha = baseA
tex._isVert = false
tex._pos = y
tex:ClearAllPoints()
tex:SetSize(w, 1)
tex:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 0, -y)
tex:Show()
y = y + GRID_SPACING
end
-- Center crosshair (brighter)
for _, axis in ipairs({"V", "H"}) do
idx = idx + 1
local tex = self._lines[idx]
if not tex then
tex = self:CreateTexture(nil, "BACKGROUND", nil, -6)
self._lines[idx] = tex
end
tex:SetColorTexture(ar, ag, ab, centerA)
tex._baseAlpha = centerA
tex._isVert = (axis == "V")
tex._pos = 0
tex:ClearAllPoints()
if axis == "V" then
tex:SetSize(1, h)
tex:SetPoint("TOP", UIParent, "TOP", 0, 0)
else
tex:SetSize(w, 1)
tex:SetPoint("LEFT", UIParent, "LEFT", 0, 0)
end
tex:Show()
end
self._lineCount = idx
end
-- Cursor flashlight: radial glow around the cursor.
-- Each nearby grid line is split into small segments, each segment's
-- alpha is based on its TRUE 2D distance from the cursor, giving a
-- smooth circular falloff like a real flashlight.
local LIGHT_RADIUS = 220 -- px radius of the flashlight circle
local LIGHT_BOOST = 0.70 -- max alpha boost at cursor center
local SEG_SIZE = 8 -- px length of each mini-segment
-- Pool of mini glow-segment textures
gridFrame._glows = {}
local glowIdx = 0
local function GetGlow(idx)
local g = gridFrame._glows[idx]
if not g then
g = gridFrame:CreateTexture(nil, "BACKGROUND", nil, -6)
gridFrame._glows[idx] = g
end
return g
end
-- Cache accent color; refreshed when grid is rebuilt
local cachedAR, cachedAG, cachedAB = GetAccent()
local origRebuild = gridFrame.Rebuild
function gridFrame:Rebuild()
origRebuild(self)
cachedAR, cachedAG, cachedAB = GetAccent()
end
gridFrame:SetScript("OnUpdate", function(self, dt)
-- Early-out: hide all glows and skip work when grid is not visible
if not self:IsShown() then return end
-- If flashlight is disabled, just hide all glows and reset base alphas
if not flashlightEnabled then
for j = 1, #self._glows do
if self._glows[j] then self._glows[j]:Hide() end
end
return
end
local scale = UIParent:GetEffectiveScale()
local cx, cy = GetCursorPosition()
cx = cx / scale
cy = cy / scale
local cyFromTop = UIParent:GetHeight() - cy
glowIdx = 0
local R2 = LIGHT_RADIUS * LIGHT_RADIUS
local lineCount = self._lineCount or #self._lines
for i = 1, lineCount do
local tex = self._lines[i]
if tex and tex:IsShown() and tex._baseAlpha then
tex:SetColorTexture(cachedAR, cachedAG, cachedAB, tex._baseAlpha)
-- Perpendicular distance from cursor to this line
local perpDist
if tex._isVert then
perpDist = abs(tex._pos - cx)
else
perpDist = abs(tex._pos - cyFromTop)
end
-- Skip lines too far away (no part can be within radius)
if perpDist < LIGHT_RADIUS then
-- How far along the line we can reach within the radius
local halfSpan = sqrt(R2 - perpDist * perpDist)
if tex._isVert then
-- Vertical line: segments along Y axis
local startY = cy - halfSpan
local endY = cy + halfSpan
local segY = startY
while segY < endY do
local segEnd = min(segY + SEG_SIZE, endY)
local midY = (segY + segEnd) / 2
-- 2D distance from cursor to segment midpoint
local dx = tex._pos - cx
local dy = midY - cy
local d2 = dx * dx + dy * dy
if d2 < R2 then
local t = 1 - sqrt(d2) / LIGHT_RADIUS
local alpha = LIGHT_BOOST * t * t
if alpha > 0.003 then
glowIdx = glowIdx + 1
local g = GetGlow(glowIdx)
g:SetColorTexture(cachedAR, cachedAG, cachedAB, alpha)
g:ClearAllPoints()
g:SetSize(1, segEnd - segY)
g:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", tex._pos, max(0, segY))
g:Show()
end
end
segY = segEnd
end
else
-- Horizontal line: segments along X axis
local startX = cx - halfSpan
local endX = cx + halfSpan
local segX = startX
while segX < endX do
local segEnd = min(segX + SEG_SIZE, endX)
local midX = (segX + segEnd) / 2
local dx = midX - cx
local dy = tex._pos - cyFromTop
local d2 = dx * dx + dy * dy
if d2 < R2 then
local t = 1 - sqrt(d2) / LIGHT_RADIUS
local alpha = LIGHT_BOOST * t * t
if alpha > 0.003 then
glowIdx = glowIdx + 1
local g = GetGlow(glowIdx)
g:SetColorTexture(cachedAR, cachedAG, cachedAB, alpha)
g:ClearAllPoints()
g:SetSize(segEnd - segX, 1)
g:SetPoint("TOPLEFT", UIParent, "TOPLEFT", max(0, segX), -tex._pos)
g:Show()
end
end
segX = segEnd
end
end
end
end
end
-- Hide unused glow segments
for j = glowIdx + 1, #self._glows do
if self._glows[j] then self._glows[j]:Hide() end
end
end)
return gridFrame
end
-------------------------------------------------------------------------------
-- Alignment guide lines + measurement labels (snap guides between bars)
-------------------------------------------------------------------------------
local activeGuides = {}
local measurePool = {} -- pool of { frame, line, label } for distance markers
local function GetGuide(idx)
if guidePool[idx] then return guidePool[idx] end
local tex = unlockFrame:CreateTexture(nil, "OVERLAY", nil, 6)
tex:SetColorTexture(1, 1, 1, 1)
local PP = EllesmereUI and EllesmereUI.PP
if PP then PP.DisablePixelSnap(tex)
elseif tex.SetSnapToPixelGrid then tex:SetSnapToPixelGrid(false); tex:SetTexelSnappingBias(0) end
guidePool[idx] = tex
return tex
end
local function GetMeasure(idx)
if measurePool[idx] then return measurePool[idx] end
-- Each measurement marker: a small frame with a line + label
local f = CreateFrame("Frame", nil, unlockFrame)
f:SetFrameStrata("FULLSCREEN_DIALOG")
f:SetFrameLevel(200)
-- Background pill for the label
local bg = f:CreateTexture(nil, "BACKGROUND")
bg:SetColorTexture(0.85, 0.15, 0.85, 0.85)
f._bg = bg
-- Distance text
local fs = f:CreateFontString(nil, "OVERLAY")
fs:SetFont(FONT_PATH, 9, "OUTLINE")
fs:SetTextColor(1, 1, 1, 1)
f._label = fs
-- Connector line (magenta)
local line = f:CreateTexture(nil, "OVERLAY", nil, 5)
line:SetColorTexture(0.85, 0.15, 0.85, 0.7)
f._line = line
-- Arrow caps (small triangles simulated with tiny textures)
local arrowA = f:CreateTexture(nil, "OVERLAY", nil, 6)
arrowA:SetColorTexture(0.85, 0.15, 0.85, 0.85)
f._arrowA = arrowA
local arrowB = f:CreateTexture(nil, "OVERLAY", nil, 6)
arrowB:SetColorTexture(0.85, 0.15, 0.85, 0.85)
f._arrowB = arrowB
measurePool[idx] = f
return f
end
-- Snap highlight: pulsing white border layered ON TOP of the green border.
-- Each mover gets a lazy-created _snapBrd (a second MakeBorder at a higher
-- frame level) so the green accent border stays visible underneath.
local snapHighlightElapsed = 0
local function GetOrCreateSnapBorder(m)
if m._snapBrd then return m._snapBrd end
local brd = EllesmereUI.MakeBorder(m, 1, 1, 1, 0)
-- Raise above the accent border
brd._frame:SetFrameLevel(m:GetFrameLevel() + 3)
m._snapBrd = brd
return brd
end
local function ClearSnapHighlight()
if snapHighlightKey and movers[snapHighlightKey] then
local m = movers[snapHighlightKey]
if m._snapBrd then m._snapBrd:SetColor(1, 1, 1, 0) end
end
snapHighlightKey = nil
snapHighlightElapsed = 0
if snapHighlightAnim then
snapHighlightAnim:SetScript("OnUpdate", nil)
snapHighlightAnim:Hide()
end
end
local function ShowSnapHighlight(targetKey)
if targetKey == snapHighlightKey then return end
-- Hide old highlight
if snapHighlightKey and movers[snapHighlightKey] then
local old = movers[snapHighlightKey]
if old._snapBrd then old._snapBrd:SetColor(1, 1, 1, 0) end
end
local m = movers[targetKey]
if not m then
ClearSnapHighlight()
return
end
snapHighlightKey = targetKey
snapHighlightElapsed = 0
GetOrCreateSnapBorder(m)
if not snapHighlightAnim then
snapHighlightAnim = CreateFrame("Frame")
end
snapHighlightAnim:SetScript("OnUpdate", function(self, dt)
snapHighlightElapsed = snapHighlightElapsed + dt
local target = movers[snapHighlightKey]
if not target or not target._snapBrd then
ClearSnapHighlight()
return
end
local alpha = 0.45 + 0.45 * sin(snapHighlightElapsed * 9.42)
target._snapBrd:SetColor(1, 1, 1, alpha * 0.9)
end)
snapHighlightAnim:Show()
end
local function HideAllGuides()
for _, tex in ipairs(guidePool) do tex:Hide() end
for _, m in ipairs(measurePool) do m:Hide() end
wipe(activeGuides)
end
-- Full cleanup including snap highlight (used when drag stops)
local function HideAllGuidesAndHighlight()
HideAllGuides()
ClearSnapHighlight()
end
-- Show a vertical measurement marker between two Y positions at a given X
-- yTop > yBot in screen coords (bottom-left origin)
local function ShowVerticalMeasure(idx, xPos, yBot, yTop, dist)
local f = GetMeasure(idx)
local gap = yTop - yBot
if gap < 2 then f:Hide(); return idx end
f:SetSize(1, 1)
f:ClearAllPoints()
f:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 0, 0)
f:SetAllPoints(UIParent)
-- Connector line
f._line:ClearAllPoints()
f._line:SetSize(1, gap)
f._line:SetPoint("BOTTOM", UIParent, "BOTTOMLEFT", xPos, yBot)
f._line:Show()
-- Arrow caps (small horizontal bars at each end)
f._arrowA:ClearAllPoints()
f._arrowA:SetSize(5, 1)
f._arrowA:SetPoint("BOTTOM", UIParent, "BOTTOMLEFT", xPos, yBot)
f._arrowA:Show()
f._arrowB:ClearAllPoints()
f._arrowB:SetSize(5, 1)
f._arrowB:SetPoint("BOTTOM", UIParent, "BOTTOMLEFT", xPos, yTop)
f._arrowB:Show()
-- Label
local text = floor(dist + 0.5) .. " px"
f._label:SetText(text)
local tw = f._label:GetStringWidth() + 8
local th = f._label:GetStringHeight() + 4
f._bg:ClearAllPoints()
f._bg:SetSize(tw, th)
local midY = (yBot + yTop) / 2
f._bg:SetPoint("LEFT", UIParent, "BOTTOMLEFT", xPos + 4, midY)
f._label:ClearAllPoints()
f._label:SetPoint("CENTER", f._bg, "CENTER", 0, 0)
f._bg:Show()
f._label:Show()
f:Show()
return idx
end
-- Show a horizontal measurement marker between two X positions at a given Y
local function ShowHorizontalMeasure(idx, yPos, xLeft, xRight, dist)
local f = GetMeasure(idx)
local gap = xRight - xLeft
if gap < 2 then f:Hide(); return idx end
f:SetSize(1, 1)
f:ClearAllPoints()
f:SetAllPoints(UIParent)
-- Connector line
f._line:ClearAllPoints()
f._line:SetSize(gap, 1)
f._line:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", xLeft, yPos)
f._line:Show()
-- Arrow caps
f._arrowA:ClearAllPoints()
f._arrowA:SetSize(1, 5)
f._arrowA:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", xLeft, yPos - 2)
f._arrowA:Show()
f._arrowB:ClearAllPoints()
f._arrowB:SetSize(1, 5)
f._arrowB:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", xRight, yPos - 2)
f._arrowB:Show()
-- Label
local text = floor(dist + 0.5) .. " px"
f._label:SetText(text)
local tw = f._label:GetStringWidth() + 8
local th = f._label:GetStringHeight() + 4
f._bg:ClearAllPoints()
f._bg:SetSize(tw, th)
local midX = (xLeft + xRight) / 2
f._bg:SetPoint("BOTTOM", UIParent, "BOTTOMLEFT", midX, yPos + 4)
f._label:ClearAllPoints()
f._label:SetPoint("CENTER", f._bg, "CENTER", 0, 0)
f._bg:Show()
f._label:Show()
f:Show()
return idx
end
-------------------------------------------------------------------------------
-- ShowAlignmentGuides — draws full-screen guide lines at snap positions
-- and measurement markers for equal-spacing snaps.
-- Called from the drag OnUpdate; snapInfo is populated by SnapPosition.
-------------------------------------------------------------------------------
local lastSnapInfo = {} -- written by SnapPosition, read by ShowAlignmentGuides
local function ShowAlignmentGuides(dragKey)
HideAllGuides()
if not lastSnapInfo then return end
local ar, ag, ab = GetAccent()
local guideIdx = 0
local screenW = UIParent:GetWidth()
local screenH = UIParent:GetHeight()
-- Edge/center snap guide lines
if lastSnapInfo.snapXPos then
guideIdx = guideIdx + 1
local g = GetGuide(guideIdx)
g:SetColorTexture(ar, ag, ab, 0.5)
g:ClearAllPoints()
g:SetSize(1, screenH)
g:SetPoint("BOTTOM", UIParent, "BOTTOMLEFT", lastSnapInfo.snapXPos, 0)
g:Show()
activeGuides[guideIdx] = g
end
if lastSnapInfo.snapYPos then
guideIdx = guideIdx + 1
local g = GetGuide(guideIdx)
g:SetColorTexture(ar, ag, ab, 0.5)
g:ClearAllPoints()
g:SetSize(screenW, 1)
g:SetPoint("LEFT", UIParent, "BOTTOMLEFT", 0, lastSnapInfo.snapYPos)
g:Show()
activeGuides[guideIdx] = g
end
-- Snap highlight: pulse the border of the element being snapped to
local dragMover = movers[dragKey]
local hasSpecificTarget = dragMover and dragMover._snapTarget
and dragMover._snapTarget ~= "_disable_"
and dragMover._snapTarget ~= "_select_"
if hasSpecificTarget and movers[dragMover._snapTarget] then
ShowSnapHighlight(dragMover._snapTarget)
elseif lastSnapInfo.closestKey then
ShowSnapHighlight(lastSnapInfo.closestKey)
else
ClearSnapHighlight()
end
end
-------------------------------------------------------------------------------
-- Snap-to-element helper
-- 1) Find the single closest mover (by minimum edge-to-edge distance).
-- Only consider movers within SNAP_PROXIMITY px.
-- 2) Check 9 X-axis pairs + 9 Y-axis pairs against that one mover.
-- Populates lastSnapInfo for ShowAlignmentGuides to read.
-------------------------------------------------------------------------------
local function SnapPosition(dragKey, cx, cy, halfW, halfH)
wipe(lastSnapInfo)
if not snapEnabled then return cx, cy end
local dL = cx - halfW
local dR = cx + halfW
local dT = cy + halfH
local dB = cy - halfH
-- Step 1: find snap target mover
-- If this mover has a specific snap target, use it; otherwise find closest
local closestKey = nil
local dragMover = movers[dragKey]
local perMoverTarget = dragMover and dragMover._snapTarget
-- "_disable_" = snapping disabled for this specific mover
if perMoverTarget == "_disable_" then return cx, cy end
if perMoverTarget and perMoverTarget ~= dragKey and movers[perMoverTarget] and movers[perMoverTarget]:IsShown() then
closestKey = perMoverTarget
else
-- Find closest by true 2D edge-to-edge distance (no limit)
local closestMinDist = math.huge
for key, mover in pairs(movers) do
if key ~= dragKey and mover:IsShown() then
local oL = mover:GetLeft() or 0
local oR = mover:GetRight() or 0
local oT = mover:GetTop() or 0
local oB = mover:GetBottom() or 0
-- Signed axis distances (negative = overlapping on that axis)
local gapX = 0
if dR < oL then gapX = oL - dR
elseif dL > oR then gapX = dL - oR end
local gapY = 0
if dB > oT then gapY = dB - oT
elseif dT < oB then gapY = oB - dT end
-- 2D edge-to-edge distance (0 if overlapping)
local edgeDist = sqrt(gapX * gapX + gapY * gapY)
if edgeDist < closestMinDist then
closestMinDist = edgeDist
closestKey = key
end
end
end
end
lastSnapInfo.closestKey = closestKey
local bestDX, bestDistX = 0, SNAP_THRESH
local bestDY, bestDistY = 0, SNAP_THRESH
local snapXLinePos, snapYLinePos = nil, nil
-- Step 2: 9+9 edge pairs against closest mover
if closestKey then
local m = movers[closestKey]
local oL = m:GetLeft() or 0
local oR = m:GetRight() or 0
local oT = m:GetTop() or 0
local oB = m:GetBottom() or 0
local oCX = (oL + oR) * 0.5
local oCY = (oT + oB) * 0.5
-- X-axis: dragged {left, center, right} vs target {left, center, right}
local dragXEdges = { dL, cx, dR }
local targXEdges = { oL, oCX, oR }
for _, de in ipairs(dragXEdges) do
for _, te in ipairs(targXEdges) do
local dx = de - te
local adx = abs(dx)
if adx < bestDistX then
bestDistX = adx
bestDX = dx
snapXLinePos = te
end
end
end
-- Y-axis: dragged {top, center, bottom} vs target {top, center, bottom}
local dragYEdges = { dT, cy, dB }
local targYEdges = { oT, oCY, oB }
for _, de in ipairs(dragYEdges) do
for _, te in ipairs(targYEdges) do
local dy = de - te
local ady = abs(dy)
if ady < bestDistY then
bestDistY = ady
bestDY = dy
snapYLinePos = te
end
end
end
end