PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 1ms

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

http://github.com/Asphyxia/Tukui
Lua | 125 lines | 104 code | 21 blank | 0 comment | 19 complexity | a19f705acbb288aba82a8d4e41e55dda MD5 | raw file
  1local parent, ns = ...
  2local oUF = ns.oUF
  3
  4local _TIMERS = {}
  5local ReadyCheckFrame
  6
  7local removeEntry = function(icon)
  8	_TIMERS[icon] = nil
  9	if(not next(_TIMERS)) then
 10		return ReadyCheckFrame:Hide()
 11	end
 12end
 13
 14local Start = function(self)
 15	removeEntry(self)
 16
 17	self:SetTexture(READY_CHECK_WAITING_TEXTURE)
 18	self.state = 'waiting'
 19	self:SetAlpha(1)
 20	self:Show()
 21end
 22
 23local Confirm = function(self, ready)
 24	removeEntry(self)
 25
 26	if(ready) then
 27		self:SetTexture(READY_CHECK_READY_TEXTURE)
 28		self.state = 'ready'
 29	else
 30		self:SetTexture(READY_CHECK_NOT_READY_TEXTURE)
 31		self.state = 'notready'
 32	end
 33
 34	self:SetAlpha(1)
 35	self:Show()
 36end
 37
 38local Finish = function(self)
 39	if(self.state == 'waiting') then
 40		self:SetTexture(READY_CHECK_AFK_TEXTURE)
 41		self.state = 'afk'
 42	end
 43
 44	self.finishedTimer = self.finishedTime or 10
 45	self.fadeTimer = self.fadeTime or 1.5
 46
 47	_TIMERS[self] = true
 48	ReadyCheckFrame:Show()
 49end
 50
 51local OnUpdate = function(self, elapsed)
 52	for icon in next, _TIMERS do
 53		if(icon.finishedTimer) then
 54			icon.finishedTimer = icon.finishedTimer - elapsed
 55			if(icon.finishedTimer <= 0) then
 56				icon.finishedTimer = nil
 57			end
 58		elseif(icon.fadeTimer) then
 59			icon.fadeTimer = icon.fadeTimer - elapsed
 60			icon:SetAlpha(icon.fadeTimer / (icon.fadeTime or 1.5))
 61
 62			if(icon.fadeTimer <= 0) then
 63				icon:Hide()
 64				removeEntry(icon)
 65			end
 66		end
 67	end
 68end
 69
 70local Update = function(self, event)
 71	local unit = self.unit
 72	local readyCheck = self.ReadyCheck
 73	if(event == 'READY_CHECK_FINISHED') then
 74		Finish(readyCheck)
 75	else
 76		local status = GetReadyCheckStatus(unit)
 77		if(UnitExists(unit) and status) then
 78			if(status == 'ready') then
 79				Confirm(readyCheck, 1)
 80			elseif(status == 'notready') then
 81				Confirm(readyCheck)
 82			else
 83				Start(readyCheck)
 84			end
 85		end
 86	end
 87end
 88
 89local Path = function(self, ...)
 90	return (self.ReadyCheck.Override or Update) (self, ...)
 91end
 92
 93local ForceUpdate = function(element)
 94	return Path(element.__owner, 'ForceUpdate')
 95end
 96
 97local Enable = function(self, unit)
 98	local readyCheck = self.ReadyCheck
 99	if(readyCheck and (unit and (unit:sub(1, 5) == 'party' or unit:sub(1,4) == 'raid'))) then
100		readyCheck.__owner = self
101		readyCheck.ForceUpdate = ForceUpdate
102
103		if(not ReadyCheckFrame) then
104			ReadyCheckFrame = CreateFrame'Frame'
105			ReadyCheckFrame:SetScript('OnUpdate', OnUpdate)
106		end
107
108		self:RegisterEvent('READY_CHECK', Path)
109		self:RegisterEvent('READY_CHECK_CONFIRM', Path)
110		self:RegisterEvent('READY_CHECK_FINISHED', Path)
111
112		return true
113	end
114end
115
116local Disable = function(self)
117	local readyCheck = self.ReadyCheck
118	if(readyCheck) then
119		self:UnregisterEvent('READY_CHECK', Path)
120		self:UnregisterEvent('READY_CHECK_CONFIRM', Path)
121		self:UnregisterEvent('READY_CHECK_FINISHED', Path)
122	end
123end
124
125oUF:AddElement('ReadyCheck', Path, Enable, Disable)