-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsHUD.lua
191 lines (160 loc) · 4.83 KB
/
sHUD.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
local Bar = {
bars = {},
count = 0,
options = sHUDConfig
}
function Bar:new(o)
if not o.kind then
return nil
end
if not o.unitId then
return nil
end
setmetatable(o, self)
self.__index = self
o:Initialize()
o:Reset()
Bar.bars[o.unitId..o.kind] = o
Bar.count = Bar.count + 1
return o
end
function Bar:Initialize()
if self.kind == "POWER" then
local powerId, powerString = UnitPowerType(self.unitId)
self.power = powerString
end
local index, even = math.modf(Bar.count / 2)
local offsetX = Bar.options.distance + index * (Bar.options.width + Bar.options.spacing)
if even < 0.4 then
offsetX = -offsetX
end
local frame = CreateFrame("Frame", nil, UIParent)
frame:SetFrameStrata("BACKGROUND")
frame:SetWidth(Bar.options["width"])
frame:SetHeight(Bar.options["height"])
frame.texture = frame:CreateTexture()
frame.texture:SetAllPoints(frame)
frame.texture:SetTexture(0, 0, 0)
frame:SetPoint("CENTER", offsetX, 0)
self.frame = frame
local filler = CreateFrame("Frame", nil, self.frame)
filler:SetFrameStrata("BACKGROUND")
filler:SetWidth(Bar.options.width - Bar.options.padding * 2)
filler:SetHeight(Bar.options.height - Bar.options.padding * 2)
filler.texture = filler:CreateTexture()
filler.texture:SetAllPoints(filler)
filler:SetPoint("BOTTOMLEFT", Bar.options.padding, Bar.options.padding)
self.filler = filler
self:UpdateFillerColor()
end
function Bar:UpdateFillerColor()
local r = 0
local g = 0
local b = 0
if self.kind == "HEALTH" then
r, g, b = 32, 112, 1
elseif self.power == "MANA" then
r, g, b = 23, 74, 137
elseif self.power == "RAGE" then
r, g, b = 147, 9, 0
elseif self.power == "ENERGY" then
r, g, b = 147, 144, 0
elseif self.power == "RUNIC_POWER" then
r, g, b = 48, 149, 167
elseif self.power == "FOCUS" then
r, g, b = 48, 149, 167
elseif self.power == "AMMOSLOT" then
r, g, b = 48, 149, 167
elseif self.power == "FUEL" then
r, g, b = 48, 149, 167
end
self.filler.texture:SetTexture(r / 255, g / 255, b / 255)
end
function Bar:Reset()
if UnitExists(self.unitId) then
if self.kind == "POWER" then
local powerId, powerString = UnitPowerType(self.unitId)
self.power = powerString
end
self:BindFiller()
self:UpdateFillerColor()
self:UpdateFillerHeight()
self.frame:Show()
else
self.filler:UnregisterAllEvents()
self.frame:Hide()
end
end
function Bar:BindFiller()
local kind = self.power or self.kind
local kindValueEvent = "UNIT_"..kind
local kindMaxValueEvent = "UNIT_MAX"..kind
self.filler:UnregisterAllEvents()
self.filler:SetScript("OnEvent", function()
if self.unitId == arg1 and (event == kindValueEvent or event == kindMaxValueEvent) then
self:UpdateFillerHeight()
end
end)
self.filler:RegisterEvent(kindValueEvent)
self.filler:RegisterEvent(kindMaxValueEvent)
end
function Bar:UpdateFillerHeight()
local value, maxValue
if self.kind == "HEALTH" then
value = UnitHealth(self.unitId)
maxValue = UnitHealthMax(self.unitId)
else
value = UnitPower(self.unitId)
maxValue = UnitPowerMax(self.unitId)
end
local newHeight = (Bar.options.height - Bar.options.padding * 2) * value / maxValue
if newHeight < 1 then
self.filler:Hide()
else
self.filler:SetHeight(newHeight)
self.filler:Show()
end
end
function MainEventHandler()
if event == "PLAYER_TARGET_CHANGED" then
Bar.bars['targetHEALTH']:Reset()
Bar.bars['targetPOWER']:Reset()
elseif event == "UNIT_DISPLAYPOWER" then
if arg1 == "player" or arg1 == "target" then
Bar.bars[arg1..'POWER']:Reset()
end
else -- PLAYER_ENTERING_WORLD
Bar.bars['playerPOWER']:Reset()
Bar.bars['playerHEALTH']:Reset()
Bar.bars['targetPOWER']:Reset()
Bar.bars['targetHEALTH']:Reset()
end
end
function CreateBar(kind, unitId)
return Bar:new{kind=strupper(kind), unitId=unitId}
end
local overrideFrame = CreateFrame("Frame")
function sHUD_OnLoad()
if arg1 == "sHUD" then
for i,opts in ipairs(Bar.options.bars) do
CreateBar(opts[1], opts[2])
end
overrideFrame:UnregisterAllEvents();
overrideFrame:SetScript("OnEvent", MainEventHandler);
overrideFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
overrideFrame:RegisterEvent("UNIT_DISPLAYPOWER")
overrideFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
if Bar.options.perfect then
local uiScale = 768/string.match(({GetScreenResolutions()})[GetCurrentResolution()], "%d+x(%d+)")
RegisterCVar("useUiScale", 1)
RegisterCVar("uiScale", uiScale)
SetCVar("useUiScale", 1)
SetCVar("uiScale", uiScale)
end
end
end
overrideFrame:SetScript("OnEvent", sHUD_OnLoad);
overrideFrame:RegisterEvent("ADDON_LOADED");
function Debug(s, arg1, arg2, arg3)
DEFAULT_CHAT_FRAME:AddMessage(s:format(arg1, arg2, arg3))
end