This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprogressBar.lua
794 lines (604 loc) · 16.3 KB
/
progressBar.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
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
local AddonName, Addon = ...
local Dominos = LibStub('AceAddon-3.0'):GetAddon('Dominos')
local ProgressBar = Dominos:CreateClass('Frame', Dominos.ButtonBar)
function ProgressBar:New(id, modes, ...)
local bar = ProgressBar.proto.New(self, id, ...)
if not bar.sets.display then
bar.sets.display = {
label = true,
value = true,
max = true,
bonus = true
}
end
bar.modes = modes
bar:SetFrameStrata(bar.sets.strata or 'BACKGROUND')
bar:UpdateFont()
bar:UpdateAlwaysShowText()
bar:UpdateMode(true)
return bar
end
function ProgressBar:Create(...)
local bar = ProgressBar.proto.Create(self, ...)
bar:SetFrameStrata('BACKGROUND')
bar.colors = {
base = {0, 0, 0},
bonus = {0, 0, 0, 0},
bg = {0, 0, 0, 1}
}
local bg = bar.header:CreateTexture(nil, 'BACKGROUND')
bg:SetColorTexture(0, 0, 0, 1)
bg:SetAllPoints(bar)
bar.bg = bg
local click = CreateFrame('Button', nil, bar.header)
click:SetScript('OnClick', function(_, ...) bar:OnClick(...) end)
click:SetScript('OnEnter', function(_, ...) bar:OnEnter(...) end)
click:SetScript('OnLeave', function(_, ...) bar:OnLeave(...) end)
click:RegisterForClicks('anyUp')
click:SetAllPoints(bar)
click:SetFrameStrata('LOW')
local text = click:CreateFontString(nil, 'OVERLAY', 'GameFontHighlightSmall')
text:SetPoint('CENTER')
bar.text = text
return bar
end
function ProgressBar:GetDefaults()
return {
point = 'TOP',
x = 0,
y = 0,
columns = 20,
numButtons = 20,
padW = 2,
padH = 2,
spacing = 1,
texture = 'blizzard',
font = 'Friz Quadrata TT',
display = {
label = true,
value = true,
max = true,
bonus = true
},
alwaysShowText = true,
lockMode = true
}
end
--[[ events ]]--
function ProgressBar:OnEnter()
self.text:Show()
end
function ProgressBar:OnLeave()
if not self:GetAlwaysShowText() then
self.text:Hide()
end
end
function ProgressBar:OnClick()
if self:IsModeLocked() then
self:NextMode()
else
self:SetLockMode(false)
self:NextMode()
end
end
--[[ actions ]]--
function ProgressBar:Update()
end
do
local buffer = nil
local twipe = table.wipe
local tinsert = table.insert
local tconcat = table.concat
function ProgressBar:UpdateText(label, value, max, bonus, capped)
buffer = buffer or {}
twipe(buffer)
local fn = self:CompressValues() and _G.AbbreviateLargeNumbers or _G.BreakUpLargeNumbers
if label and self:Displaying('label') then
tinsert(buffer, ('%s:'):format(label))
end
if capped then
if tostring(bonus) then
tinsert(buffer, bonus)
end
else
if self:Displaying('value') then
if self:Displaying('max') then
tinsert(buffer, ('%s / %s'):format(fn(value), fn(max)))
else
tinsert(buffer, fn(value))
end
end
if value < max and self:Displaying('remaining') then
tinsert(buffer, ('-%s'):format(fn(max - value)))
end
if tonumber(bonus) then
if bonus > 0 and self:Displaying('bonus') then
tinsert(buffer, ('(+%s)'):format(fn(bonus)))
end
elseif tostring(bonus) and self:Displaying('label') then
tinsert(buffer, ('(%s)'):format(bonus))
end
if self:Displaying('percent') and max ~= 0 then
tinsert(buffer, ('%.1f%%'):format(value / max * 100))
end
end
self:SetText(tconcat(buffer, ' '))
end
end
--[[ mode ]]--
function ProgressBar:SetMode(mode, force)
if self:GetMode() ~= mode or force then
self.sets.mode = mode
self:OnModeChanged(self:GetMode())
end
end
function ProgressBar:GetMode()
return self.sets.mode or self.modes[1]
end
function ProgressBar:OnModeChanged(mode)
local newType = Addon.progressBarModes and Addon.progressBarModes[mode]
if newType then
newType:Bind(self)
self:Init()
end
end
function ProgressBar:UpdateMode(force)
if self:IsModeLocked() then
self:SetMode(self:GetMode(), force)
else
local newModeId = nil
for i = #self.modes, 1, -1 do
newModeId = self.modes[i]
if Addon.progressBarModes[newModeId]:IsModeActive() then
break
end
end
self:SetMode(newModeId, force)
end
end
function ProgressBar:IsModeActive()
return false
end
function ProgressBar:GetModeIndex()
local mode = self:GetMode()
for i, availableMode in ipairs(self.modes) do
if availableMode == mode then
return i
end
end
return 1
end
function ProgressBar:NextMode()
local nextIndex = self:GetModeIndex() + 1
if nextIndex > #self.modes then
nextIndex = 1
end
self:SetMode(self.modes[nextIndex])
end
function ProgressBar:GetCurrentModeIndex()
return self.modeIndex or 1
end
function ProgressBar:SetLockMode(lock)
if self:IsModeLocked() ~= lock then
self.sets.lockMode = lock
self:UpdateLockMode()
end
end
function ProgressBar:IsModeLocked()
return self.sets.lockMode or #self.modes <= 1
end
function ProgressBar:UpdateLockMode()
self:UpdateMode()
end
--[[ value display ]]--
function ProgressBar:SetValues(value, max, bonus)
local valueChanged = false
local maxChanged = false
local max = math.max(tonumber(max) or 0, 1)
if self.max ~= max then
self.max = max
maxChanged = true
end
local value = math.min(math.max(tonumber(value) or 0, 0), max)
if self.value ~= value then
self.value = value
valueChanged = true
end
local bonusChanged = false
local bonus = tonumber(bonus) or 0
if self.bonus ~= bonus then
self.bonus = bonus
bonusChanged = true
end
if valueChanged or maxChanged then
self:UpdateValue()
end
if valueChanged or bonusChanged or maxChanged then
self:UpdateBonusValue()
end
return self
end
function ProgressBar:GetValues()
return self.value or 0, self.max or 0, self.bonus or 0
end
function ProgressBar:UpdateValue()
local value, max = self:GetValues()
value = math.min(value, max)
local segmentValue = max / self:GetSegmentCount()
local lastFilledIndex = floor(value / segmentValue)
local remainder = floor(100 * (value % segmentValue) / (segmentValue * 1.0) + 0.5)
for i, segment in pairs(self.buttons) do
if i <= lastFilledIndex then
segment.value:SetValue(100)
elseif i == lastFilledIndex + 1 then
segment.value:SetValue(remainder)
else
segment.value:SetValue(0)
end
end
end
function ProgressBar:UpdateBonusValue()
local value, max, bonus = self:GetValues()
local bonus = bonus > 0 and math.min(value + bonus, max) or bonus
local segmentValue = max / self:GetSegmentCount()
local lastFilledIndex = floor(bonus / segmentValue)
local remainder = floor(100 * (bonus % segmentValue) / (segmentValue * 1.0) + 0.5)
for i, segment in pairs(self.buttons) do
if i <= lastFilledIndex then
segment.bonus:SetValue(100)
elseif i == lastFilledIndex + 1 then
segment.bonus:SetValue(remainder)
else
segment.bonus:SetValue(0)
end
end
end
--[[ text display ]]--
function ProgressBar:SetText(text, ...)
if select('#', ...) > 0 then
self.text:SetFormattedText(text, ...)
else
self.text:SetText(text, ...)
end
return self
end
function ProgressBar:GetText()
return self.text:GetText()
end
--[[ coloring ]]--
function ProgressBar:SetColor(r, g, b, a)
local colors = self.colors.base
colors[1] = tonumber(r) or 0
colors[2] = tonumber(g) or 0
colors[3] = tonumber(b) or 0
colors[4] = tonumber(a) or 1
local r, g, b, a = self:GetColor()
for i, bar in pairs(self.buttons) do
bar.bg:SetVertexColor(r / 2, g / 2, b / 2, a / 2)
bar.value:SetStatusBarColor(r, g, b, a)
end
return self
end
function ProgressBar:GetColor()
local r, g, b, a = unpack(self.colors.base)
return r or 0, g or 0, b or 0, a or 1
end
function ProgressBar:SetBonusColor(r, g, b, a)
local colors = self.colors.bonus
colors[1] = tonumber(r) or 0
colors[2] = tonumber(g) or 0
colors[3] = tonumber(b) or 0
colors[4] = tonumber(a) or 1
local r, g, b, a = self:GetBonusColor()
for i, bar in pairs(self.buttons) do
bar.bonus:SetStatusBarColor(r, g, b, a)
end
return self
end
function ProgressBar:GetBonusColor()
local r, g, b, a = unpack(self.colors.bonus)
return r or 0, g or 0, b or 0, a or 1
end
function ProgressBar:SetBackgroundColor(r, g, b, a)
local colors = self.colors.bg
colors[1] = tonumber(r) or 0
colors[2] = tonumber(g) or 0
colors[3] = tonumber(b) or 0
colors[4] = tonumber(a) or 1
self.bg:SetVertexColor(self:GetBackgroundColor())
return self
end
function ProgressBar:GetBackgroundColor()
local r, g, b, a = unpack(self.colors.bg)
return r or 0, g or 0, b or 0, a or 1
end
--[[ sizing ]]--
function ProgressBar:SetDesiredWidth(width)
self.sets.width = width
self:Layout()
end
function ProgressBar:GetDesiredWidth()
return self.sets.width or 1024
end
function ProgressBar:SetDesiredHeight(height)
self.sets.height = height
self:Layout()
end
function ProgressBar:GetDesiredHeight()
return self.sets.height or 12
end
--[[ segments ]]--
function ProgressBar:SetSegmentCount(count)
local count = tonumber(count) or 1
if count ~= self:GetSegmentCount() then
self:SetNumButtons(count)
self:UpdateValue()
self:UpdateBonusValue()
end
end
function ProgressBar:GetSegmentCount()
return self:NumButtons()
end
function ProgressBar:NumColumns()
return self:GetSegmentCount()
end
function ProgressBar:GetSegmentSize()
local width = self:GetDesiredWidth()
local height = self:GetDesiredHeight()
local numButtons = self:NumButtons()
local columns = self:NumColumns()
local rows = math.ceil(numButtons / columns)
local spacing = self:GetSpacing()
-- subtract spacing between segments
width = width - (spacing * (columns - 1))
-- divide by the number of columns
width = (width / columns)
-- subtract the spacing between segments
height = height - (spacing * (rows - 1))
-- divide height by the number of rows
height = (height / rows)
return width, height
end
--[[ status bar texture ]]---
function ProgressBar:SetTextureID(textureID)
if self.sets.texture ~= textureID then
self.sets.texture = textureID
self:UpdateTexture()
end
return self
end
function ProgressBar:GetTextureID()
return self.sets.texture
end
function ProgressBar:GetSegmentTexture()
return LibStub('LibSharedMedia-3.0'):Fetch('statusbar', self:GetTextureID())
end
function ProgressBar:UpdateTexture()
local texture = LibStub('LibSharedMedia-3.0'):Fetch('statusbar', self:GetTextureID())
for i, segment in pairs(self.buttons) do
segment.bg:SetTexture(texture)
segment.value:SetStatusBarTexture(texture)
segment.bonus:SetStatusBarTexture(texture)
end
end
--[[ text font ]]--
function ProgressBar:SetFontID(fontID)
if self.sets.font ~= fontID then
self.sets.font = fontID
self:UpdateFont()
end
return self
end
function ProgressBar:GetFontID()
return self.sets.font or 'Friz Quadrata TT'
end
function ProgressBar:UpdateFont()
local newFont = LibStub('LibSharedMedia-3.0'):Fetch('font', self:GetFontID())
local oldFont, fontSize, fontFlags = self.text:GetFont()
if newFont and newFont ~= oldFont then
self.text:SetFont(newFont, fontSize, fontFlags)
end
end
--[[ text display ]]--
-- always show text
function ProgressBar:SetAlwaysShowText(enable)
if self.sets.alwaysShowText ~= enable then
self.sets.alwaysShowText = enable
self:UpdateAlwaysShowText()
end
end
function ProgressBar:GetAlwaysShowText()
return self.sets.alwaysShowText
end
function ProgressBar:UpdateAlwaysShowText()
if self:IsMouseOver() or self:GetAlwaysShowText() then
self.text:Show()
else
self.text:Hide()
end
end
function ProgressBar:SetDisplay(part, enable)
if self.sets.display[part] ~= enable then
self.sets.display[part] = enable or nil
self:Update()
end
end
function ProgressBar:Displaying(part)
return self.sets.display[part]
end
-- compress values
function ProgressBar:SetCompressValues(enable)
if self.sets.compressValues ~= enable then
self.sets.compressValues = enable
self:Update()
end
end
function ProgressBar:CompressValues()
return self.sets.compressValues
end
--[[ rendering ]]--
function ProgressBar:Layout()
local width, height = self:GetSegmentSize()
for i, segment in pairs(self.buttons) do
segment:SetSize(width, height)
end
Dominos.ButtonBar.Layout(self)
end
do
local segmentPool = CreateFramePool('Frame')
function ProgressBar:GetButton(index)
local segment = segmentPool:Acquire()
if not segment.value then
local bg = segment:CreateTexture(nil, 'ARTWORK')
bg:SetAllPoints(segment)
segment.bg = bg
local bonus = CreateFrame('StatusBar', nil, segment)
bonus:SetMinMaxValues(0, 100)
bonus:SetValue(0)
bonus:EnableMouse(false)
bonus:SetAllPoints(segment)
segment.bonus = bonus
local value = CreateFrame('StatusBar', nil, bonus)
value:SetMinMaxValues(0, 100)
value:SetValue(0)
value:EnableMouse(false)
value:SetAllPoints(bonus)
segment.value = value
end
segment:SetSize(self:GetSegmentSize())
local r, g, b, a = self:GetColor()
segment.bg:SetTexture(self:GetSegmentTexture())
segment.bg:SetVertexColor(r / 3, g / 3, b / 3, a)
segment.value:SetStatusBarTexture(self:GetSegmentTexture())
segment.value:SetStatusBarColor(self:GetColor())
segment.bonus:SetStatusBarTexture(self:GetSegmentTexture())
segment.bonus:SetStatusBarColor(self:GetBonusColor())
return segment
end
end
--[[ menu ]]--
do
function ProgressBar:CreateMenu()
local menu = Dominos:NewMenu(self.id)
self:AddLayoutPanel(menu)
self:AddTextPanel(menu)
self:AddTexturePanel(menu)
self:AddFontPanel(menu)
self.menu = menu
return menu
end
function ProgressBar:AddLayoutPanel(menu)
local panel = menu:NewPanel(LibStub('AceLocale-3.0'):GetLocale('Dominos-Config').Layout)
local l = LibStub('AceLocale-3.0'):GetLocale('Dominos-Progress')
panel.segmentedCheckbox = panel:NewCheckButton{
name = l.Segmented,
get = function()
return panel.owner:GetSegmentCount() > 1
end,
set = function(_, enable)
local segmentCount = enable and 20 or 1
panel.owner:SetSegmentCount(segmentCount)
end
}
panel.widthSlider = panel:NewSlider{
name = l.Width,
min = 1,
max = function()
return math.ceil(_G.UIParent:GetWidth() / panel.owner:GetScale())
end,
get = function()
return panel.owner:GetDesiredWidth()
end,
set = function(_, value)
panel.owner:SetDesiredWidth(value)
end,
}
panel.heightSlider = panel:NewSlider{
name = l.Height,
min = 1,
max = function()
return math.ceil(_G.UIParent:GetHeight() / panel.owner:GetScale())
end,
get = function()
return panel.owner:GetDesiredHeight()
end,
set = function(_, value)
panel.owner:SetDesiredHeight(value)
end,
}
panel.spacingSlider = panel:NewSpacingSlider()
panel.paddingSlider = panel:NewPaddingSlider()
panel.scaleSlider = panel:NewScaleSlider()
panel.opacitySlider = panel:NewOpacitySlider()
panel.fadeSlider = panel:NewFadeSlider()
end
function ProgressBar:AddTextPanel(menu)
local l = LibStub('AceLocale-3.0'):GetLocale('Dominos-Progress')
local panel = menu:NewPanel(_G.DISPLAY)
if #self.modes > 1 then
panel:NewCheckButton{
name = l.AutoSwitchModes,
get = function()
return not panel.owner:IsModeLocked()
end,
set = function(_, enable)
panel.owner:SetLockMode(not enable)
end
}
end
panel:NewCheckButton{
name = l.AlwaysShowText,
get = function()
return panel.owner:GetAlwaysShowText()
end,
set = function(_, enable)
panel.owner:SetAlwaysShowText(enable)
end
}
panel:NewCheckButton{
name = l.CompressValues,
get = function()
return panel.owner:CompressValues()
end,
set = function(_, enable)
panel.owner:SetCompressValues(enable)
end
}
for _, part in ipairs{'label', 'value', 'max', 'bonus', 'remaining', 'percent'} do
panel:NewCheckButton{
name = l['Display_' .. part],
get = function() return panel.owner:Displaying(part) end,
set = function(_, enable) panel.owner:SetDisplay(part, enable) end
}
end
return panel
end
function ProgressBar:AddFontPanel(menu)
local l = LibStub('AceLocale-3.0'):GetLocale('Dominos-Progress')
local panel = menu:NewPanel(l.Font)
panel.fontSelector = Dominos.Options.FontSelector:New{
parent = panel,
get = function()
return panel.owner:GetFontID()
end,
set = function(_, value)
panel.owner:SetFontID(value)
end,
}
end
function ProgressBar:AddTexturePanel(menu)
local l = LibStub('AceLocale-3.0'):GetLocale('Dominos-Progress')
local panel = menu:NewPanel(l.Texture)
panel.textureSelector = Dominos.Options.TextureSelector:New{
parent = panel,
get = function()
return panel.owner:GetTextureID()
end,
set = function(_, value)
panel.owner:SetTextureID(value)
end,
}
end
end
Addon.ProgressBar = ProgressBar
Addon.progressBarModes = Addon.progressBarModes or {}