PageRenderTime 143ms CodeModel.GetById 118ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Tukui/modules/unitframes/core/oUF/elements/castbar.lua

http://github.com/Asphyxia/Tukui
Lua | 435 lines | 339 code | 81 blank | 15 comment | 70 complexity | dea9d4d69a902531448207937c26a1f0 MD5 | raw file
  1--[[
  2	Original codebase:
  3		oUF_Castbar by starlon.
  4		http://svn.wowace.com/wowace/trunk/oUF_Castbar/
  5--]]
  6local parent, ns = ...
  7local oUF = ns.oUF
  8
  9local UnitName = UnitName
 10local GetTime = GetTime
 11local UnitCastingInfo = UnitCastingInfo
 12local UnitChannelInfo = UnitChannelInfo
 13
 14local UNIT_SPELLCAST_START = function(self, event, unit, spell)
 15	if(self.unit ~= unit) then return end
 16
 17	local castbar = self.Castbar
 18	local name, _, text, texture, startTime, endTime, _, castid, interrupt = UnitCastingInfo(unit)
 19	if(not name) then
 20		castbar:Hide()
 21		return
 22	end
 23
 24	endTime = endTime / 1e3
 25	startTime = startTime / 1e3
 26	local max = endTime - startTime
 27
 28	castbar.castid = castid
 29	castbar.duration = GetTime() - startTime
 30	castbar.max = max
 31	castbar.delay = 0
 32	castbar.casting = true
 33	castbar.interrupt = interrupt
 34
 35	castbar:SetMinMaxValues(0, max)
 36	castbar:SetValue(0)
 37
 38	if(castbar.Text) then castbar.Text:SetText(text) end
 39	if(castbar.Icon) then castbar.Icon:SetTexture(texture) end
 40	if(castbar.Time) then castbar.Time:SetText() end
 41
 42	local shield = castbar.Shield
 43	if(shield and interrupt) then
 44		shield:Show()
 45	elseif(shield) then
 46		shield:Hide()
 47	end
 48
 49	local sf = castbar.SafeZone
 50	if(sf) then
 51		sf:ClearAllPoints()
 52		sf:SetPoint'RIGHT'
 53		sf:SetPoint'TOP'
 54		sf:SetPoint'BOTTOM'
 55	end
 56
 57	--- XXX: 1.6: Kill the rank field.
 58	if(castbar.PostCastStart) then
 59		castbar:PostCastStart(unit, name, nil, castid)
 60	end
 61	castbar:Show()
 62end
 63
 64local UNIT_SPELLCAST_FAILED = function(self, event, unit, spellname, _, castid)
 65	if(self.unit ~= unit) then return end
 66
 67	local castbar = self.Castbar
 68	if(castbar.castid ~= castid) then
 69		return
 70	end
 71
 72	castbar.casting = nil
 73	castbar.interrupt = nil
 74	castbar:SetValue(0)
 75	castbar:Hide()
 76
 77	--- XXX: 1.6: Kill the rank field.
 78	if(castbar.PostCastFailed) then
 79		return castbar:PostCastFailed(unit, spellname, nil, castid)
 80	end
 81end
 82
 83local UNIT_SPELLCAST_INTERRUPTED = function(self, event, unit, spellname, _, castid)
 84	if(self.unit ~= unit) then return end
 85
 86	local castbar = self.Castbar
 87	if(castbar.castid ~= castid) then
 88		return
 89	end
 90	castbar.casting = nil
 91	castbar.channeling = nil
 92
 93	castbar:SetValue(0)
 94	castbar:Hide()
 95
 96	--- XXX: 1.6: Kill the rank field.
 97	if(castbar.PostCastInterrupted) then
 98		return castbar:PostCastInterrupted(unit, spellname, nil, castid)
 99	end
