vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | |
2 | -- TODO: flashDuration = -1 to keep running forever? check blizz's code |
||
3 | |||
4 | function TNE_LowHealth_OnLoad() |
||
5 | |||
6 | local health, mana, sound = "UNIT_HEALTH", "UNIT_MANA", "SOUND" |
||
7 | |||
8 | TNE_LowHealth_Version = "2.0" |
||
9 | |||
10 | -- these are the default values |
||
11 | TNE_LowHealth_Default_Enabled = true |
||
12 | TNE_LowHealth_Default_SoundEnabled = true |
||
13 | TNE_LowHealth_Default_HealthEnabled = true |
||
14 | TNE_LowHealth_Default_ManaEnabled = true |
||
15 | TNE_LowHealth_Default_SoundCombatOnly = false |
||
16 | TNE_LowHealth_Default_HealthCombatOnly = false |
||
17 | TNE_LowHealth_Default_ManaCombatOnly = false |
||
18 | TNE_LowHealth_Default_HealthSync = false |
||
19 | TNE_LowHealth_Default_ManaSync = false |
||
20 | TNE_LowHealth_Default_HealthEmote = false |
||
21 | TNE_LowHealth_Default_Thresholds = { |
||
22 | [health] = { [1] = 40, [2] = 20 }, -- regular and critical, in percent |
||
23 | [mana] = { [1] = 40, [2] = 20 }, |
||
24 | [sound] = { [1] = 50 }, |
||
25 | } |
||
26 | |||
27 | -- set variables to their default values. these will be overriden when variables are loaded |
||
28 | TNE_LowHealth_Enabled = TNE_LowHealth_Default_Enabled |
||
29 | TNE_LowHealth_SoundEnabled = TNE_LowHealth_Default_SoundEnabled |
||
30 | TNE_LowHealth_HealthEnabled = TNE_LowHealth_Default_HealthEnabled |
||
31 | TNE_LowHealth_ManaEnabled = TNE_LowHealth_Default_ManaEnabled |
||
32 | TNE_LowHealth_SoundCombatOnly = TNE_LowHealth_Default_SoundCombatOnly |
||
33 | TNE_LowHealth_HealthCombatOnly = TNE_LowHealth_Default_HealthCombatOnly |
||
34 | TNE_LowHealth_ManaCombatOnly = TNE_LowHealth_Default_ManaCombatOnly |
||
35 | TNE_LowHealth_HealthSync = TNE_LowHealth_Default_HealthSync |
||
36 | TNE_LowHealth_ManaSync = TNE_LowHealth_Default_ManaSync |
||
37 | TNE_LowHealth_HealthEmote = TNE_LowHealth_Default_HealthEmote |
||
38 | TNE_LowHealth_Thresholds = TNE_LowHealth_Default_Thresholds |
||
39 | |||
40 | -- these values keep track of animation |
||
41 | this.states = { [health] = 0, [mana] = 0 } |
||
42 | |||
43 | -- change the blue frame to act as the red one |
||
44 | OutOfControlFrame:ClearAllPoints() |
||
45 | OutOfControlFrame:SetParent("WorldFrame") |
||
46 | OutOfControlFrame:SetAllPoints("WorldFrame") |
||
47 | |||
48 | -- set both frames transparent to avoid onload flashing |
||
49 | LowHealthFrame:SetAlpha(0) |
||
50 | OutOfControlFrame:SetAlpha(0) |
||
51 | |||
52 | -- command line |
||
53 | SlashCmdList["LOWHEALTHWARNINGCMD"] = TNE_LowHealth_CMD |
||
54 | SLASH_LOWHEALTHWARNINGCMD1 = "/lowhealthwarning" |
||
55 | SLASH_LOWHEALTHWARNINGCMD2 = "/lowhealth" |
||
56 | SLASH_LOWHEALTHWARNINGCMD3 = "/lhw" |
||
57 | |||
58 | end |
||
59 | |||
60 | |||
61 | function TNE_LowHealth_ApplySettings(leavingWorld) |
||
62 | |||
63 | -- for compatibility with older versions of saved variables |
||
64 | if (not TNE_LowHealth_Thresholds["SOUND"]) then |
||
65 | TNE_LowHealth_Thresholds["SOUND"] = { [1] = 30 } |
||
66 | end |
||
67 | |||
68 | local frame = getglobal("LowHealthWarningFrame") |
||
69 | |||
70 | -- register or unregister events (always unregister when we leave the world) |
||
71 | if (TNE_LowHealth_Enabled and not leavingWorld) then |
||
72 | frame:RegisterEvent("UNIT_DISPLAYPOWER") |
||
73 | if (TNE_LowHealth_HealthEnabled or TNE_LowHealth_SoundEnabled) then |
||
74 | frame:RegisterEvent("UNIT_HEALTH") |
||
75 | end |
||
76 | TNE_LowHealth_ManaCheck() |
||
77 | else |
||
78 | frame:UnregisterEvent("UNIT_HEALTH") |
||
79 | frame:UnregisterEvent("UNIT_HEALTH") |
||
80 | frame:UnregisterEvent("UNIT_DISPLAYPOWER") |
||
81 | TNE_LowHealth_FlashFrameStop(LowHealthFrame, "UNIT_HEALTH") |
||
82 | TNE_LowHealth_FlashFrameStop(OutOfControlFrame, "UNIT_MANA") |
||
83 | end |
||
84 | |||
85 | end |
||
86 | |||
87 | |||
88 | function TNE_LowHealth_FlashFrame(value, regular, critical, frame, state) |
||
89 | |||
90 | local REGULAR_FLASH, CRITICAL_FLASH = 1, 2 |
||
91 | |||
92 | -- disable |
||
93 | if (value > regular or TNE_LowHealth_CombatCheck(state) or UnitIsDeadOrGhost("player")) then |
||
94 | TNE_LowHealth_FlashFrameStop(frame, state) |
||
95 | -- regular flash |
||
96 | elseif (value > critical) then |
||
97 | if (not (LowHealthWarningFrame.states[state] == REGULAR_FLASH)) then |
||
98 | if (UIFrameIsFlashing(frame)) then |
||
99 | frame.flashDuration = frame.flashDuration + 10 |
||
100 | frame.fadeInTime = 0.4 |
||
101 | frame.fadeOutTime = 0.6 |
||
102 | frame.flashInHoldTime = 1 |
||
103 | else |
||
104 | UIFrameFlash(frame, 0.4, 0.6, 60, nil, 1, 0) |
||
105 | end |
||
106 | LowHealthWarningFrame.states[state] = REGULAR_FLASH |
||
107 | end |
||
108 | -- critical flash |
||
109 | else |
||
110 | if (not (LowHealthWarningFrame.states[state] == CRITICAL_FLASH)) then |
||
111 | if (TNE_LowHealth_HealthEmote and state == "UNIT_HEALTH") then DoEmote("healme") end |
||
112 | if (UIFrameIsFlashing(frame)) then |
||
113 | frame.flashDuration = frame.flashDuration + 10 |
||
114 | frame.fadeInTime = 0.2 |
||
115 | frame.fadeOutTime = 0.8 |
||
116 | frame.flashInHoldTime = 0 |
||
117 | else |
||
118 | UIFrameFlash(frame, 0.2, 0.8, 60, nil, 0, 0) |
||
119 | end |
||
120 | LowHealthWarningFrame.states[state] = CRITICAL_FLASH |
||
121 | end |
||
122 | end |
||
123 | |||
124 | end |
||
125 | |||
126 | function TNE_LowHealth_SmoothFlashFrame(value, threshold, frame, state) |
||
127 | |||
128 | -- disable |
||
129 | if (value > threshold or TNE_LowHealth_CombatCheck(state) or UnitIsDeadOrGhost("player")) then |
||
130 | TNE_LowHealth_FlashFrameStop(frame, state) |
||
131 | -- smooth flash |
||
132 | else |
||
133 | local rate = TNE_LowHealth_GetHeartRate(state) |
||
134 | local flashIn, flashOut = rate * 0.1, rate * 0.4 |
||
135 | if (UIFrameIsFlashing(frame)) then |
||
136 | frame.flashDuration = frame.flashDuration + rate + 1 |
||
137 | if (rate < 1) then |
||
138 | frame.fadeInTime = rate * 0.15 |
||
139 | frame.fadeOutTime = rate * 0.85 |
||
140 | frame.flashInHoldTime = 0 |
||
141 | else |
||
142 | frame.fadeInTime = 0.2 |
||
143 | frame.fadeOutTime = 0.8 |
||
144 | frame.flashInHoldTime = rate - 1 |
||
145 | end |
||
146 | else |
||
147 | UIFrameFlash(frame, 0.2, 0.8, 10, nil, rate -1, 0) |
||
148 | end |
||
149 | end |
||
150 | |||
151 | end |
||
152 | |||
153 | |||
154 | function TNE_LowHealth_FlashFrameStop(frame, state) |
||
155 | |||
156 | UIFrameFlashRemoveFrame(frame) |
||
157 | UIFrameFadeRemoveFrame(frame) |
||
158 | UIFrameFadeOut(frame, 0.5, frame:GetAlpha(), 0) |
||
159 | LowHealthWarningFrame.states[state] = 0 |
||
160 | |||
161 | end |
||
162 | |||
163 | |||
164 | function TNE_LowHealth_OnEvent() |
||
165 | |||
166 | if (event == "UNIT_DISPLAYPOWER") then |
||
167 | TNE_LowHealth_ManaCheck() |
||
168 | return |
||
169 | end |
||
170 | |||
171 | -- unpacks regular threshold, critical threshold, nil, nil, nil |
||
172 | local t1, t2, value, frame, smooth = unpack(TNE_LowHealth_Thresholds[event]) |
||
173 | |||
174 | -- pick frame based on event |
||
175 | if (event == "UNIT_HEALTH") then |
||
176 | value = UnitHealth("player") / UnitHealthMax("player") |
||
177 | TNE_LowHealth_SoundCheck(this, value * 100, unpack(TNE_LowHealth_Thresholds["SOUND"])) |
||
178 | if (not TNE_LowHealth_HealthEnabled) then |
||
179 | return -- this 'hack' is because both sound and health flash need this event |
||
180 | end |
||
181 | frame = LowHealthFrame |
||
182 | smooth = TNE_LowHealth_HealthSync |
||
183 | elseif (event == "UNIT_MANA") then |
||
184 | value = UnitMana("player") / UnitManaMax("player") |
||
185 | frame = OutOfControlFrame |
||
186 | smooth = TNE_LowHealth_ManaSync |
||
187 | end |
||
188 | |||
189 | if (smooth) then -- flash differently depending on settings |
||
190 | TNE_LowHealth_SmoothFlashFrame(value * 100, t1, frame, event) |
||
191 | else |
||
192 | TNE_LowHealth_FlashFrame(value * 100, t1, t2, frame, event) |
||
193 | end |
||
194 | |||
195 | end |
||
196 | |||
197 | |||
198 | function TNE_LowHealth_CombatCheck(state) |
||
199 | |||
200 | local combat = UnitAffectingCombat("player") |
||
201 | if (not state or state == "UNIT_HEALTH") then |
||
202 | return TNE_LowHealth_HealthCombatOnly and not combat |
||
203 | elseif (state == "UNIT_MANA") then |
||
204 | return TNE_LowHealth_ManaCombatOnly and not combat |
||
205 | else |
||
206 | return TNE_LowHealth_SoundCombatOnly and not combat |
||
207 | end |
||
208 | |||
209 | end |
||
210 | |||
211 | |||
212 | function TNE_LowHealth_SoundCheck(frame, value, threshold) |
||
213 | |||
214 | -- disable |
||
215 | if (value > threshold or not TNE_LowHealth_SoundEnabled or TNE_LowHealth_CombatCheck("SOUND") or UnitIsDeadOrGhost("player")) then |
||
216 | if (frame.heartRate) then |
||
217 | frame:SetScript("OnUpdate", nil) |
||
218 | frame.heartRate = nil |
||
219 | end |
||
220 | -- regular |
||
221 | elseif (not frame.heartRate) then |
||
222 | frame:SetScript("OnUpdate", TNE_LowHealth_OnUpdate) |
||
223 | frame.timer = 1 |
||
224 | frame.heartRate = 0 |
||
225 | end |
||
226 | end |
||
227 | |||
228 | |||
229 | function TNE_LowHealth_ManaCheck() |
||
230 | |||
231 | local mana, frame = "UNIT_MANA", getglobal("LowHealthWarningFrame") |
||
232 | if (TNE_LowHealth_ManaEnabled and UnitPowerType("player") == 0) then |
||
233 | -- player is using mana |
||
234 | frame:RegisterEvent(mana) |
||
235 | else |
||
236 | frame:UnregisterEvent(mana) |
||
237 | TNE_LowHealth_FlashFrameStop(OutOfControlFrame, mana) |
||
238 | end |
||
239 | end |
||
240 | |||
241 | |||
242 | function TNE_LowHealth_OnUpdate() |
||
243 | |||
244 | if (this.timer > this.heartRate) then |
||
245 | PlaySoundFile("Interface\\AddOns\\TNE_LowHealthWarning\\Sounds\\Heartbeat.wav") |
||
246 | this.heartRate = TNE_LowHealth_GetHeartRate() |
||
247 | this.timer = 0 |
||
248 | else |
||
249 | this.timer = this.timer + arg1 |
||
250 | end |
||
251 | |||
252 | end |
||
253 | |||
254 | |||
255 | function TNE_LowHealth_GetHeartRate(state) |
||
256 | if (state and state == "UNIT_MANA") then |
||
257 | return 0.5 + 2.0 * UnitMana("player") / UnitManaMax("player") |
||
258 | else |
||
259 | return 0.5 + 2.0 * UnitHealth("player") / UnitHealthMax("player") |
||
260 | end |
||
261 | end |
||
262 | |||
263 | |||
264 | function TNE_LowHealth_CMD(arg1) |
||
265 | |||
266 | if (not arg1) then |
||
267 | return |
||
268 | end |
||
269 | |||
270 | if (string.find(arg1, "^settings$") or arg1 == "") then |
||
271 | ShowUIPanel(LowHealthSettingsFrame) |
||
272 | return |
||
273 | end |
||
274 | |||
275 | end |