-
Notifications
You must be signed in to change notification settings - Fork 9
/
Bar.lua
executable file
·559 lines (503 loc) · 14.7 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
local ChocolateBar = LibStub("AceAddon-3.0"):GetAddon("ChocolateBar")
local LSM = LibStub("LibSharedMedia-3.0")
local Bar = ChocolateBar.Bar
local chocolate = ChocolateBar.ChocolatePiece
local debug = ChocolateBar and ChocolateBar.Debug or function() end
local jostle = ChocolateBar.Jostle
local jostle2 = ChocolateBar.Jostle2
local _G, pairs, ipairs, table, math, mod = _G, pairs, ipairs, table, math, mod
local CreateFrame, UIParent = CreateFrame, UIParent
local db
function Bar:OnMouseUp(button)
if (db.combathidebar or self.settings.hideBarInCombat) and ChocolateBar.InCombat then return end
if button == "RightButton" then
if db.disableoptons and ChocolateBar.InCombat then return end
if db.barRightClick == "OPTIONS" then
ChocolateBar:LoadOptions()
elseif db.barRightClick == "BLIZZ" then
ChocolateBar:LoadOptions(nil, nil, true)
end
else
if db.moreBar == self:GetName() then
self:Hide()
end
end
end
function Bar:New(name, settings, database)
db = database
local frame = CreateFrame("Frame",name, _G.UIParent, BackdropTemplateMixin and "BackdropTemplate")
frame.chocolist = {} --create list of chocolate chocolist in the bar
-- add class methods to frame object
for k, v in pairs(Bar) do
frame[k] = v
end
frame:SetPoint("TOPLEFT",-1,1);
--frame:SetPoint("TOPLEFT", settings.xoff, settings.yoff);
--frame:SetClampedToScreen(true)
if settings.width == 0 then
frame:ClearAllPoints()
frame:SetPoint("TOPLEFT","UIParent",-1,1);
frame:SetPoint("RIGHT", "UIParent" ,"RIGHT",0, 0);
else
frame:SetPoint("RIGHT", "UIParent",0, 0);
frame:SetWidth(settings.width)
end
frame:SetHeight(db.height)
frame:EnableMouse(true)
frame:SetScript("OnEnter", function(self)
if (db.combathidebar or settings.hideBarInCombat) and ChocolateBar.InCombat then return end
self:ShowAll()
end)
frame:SetScript("OnLeave", function(self)
if (db.combathidebar or settings.hideBarInCombat) and ChocolateBar.InCombat then return end
if self.autohide then
self:HideAll()
end
end)
--frame:SetScript("OnMouseUp", function(self, button)
--
--end)
frame:SetScript("OnMouseUp", self.OnMouseUp)
frame.settings = settings
frame.autohide = settings.hideonleave
frame:UpdateTexture(db)
frame:UpdateColors(db)
frame:UpdateStrata(db)
return frame
end
function Bar:UpdateStrata(db)
self:SetFrameStrata(db.strata)
end
function Bar:UpdateAutoHide(db)
if self.settings.autohide then
self.autohide = true
self:HideAll()
if jostle then jostle:Unregister(self) end
if jostle2 then jostle2:Unregister(self) end
else
self.autohide = false
self:ShowAll()
self:UpdateJostle(db)
end
end
function Bar:UpdateJostle(db)
if ChocolateBar:IsRetail() then
if jostle2 then
jostle2:Unregister(self)
if db.moveFrames then
if self.settings.align == "bottom" then
jostle2:RegisterBottom(self)
elseif self.settings.align == "top" then
jostle2:RegisterTop(self)
end
end
end
else
if jostle then
jostle:Unregister(self)
if db.moveFrames then
if self.settings.align == "bottom" then
jostle:RegisterBottom(self)
elseif self.settings.align == "top" then
jostle:RegisterTop(self)
end
end
end
end
end
function Bar:UpdateScale(db)
self.scale = db.scale
self:SetScale(db.scale)
self:UpdateJostle(db)
end
function Bar:UpdateHeight(db)
local height = db.height
self.height = height
self:SetHeight(height)
ChocolateBar:UpdateChoclates("updateSettings")
db.fontSize = height - 8
ChocolateBar:UpdateChoclates("updatefont")
self:UpdateJostle(db)
end
function Bar:UpdateColors(db)
local bg = db.background
local color = bg.borderColor
self:SetBackdropBorderColor(color.r,color.g,color.b,color.a)
color = bg.color
self:SetBackdropColor(color.r,color.g,color.b,color.a)
end
function Bar:UpdateTexture(db)
--local background = LSM:Fetch("statusbar", db.background.texture)
local bg = {
bgFile = db.background.texture,
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = db.background.tile, tileSize = db.background.tileSize, edgeSize = db.background.edgeSize,
insets = { left = 0, right = 0, top = 0, bottom = 0}
}
--db.barSettings[name].align == "bottom" then
--bg.bgFile = background
self:SetBackdrop(bg);
self:UpdateColors(db)
end
function Bar:ResetDrag(choco, name)
self.chocolist[name] = choco
self.dummy:Hide()
self.dummy = nil
choco:SetAlpha(1)
self:UpdateBar()
end
-- add some chocolate to a bar
function Bar:AddChocolatePiece(choco, name, noupdate)
local chocolist = self.chocolist
if chocolist[name] then
debug("Bar:AddChocolatePiece: ",name," already in list.")
return
end
chocolist[name] = choco
choco:SetParent(self)
choco.bar = self
local settings = choco.settings
settings.barName = self:GetName()
if not noupdate then
self:UpdateBar()
end
if self:GetAlpha() < 1 then
choco.text:Hide()
if choco.icon then
choco.icon:Hide()
end
end
end
-- eat some chocolate from a ChocolateBar
function Bar:EatChocolatePiece(name)
self.chocolist = self.chocolist or {}
local choco = self.chocolist[name]
if choco then
choco:Hide()
self.chocolist[name] = nil
self:UpdateBar()
end
end
function Bar:HideAll()
self:SetAlpha(0)
for k, v in pairs(self.chocolist) do
v.text:Hide()
if v.icon then
v.icon:Hide()
end
end
end
function Bar:ShowAll()
--Timer:SetScript("OnUpdate", nil)
self:SetAlpha(1)
local settings
for k, v in pairs(self.chocolist) do
settings = v.settings
--v:Show()
if settings.showText then
v.text:Show()
end
if settings.showIcon and v.icon then
v.icon:Show()
end
end
end
function Bar:Disable()
self:Hide()
if jostle then jostle:Unregister(self) end
if jostle2 then jostle2:Unregister(self) end
end
local function SortTab(tab)
local templeft = {}
local tempright = {}
local tempcenter = {}
for k,v in pairs(tab) do
local index = v.settings.index or 500
if v.settings.align == "left" then
table.insert(templeft,{v,index})
elseif v.settings.align == "center" then
table.insert(tempcenter,{v,index})
else
table.insert(tempright,{v,index})
end
end
table.sort(templeft, function(a,b)return a[2] < b[2] end)
table.sort(tempcenter, function(a,b)return a[2] < b[2] end)
table.sort(tempright, function(a,b)return a[2] < b[2] end)
return templeft, tempcenter, tempright
end
local function SortList(list, side)
local temp = {}
for k,v in pairs(list) do
local index = v.settings.index or 500
if v.settings.align == side then
table.insert(temp,{v,index})
end
end
table.sort(temp, function(a,b)return a[2] < b[2] end)
return temp
end
function Bar:GetChocolateAtCursor()
local s = self:GetEffectiveScale()
local x, y = GetCursorPosition()
x = x/s
for k, v in pairs(self.chocolist) do
if x > v:GetLeft() and x < v:GetRight() then --plugin found
if x < v:GetLeft()+v:GetWidth()/2 then
return v,"left"
else
return v,"right"
end
end
end
-- cursor over bar
local left = self.chocoMostLeft and self.chocoMostLeft:GetRight() or self:GetLeft()
local right = self.chocoMostRight and self.chocoMostRight:GetLeft() or self:GetRight()
local centerL = self.chocoCenterLeft and self.chocoCenterLeft:GetLeft() or self:GetWidth()/2
local centerR = self.chocoCenterRight and self.chocoCenterRight:GetRight() or self:GetWidth()/2
local nameCenterLeft = self.chocoCenterLeft and self.chocoCenterLeft:GetName()
local nameCenterRight = self.chocoCenterRight and self.chocoCenterRight:GetName()
if x < 8 then
return nil, "left" , "left" --left half on left side
end
if x > UIParent:GetWidth()-8 then
return nil, "right", "right"
end
local centerPos = "center"
-- nocenter
if left > centerR then
if x < right/2+left/2 then
return self.chocoMostLeft, "right", "left" --left half on right side
else
return self.chocoMostRight, "left" , "right" --right half on right side,
end
end
if right < centerL then
if x < right/2+left/2 then
return self.chocoMostLeft, "right" , "left" --left half on left side
else
return self.chocoMostRight, "left" , "right" --right half on left side
end
end
-- with center
if x < centerL then
if x < centerL/2+left/2 then
return self.chocoMostLeft, "right" , "left" --left half on left side
else
return self.chocoCenterLeft, "left" , "center" --right half on left side
end
end
if x > centerR and x < right then
if x < right/2+centerR/2 then
return self.chocoCenterRight, "right", "center" --left half on right side
else
return self.chocoMostRight, "left" , "right" --right half on right side,
end
end
return nil, nil
end
--create a copy of the choco
local function createDummy(self, choco, name)
local dummy = self.dummy
if not dummy then
--dummy = CreateFrame("Frame", "ChocolateDummy", self)
dummy = chocolate:New("dummy", choco.obj, choco.settings, ChocolateBar.db.profile)
dummy:SetParent(self)
--dummy:SetAllPoints(chocolate.frame)
dummy.name = "dummy"
dummy:SetAlpha(0.5)
dummy.bar = choco.bar
self.dummy = dummy
end
dummy:Show()
dummy:SetWidth(choco:GetWidth())
dummy:SetHeight(choco:GetHeight())
dummy.settings.index = choco.settings.index
dummy.settings.align = choco.settings.align
self.chocolist[name] = dummy --replace original with dummy to free the original
end
function Bar:UpdateDragChocolate()
local choco, side, align = self:GetChocolateAtCursor()
self.pointer = self.pointer or ChocolateBar:GetPointer(self)
local pointer = self.pointer
if choco then
pointer.align = choco.settings.align --align
local offset = 0.5
if choco.settings.align == "right" then -- the right
offset = -0.5
end
if side == "left" then
pointer.index = choco.settings.index - offset
pointer:SetPoint("TOPLEFT",choco,0,0)
else
pointer.index = choco.settings.index + offset
pointer:SetPoint("TOPLEFT",choco,"TOPRIGHT",0,0)
--pointer:SetPoint("TOPRIGHT",choco,-5,0)
end
else
if align == "left" then
pointer.index = 0.5
pointer.align = "left"
pointer:SetPoint("TOPLEFT",self,6,-1)
elseif align == "right" then
pointer.index = 0.5
pointer.align = "right"
pointer:SetPoint("TOPLEFT",self,"TOPRIGHT",0,0)
--pointer:SetPoint("TOPRIGHT",choco,-5,0)
else
pointer.index = 1
pointer.align = "center"
--pointer:SetPoint("CENTER",self,"CENTER",0,0)
pointer:SetPoint("TOPLEFT",self,self:GetWidth()/2,0)
end
end
pointer:Show()
end
function Bar:Drag(name)
local choco = self.chocolist[name]
choco:SetAlpha(0.8)
--if choco.OnLeave then choco:OnLeave() end
createDummy(self, choco, name)
self.pointer = ChocolateBar:GetPointer(self)
self:UpdateBar(true)
end
function Bar:Drop(choco, pos)
local settings = choco.settings
settings.index = self.pointer.index
settings.align = self.pointer.align
choco:SetAlpha(1)
self.pointer:Hide()
local oldbar = ChocolateBar:GetBar(settings.barName)
--check if droped from different bar
if oldbar == self then -- same bar
self.dummy:Hide()
self.dummy = nil
self.chocolist[choco.obj.name] = choco --replace dummy with original
else -- cross bars
oldbar.chocolist[choco.obj.name] = nil --remove from oldbar
oldbar.dummy:Hide()
oldbar.dummy = nil
oldbar:UpdateBar(true)
self:AddChocolatePiece(choco, choco.obj.name)
end
self:UpdateBar(true)
end
function Bar:LoseFocus(name)
self.pointer:Hide()
end
function Bar:GetFocus(name)
local choco = ChocolateBar:GetChocolate(name)
ChocolateBar:GetPointer(self)
end
-- rearange all chocolate chocolist in a given bar
-- called only when chocolates are added, removed or moved
function Bar:UpdateBar(updateindex)
local chocolates = self.chocolist
-- set left plugins
local tempList = SortList(chocolates, "left")
self.chocoMostLeft = tempList[#tempList] and tempList[#tempList][1]
local yoff = 0
local relative = nil
for i, v in ipairs(tempList) do
local choco = v[1]
choco:ClearAllPoints()
if(relative)then
choco:SetPoint("TOPLEFT",relative,"TOPRIGHT", 0,0)
choco:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
choco:SetPoint("TOP",self,0,0)
else
choco:SetPoint("TOPLEFT",self, 6,yoff)
choco:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
choco:SetPoint("TOP",self,0,0)
end
if updateindex then
choco.settings.index = i
end
relative = choco
end
-- set center plugins
self.listCenter = SortList(chocolates, "center")
local listCenter = self.listCenter
self.chocoCenterLeft = listCenter[1] and listCenter[1][1]
self.chocoCenterRight = listCenter[#listCenter] and listCenter[#listCenter][1]
local centerIndex = math.ceil(#listCenter/2)
local v = listCenter[centerIndex]
local relative = nil
if v then
local centerChoco = v[1]
self.centerChoco = centerChoco
local last = nil
if centerChoco then
if mod(#listCenter,2) > 0 then --uneven
centerChoco:ClearAllPoints()
centerChoco:SetPoint("CENTER",self, 0,yoff)
centerChoco:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
else --even
centerChoco:ClearAllPoints()
centerChoco:SetPoint("RIGHT",self,"CENTER", 0,yoff)
centerChoco:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
end
local relativeR = centerChoco
for i, v in ipairs(listCenter) do
local choco = v[1]
if i <= centerIndex then
if last then
last:ClearAllPoints()
last:SetPoint("TOPRIGHT",choco,"TOPLEFT", 0,0)
last:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
end
last = choco
elseif i > centerIndex then
choco:ClearAllPoints()
choco:SetPoint("TOPLEFT",relativeR,"TOPRIGHT", 0,0)
choco:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
relativeR = choco
end
if updateindex then
choco.settings.index = i
end
end
end
self:UpdateCenter()
end
-- set right plugins
tempList = SortList(chocolates, "right")
self.chocoMostRight = tempList[#tempList] and tempList[#tempList][1]
relative = nil
for i, v in ipairs(tempList) do
local choco = v[1]
choco:ClearAllPoints()
if(relative)then
choco:SetPoint("TOPRIGHT",relative,"TOPLEFT", 0,0)
choco:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
--list them downwards
--choco:SetPoint("TOPLEFT",relative,"BOTTOMLEFT", 0,0)
else
choco:SetPoint("TOPRIGHT",self, 0,yoff)
choco:SetPoint("BOTTOM",self,"BOTTOM", 0,0)
end
if updateindex then
choco.settings.index = i
end
relative = choco
end
end
function Bar:UpdateCenter()
local centerChoco = self.centerChoco --the chocolate the others are aligend to
if not centerChoco or not db.adjustCenter then return end
local totalwidth = 0
local centerChocoPosX = 0
--get the total width of all center chocolate's and the relative position of the chocolate they are aligend to
local listCenter = self.listCenter
for i, v in ipairs(listCenter) do
local choco = v[1]
if i == math.ceil(#listCenter/2) then
centerChocoPosX = totalwidth
end
totalwidth = totalwidth + choco:GetWidth()
end
local deltaX = totalwidth/2 - centerChocoPosX
centerChoco:ClearAllPoints()
centerChoco:SetPoint("LEFT",self,"CENTER", -deltaX,0)
centerChoco:SetPoint("BOTTOM",self,"BOTTOM", -deltaX,0)
end