-
Notifications
You must be signed in to change notification settings - Fork 9
/
Core.lua
executable file
·665 lines (579 loc) · 17 KB
/
Core.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
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
local LibStub, broker, LSM = LibStub, LibStub("LibDataBroker-1.1"), LibStub("LibSharedMedia-3.0")
local ChocolateBar = LibStub("AceAddon-3.0"):NewAddon("ChocolateBar", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("ChocolateBar")
local _G, pairs, ipairs, table, string, tostring = _G, pairs, ipairs, table, string, tostring
local select, strjoin, CreateFrame = select, strjoin, CreateFrame
local _, _, _, tocversion = GetBuildInfo()
local addonVersion = C_AddOns.GetAddOnMetadata("ChocolateBar", "Version")
ChocolateBar.Jostle = {}
ChocolateBar.Jostle2 = {}
ChocolateBar.Bar = {}
ChocolateBar.ChocolatePiece = {}
ChocolateBar.Drag = {}
ChocolateBar.modules = {}
local Drag = ChocolateBar.Drag
local Chocolate = ChocolateBar.ChocolatePiece
local Bar = ChocolateBar.Bar
local chocolateBars = {}
local chocolateObjects = {}
local db --reference to ChocolateBar.db.profile
--------
-- utility functions
--------
local function debug(...)
if ChocolateBar.db and ChocolateBar.db.char.debug then
local s = "CB:"
for i=1,select("#", ...) do
local x = select(i, ...)
s = strjoin(" ",s,tostring(x))
end
_G.DEFAULT_CHAT_FRAME:AddMessage(s)
end
end
function ChocolateBar:Debug(...)
debug(self, ...)
end
local defaults = {
profile = {
petBattleHideBars = true, combatopacity = 1, scale = 1,
height = 21, iconSize = 0.75, moveFrames = true, adjustCenter = true,
strata = "BACKGROUND", barRightClick = "OPTIONS",
gap = 7, textOffset = 1, moreBar = "none", moreBarDelay = 4,
fontPath = " ", fontSize = 12, labelColor = {r=1, g=0.82, b=0, a=1},
background = {
textureName = "ChocolateBar Gray",
texture = "Interface\\AddOns\\ChocolateBar\\pics\\chocolatebargray",
borderTexture = "Tooltip-Border",
color = {r = 0.38, g = 0.36, b = 0.4, a = .94,},
borderColor = {r = 0, g = 0, b = 0, a = 0,},
tileSize = 130,
edgeSize = 8,
barInset = 3,
},
moduleSettings = {
['*'] = {
enabled = false,
}
},
barSettings = {
['*'] = {
barName = "ChocolateBar1", align = "top", enabled = true, index = 10, width = 0,
},
['ChocolateBar1'] = {
barName = "ChocolateBar1", align = "top", enabled = true, index = 1, width = 0,
},
},
objSettings = {
['*'] = {
barName = "", align = "left", enabled = true, showText = true, showLabel = false,
showIcon = true, index = 500, width = 0, isNew = true
},
},
},
char = {
debug = false,
}
}
function ChocolateBar:IsRetail()
return WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
end
function ChocolateBar:IsClassic()
return WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
end
--------
-- Ace3 callbacks
--------
function ChocolateBar:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("ChocolateBarDB", defaults, "Default")
self.db.RegisterCallback(self, "OnDatabaseShutdown", "OnDatabaseShutdown")
self:RegisterChatCommand("chocolatebar", "ChatCommand")
db = self.db.profile
debug("ChocolateBarDB.addonVersion=", ChocolateBarDB.addonVersion)
debug("isNewInstall()=", self:isNewInstall())
local AceCfgDlg = LibStub("AceConfigDialog-3.0")
AceCfgDlg:AddToBlizOptions("ChocolateBar", "ChocolateBar")
LSM:Register("statusbar", "ChocolateBar Gold", "Interface\\AddOns\\ChocolateBar\\pics\\chocolatebar")
LSM:Register("statusbar", "ChocolateBar Gray", "Interface\\AddOns\\ChocolateBar\\pics\\chocolatebargray")
LSM:Register("statusbar", "Tooltip", "Interface\\Tooltips\\UI-Tooltip-Background")
LSM:Register("statusbar", "Solid", "Interface\\Buttons\\WHITE8X8")
LSM:Register("statusbar", "Gloss","Interface\\AddOns\\ChocolateBar\\pics\\Gloss")
LSM:Register("statusbar", "DarkBottom","Interface\\AddOns\\ChocolateBar\\pics\\DarkBottom")
LSM:Register("background", "Titan","Interface\\AddOns\\ChocolateBar\\pics\\Titan")
LSM:Register("background", "Tribal","Interface\\AddOns\\ChocolateBar\\pics\\Tribal")
self:RegisterEvent("PLAYER_REGEN_DISABLED","OnEnterCombat")
self:RegisterEvent("PLAYER_REGEN_ENABLED","OnLeaveCombat")
self:RegisterEvent("PLAYER_ENTERING_WORLD","OnEnterWorld")
if ChocolateBar:IsRetail() then
self:RegisterEvent("PET_BATTLE_OPENING_START","OnPetBattleOpen")
self:RegisterEvent("PET_BATTLE_CLOSE","OnPetBattleOver")
end
self:RegisterEvent("ADDON_LOADED",function(event, addonName)
if self[addonName] then self[addonName](self) end
end)
--fix frame strata for 8.0
if not self.db.profile.fixedStrata then
self.db.profile.strata = "BACKGROUND"
self.db.profile.fixedStrata = true
end
local barSettings = db.barSettings
for k, v in pairs(barSettings) do
local name = v.barName
self:AddBar(k, v, true) --force no anchor update
end
self:AnchorBars()
ChocolateBar:RegisterOptions(db, chocolateBars, self.modules)
ChocolateBar:EnableModules()
end
function ChocolateBar:OnEnable()
for name, obj in broker:DataObjectIterator() do
self:LibDataBroker_DataObjectCreated(nil, name, obj, true) --force noupdate on chocolateBars
end
self:UpdateBars() --update chocolateBars here
broker.RegisterCallback(self, "LibDataBroker_DataObjectCreated")
local moreChocolate = LibStub("LibDataBroker-1.1"):GetDataObjectByName("MoreChocolate")
if moreChocolate then
moreChocolate:SetBar(db)
end
end
function ChocolateBar:OnDatabaseShutdown()
ChocolateBarDB.addonVersion = addonVersion
end
function ChocolateBar:EnableModules()
-- itaret modules list and call each enable fuction
for name, module in pairs(ChocolateBar.modules) do
if db.moduleSettings[name].enabled then
ChocolateBar:EnableModule(name)
end
end
end
function ChocolateBar:EnableModule(name)
ChocolateBar.modules[name]:EnableModule()
local subModuleOptions = ChocolateBar:GetAceOptions().args.moduleOptions.args[name].args
subModuleOptions.Options = ChocolateBar.modules[name].optionsExtended
-- in case the module was enabled in this seesion before but was disabled we need to enable it again
local obj = broker:GetDataObjectByName(name)
if obj then
ChocolateBar:EnableDataObject(name, obj)
end
end
function ChocolateBar:DisableModule(name)
ChocolateBar.modules[name]:DisableModule()
ChocolateBar:DisableDataObject(name)
local subModuleOptions = ChocolateBar:GetAceOptions().args.moduleOptions.args[name].args
subModuleOptions.Options = {}
end
function ChocolateBar:GetModule(name)
local module = ChocolateBar.modules[name]
if not module then
debug("Invalide Module:", name)
end
return module
end
local function GetModuleEnabled(info)
local name = info[#info-1]
return ChocolateBar.db.profile.moduleSettings[name].enabled
end
local function SetModuleEnabled(info, value)
local name = info[#info-1]
debug("SetModuleEnabled", name)
ChocolateBar.db.profile.moduleSettings[name].enabled = value
if value then
ChocolateBar:EnableModule(name)
else
ChocolateBar:DisableModule(name)
end
end
function ChocolateBar:NewModule(name, values)
debug("NewModule Name:", name)
local module = self.modules[name] or {}
module.defaults = values.moduleDefaults
local moduleName = name
local baseOptions = {
inline = true,
name=moduleName,
type="group",
order = 2,
args={
label = {
order = 1,
type = "description",
name = values.description,
},
enabled = {
type = 'toggle',
order = 1,
name = L["Enabled"],
desc = "Toggle enable/disable this Module.",
get = GetModuleEnabled,
set = SetModuleEnabled,
},
},
}
module.options = baseOptions
module.optionsExtended = values.options
module.name = moduleName
defaults.profile.moduleSettings[name] = {}
self.modules[name] = module
return module
end
-- called on ADDON_LOADED of Blizzard_OrderHallUI
function ChocolateBar:Blizzard_OrderHallUI()
--hookOrderHallCommandBar(self)
if not self.hookedOrderHallCommandBar and db.hideOrderHallCommandBar then
local orderHallCommandBar = _G.OrderHallCommandBar
if orderHallCommandBar then
orderHallCommandBar:HookScript("OnShow", function() ChocolateBar:ToggleOrderHallCommandBar() end)
orderHallCommandBar:Hide()
self.hookedOrderHallCommandBar = true
end
end
end
function ChocolateBar:UpdateJostle()
for name, bar in pairs(chocolateBars) do
bar:UpdateJostle(db)
end
end
function ChocolateBar:isNewInstall()
local lastversion = ChocolateBarDB.addonVersion or ""
return lastversion < C_AddOns.GetAddOnMetadata("ChocolateBar", "Version") and true or false
end
function ChocolateBar:ToggleOrderHallCommandBar()
local orderHallCommandBar = _G.OrderHallCommandBar
if orderHallCommandBar then
if db.hideOrderHallCommandBar then
orderHallCommandBar:Hide()
else
orderHallCommandBar:Show()
end
end
ChocolateBar:UpdateJostle()
end
function ChocolateBar:OnDisable()
for name, obj in broker:DataObjectIterator() do
if chocolateObjects[name] then chocolateObjects[name]:Hide() end
end
for k,v in pairs(chocolateBars) do
v:Hide()
end
broker.UnregisterCallback(self, "LibDataBroker_DataObjectCreated")
end
function ChocolateBar:OnEnterWorld()
self:UpdateChoclates("resizeFrame")
self:UnregisterEvent("PLAYER_ENTERING_WORLD")
ChocolateBar:UpdateOptions(chocolateBars)
end
function ChocolateBar:OnPetBattleOpen(...)
self.InCombat = true
if db.petBattleHideBars then
for name,bar in pairs(chocolateBars) do
bar.petHide = bar:IsShown()
bar:Hide()
end
end
end
function ChocolateBar:OnPetBattleOver(...)
self.InCombat = false
if db.petBattleHideBars then
for name,bar in pairs(chocolateBars) do
if bar.petHide then
bar:Show()
bar:UpdateJostle(db)
end
end
end
end
function ChocolateBar:OnEnterCombat()
self.InCombat = true
local combatHideAllBars = db.combathidebar
local combatOpacityAllBars = db.combatopacity
for name,bar in pairs(chocolateBars) do
local settings = bar.settings
if combatHideAllBars or settings.hideBarInCombat then
bar.tempHide = bar:IsShown()
bar:Hide()
elseif combatOpacityAllBars < 1 then
bar.tempHide = bar:GetAlpha()
bar:SetAlpha(db.combatopacity)
end
end
end
function ChocolateBar:OnLeaveCombat()
self.InCombat = false
local combatHideAllBars = db.combathidebar
local combatOpacityAllBars = db.combatopacity
for name,bar in pairs(chocolateBars) do
local settings = bar.settings
if combatHideAllBars or settings.hideBarInCombat then
if bar.tempHide then
bar:Show()
end
elseif combatOpacityAllBars < 1 then
if bar.tempHide then
bar:SetAlpha(1)
end
end
end
end
--------
-- LDB callbacks
--------
function ChocolateBar:LibDataBroker_DataObjectCreated(event, name, obj, noupdate)
--debug("LibDataBroker_DataObjectCreated", name)
local t = obj.type
if t == "data source" or t == "launcher" then
if db.objSettings[name].isNew then
db.objSettings[name].isNew = false
if obj.defauldDisabled then db.objSettings[name].enabled = false end
end
if db.objSettings[name].enabled then
--debug("EnableDataObject", name)
self:EnableDataObject(name, obj, noupdate)
end
else
debug("Unknown type", t, name)
end
end
function ChocolateBar:EnableDataObject(name, obj, noupdate)
local t = obj.type
if t ~= "data source" and t ~= "launcher" then
debug("Unknown type", t, name)
return 0
end
local settings = db.objSettings[name]
settings.enabled = true
local barName = settings.barName
-- set default values depending on data source
if barName == "" then
--barName = "ChocolateBar1"
settings.barName = "ChocolateBar1"
if t and t == "data source" then
settings.align = "left"
settings.showText = true
if db.autodissource then
settings.enabled = false
return
end
if name == "ChocolateClock" or name == "Broker_uClock" then
settings.align = "right"
settings.index = -1
end
else
settings.align = "right"
settings.showText = false
if db.autodislauncher then
settings.enabled = false
return
end
end
end
obj.name = name
local choco = chocolateObjects[name] or Chocolate:New(name, obj, settings, db)
chocolateObjects[name] = choco
choco:Show()
local bar = chocolateBars[barName]
if bar then
bar:AddChocolatePiece(choco, name,noupdate)
else
chocolateBars["ChocolateBar1"]:AddChocolatePiece(choco, name,noupdate)
end
broker.RegisterCallback(self, "LibDataBroker_AttributeChanged_"..name, "AttributeChanged")
ChocolateBar:AddObjectOptions(name, obj)
end
function ChocolateBar:DisableDataObject(name)
broker.UnregisterCallback(self,"LibDataBroker_AttributeChanged_"..name)
--get bar from setings
if db.objSettings[name] then
db.objSettings[name].enabled = false
local barName = db.objSettings[name].barName
if(barName and chocolateBars[barName])then
chocolateBars[barName]:EatChocolatePiece(name)
end
end
end
function ChocolateBar:AttributeChanged(event, name, key, value)
local settings = db.objSettings[name]
if not settings.enabled then
debug("not settings.enabled")
return
end
local choco = chocolateObjects[name]
choco:Update(choco, key, value, name)
end
-- disable autohide for all bars during drag and drop
function ChocolateBar:TempDisAutohide(value)
for name,bar in pairs(chocolateBars) do
if value then
bar.tempHide = bar.autohide
bar.autohide = false
bar:ShowAll()
else
if bar.tempHide then
bar.autohide = true
bar:HideAll()
end
end
end
end
-- returns nil if the plugin is disabled
function ChocolateBar:GetChocolate(name)
return chocolateObjects[name]
end
function ChocolateBar:GetChocolates()
return chocolateObjects
end
function ChocolateBar:GetBar(name)
return chocolateBars[name]
end
function ChocolateBar:GetBars()
return chocolateBars
end
function ChocolateBar:SetBars(tab)
chocolateBars = tab or {}
end
local function getFreeBarName()
local used = false
local name
for i=1,100 do
name = "ChocolateBar"..i
for k,v in pairs(chocolateBars) do
if name == v:GetName() then
used = true
end
end
if not used then
return name
end
used = false
end
debug("no free bar name found ")
end
function ChocolateBar:UpdateChoclates(key, val)
for name,choco in pairs(chocolateObjects) do
choco:Update(choco, key, val)
end
end
function ChocolateBar:ExecuteforAllChoclates(func, ...)
for name,choco in pairs(chocolateObjects) do
func(choco, ...)
end
end
--------
-- Bars Management
--------
function ChocolateBar:AddBar(name, settings, noupdate)
if not name then --find free name
name = getFreeBarName()
end
if not settings then
settings = db.barSettings[name]
end
local bar = Bar:New(name,settings,db)
Drag:RegisterFrame(bar)
chocolateBars[name] = bar
settings.barName = name
if not noupdate then
self:AnchorBars()
end
return name, bar
end
function ChocolateBar:UpdateBars(updateindex)
for k,v in pairs(chocolateBars) do
v:UpdateBar(updateindex)
v:UpdateAutoHide(db)
end
end
-- sort and anchor all bars
function ChocolateBar:AnchorBars()
local temptop = {}
local tempbottom = {}
for k,v in pairs(chocolateBars) do
local settings = v.settings
local index = settings.index or 500
if settings.align == "top" then
table.insert(temptop,{v,index})
elseif settings.align == "bottom" then
table.insert(tempbottom,{v,index})
else
v:ClearAllPoints()
if settings.barPoint and settings.barOffx and settings.barOffy then
v:SetPoint(settings.barPoint, "UIParent",settings.barOffx ,settings.barOffy)
v:SetWidth(settings.width)
else
settings.align = "top"
table.insert(temptop,{v,index})
end
end
end
table.sort(temptop, function(a,b)return a[2] < b[2] end)
table.sort(tempbottom, function(a,b)return a[2] < b[2] end)
local yoff = 0
local relative = nil
for i, v in ipairs(temptop) do
local bar = v[1]
bar:ClearAllPoints()
if(relative)then
bar:SetPoint("TOPLEFT",relative,"BOTTOMLEFT", 0,-yoff)
bar:SetPoint("RIGHT", relative ,"RIGHT",0, 0);
else
bar:SetPoint("TOPLEFT",-1,1);
bar:SetPoint("RIGHT", "UIParent" ,"RIGHT",0, 0);
end
--if updateindex then
bar.settings.index = i
--end
relative = bar
end
local relative = nil
for i, v in ipairs(tempbottom) do
local bar = v[1]
bar:ClearAllPoints()
if(relative)then
bar:SetPoint("BOTTOMLEFT",relative,"TOPLEFT", 0,-yoff)
bar:SetPoint("RIGHT", relative ,"RIGHT",0, 0);
else
bar:SetPoint("BOTTOMLEFT",-1,0);
bar:SetPoint("RIGHT", "UIParent" ,"RIGHT",0, 0);
end
--if updateindex then
bar.settings.index = i
--end
relative = bar
end
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function onRightClick(self)
self:GetParent():OnMouseUp("RightButton")
end
local function createPointer()
pointer = CreateFrame("Frame", "ChocolatePointer")
pointer:SetFrameStrata("FULLSCREEN_DIALOG")
pointer:SetFrameLevel(20)
pointer:SetWidth(15)
local arrow = pointer:CreateTexture(nil, "BACKGROUND")
arrow:SetPoint("CENTER",pointer,"LEFT", 0, 0)
arrow:SetTexture("Interface\\AddOns\\ChocolateBar\\pics\\pointer")
return pointer
end
function ChocolateBar:GetPointer(parent)
local pointer = self.pointer or createPointer()
pointer:SetHeight(parent:GetHeight())
pointer:SetParent(parent)
return pointer
end
--------
-- option functions
--------
function ChocolateBar:ChatCommand(input)
ChocolateBar:LoadOptions(nil, input)
end
function ChocolateBar:LoadOptions(pluginName, input, blizzard)
ChocolateBar:OpenOptions(chocolateBars, db, input, pluginName, modules, blizzard)
end
function ChocolateBar:UpdateDB(data)
db = data
Chocolate:UpdateDB(db)
end