-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_bar.lua
326 lines (285 loc) · 10.6 KB
/
header_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
local addonName, addon = ...
setfenv(1, addon)
HeaderBar = { -- Prototype
}
function HeaderBar:new(unit, leftTag, centerTag, rightTag)
local bar = _G.setmetatable(_G.CreateFrame("Frame"), { __index = self})
return bar
end
function HeaderBar:new(unit, attributes)
return createHeaderBar(unit, attributes.mirror)
end
function createHeaderBar(unit, mirror)
local headerBar = _G.CreateFrame("Frame")
--[[
headerBar:SetBackdrop(settings.headerBarBackdrop)
headerBar:SetBackdropColor(settings.colors.background.r, settings.colors.background.g,
settings.colors.background.b, settings.colors.background.a)
--]]
headerBar.healthMissingStatusBar = _G.CreateFrame("StatusBar", nil, headerBar)
do
local statusBar = headerBar.healthMissingStatusBar
local color = settings.colors.health
statusBar:SetAllPoints()
if not mirror then
statusBar:SetReverseFill(true)
end
statusBar:SetStatusBarTexture(settings.barTexture)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(0)
statusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(0)
end
headerBar.healthStatusBar= _G.CreateFrame("StatusBar", nil, headerBar)
do
local statusBar = headerBar.healthStatusBar
statusBar:SetAllPoints()
if mirror then
statusBar:SetReverseFill(true)
end
statusBar:SetStatusBarTexture(settings.barTexture)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(1)
statusBar:SetStatusBarColor(settings.colors.background.r, settings.colors.background.g,
settings.colors.background.b, settings.colors.background.a)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(1)
end
headerBar.incomingStatusBar= _G.CreateFrame("StatusBar", nil, headerBar)
do
local statusBar = headerBar.incomingStatusBar
local color = settings.colors.incomingDark
statusBar:SetAllPoints()
if mirror then
statusBar:SetReverseFill(true)
end
statusBar:SetStatusBarTexture(settings.barTexture)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(0)
statusBar:SetStatusBarColor(color.r, color.g, color.b, color.a)
statusBar:SetMinMaxValues(0, 1)
statusBar:SetValue(0)
end
headerBar.leftFontString = headerBar.healthStatusBar:CreateFontString(nil, nil,
"NinjaKittyFontStringLeft")
headerBar.centerFontString = headerBar.healthStatusBar:CreateFontString(nil, nil,
"NinjaKittyFontStringCenter")
headerBar.rightFontString = headerBar.healthStatusBar:CreateFontString(nil, nil,
"NinjaKittyFontStringRight")
headerBar.leftFontString:SetPoint("LEFT", headerBar, "LEFT", settings.fontSpacing, 0)
headerBar.centerFontString:SetPoint("LEFT", headerBar.leftFontString, "RIGHT",
settings.fontSpacing, 0)
headerBar.centerFontString:SetPoint("RIGHT", headerBar.rightFontString, "LEFT",
settings.fontSpacing, 0)
headerBar.rightFontString:SetPoint("RIGHT", headerBar, "RIGHT", -settings.fontSpacing, 0)
--headerBar.rightFontString:SetWordWrap(false)
function headerBar:realignTags()
if self.leftFontString:IsTruncated() then
self.leftFontString:SetWidth(self:GetWidth())
end
if self.rightFontString:IsTruncated() then
self.rightFontString:SetWidth(self:GetWidth())
end
local leftTagWidth = self.leftFontString:GetStringWidth() or 0
local rightTagWidth = self.rightFontString:GetStringWidth() or 0
self.leftFontString:SetWidth(leftTagWidth + 2) -- TODO: no magic numbers.
self.rightFontString:SetWidth(rightTagWidth + 2) -- TODO: no magic numbers.
end
local function onShow(self)
self.rangeTag:enable()
self.nameTag:enable()
self.arenaIDTag:enable()
self.specTag:enable()
end
local function onHide(self)
self.rangeTag:disable()
self.nameTag:disable()
self.arenaIDTag:disable()
self.specTag:disable()
end
function headerBar:initialize(unit)
self.rangeTag = RangeTag:new(unit, function(text)
local stringWidth = self.rightFontString:GetStringWidth()
self.rightFontString:SetText(text)
if stringWidth ~= self.rightFontString:GetStringWidth() then
self:realignTags()
end
end)
do
local nameText, arenaID = "", ""
--[[
self.nameTag = NameTag:new(unit, function(text)
nameText = text
if arenaID then
self.leftFontString:SetText("[" .. arenaID .. "] " .. nameText)
else
self.leftFontString:SetText(nameText)
end
self:realignTags()
end)
self.arenaIDTag = ArenaIDTag:new(unit, function(text)
arenaID = text
if arenaID then
self.leftFontString:SetText("[" .. arenaID .. "] " .. nameText)
else
self.leftFontString:SetText(nameText)
end
self:realignTags()
end)
]]
self.nameTag = NameTag:new(unit, function(text)
nameText = text
if arenaID and arenaID ~= "" then
self.leftFontString:SetText(arenaID .. " " .. nameText)
else
self.leftFontString:SetText(nameText)
end
self:realignTags()
end)
if _G.string.match(unit, "arena") then
self.arenaIDTag = TargetTag:new(unit, function(text)
arenaID = text
if arenaID and arenaID ~= "" then
self.leftFontString:SetText(arenaID .. " " .. nameText)
else
self.leftFontString:SetText(nameText)
end
self:realignTags()
end)
else
self.arenaIDTag = TargetTag:new(unit, function(text) end)
end
end
self.specTag = SpecTag:new(unit, function(text)
self.centerFontString:SetText(text)
self:realignTags()
end)
-- The OnShow handler is not run if the frame is implicitly shown upon its creation.
self:SetScript("OnShow", onShow)
self:SetScript("OnHide", onHide)
if self:IsVisible() then
self:GetScript("OnShow")(self)
end
end
function headerBar:update(unit)
self.rangeTag:update()
self.nameTag:update()
self.arenaIDTag:update()
self.specTag:update()
local healthMax, totalAbsorbs
if _G.UnitIsConnected(unit) then
healthMax = _G.UnitHealthMax(unit)
totalAbsorbs = _G.UnitGetTotalAbsorbs(unit) and 0 -- Temporary fix. TODO: remove.
else
healthMax = _G.select(2, self.healthStatusBar:GetMinMaxValues())
totalAbsorbs = 0
end
self.healthMissingStatusBar:SetMinMaxValues(0, healthMax + totalAbsorbs)
self.healthStatusBar:SetMinMaxValues(0, healthMax + totalAbsorbs)
self:UNIT_HEALTH_FREQUENT(unit)
end
headerBar:SetScript("OnEvent", function(self, event, ...)
return self[event](self, ...)
end)
function headerBar:UNIT_HEALTH_FREQUENT(unit)
local maxValue = _G.select(2, self.healthStatusBar:GetMinMaxValues())
local healthMax
local health
local totalAbsorbs
local incomingHeals
local healAbsorbs
if _G.UnitIsConnected(unit) then
healthMax = _G.UnitHealthMax(unit)
health = _G.UnitHealth(unit)
totalAbsorbs = _G.UnitGetTotalAbsorbs(unit) and 0 -- Temporary fix. TODO: remove.
incomingHeals = _G.UnitGetIncomingHeals(unit)
healAbsorbs = _G.UnitGetTotalHealAbsorbs(unit)
else
healthMax = maxValue
health = maxValue
totalAbsorbs = 0
incomingHeals = 0
healAbsorbs = 0
end
-- UnitGetTotalHealAbsorbs() returns the amount of healing the unit will absorb without gaining
-- health. Caused by abilities like Necrotic Strike.
-- The actual amount of incoming healing. Added to the background bar but ignored by the
-- health bar.
if not incomingHeals or not healAbsorbs then
incomingHeals = 0
else
incomingHeals = incomingHeals - healAbsorbs
if incomingHeals < 0 then incomingHeals = 0 end
end
-- I don't think there's a dedicated event to inform us of a unit having died.
local color, alpha
if _G.UnitIsDeadOrGhost(unit) then
color = settings.colors.dead
alpha = color.a
--[[
elseif _G.UnitIsPlayer(unit) then
local class = (_G.select(2, _G.UnitClassBase(unit)))
color = class and settings.classColors[class] or settings.colors.healthDark
alpha = settings.colors.healthDark.a
]]
else
color = settings.colors.healthDark
alpha = settings.colors.healthDark.a
end
if _G.UnitIsDeadOrGhost(unit) then
--local color = settings.colors.dead
self.healthMissingStatusBar:SetStatusBarColor(color.r, color.g, color.b, alpha)
self.healthMissingStatusBar:SetValue(maxValue)
self.healthStatusBar:SetValue(0)
self.incomingStatusBar:SetValue((self.incomingStatusBar:GetMinMaxValues()))
else
--local color = settings.colors.healthDark
self.healthMissingStatusBar:SetStatusBarColor(color.r, color.g, color.b, alpha)
--local offset = _G.math.floor((health + totalAbsorbs) * self:GetWidth() / maxValue + 0.5)
local offset = (health + totalAbsorbs) * self:GetWidth() / maxValue
if not mirror then
self.incomingStatusBar:SetPoint("TOPLEFT", self, "TOPLEFT", offset, 0)
else
self.incomingStatusBar:SetPoint("TOPRIGHT", self, "TOPRIGHT", -offset, 0)
end
self.incomingStatusBar:SetMinMaxValues(health + totalAbsorbs, maxValue)
self.incomingStatusBar:SetValue(health + totalAbsorbs + incomingHeals)
self.healthStatusBar:SetValue(health + totalAbsorbs)
self.healthMissingStatusBar:SetValue(maxValue - health - totalAbsorbs - incomingHeals)
end
end
headerBar.UNIT_HEALTH = headerBar.UNIT_HEALTH_FREQUENT
function headerBar:UNIT_MAXHEALTH(unit)
self:update(unit)
end
function headerBar:UNIT_FACTION(unit)
self:update(unit)
end
function headerBar:UNIT_HEAL_PREDICTION(unit)
self:UNIT_HEALTH_FREQUENT(unit)
end
function headerBar:UNIT_ABSORB_AMOUNT_CHANGED(unit)
self:UNIT_MAXHEALTH(unit)
end
function headerBar:UNIT_HEAL_ABSORB_AMOUNT_CHANGED(unit)
self:UNIT_HEALTH_FREQUENT(unit)
end
function headerBar:UNIT_CONNECTION(unit)
self:update(unit)
end
function headerBar:UNIT_PHASE(unit)
self:update(unit)
end
headerBar:RegisterUnitEvent("UNIT_CONNECTION", unit)
headerBar:RegisterUnitEvent("UNIT_PHASE", unit)
headerBar:RegisterUnitEvent("UNIT_FACTION", unit)
headerBar:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", unit)
headerBar:RegisterUnitEvent("UNIT_HEALTH", unit)
headerBar:RegisterUnitEvent("UNIT_HEAL_PREDICTION", unit)
headerBar:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", unit)
headerBar:RegisterUnitEvent("UNIT_MAXHEALTH", unit)
headerBar:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", unit)
return headerBar
end
-- vim: tw=120 sts=2 sw=2 et