-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrame.lua
More file actions
737 lines (618 loc) · 16.3 KB
/
Copy pathFrame.lua
File metadata and controls
737 lines (618 loc) · 16.3 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
local Frame = CleanBars:CreateClass('Frame')
CleanBars.Frame = Frame
local FadeManager = CleanBars.FadeManager
local active = {}
local unused = {}
function Frame:New(id, tooltipText)
local id = tonumber(id) or id
local f = self:Restore(id) or self:Create(id)
f:LoadSettings()
f.buttons = {}
f:SetTooltipText(tooltipText)
active[id] = f
return f
end
function Frame:Create(id)
local f = self:Bind(CreateFrame('Frame', nil, UIParent))
f:SetClampedToScreen(true)
f:SetMovable(true)
f.id = id
f.header = CreateFrame('Frame', nil, f, 'SecureHandlerStateTemplate')
f.header:SetAttribute('_onstate-display', [[
local newstate = newstate or 'show'
if newstate == 'hide' then
self:SetAttribute('frame-alpha', nil)
self:Hide()
else
local stateAlpha = tonumber(newstate)
if stateAlpha then
self:SetAttribute('frame-alpha', stateAlpha/100)
else
self:SetAttribute('frame-alpha', nil)
end
self:Show()
end
]])
f.header:SetAllPoints(f)
f.drag = CleanBars.DragFrame:New(f)
return f
end
function Frame:Restore(id)
local f = unused[id]
if f then
unused[id] = nil
return f
end
end
function Frame:Free()
active[self.id] = nil
UnregisterStateDriver(self.header, 'display', 'show')
FadeManager:Remove(self)
for i in pairs(self.buttons) do
self:RemoveButton(i)
end
self.buttons = nil
self.docked = nil
self:ClearAllPoints()
self:SetUserPlaced(nil)
self.drag:Hide()
self:Hide()
unused[self.id] = self
end
function Frame:Delete()
self:Free()
CleanBars:SetFrameSets(self.id, nil)
end
function Frame:GetDefaults()
return {}
end
function Frame:LoadSettings()
local sets = CleanBars:GetFrameSets(self.id)
if not sets then
local defaults = self:GetDefaults()
sets = {}
if type(defaults) == 'table' then
for k, v in pairs(defaults) do
if type(v) == 'table' then
sets[k] = {}
for subK, subV in pairs(v) do
sets[k][subK] = subV
end
else
sets[k] = v
end
end
end
sets = CleanBars:SetFrameSets(self.id, sets)
end
self.sets = sets
self:Reposition()
if self.sets.hidden then
self:HideFrame()
else
self:ShowFrame()
end
if CleanBars:Locked() then
self:Lock()
else
self:Unlock()
end
self:UpdateShowStates()
self:UpdateAlpha()
self:UpdateFader()
end
function Frame:LoadButtons()
for i = 1, self:NumButtons() do
self:AddButton(i)
end
end
function Frame:AddButton(i)
end
function Frame:RemoveButton(i)
local b = self.buttons and self.buttons[i]
if b and b.Free then
b:Free()
self.buttons[i] = nil
end
end
function Frame:UpdateButtonCount(numButtons)
for i = numButtons + 1, #self.buttons do
self:RemoveButton(i)
end
for i = #self.buttons + 1, numButtons do
self:AddButton(i)
end
end
function Frame:SetNumButtons(numButtons)
self.sets.numButtons = numButtons
self:UpdateButtonCount(self:NumButtons())
self:Layout()
end
function Frame:NumButtons()
return self.sets.numButtons or 0
end
function Frame:SetColumns(columns)
self.sets.columns = columns ~= self:NumButtons() and columns or nil
self:Layout()
end
function Frame:NumColumns()
return self.sets.columns or self:NumButtons()
end
function Frame:SetSpacing(spacing)
self.sets.spacing = spacing
self:Layout()
end
function Frame:GetSpacing()
return self.sets.spacing or 0
end
function Frame:SetPadding(w, h)
self.sets.padW = w
self.sets.padH = h or w
self:Layout()
end
function Frame:GetPadding()
return self.sets.padW or 0, self.sets.padH or self.sets.padW or 0
end
-- Layout direction assignment settings loops
function Frame:SetLeftToRight(isLeftToRight)
local isRightToLeft = not isLeftToRight
self.sets.isRightToLeft = isRightToLeft and true or nil
self:Layout()
end
function Frame:GetLeftToRight()
return not self.sets.isRightToLeft
end
function Frame:SetTopToBottom(isTopToBottom)
local isBottomToTop = not isTopToBottom
self.sets.isBottomToTop = isBottomToTop and true or nil
self:Layout()
end
function Frame:GetTopToBottom()
return not self.sets.isBottomToTop
end
function Frame:Layout()
local width, height
if #self.buttons > 0 then
local cols = min(self:NumColumns(), #self.buttons)
local rows = ceil(#self.buttons / cols)
local pW, pH = self:GetPadding()
local spacing = self:GetSpacing()
local isLeftToRight = self:GetLeftToRight()
local isTopToBottom = self:GetTopToBottom()
local b = self.buttons[1]
local w = b:GetWidth() + spacing
local h = b:GetHeight() + spacing
for i,b in pairs(self.buttons) do
local col
local row
if isLeftToRight then
col = (i-1) % cols
else
col = (cols-1) - (i-1) % cols
end
if isTopToBottom then
row = ceil(i / cols) - 1
else
row = rows - ceil(i / cols)
end
b:ClearAllPoints()
b:SetPoint('TOPLEFT', w*col + pW, -(h*row + pH))
end
width = w*cols - spacing + pW*2
height = h*ceil(#self.buttons/cols) - spacing + pH*2
else
width = 30
height = 30
end
self:SetWidth(max(width, 8))
self:SetHeight(max(height, 8))
end
function Frame:GetScaledCoords(scale)
local ratio = self:GetScale() / scale
return (self:GetLeft() or 0) * ratio, (self:GetTop() or 0) * ratio
end
function Frame:SetFrameScale(scale, scaleAnchored)
local x, y = self:GetScaledCoords(scale)
self.sets.scale = scale
self:Rescale()
if not self.sets.anchor then
self:ClearAllPoints()
self:SetPoint('TOPLEFT', self:GetParent(), 'BOTTOMLEFT', x, y)
self:SavePosition()
end
if scaleAnchored then
for _,f in self:GetAll() do
if f:GetAnchor() == self then
f:SetFrameScale(scale, true)
end
end
end
end
function Frame:Rescale()
self:SetScale(self:GetScale())
self.drag:SetScale(self:GetScale())
end
function Frame:GetScale()
return self.sets.scale or 1
end
function Frame:SetFrameAlpha(alpha)
if alpha == 1 then
self.sets.alpha = nil
else
self.sets.alpha = alpha
end
self:UpdateAlpha()
end
function Frame:GetFrameAlpha()
return self.sets.alpha or 1
end
function Frame:SetFadeMultiplier(alpha)
local alpha = alpha or 1
if alpha == 1 then
self.sets.fadeAlpha = nil
else
self.sets.fadeAlpha = alpha
end
self:UpdateAlpha()
self:UpdateFader()
end
function Frame:GetFadeMultiplier()
return self.sets.fadeAlpha or 1
end
function Frame:UpdateAlpha()
self:SetAlpha(self:GetExpectedAlpha())
end
function Frame:GetExpectedAlpha()
if CleanBars:IsLinkedOpacityEnabled() then
local anchor = (self:GetAnchor())
if anchor then
return anchor:GetExpectedAlpha()
end
end
local stateAlpha = self.header:GetAttribute('frame-alpha')
if stateAlpha then
return stateAlpha
end
local alpha = self:GetFrameAlpha()
local fadeMultiplier = self:GetFadeMultiplier()
if fadeMultiplier >= 1 or self:IsFocus() then
return alpha
end
return alpha * fadeMultiplier
end
local function isChildFocus(...)
local focus = GetMouseFocus()
for i = 1, select('#', ...) do
if focus == select(i, ...) then
return true
end
end
for i = 1, select('#', ...) do
local f = select(i, ...)
if f:IsShown() and isChildFocus(f:GetChildren()) then
return true
end
end
return false
end
if Frame.IsMouseOver then
function Frame:IsFocus()
if self:IsMouseOver(1, -1, -1, 1) then
return (GetMouseFocus() == _G['WorldFrame']) or isChildFocus(self:GetChildren())
end
return CleanBars:IsLinkedOpacityEnabled() and self:IsDockedFocus()
end
else
function Frame:IsFocus()
if MouseIsOver(self, 1, -1, -1, 1) then
return (GetMouseFocus() == _G['WorldFrame']) or isChildFocus(self:GetChildren())
end
return CleanBars:IsLinkedOpacityEnabled() and self:IsDockedFocus()
end
end
function Frame:IsDockedFocus()
local docked = self.docked
if docked then
for _,f in pairs(docked) do
if f:IsFocus() then
return true
end
end
end
return false
end
function Frame:ShowFrame()
self.sets.hidden = nil
self:Show()
self:UpdateFader()
self.drag:UpdateColor()
end
function Frame:HideFrame()
self.sets.hidden = true
self:Hide()
self:UpdateFader()
self.drag:UpdateColor()
end
function Frame:ToggleFrame()
if self:FrameIsShown() then
self:HideFrame()
else
self:ShowFrame()
end
end
function Frame:FrameIsShown()
return not self.sets.hidden
end
function Frame:SetShowStates(states)
self.sets.showstates = states
self:UpdateShowStates()
end
function Frame:GetShowStates()
local states = self.sets.showstates
if states then
if states:sub(#states) == ']' then
states = states .. 'show;hide'
self.sets.showstates = states
end
end
return states
end
function Frame:UpdateShowStates()
local showstates = self:GetShowStates()
if showstates then
RegisterStateDriver(self.header, 'display', showstates)
self.header:SetAttribute('state-display', SecureCmdOptionParse(showstates))
else
UnregisterStateDriver(self.header, 'display')
self.header:Show()
end
end
function Frame:Lock()
self.drag:Hide()
end
function Frame:Unlock()
self.drag:Show()
end
Frame.stickyTolerance = 16
function Frame:StickToEdge()
local point, x, y = self:GetRelPosition()
local s = self:GetScale()
local w = self:GetParent():GetWidth()/s
local h = self:GetParent():GetHeight()/s
local rTolerance = self.stickyTolerance/s
local changed = false
if abs(x) <= rTolerance then
x = 0
changed = true
end
if abs(y) <= rTolerance then
y = 0
changed = true
end
local cX, cY = self:GetCenter()
if y == 0 then
if abs(cX - w/2) <= rTolerance*2 then
if point == 'TOPLEFT' or point == 'TOPRIGHT' then
point = 'TOP'
else
point = 'BOTTOM'
end
x = 0
changed = true
end
elseif x == 0 then
if abs(cY - h/2) <= rTolerance*2 then
if point == 'TOPLEFT' or point == 'BOTTOMLEFT' then
point = 'LEFT'
else
point = 'RIGHT'
end
y = 0
changed = true
end
end
if changed then
self.sets.point = point
self.sets.x = x
self.sets.y = y
self:ClearAllPoints()
self:SetPoint(point, x, y)
end
end
function Frame:ClearAnchor()
local anchor, point = self:GetAnchor()
if anchor and anchor.docked then
for i,f in pairs(anchor.docked) do
if f == self then
tremove(anchor.docked, i)
break
end
end
if not next(anchor.docked) then
anchor.docked = nil
end
end
self.sets.anchor = nil
self:UpdateFader()
end
function Frame:SetAnchor(anchor, point)
self:ClearAnchor()
if anchor.docked then
local found = false
for i,f in pairs(anchor.docked) do
if f == self then
found = i
break
end
end
if not found then
tinsert(anchor.docked, self)
end
else
anchor.docked = {self}
end
self.sets.anchor = anchor.id .. point
self:UpdateFader()
end
function Frame:Stick()
self:ClearAnchor()
if CleanBars:Sticky() and not IsAltKeyDown() then
for _, f in self:GetAll() do
if f ~= self then
local point = FlyPaper.Stick(self, f, self.stickyTolerance)
if point then
self:SetAnchor(f, point)
break
end
end
end
if not self.sets.anchor then
self:StickToEdge()
end
end
self:SavePosition()
self.drag:UpdateColor()
end
function Frame:Reanchor()
local f, point = self:GetAnchor()
if not(f and FlyPaper.StickToPoint(self, f, point)) then
self:ClearAnchor()
if not self:Reposition() then
self:ClearAllPoints()
self:SetPoint('CENTER')
end
else
self:SetAnchor(f, point)
end
self.drag:UpdateColor()
end
function Frame:GetAnchor()
local anchorString = self.sets.anchor
if anchorString then
local pointStart = #anchorString - 1
return self:Get(anchorString:sub(1, pointStart - 1)), anchorString:sub(pointStart)
end
end
function Frame:GetRelPosition()
local parent = self:GetParent()
local w, h = parent:GetWidth(), parent:GetHeight()
local x, y = self:GetCenter()
local s = self:GetScale()
w = w/s h = h/s
local dx, dy
local hHalf = (x > w/2) and 'RIGHT' or 'LEFT'
if hHalf == 'RIGHT' then
dx = self:GetRight() - w
else
dx = self:GetLeft()
end
local vHalf = (y > h/2) and 'TOP' or 'BOTTOM'
if vHalf == 'TOP' then
dy = self:GetTop() - h
else
dy = self:GetBottom()
end
return vHalf..hHalf, dx, dy
end
function Frame:SavePosition()
local point, x, y = self:GetRelPosition()
local sets = self.sets
sets.point = point
sets.x = x
sets.y = y
end
function Frame:Reposition()
self:Rescale()
local sets = self.sets
local point, x, y = sets.point, sets.x, sets.y
if point then
self:ClearAllPoints()
self:SetPoint(point, x, y)
self:SetUserPlaced(true)
return true
end
end
function Frame:SetFramePoint(...)
self:ClearAllPoints()
self:SetPoint(...)
self:SavePosition()
end
function Frame:CreateMenu()
self.menu = CleanBars:NewMenu(self.id)
self.menu:AddLayoutPanel()
self.menu:AddAdvancedPanel()
end
function Frame:ShowMenu()
if CleanBars.Menu then
if not self.menu then
self:CreateMenu()
end
local menu = self.menu
if menu then
menu:Hide()
menu:SetOwner(self)
menu:ShowPanel(ConfigLocale.Layout)
menu:Show()
end
end
end
function Frame:SetTooltipText(text)
self.tooltipText = text
end
function Frame:GetTooltipText()
return self.tooltipText
end
function Frame:UpdateFader()
if self.sets.hidden then
FadeManager:Remove(self)
else
FadeManager:Add(self)
end
end
function Frame:Get(id)
return active[tonumber(id) or id]
end
function Frame:GetAll()
return pairs(active)
end
function Frame:ForAll(method, ...)
for _,f in self:GetAll() do
local action = f[method]
if action then
action(f, ...)
end
end
end
function Frame:ForFrame(id, method, ...)
if id == 'all' then
self:ForAll(method, ...)
else
local startID, endID = tostring(id):match('(%d+)-(%d+)')
startID = tonumber(startID)
endID = tonumber(endID)
if startID and endID then
if startID > endID then
local t = startID
startID = endID
endID = t
end
for i = startID, endID do
local f = self:Get(i)
if f then
local action = f[method]
if action then
action(f, ...)
end
end
end
else
local f = self:Get(id)
if f then
local action = f[method]
if action then
action(f, ...)
end
end
end
end
end