100end
101
102local UNIT_SPELLCAST_INTERRUPTIBLE = function(self, event, unit)
103	if(self.unit ~= unit) then return end
104
105	local shield = self.Castbar.Shield
106	if(shield) then
107		shield:Hide()
108	end
109
110	local castbar = self.Castbar
111	if(castbar.PostCastInterruptible) then
112		return castbar:PostCastInterruptible(unit)
113	end
114end
115
116local UNIT_SPELLCAST_NOT_INTERRUPTIBLE = function(self, event, unit)
117	if(self.unit ~= unit) then return end
118
119	local shield = self.Castbar.Shield
120	if(shield) then
121		shield:Show()
122	end
123
124	local castbar = self.Castbar
125	if(castbar.PostCastNotInterruptible) then
126		return castbar:PostCastNotInterruptible(unit)
127	end
128end
129
130local UNIT_SPELLCAST_DELAYED = function(self, event, unit, spellname, _, castid)
131	if(self.unit ~= unit) then return end
132
133	local name, _, text, texture, startTime, endTime = UnitCastingInfo(unit)
134	if(not startTime) then return end
135
136	local castbar = self.Castbar
137	local duration = GetTime() - (startTime / 1000)
138	if(duration < 0) then duration = 0 end
139
140	castbar.delay = castbar.delay + castbar.duration - duration
141	castbar.duration = duration
142
143	castbar:SetValue(duration)
144
145	--- XXX: 1.6: Kill the rank field.
146	if(castbar.PostCastDelayed) then
147		return castbar:PostCastDelayed(unit, name, nil, castid)
148	end
149end
150
151local UNIT_SPELLCAST_STOP = function(self, event, unit, spellname, _, castid)
152	if(self.unit ~= unit) then return end
153
154	local castbar = self.Castbar
155	if(castbar.castid ~= castid) then
156		return
157	end
158
159	castbar.casting = nil
160	castbar.interrupt = nil
161	castbar:SetValue(0)
162	castbar:Hide()
163
164	--- XXX: 1.6: Kill the rank field.
165	if(castbar.PostCastStop) then
166		return castbar:PostCastStop(unit, spellname, nil, castid)
167	end
168end
169
170local UNIT_SPELLCAST_CHANNEL_START = function(self, event, unit, spellname)
171	if(self.unit ~= unit) then return end
172
173	local castbar = self.Castbar
174	local name, _, text, texture, startTime, endTime, isTrade, interrupt = UnitChannelInfo(unit)
175	if(not name) then
176		return
177	end
178
179	endTime = endTime / 1e3
180	startTime = startTime / 1e3
181	local max = (endTime - startTime)
182	local duration = endTime - GetTime()
183
184	castbar.duration = duration
185	castbar.max = max
186	castbar.delay = 0
187	castbar.channeling = true
188	castbar.interrupt = interrupt
189
190	castbar:SetMinMaxValues(0, max)
191	castbar:SetValue(duration)
192
193	if(castbar.Text) then castbar.Text:SetText(name) end
194	if(castbar.Icon) then castbar.Icon:SetTexture(texture) end
195	if(castbar.Time) then castbar.Time:SetText() end
196
197	local shield = castbar.Shield
198	if(shield and interrupt) then
199		shield:Show()
200	elseif(shield) then
201		shield:Hide()
202	end
203
204	local sf = castbar.SafeZone
205	if(sf) then
206		sf:ClearAllPoints()
207		sf:SetPoint'LEFT'
208		sf:SetPoint'TOP'
209		sf:SetPoint'BOTTOM'
210	end
211
212	--- XXX: 1.6: Kill the rank field.
213	if(castbar.PostChannelStart) then castbar:PostChannelStart(unit, name) end
214	castbar:Show()
215end
216
217local UNIT_SPELLCAST_CHANNEL_UPDATE = function(self, event, unit, spellname)
218	if(self.unit ~= unit) then return end
219
220	local name, _, text, texture, startTime, endTime, oldStart = UnitChannelInfo(unit)
221	if(not name) then
222		return
223	end
224
225	local castbar = self.Castbar
226	local duration = (endTime / 1000) - GetTime()
227
228	castbar.delay = castbar.delay + castbar.duration - duration
229	castbar.duration = duration
230	castbar.max = (endTime - startTime) / 1000
231
232	castbar:SetMinMaxValues(0, castbar.max)
233	castbar:SetValue(duration)
234
235	--- XXX: 1.6: Kill the rank field.
236	if(castbar.PostChannelUpdate) then
237		return castbar:PostChannelUpdate(unit, name)
238	end
239end
240
241local UNIT_SPELLCAST_CHANNEL_STOP = function(self, event, unit, spellname)
242	if(self.unit ~= unit) then return end
243
244	local castbar = self.Castbar
245	if(castbar:IsShown()) then
246		castbar.channeling = nil
247		castbar.interrupt = nil
248
249		castbar:SetValue(castbar.max)
250		castbar:Hide()
251
252		--- XXX: 1.6: Kill the rank field.
253		if(castbar.PostChannelStop) then
254			return castbar:PostChannelStop(unit, spellname)
255		end
256	end
257end
258
259local onUpdate = function(self, elapsed)
260	if(self.casting) then
261		local duration = self.duration + elapsed
262		if(duration >= self.max) then
263			self.casting = nil
264			self:Hide()
265
266			if(self.PostCastStop) then self:PostCastStop(self.__owner.unit) end
267			return
268		end
269
270		if(self.SafeZone) then
271			local width = self:GetWidth()
272			local _, _, _, ms = GetNetStats()
273			-- MADNESS!
274			local safeZonePercent = (width / self.max) * (ms / 1e5)
275			if(safeZonePercent > 1) then safeZonePercent = 1 end
276			self.SafeZone:SetWidth(width * safeZonePercent)
277		end
278
279		if(self.Time) then
280			if(self.delay ~= 0) then
281				if(self.CustomDelayText) then
282					self:CustomDelayText(duration)
283				else
284					self.Time:SetFormattedText("%.1f|cffff0000-%.1f|r", duration, self.delay)
285				end
286			else
287				if(self.CustomTimeText) then
288					self:CustomTimeText(duration)
289				else
290					self.Time:SetFormattedText("%.1f", duration)
291				end
292			end
293		end
294
295		self.duration = duration
296		self:SetValue(duration)
297
298		if(self.Spark) then
299			self.Spark:SetPoint("CENTER", self, "LEFT", (duration / self.max) * self:GetWidth(), 0)
300		end
301	elseif(self.channeling) then
302		local duration = self.duration - elapsed
303
304		if(duration <= 0) then
305			self.channeling = nil
306			self:Hide()
307
308			if(self.PostChannelStop) then self:PostChannelStop(self.__owner.unit) end
309			return
310		end
311
312		if(self.SafeZone) then
313			local width = self:GetWidth()
314			local _, _, _, ms = GetNetStats()
315			-- MADNESS!
316			local safeZonePercent = (width / self.max) * (ms / 1e5)
317			if(safeZonePercent > 1) then safeZonePercent = 1 end
318			self.SafeZone:SetWidth(width * safeZonePercent)
319		end
320
321		if(self.Time) then
322			if(self.delay ~= 0) then
323				if(self.CustomDelayText) then
324					self:CustomDelayText(duration)
325				else
326					self.Time:SetFormattedText("%.1f|cffff0000-%.1f|r", duration, self.delay)
327				end
328			else
329				if(self.CustomTimeText) then
330					self:CustomTimeText(duration)
331				else
332					self.Time:SetFormattedText("%.1f", duration)
333				end
334			end
335		end
336
337		self.duration = duration
338		self:SetValue(duration)
339		if(self.Spark) then
340			self.Spark:SetPoint("CENTER", self, "LEFT", (duration / self.max) * self:GetWidth(), 0)
341		end
342	else
343		self.unitName = nil
344		self.channeling = nil
345		self:SetValue(1)
346		self:Hide()
347	end
348end
349
350local Update = function(self, ...)
351	UNIT_SPELLCAST_START(self, ...)
352	return UNIT_SPELLCAST_CHANNEL_START(self, ...)
353end
354
355local ForceUpdate = function(element)
356	return Update(element.__owner, 'ForceUpdate', element.__owner.unit)
357end
358
359local Enable = function(object, unit)
360	local castbar = object.Castbar
361
362	if(castbar) then
363		castbar.__owner = object
364		castbar.ForceUpdate = ForceUpdate
365
366		if(not (unit and unit:match'%wtarget$')) then
367			object:RegisterEvent("UNIT_SPELLCAST_START", UNIT_SPELLCAST_START)
368			object:RegisterEvent("UNIT_SPELLCAST_FAILED", UNIT_SPELLCAST_FAILED)
369			object:RegisterEvent("UNIT_SPELLCAST_STOP", UNIT_SPELLCAST_STOP)
370			object:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED", UNIT_SPELLCAST_INTERRUPTED)
371			object:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE", UNIT_SPELLCAST_INTERRUPTIBLE)
372			object:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE", UNIT_SPELLCAST_NOT_INTERRUPTIBLE)
373			object:RegisterEvent("UNIT_SPELLCAST_DELAYED", UNIT_SPELLCAST_DELAYED)
374			object:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START", UNIT_SPELLCAST_CHANNEL_START)
375			object:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE", UNIT_SPELLCAST_CHANNEL_UPDATE)
376			object:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP", UNIT_SPELLCAST_CHANNEL_STOP)
377		end
378
379		castbar:SetScript("OnUpdate", castbar.OnUpdate or onUpdate)
380
381		if(object.unit == "player") then
382			CastingBarFrame:UnregisterAllEvents()
383			CastingBarFrame.Show = CastingBarFrame.Hide
384			CastingBarFrame:Hide()
385		elseif(object.unit == 'pet') then
386			PetCastingBarFrame:UnregisterAllEvents()
387			PetCastingBarFrame.Show = PetCastingBarFrame.Hide
388			PetCastingBarFrame:Hide()
389		end
390
391		if(not castbar:GetStatusBarTexture()) then
392			castbar:SetStatusBarTexture[[Interface\TargetingFrame\UI-StatusBar]]
393		end
394
395		local spark = castbar.Spark
396		if(spark and spark:IsObjectType'Texture' and not spark:GetTexture()) then
397			spark:SetTexture[[Interface\CastingBar\UI-CastingBar-Spark]]
398		end
399
400		local shield = castbar.Shield
401		if(shield and shield:IsObjectType'Texture' and not shield:GetTexture()) then
402			shield:SetTexture[[Interface\CastingBar\UI-CastingBar-Small-Shield]]
403		end
404
405		local sz = castbar.SafeZone
406		if(sz and sz:IsObjectType'Texture' and not sz:GetTexture()) then
407			sz:SetTexture(1, 0, 0)
408		end
409
410		castbar:Hide()
411
412		return true
413	end
414end
415
416local Disable = function(object, unit)
417	local castbar = object.Castbar
418
419	if(castbar) then
420		object:UnregisterEvent("UNIT_SPELLCAST_START", UNIT_SPELLCAST_START)
421		object:UnregisterEvent("UNIT_SPELLCAST_FAILED", UNIT_SPELLCAST_FAILED)
422		object:UnregisterEvent("UNIT_SPELLCAST_STOP", UNIT_SPELLCAST_STOP)
423		object:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED", UNIT_SPELLCAST_INTERRUPTED)
424		object:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE", UNIT_SPELLCAST_INTERRUPTIBLE)
425		object:UnregisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE", UNIT_SPELLCAST_NOT_INTERRUPTIBLE)
426		object:UnregisterEvent("UNIT_SPELLCAST_DELAYED", UNIT_SPELLCAST_DELAYED)
427		object:UnregisterEvent("UNIT_SPELLCAST_CHANNEL_START", UNIT_SPELLCAST_CHANNEL_START)
428		object:UnregisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE", UNIT_SPELLCAST_CHANNEL_UPDATE)
429		object:UnregisterEvent("UNIT_SPELLCAST_CHANNEL_STOP", UNIT_SPELLCAST_CHANNEL_STOP)
430
431		castbar:SetScript("OnUpdate", nil)
432	end
433end
434
435oUF:AddElement('Castbar', Update, Enable, Disable)