-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.lua
More file actions
401 lines (319 loc) · 11.5 KB
/
frame.lua
File metadata and controls
401 lines (319 loc) · 11.5 KB
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
--[[
Copyright (c) 2014 by Adam Hellberg.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
local NAME, T = ...
T.BodyguardFrame = {}
local bf = T.BodyguardFrame
local locked = true
local frame
local created = false
local function Create()
if created then return end
-- A global name required for dropdown functionality
frame = CreateFrame("Button", "BodyguardHealthFrame", UIParent, "SecureActionButtonTemplate")
-- DEBUG
bf.Frame = frame
frame:Hide()
--frame:EnableMouse(false)
frame:SetMovable(false)
frame.statusLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.statusLabel:SetWidth(70)
frame.statusLabel:SetHeight(16)
frame.statusLabel:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -5, -5)
frame.statusLabel:SetText("Unknown")
frame.statusLabel:SetJustifyH("RIGHT")
frame.nameLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.nameLabel:SetWidth(100)
frame.nameLabel:SetHeight(16)
frame.nameLabel:SetPoint("TOPLEFT", frame, "TOPLEFT", 5, -5)
frame.nameLabel:SetPoint("RIGHT", frame.statusLabel, "LEFT", -5, 0)
frame.nameLabel:SetText("Bodyguard")
frame.nameLabel:SetJustifyH("LEFT")
frame.healthBar = CreateFrame("StatusBar", nil, frame)
local hb = frame.healthBar
hb:SetMinMaxValues(0, 1)
hb:SetValue(1)
hb:SetPoint("TOP", frame.nameLabel, "BOTTOM", 0, -5)
hb:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 5, 5)
hb:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -5, 5)
hb:SetStatusBarColor(0, 1, 0)
frame.healthLabel = hb:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.healthLabel:SetHeight(25)
frame.healthLabel:SetPoint("TOPLEFT", frame.healthBar, "TOPLEFT")
frame.healthLabel:SetPoint("BOTTOMRIGHT", frame.healthBar, "BOTTOMRIGHT")
frame.healthLabel:SetTextColor(1, 1, 1)
frame.healthLabel:SetText("No HP data")
created = true
bf:UpdateSettings()
end
function bf:ResetSettings()
Create()
frame:ClearAllPoints()
frame:SetWidth(200)
frame:SetHeight(60)
frame:SetScale(1)
frame:SetPoint("CENTER")
self:SaveSettings()
end
function bf:UpdateSettings()
if T.InCombat then return end
Create()
local settings = T.DB.profile.FrameSettings
frame:ClearAllPoints()
frame:SetWidth(settings.Width or 200)
frame:SetHeight(settings.Height or 60)
if settings.RelPoint then
frame:SetPoint(settings.Point or "CENTER", nil, settings.RelPoint, settings.Offset.X or 0, settings.Offset.Y or 0)
else
frame:SetPoint(settings.Point or "CENTER", settings.Offset.X or 0, settings.Offset.Y or 0)
end
if settings.Scale <= 0 then
settings.Scale = 1
end
frame:SetScale(settings.Scale)
local lsm = T.LSM
local backdrop = {
bgFile = lsm:Fetch(lsm.MediaType.BACKGROUND, settings.Backdrop.Background),
edgeFile = lsm:Fetch(lsm.MediaType.BORDER, settings.Backdrop.Border),
tile = settings.Backdrop.Tile,
edgeSize = settings.Backdrop.BorderSize,
tileSize = settings.Backdrop.TileSize,
insets = {
left = settings.Backdrop.Insets.Left,
right = settings.Backdrop.Insets.Right,
top = settings.Backdrop.Insets.Top,
bottom = settings.Backdrop.Insets.Bottom
}
}
frame:SetBackdrop(backdrop)
local bdcolor = settings.Backdrop.Color
frame:SetBackdropColor(bdcolor.R, bdcolor.G, bdcolor.B, bdcolor.A)
local bdbcolor = settings.Backdrop.BorderColor
frame:SetBackdropBorderColor(bdbcolor.R, bdbcolor.G, bdbcolor.B, bdbcolor.A)
frame.healthBar:SetStatusBarTexture(lsm:Fetch(lsm.MediaType.STATUSBAR, settings.Texture), "ARTWORK")
local fontFlags = settings.FontFlags
if fontFlags == "NONE" then fontFlags = nil end
frame.healthLabel:SetFont(lsm:Fetch(lsm.MediaType.FONT, settings.Font), settings.FontSize, fontFlags)
frame.healthLabel:SetTextColor(settings.FontColor.R, settings.FontColor.G, settings.FontColor.B, settings.FontColor.A)
frame:SetAlpha(settings.Opacity / 100)
self:SaveSettings()
end
function bf:SaveSettings()
Create()
local settings = T.DB.profile.FrameSettings
settings.Width = frame:GetWidth()
settings.Height = frame:GetHeight()
settings.Scale = frame:GetScale()
local point, _, relPoint, x, y = frame:GetPoint(1)
settings.Point = point
settings.RelPoint = relPoint
settings.Offset.X = x
settings.Offset.Y = y
--settings.Texture = hb:GetStatusBarTexture():GetTexture()
end
function bf:UpdateName(name)
Create()
frame.nameLabel:SetText(name)
if not T.DB.profile.FrameSettings.ClickThrough and not T.InCombat then
frame:SetAttribute("macrotext1", "/targetexact " .. name)
end
end
function bf:UpdateStatus(status)
Create()
local text = "Unknown"
for label, id in pairs(T.LBG.Status) do
if id == status then text = label break end
end
frame.statusLabel:SetText(text)
end
local function round(num)
return math.floor(num + 0.5)
end
local at_warn_threshold = false
local health_warnings = {5, 10, 20, 30}
local health_warns = {}
local shown_by_unlock = false
local BreakUpLargeNumbers = BreakUpLargeNumbers
local suffixes = {"k", "M"}
-- Upvalue since this is used from combat events
local format = string.format
local gsub = string.gsub
local function shorten(number)
local suffix = ""
local suffixIndex = 0
while number > 1000 and suffixIndex < #suffixes do
number = number / 1000
suffixIndex = suffixIndex + 1
suffix = suffixes[suffixIndex]
end
-- The gsub will strip trailing zeroes and decimal points when needed
-- 100.00 becomes 100, but 100.02 stays the same
return gsub(format("%.2f", number), "%.?0+$", "") .. suffix
end
local text_formatters = {
NONE = function() return "" end,
PERCENTAGE = function(health, maxHealth, pct) return format("%d%%", pct) end,
SHORT = function(health) return shorten(health) end,
LONG = function(health)
return BreakUpLargeNumbers(health)
end,
MIXED = function(health, maxHealth, pct)
return format("%s (%d%%)", shorten(health), pct)
end
}
function bf:UpdateHealthBar(health, maxHealth)
health = health or T.DB.char.Health or T.LBG:GetHealth()
maxHealth = maxHealth or T.DB.char.MaxHealth or T.LBG:GetMaxHealth()
Create()
local red = 1
local green = 1
local blue = 0
local ratio = (maxHealth > 0) and (health / maxHealth) or 0
if T.DB.profile.FrameSettings.HealthBasedColor then
if ratio > 0.5 then
red = 2 - ratio * 2
elseif ratio < 0.5 then
green = ratio * 2
end
else
red = T.DB.profile.FrameSettings.CustomColor.R
green = T.DB.profile.FrameSettings.CustomColor.G
blue = T.DB.profile.FrameSettings.CustomColor.B
end
local hb = frame.healthBar
hb:SetStatusBarColor(red, green, blue)
hb:SetMinMaxValues(0, maxHealth)
hb:SetValue(health)
local percentage = round(ratio * 100)
local style = T.DB.profile.FrameSettings.HealthTextStyle
local text = text_formatters[style](health, maxHealth, percentage)
frame.healthLabel:SetText(text)
if T.LBG:GetStatus() == T.LBG.Status.Inactive then return end
if percentage <= 30 then
at_warn_threshold = true
for i = 1, #health_warnings do
local threshold = health_warnings[i]
if percentage <= threshold then
if not health_warns[i] then
if T.DB.profile.EnableWarn then
PlaySoundFile(T.LSM.Fetch(T.LSM.MediaType.SOUND, T.DB.WarnSound), "Master")
RaidNotice_AddMessage(RaidWarningFrame, ("%s @ %d%%!"):format(T.LBG:GetName(), percentage), ChatTypeInfo["RAID_WARNING"])
end
health_warns[i] = true
end
break
end
end
elseif at_warn_threshold then
at_warn_threshold = false
for i = 1, #health_warnings do
health_warns[i] = false
end
end
end
function bf:SetMenu(enabled)
Create()
if enabled then
frame:EnableMouse(true)
frame:SetAttribute("type1", "macro")
local name = T.LBG:GetName()
if name then
frame:SetAttribute("macrotext1", "/targetexact " .. name)
end
frame:SetScript("OnMouseUp", function(self, button)
if button ~= "RightButton" or T.InCombat then return end
T.Dropdown:Show(frame)
end)
else
frame:SetAttribute("type1", nil)
frame:SetScript("OnMouseUp", nil)
frame:EnableMouse(false)
end
T.DB.profile.FrameSettings.ClickThrough = not enabled
end
function bf:EnableMenu()
self:SetMenu(true)
end
function bf:DisableMenu()
self:SetMenu(false)
end
function bf:Show(force)
if T.InCombat then
T:Log("Tried to show frame in combat, queueing action.", true)
T:QueueShow()
return
end
if not force and (self:IsShowing() or not T.DB.profile.Enabled) then return end
if not force then shown_by_unlock = false end
Create()
self:UpdateSettings()
frame:Show()
end
function bf:Hide()
if T.InCombat then
T:Log("Tried to hide frame in combat, queueing action.", true)
T:QueueHide()
return
end
if not self:IsShowing() then return end
Create()
frame:Hide()
shown_by_unlock = false
end
function bf:IsShowing()
return created and frame:IsVisible()
end
function bf:IsLocked()
return locked
end
function bf:Unlock()
Create()
frame:SetMovable(true)
frame:SetScript("OnMouseDown", function(f) f:StartMoving() end)
frame:SetScript("OnMouseUp", function(f)
f:StopMovingOrSizing()
bf:SaveSettings()
end)
if not self:IsShowing() then
self:Show(true)
shown_by_unlock = true
end
locked = false
end
function bf:Lock()
Create()
frame:SetMovable(false)
frame:SetScript("OnMouseDown", nil)
frame:SetScript("OnMouseUp", nil)
self:SaveSettings()
locked = true
if not T.DB.profile.FrameSettings.ClickThrough then
self:EnableMenu()
else
frame:EnableMouse(false)
end
if not T.DB.profile.Enabled or not T.DB.char.HasBodyguard or shown_by_unlock then
self:Hide()
end
shown_by_unlock = false
end
function bf:EnterCombat()
if T.DB.profile.FrameSettings.UseCombatOpacity then
frame:SetAlpha(T.DB.profile.FrameSettings.CombatOpacity / 100)
end
end