forked from Schaka/ArenaLive-TBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbsorbBar.lua
228 lines (183 loc) · 6.25 KB
/
AbsorbBar.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
--[[ ArenaLive Core Functions: AbsorbBar Handler
Created by: Vadrak
Creation Date: 02.06.2013
Last Update: 06.06.2013
This file contains all relevant functions for health bars and their behaviour.
]]--
local addonName = "ArenaLiveCore";
-- Local version is said to be faster.
local ArenaLiveCore = ArenaLiveCore;
-- Set up a new handler.
local AbsorbBar = ArenaLiveCore:AddHandler("AbsorbBar", "EventCore");
-- Get the global UnitFrame handler.
local UnitFrame = ArenaLiveCore:GetHandler("UnitFrame");
-- Register the handler for all needed events.
AbsorbBar:RegisterEvent("UNIT_ABSORB_AMOUNT_CHANGED");
AbsorbBar:RegisterEvent("UNIT_MAXHEALTH");
-- Set up a table to store all absorb bars. This way it is easier to update them.
local absorbBars = {};
-- *** FRAME FUNCTIONS ***
--[[ Function: Enable
Enables the absorb bar.
]]--
local function Enable (self, elapsed)
self.enabled = true;
self.unitFrame.handlerList.absorbBar = true;
self:Update();
end
--[[ Function: Disable
Disables the absorb bar.
]]--
local function Disable(self)
self.enabled = nil;
self.unitFrame.handlerList.absorbBar = nil;
self:Update();
end
--[[ Function: Update
General update function for the absorb bar.
]]--
local function Update(self)
local unit = self.unitFrame.unit;
local healthBar = self.unitFrame.healthBar;
-- Only use absorb bar, if there is a healthbar attached to the unit frame. otherwise it would cause LUA errors.
if ( not healthBar or healthBar.lockValues or not unit or not self.enabled ) then
self:Reset();
return;
end
local absorb = UnitGetTotalAbsorbs(unit) or 0;
local health = UnitHealth(unit);
local minHealth = healthBar:GetMinMaxValues();
local maxHealth = UnitHealthMax(unit);
local oldMaxHealth;
-- If maxhealth is smaller than current health and absorb combined, we set the new maxValue to be current health plus the amount of absorb.
if ( health + absorb > maxHealth ) then
oldMaxHealth = maxHealth;
maxHealth = health + absorb;
-- The full HP indicvator is a small line to indicate the 100% HP mark, when current HP + Absobr > Max-HP
if ( self.fullHPIndicator ) then
self.fullHPIndicator:Show();
end
else
if ( self.fullHPIndicator ) then
self.fullHPIndicator:Hide();
end
end
healthBar:SetMinMaxValues(minHealth, maxHealth);
if ( absorb == 0 ) then
self:Hide();
if ( self.overlay ) then
self.overlay:Hide();
end
else
self:ClearAllPoints();
-- Set the position of the absorb bar according to reverse fill settings of the healthbar
if ( healthBar:GetReverseFill() ) then
self:SetPoint("TOPRIGHT", healthBar:GetStatusBarTexture(), "TOPLEFT", 0, 0);
self:SetPoint("BOTTOMRIGHT",healthBar:GetStatusBarTexture(), "BOTTOMLEFT", 0, 0);
else
self:SetPoint("TOPLEFT", healthBar:GetStatusBarTexture(), "TOPRIGHT", 0, 0);
self:SetPoint("BOTTOMLEFT", healthBar:GetStatusBarTexture(), "BOTTOMRIGHT", 0, 0);
end
local totalWidth, totalHeight = healthBar:GetSize();
local _, totalMax = healthBar:GetMinMaxValues();
local barWidth = (absorb / totalMax) * totalWidth;
self:SetWidth(barWidth);
self:Show();
if ( self.overlay ) then
self.overlay:SetTexCoord(0, barWidth / self.overlay.tileSize, 0, totalHeight / self.overlay.tileSize);
self.overlay:Show();
end
if ( self.fullHPIndicator and self.fullHPIndicator:IsShown() ) then
local xOffset = (oldMaxHealth / totalMax ) * totalWidth;
self.fullHPIndicator:ClearAllPoints();
if ( healthBar:GetReverseFill() ) then
self.fullHPIndicator:SetPoint("TOPRIGHT", healthBar:GetStatusBarTexture(), "TOPRIGHT", -xOffset, 0);
else
self.fullHPIndicator:SetPoint("TOPLEFT", healthBar:GetStatusBarTexture(), "TOPLEFT", xOffset, 0);
end
self.fullHPIndicator:SetHeight(healthBar:GetHeight());
end
end
end
--[[ Function: Reset
Reset the absorb bar.
]]--
local function Reset(self)
self:Hide();
if ( self.overlay ) then
self.overlay:Hide();
end
if ( self.fullHPIndicator ) then
self.fullHPIndicator:Hide();
end
end
-- *** HANDLER FUNCTIONS ***
--[[ Function: AddFrame
Sets up a statusbar to be the healthbar of a unit frame.
Arguments:
healthBar: The statusBar that will be registered as a healthbar.
unitFrame: the unit frame the healthbar belongs to.
frequentUpdates: If true, the healthbar will update itself via an OnUpdate script.
]]--
function AbsorbBar:AddFrame (absorbBar, overlay, overlayTileSize, fullHPIndicator, unitFrame)
-- Create a reference for the absorb bar inside the unit frame and vice versa.
unitFrame.absorbBar = absorbBar;
absorbBar.unitFrame = unitFrame;
-- Register the healthbar in the unit frame's handler list.
unitFrame.handlerList.absorbBar = true;
-- Create references for overlay and full HP indicator.
absorbBar.overlay = overlay;
absorbBar.fullHPIndicator = fullHPIndicator;
-- Set the absorb bar overlay to be as large as the absorb bar.
if ( overlay ) then
overlay:SetAllPoints(absorbBar);
overlay.tileSize = overlayTileSize;
end
absorbBar.Enable = Enable;
absorbBar.Disable = Disable;
absorbBar.Update = Update;
absorbBar.Reset = Reset;
-- Enable or disable the bar according to saved variables
local DBKey = "Absorb/Enabled"
local enabled = ArenaLiveCore:GetDBEntry(addonName, DBKey);
if ( enabled ) then
absorbBar:Enable();
else
absorbBar:Disable();
end
-- Set an entry in the absorb bar table.
table.insert(absorbBars, absorbBar)
end
function AbsorbBar:ToggleAll()
local DBKey = "Absorb/Enabled"
local enabled = ArenaLiveCore:GetDBEntry(addonName, DBKey);
for key, absorbBar in ipairs(absorbBars) do
if ( enabled ) then
absorbBar:Enable();
else
absorbBar:Disable();
end
end
end
--[[ Function: OnEvent
OnEvent function for the HealthBar handler.
Arguments:
event: The event that fired.
...: A list of arguments that accompany the event.
]]--
local affectedFrame;
function AbsorbBar:OnEvent (event, ...)
local unit = ...;
if ( event == "UNIT_ABSORB_AMOUNT_CHANGED" or "UNIT_MAXHEALTH" ) then
if ( UnitFrame.UnitIDTable[unit] ) then
for key, value in pairs(UnitFrame.UnitIDTable[unit]) do
if ( value and UnitFrame.UnitFrameTable[key] ) then
affectedFrame = UnitFrame.UnitFrameTable[key];
if ( affectedFrame.handlerList.absorbBar ) then
affectedFrame.absorbBar:Update();
end
end
end
end
end
end