-
Notifications
You must be signed in to change notification settings - Fork 1
/
BarObject.lua
372 lines (303 loc) · 10.2 KB
/
BarObject.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
-- Bar appearance and interaction
local _, NeedToKnow = ...
local Bar = NeedToKnow.Bar
local String = NeedToKnow.String
NeedToKnow.BarBorder = {}
local BarBorder = NeedToKnow.BarBorder
-- local versions of frequently-used functions
local GetTime = GetTime
local SecondsToTimeAbbrev = SecondsToTimeAbbrev
-- Functions different between Retail and Classic as of 11.0.0
local function GetMySpellInfo(spell)
local info = C_Spell.GetSpellInfo(spell) -- Only in Retail
return info.name, nil, info.iconID, info.castTime, info.minRange, info.maxRange, info.spellID, info.originalIconID
end
local GetSpellInfo = GetSpellInfo or GetMySpellInfo
-- Deprecated:
local UPDATE_INTERVAL = 0.025 -- Make this an addon-wide variable?
--[[ NeedToKnow:BarMethods() ]]--
function NeedToKnow:GetBar(groupID, barID)
return _G["NeedToKnow_Group"..groupID.."Bar"..barID]
end
function NeedToKnow:UpdateBar(groupID, barID)
-- Called by BarMenu functions
NeedToKnow:GetBar(groupID, barID):Update()
end
--[[ Bar setup ]]--
function Bar:New(group, barID)
-- Called by BarGroup:Update()
bar = CreateFrame("Frame", group:GetName().."Bar"..barID, group, "NeedToKnow_BarTemplate")
bar:SetID(barID)
Mixin(bar, Bar) -- Inherit Bar:Methods()
bar:OnLoad()
return bar
end
function Bar:OnLoad()
self:RegisterForDrag("LeftButton")
self:SetScript("OnEnter", Bar.OnEnter)
self:SetScript("OnLeave", Bar.OnLeave)
self:SetScript("OnMouseUp", Bar.OnMouseUp)
self:SetScript("OnDragStart", Bar.OnDragStart)
self:SetScript("OnDragStop", Bar.OnDragStop)
self:SetScript("OnSizeChanged", Bar.OnSizeChanged)
-- Want to not need these eventually
self.bar1 = self.Texture
self.bar2 = self.Texture2
self.spark = self.Spark
self.text = self.Text
self.time = self.Time
self.vct = self.CastTime
Mixin(self.border, BarBorder)
Mixin(self.icon.border, BarBorder)
end
function Bar:SetAppearance()
-- Set bar elements that don't change in combat
-- Called by Bar:Update()
local settings = NeedToKnow.ProfileSettings
local barSettings = self.settings
self.Texture:SetTexture(NeedToKnow.LSM:Fetch("statusbar", settings.BarTexture))
self.Texture2:SetTexture(NeedToKnow.LSM:Fetch("statusbar", settings.BarTexture))
self.background:SetVertexColor(unpack(settings.BkgdColor))
self.border:SetVertexColor(unpack(settings.BkgdColor))
self.border:SetVertexColor(unpack(settings.BorderColor))
self:SetBorder()
local fontPath = NeedToKnow.LSM:Fetch("font", settings.BarFont)
if fontPath then
local outline = settings.FontOutline
if outline == 0 then
outline = nil
elseif outline == 1 then
outline = "OUTLINE"
else
outline = "THICKOUTLINE"
end
self.Text:SetFont(fontPath, settings.FontSize, outline)
self.Time:SetFont(fontPath, settings.FontSize, outline)
self.Text:SetTextColor(unpack(settings.FontColor))
self.Time:SetTextColor(unpack(settings.FontColor))
end
local time = self.Time
if barSettings.show_time then
if barSettings.TimeFormat == "Fmt_TwoUnits" then
self.FormatTime = self.FormatTimeTwoUnits
elseif barSettings.TimeFormat == "Fmt_Float" then
self.FormatTime = self.FormatTimeDecimal
else
self.FormatTime = self.FormatTimeSingle
end
time:Show()
else
time:Hide()
end
local icon = self.icon
if barSettings.show_icon then
icon.border:SetVertexColor(unpack(settings.BkgdColor))
icon.background:SetVertexColor(unpack(settings.BkgdColor))
icon:Show()
else
icon:Hide()
end
if barSettings.vct_enabled then
local castColor = barSettings.vct_color
self.CastTime:SetColorTexture(castColor.r, castColor.g, castColor.b, castColor.a)
else
self.CastTime:Hide()
end
end
function Bar:SetBorder()
-- Called by Bar:SetAppearance(), BarGroup:SetBarWidth()
local borderSize = NeedToKnow.ProfileSettings.BarPadding
self.border:SetBorderSize(borderSize)
local icon = self.icon
PixelUtil.SetPoint(icon, "TOPRIGHT", self, "TOPLEFT", -borderSize, 0)
PixelUtil.SetPoint(icon, "BOTTOMRIGHT", self, "BOTTOMLEFT", -borderSize, 0)
PixelUtil.SetWidth(icon, self:GetHeight())
icon.border:SetBorderSize(borderSize)
icon.border.right:Hide() -- For uniform border if alpha < 1
end
--[[ Bar border ]]--
function BarBorder:SetVertexColor(r, g, b, a)
for _, texture in ipairs(self.textures) do
texture:SetVertexColor(r, g, b, a)
end
end
function BarBorder:SetBorderSize(borderSize)
-- Set border size, clamped to whole pixels
PixelUtil.SetPoint(self.left, "TOPRIGHT", self, "TOPLEFT", 0, borderSize)
PixelUtil.SetPoint(self.left, "BOTTOMRIGHT", self, "BOTTOMLEFT", 0, -borderSize)
PixelUtil.SetWidth(self.left, borderSize)
PixelUtil.SetPoint(self.right, "TOPLEFT", self, "TOPRIGHT", 0, borderSize)
PixelUtil.SetPoint(self.right, "BOTTOMLEFT", self, "BOTTOMRIGHT", 0, -borderSize)
PixelUtil.SetWidth(self.right, borderSize)
PixelUtil.SetPoint(self.top, "BOTTOMLEFT", self, "TOPLEFT", 0, 0)
PixelUtil.SetPoint(self.top, "BOTTOMRIGHT", self, "TOPRIGHT", 0, 0)
PixelUtil.SetHeight(self.top, borderSize)
PixelUtil.SetPoint(self.bottom, "TOPLEFT", self, "BOTTOMLEFT", 0, 0)
PixelUtil.SetPoint(self.bottom, "TOPRIGHT", self, "BOTTOMRIGHT", 0, 0)
PixelUtil.SetHeight(self.bottom, borderSize)
end
--[[ Bar behavior ]]--
function Bar:UpdateAppearance()
-- For bar elements that can change in combat
-- Called by Bar:CheckAura()
local barSettings = self.settings
local icon = self.icon
if barSettings.show_icon and self.iconPath then
icon.texture:SetTexture(self.iconPath) -- Icon changes if bar tracks multiple spells
icon:Show()
else
icon:Hide()
end
local color = barSettings.BarColor -- Blink changes bar color
self.Texture:SetVertexColor(color.r, color.g, color.b, color.a)
self.Texture2:SetVertexColor(color.r, color.g, color.b, color.a) -- Shown for indefinite auras. Bug?
if self.max_expirationTime and self.max_expirationTime ~= self.expirationTime then
self.Texture2:Show()
else
self.Texture2:Hide()
end
if self.duration > 0 then
local duration = self.fixedDuration or self.duration
self.max_value = duration
if self.settings.vct_enabled then
self:UpdateCastTime()
end
-- Kitjan: Force an update to get all the bars to the current position (sharing code)
-- This will call UpdateCastTime again, but that seems ok
self.nextUpdate = UPDATE_INTERVAL
if self.expirationTime > GetTime() then
self:OnUpdate(0)
end
self.Time:Show()
else
-- Aura has indefinite duration
self.max_value = 1
self:SetValue(self.Texture, 1)
self:SetValue(self.Texture2, 1)
self.Time:Hide()
self.Spark:Hide()
self.CastTime:Hide()
end
end
function Bar:SetValue(barTexture, value, value0)
-- Called by Bar:OnUpdate(), Bar:OnSizeChanged(), others
-- Called very frequently. Make sure it's efficient.
-- bar.Texture2 used for user-determined max bar duration
value = math.max(value, 0)
local pct = math.min(value/self.max_value, 1)
local pct0 = 0
if value0 then
pct0 = math.min(value0/self.max_value, 1)
end
local width = (pct - pct0) * self:GetWidth()
if width < 1 then
barTexture:Hide()
else
barTexture:SetWidth(width)
barTexture:SetTexCoord(pct0, 0, pct0, 1, pct, 0, pct, 1)
barTexture:Show()
end
barTexture.cur_value = value -- So bars size properly with resized group
end
--[[ Bar config ]]--
function Bar:Unlock()
-- Make bar configurable by player
-- Called by Bar:Update()
self:Show()
self:EnableMouse(true)
self.Time:Hide()
self.Spark:Hide()
local settings = self.settings
if settings.Enabled then
self:SetAlpha(1)
else
self:SetAlpha(0.4)
end
local color = settings.BarColor
self.Texture:SetVertexColor(color.r, color.g, color.b, color.a)
self.Texture2:Hide()
self:SetUnlockedText(settings)
if settings.vct_enabled then
self.CastTime:SetWidth(self:GetWidth()/8)
self.CastTime:Show()
else
self.CastTime:Hide()
end
end
function Bar:OnEnter()
GameTooltip_SetDefaultAnchor(GameTooltip, self)
GameTooltip:AddLine(String.BAR_TOOLTIP1, HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b, 1)
GameTooltip:AddLine(String.BAR_TOOLTIP2, NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 1)
GameTooltip:Show()
end
function Bar:OnLeave()
_G["GameTooltip"]:Hide()
end
function Bar:OnDragStart()
self:GetParent():StartMoving()
end
function Bar:OnDragStop()
local group = self:GetParent()
group:StopMovingOrSizing()
group:SavePosition()
end
function Bar:OnSizeChanged()
local bar1 = self.Texture
local bar2 = self.Texture2
if ( bar1.cur_value ) then
self:SetValue(bar1, bar1.cur_value)
end
if ( bar2.cur_value ) then
self:SetValue(bar2, bar2.cur_value, bar1.cur_value)
end
end
function Bar:OnMouseUp(button)
if button == "RightButton" then
PlaySound(SOUNDKIT.U_CHAT_SCROLL_BUTTON)
NeedToKnow.BarMenu:ShowMenu(self)
end
end
--[[ Cast time ]]--
-- Note: Kitjan's VCT = "Visual Cast Time"
function Bar:UpdateCastTime()
-- Called by Bar:ConfigureVisible()
-- Called by Bar:OnUpdate() if CastTime used, so make sure it's efficent
-- Does GetSpellInfo() actually factor in haste etc?
local castWidth = 0
local barDuration = self.fixedDuration or self.duration
if barDuration then
local barWidth = self:GetWidth()
local castDuration = self:GetCastTimeDuration()
castWidth = barWidth * castDuration / barDuration
if castWidth > barWidth then
castWidth = barWidth
end
end
if castWidth > 1 then
self.CastTime:SetWidth(castWidth)
self.CastTime:Show()
else
self.CastTime:Hide()
end
end
function Bar:GetCastTimeDuration()
-- Called by Bar:UpdateCastTime()
-- Which is called by Bar:OnUpdate() if CastTime used, so make sure it's efficent
local castTime
local castDuration = 0
local spell = self.settings.vct_spell
if not spell or spell == "" then
spell = self.buffName
end
_, _, _, castTime = GetSpellInfo(spell)
if castTime then
castDuration = castTime / 1000
self.vct_refresh = true
else
self.vct_refresh = false
end
if self.settings.vct_extra then
castDuration = castDuration + self.settings.vct_extra
end
return castDuration
end