-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Bar.lua
576 lines (508 loc) · 14.4 KB
/
Bar.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
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
--[[
Copyright (c) 2009, Hendrik "Nevcairiel" Leppkes < h.leppkes at gmail dot com >
All rights reserved.
]]
--[[
Generic Bar Frame Template
]]
local _, Bartender4 = ...
local Bar = CreateFrame("Frame")
local Bar_MT = {__index = Bar}
local table_concat, table_insert, tostring, assert, pairs, min, max = table.concat, table.insert, tostring, assert, pairs, min, max
local setmetatable, tonumber = setmetatable, tonumber
-- GLOBALS: SpellFlyout, UIParent, GameFontNormal
-- GLOBALS: CreateFrame, MouseIsOver, RegisterStateDriver, UnregisterStateDriver
--[[===================================================================================
Universal Bar Contructor
===================================================================================]]--
local defaults = {
alpha = 1,
fadeout = false,
fadeoutalpha = 0.1,
fadeoutdelay = 0.2,
visibility = {
vehicleui = true,
overridebar = true,
stance = {},
},
position = {
scale = 1,
growVertical = "DOWN",
growHorizontal = "RIGHT",
},
clickthrough = false,
}
local Sticky = LibStub("LibSimpleSticky-1.0")
local LibWin = LibStub("LibWindow-1.1")
local LAB = LibStub("LibActionButton-1.0")
local snapBars = { WorldFrame, UIParent }
local barOnEnter, barOnLeave, barOnDragStart, barOnDragStop, barOnClick, barOnUpdateFunc, barOnAttributeChanged
do
function barOnEnter(self)
if not self:GetParent().isMoving then
self:SetBackdropBorderColor(0.25, 1.0, 0.25, 1)
end
end
function barOnLeave(self)
self:SetBackdropBorderColor(0, 0, 0, 0)
end
local function barReAnchorForSnap(self)
local x,y,anchor = nil, nil, self:GetAnchor()
if self.config.position.growHorizontal == "RIGHT" then
x = self:GetLeft()
elseif self.config.position.growHorizontal == "LEFT" then
x = self:GetRight()
elseif self.config.position.growHorizontal == "BOTH" then
x = self:GetCenter()
end
y = (self.config.position.growVertical == "DOWN") and self:GetTop() or self:GetBottom()
self:ClearSetPoint(anchor, UIParent, "BOTTOMLEFT", x, y)
self:SetWidth(self.overlay:GetWidth())
self:SetHeight(self.overlay:GetHeight())
end
local function barReAnchorNormal(self)
local x,y,anchor = nil, nil, self:GetAnchor()
if self.config.position.growHorizontal == "RIGHT" then
x = self:GetLeft()
elseif self.config.position.growHorizontal == "LEFT" then
x = self:GetRight()
elseif self.config.position.growHorizontal == "BOTH" then
x = self:GetCenter()
end
y = (self.config.position.growVertical == "DOWN") and self:GetTop() or self:GetBottom()
self:ClearSetPoint(anchor, UIParent, "BOTTOMLEFT", x, y)
self:SetWidth(1)
self:SetHeight(1)
end
function barOnDragStart(self)
local parent = self:GetParent()
if Bartender4.db.profile.snapping then
local offset = 8 - (parent.config.padding or 0)
-- we need to re-anchor the bar and set its proper width for snaping to work properly
barReAnchorForSnap(parent)
Sticky:StartMoving(parent, snapBars, offset, offset, offset, offset)
else
parent:StartMoving()
end
self:SetBackdropBorderColor(0, 0, 0, 0)
parent.isMoving = true
end
function barOnDragStop(self)
local parent = self:GetParent()
if parent.isMoving then
if Bartender4.db.profile.snapping then
local sticky, stickTo = Sticky:StopMoving(parent)
barReAnchorNormal(parent)
--Bartender4:Print(sticky, stickTo and stickTo:GetName() or nil)
else
parent:StopMovingOrSizing()
end
parent:SavePosition()
parent.isMoving = nil
end
end
function barOnClick(self)
-- TODO: Hide/Show bar on Click
-- TODO: Once dropdown config is stable, show dropdown on rightclick
end
function barOnUpdateFunc(self, elapsed)
self.elapsed = self.elapsed + elapsed
if self.elapsed > self.config.fadeoutdelay then
self:ControlFadeOut()
self.elapsed = 0
end
end
function barOnAttributeChanged(self, attribute, value)
if attribute == "fade" then
if value then
self:SetScript("OnUpdate", barOnUpdateFunc)
self:ControlFadeOut(true)
else
self:SetScript("OnUpdate", nil)
self.faded = nil
self:SetConfigAlpha()
end
end
end
end
local barregistry = {}
Bartender4.Bar = {}
Bartender4.Bar.defaults = defaults
Bartender4.Bar.prototype = Bar
Bartender4.Bar.barregistry = barregistry
function Bartender4.Bar:Create(id, config, name, level)
id = tostring(id)
assert(not barregistry[id], "duplicated entry in barregistry.")
local bar = setmetatable(CreateFrame("Frame", ("BT4Bar%s"):format(id), UIParent, "SecureHandlerStateTemplate"), Bar_MT)
barregistry[id] = bar
bar.id = id
bar.name = name or id
bar.config = config
bar:SetMovable(true)
bar:HookScript("OnAttributeChanged", barOnAttributeChanged)
bar:SetWidth(1)
bar:SetHeight(1)
bar:SetFrameLevel(level or 5)
local overlay = CreateFrame("Button", bar:GetName() .. "Overlay", bar, BackdropTemplateMixin and "BackdropTemplate" or nil)
bar.overlay = overlay
overlay.bar = bar
table_insert(snapBars, overlay)
overlay:EnableMouse(true)
overlay:RegisterForDrag("LeftButton")
overlay:RegisterForClicks("LeftButtonUp")
overlay:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
tile = true,
tileSize = 16,
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
edgeSize = 16,
insets = {left = 5, right = 3, top = 3, bottom = 5}
})
overlay:SetBackdropColor(0.25, 1.0, 0.25, 0.75)
overlay:SetBackdropBorderColor(0, 0, 0, 0)
overlay.Text = overlay:CreateFontString(nil, "ARTWORK")
overlay.Text:SetFontObject(GameFontNormal)
overlay.Text:SetText(name)
overlay.Text:Show()
overlay.Text:ClearAllPoints()
overlay.Text:SetPoint("CENTER", overlay, "CENTER")
overlay:SetScript("OnEnter", barOnEnter)
overlay:SetScript("OnLeave", barOnLeave)
overlay:SetScript("OnDragStart", barOnDragStart)
overlay:SetScript("OnDragStop", barOnDragStop)
overlay:SetScript("OnClick", barOnClick)
overlay:SetFrameLevel(1000)
bar:AnchorOverlay()
overlay:Hide()
bar.elapsed = 0
bar.hidedriver = {}
return bar
end
function Bartender4.Bar:GetAll()
return pairs(barregistry)
end
function Bartender4.Bar:ForAll(method, ...)
for _,bar in self:GetAll() do
local func = bar[method]
if func then
func(bar, ...)
end
end
end
--[[===================================================================================
Universal Bar Prototype
===================================================================================]]--
Bar.BT4BarType = "Bar"
function Bar:ApplyConfig(config)
if config then
self.config = config
end
LibWin.RegisterConfig(self, self.config.position)
self:UpgradeConfig()
if self.disabled then return end
if Bartender4.Locked then
self:Lock()
else
self:Unlock()
end
self:LoadPosition()
self:SetConfigScale()
self:SetConfigAlpha()
self:SetClickThrough()
self:InitVisibilityDriver()
end
function Bar:GetAnchor()
local anchor = (self.config.position.growVertical == "DOWN") and "TOP" or "BOTTOM"
if self.config.position.growHorizontal == "RIGHT" then
anchor = anchor .. "LEFT"
elseif self.config.position.growHorizontal == "LEFT" then
anchor = anchor .. "RIGHT"
end
return anchor
end
function Bar:AnchorOverlay()
self.overlay:ClearAllPoints()
local anchor = self:GetAnchor()
self.overlay:SetPoint(anchor, self, anchor)
end
function Bar:UpgradeConfig()
local version = self.config.version or 1
if version < 2 then
-- LibWindow migration, move scale into position
if self.config.scale then
self.config.position.scale = self.config.scale
self.config.scale = nil
end
-- LibWindow migration, update position data
do
local pos = self.config.position
self:SetScale(pos.scale)
local x, y, s = pos.x, pos.y, self:GetEffectiveScale()
local point, relPoint = pos.point, pos.relPoint
if x and y and point and relPoint then
x, y = x/s, y/s
self:ClearSetPoint(point, UIParent, relPoint, x, y)
self:SavePosition()
pos.relPoint = nil
end
end
end
if version < 3 then
-- Size adjustment is done in first SetSize
self.needSizeFix = true
end
self.config.version = Bartender4.CONFIG_VERSION
end
function Bar:Unlock()
if self.disabled or self.unlocked then return end
self.unlocked = true
self:DisableVisibilityDriver()
self:Show()
self.overlay:Show()
end
function Bar:Lock()
if self.disabled or not self.unlocked then return end
self.unlocked = nil
self:StopDragging()
self:ApplyVisibilityDriver()
self.overlay:Hide()
end
function Bar:StopDragging()
barOnDragStop(self.overlay)
end
function Bar:LoadPosition()
LibWin.RestorePosition(self)
end
function Bar:SavePosition()
LibWin.SavePosition(self)
end
function Bar:SetSize(width, height)
self.overlay:SetWidth(width)
self.overlay:SetHeight(height or width)
if self.needSizeFix then
self:SetWidth(width)
self:SetHeight(height or width)
local x, y = self:GetLeft(), self:GetTop()
self:ClearSetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x, y)
self:SetWidth(1)
self:SetHeight(1)
self:SavePosition()
self.needSizeFix = nil
end
end
function Bar:GetSize()
return self.overlay:GetSize()
end
function Bar:GetConfigAlpha()
return self.config.alpha
end
function Bar:SetConfigAlpha(alpha)
if alpha then
self.config.alpha = alpha
end
if not self.faded then
self:SetAlpha(self.config.alpha)
if self.ForAll then
self:ForAll("UpdateAlpha")
end
end
end
function Bar:GetConfigScale()
return self.config.position.scale
end
function Bar:SetConfigScale(scale)
if scale then
LibWin.SetScale(self, scale)
end
end
function Bar:GetClickThrough()
return self.config.clickthrough
end
function Bar:SetClickThrough(click)
if click ~= nil then
self.config.clickthrough = click
end
if self.ControlClickThrough then
self:ControlClickThrough()
end
end
function Bar:GetFadeOut()
return self.config.fadeout
end
function Bar:SetFadeOut(fadeout)
if fadeout ~= nil then
self.config.fadeout = fadeout
self:InitVisibilityDriver()
end
end
function Bar:GetFadeOutAlpha()
return self.config.fadeoutalpha
end
function Bar:SetFadeOutAlpha(fadealpha)
if fadealpha ~= nil then
self.config.fadeoutalpha = fadealpha
end
if self.faded then
self:SetAlpha(self.config.fadeoutalpha)
if self.ForAll then
self:ForAll("UpdateAlpha")
end
end
end
function Bar:GetFadeOutDelay()
return self.config.fadeoutdelay
end
function Bar:SetFadeOutDelay(delay)
if delay ~= nil then
self.config.fadeoutdelay = delay
end
end
local function MouseIsOverBar(bar)
local LABSpellFlyout = LAB:GetSpellFlyoutFrame()
if MouseIsOver(bar.overlay)
or (SpellFlyout and SpellFlyout:IsShown() and SpellFlyout:GetParent() and SpellFlyout:GetParent():GetParent() == bar and MouseIsOver(SpellFlyout))
or (LABSpellFlyout and LABSpellFlyout:IsShown() and LABSpellFlyout:GetParent() and LABSpellFlyout:GetParent():GetParent() == bar and MouseIsOver(LABSpellFlyout)) then
return true
end
return false
end
function Bar:ControlFadeOut(refresh)
local changed = false
if self.faded and MouseIsOverBar(self) then
self:SetAlpha(self.config.alpha)
self.faded = nil
changed = true
elseif (not self.faded or refresh) and not MouseIsOverBar(self) then
local fade = self:GetAttribute("fade")
if tonumber(fade) then
fade = min(max(fade, 0), 100) / 100
self:SetAlpha(fade)
else
self:SetAlpha(self.config.fadeoutalpha or 0)
end
self.faded = true
changed = true
end
if changed and self.ForAll then
self:ForAll("UpdateAlpha")
end
end
local directVisCond = {
pet = true,
nopet = true,
combat = true,
nocombat = true,
mounted = true,
}
function Bar:InitVisibilityDriver(returnOnly)
local tmpDriver
if returnOnly then
tmpDriver = self.hidedriver
else
UnregisterStateDriver(self, 'vis')
end
self.hidedriver = {}
self:SetAttribute("_onstate-vis", [[
if not newstate then return end
if newstate == "show" then
self:Show()
self:SetAttribute("fade", false)
elseif strsub(newstate, 1, 4) == "fade" then
self:Show()
self:SetAttribute("fade", (newstate == "fade") and true or strsub(newstate, 6))
elseif newstate == "hide" then
self:Hide()
end
]])
if self.config.visibility.custom and not returnOnly then
table_insert(self.hidedriver, self.config.visibility.customdata or "")
else
for key, value in pairs(self.config.visibility) do
if value then
if key == "always" then
table_insert(self.hidedriver, "hide")
elseif key == "possess" then
table_insert(self.hidedriver, "[possessbar]hide")
elseif key == "overridebar" then
table_insert(self.hidedriver, "[overridebar]hide")
elseif key == "vehicleui" then
table_insert(self.hidedriver, "[vehicleui]hide")
elseif key == "vehicle" then
table_insert(self.hidedriver, "[target=vehicle,exists]hide")
elseif directVisCond[key] then
table_insert(self.hidedriver, ("[%s]hide"):format(key))
elseif key == "stance" then
for k,v in pairs(value) do
if v then
table_insert(self.hidedriver, ("[stance:%d]hide"):format(k))
end
end
elseif key == "custom" or key == "customdata" then
-- do nothing
else
Bartender4:Print("Invalid visibility state: "..key)
end
end
end
end
-- always hide in petbattles
table_insert(self.hidedriver, 1, "[petbattle]hide")
-- add fallback at the end
table_insert(self.hidedriver, self.config.fadeout and "fade" or "show")
if not returnOnly then
self:ApplyVisibilityDriver()
else
self.hidedriver, tmpDriver = tmpDriver, self.hidedriver
return table_concat(tmpDriver, ";")
end
end
function Bar:ApplyVisibilityDriver()
if self.unlocked then return end
-- default state is shown
local driver = table_concat(self.hidedriver, ";")
RegisterStateDriver(self, "vis", driver)
end
function Bar:DisableVisibilityDriver()
UnregisterStateDriver(self, "vis")
self:SetAttribute("state-vis", "show")
self:Show()
end
function Bar:GetVisibilityOption(option, index)
if option == "stance" then
return self.config.visibility.stance[index]
else
return self.config.visibility[option]
end
end
function Bar:SetVisibilityOption(option, value, arg)
if option == "stance" then
self.config.visibility.stance[value] = arg
else
self.config.visibility[option] = value
end
self:InitVisibilityDriver()
end
function Bar:CopyCustomConditionals()
self.config.visibility.customdata = self:InitVisibilityDriver(true)
self:InitVisibilityDriver()
end
function Bar:Enable()
if not self.disabled then return end
self.disabled = nil
end
function Bar:Disable()
if self.disabled then return end
self:Lock()
self.disabled = true
self:UnregisterAllEvents()
self:DisableVisibilityDriver()
self:SetAttribute("state-vis", nil)
self:Hide()
end
--[[
Lazyness functions
]]
function Bar:ClearSetPoint(...)
self:ClearAllPoints()
self:SetPoint(...)
end