
.namespace SpriteFader

---------------------------copier coller de "progressive action"-------------------------------------------------------
CurrentAction={}
TIMER=0.03 --valeur que l'on peut changer aprés, selon la fluidité
g_timer = nil

function Act() --function spécifique à chaque script
	return
end

function CheckEnd()
	return false
end

function Finish()

end

function StartTimer()
    g_timer=GlobalFunctions.CreateTimerSystem(self.Name,TIMER,false,true,false, self)
    g_timer.Enabled=true
    g_timer.Start()
	self.CanTimeOut=true
end

--a appelé lors du lancement
function CheckTimer()
	if g_timer == nil then
		StartTimer()
	end
end

function OnUpdateUnit()

end

function OnUpdate(dt)
	self.CanTimeOut=true
	OnUpdateUnit(dt)
end

function OnTimeOut()

	if self.CanTimeOut then
		if CurrentAction==nil then
			OPLog.Log('erreur ! CurrentAction=nil '..self.Name)
		end
		if table.getn(CurrentAction) == 0 then
			OPUnitFactory.CloseUnit(g_timer)
			g_timer = nil
		end
		
		for u,action in pairs(CurrentAction) do
			if not CheckEnd(action) then
				newact = Act(action)
				if newact~=nil then
					CurrentAction[u] = newact
				end
			else
				table.remove(CurrentAction,u)
				Finish(action)
			end
		end
		self.CanTimeOut=false
	end
end

--------------------------------------------------------------------------

--on utilise les vecteurs 2d
function Fade(ut,out,length,utnotify,channel,reset)
	--out : si true fade out, on revient au visible

    local action={}

	if length == nil then
		OPLog.Log("atention temps de fade non défini")
		length = 2
	end
	
	if channel==nil then
		channel='a'
	end
	
    --on vérifie d'abord si cette unit n'est pas dans la liste des move--
    for u,v in pairs(CurrentAction) do
        if v.ut.Name==ut.Name and v.channel==channel then
            v.length=length
            v.out=out
            v.utnotify=utnotify
			v.channel=channel
			v.starttime = OPTime.GetTime()
            if reset then
            	if out then
		            ut.Color[channel]=0
	            else
		            ut.Color[channel]=255
	            end
            end
			
			v.floatcolor = ut.Color[channel]
        
            return true
        end
    end
    
	action.ut=ut
	action.length=length
	action.out=out
	action.utnotify=utnotify
	action.channel=channel
	action.starttime = OPTime.GetTime()
	
	
	if out then
		ut.Color[channel]=0
	else
		ut.Color[channel]=255
	end
	
	ApplyToChildren(ut,SetColorChild)
	
	action.floatcolor = ut.Color[channel]
	
	CheckTimer()
	
	--OPLog.Log(GlobalFunctions.Display(action))
	
    table.insert(CurrentAction,action)
end

function Stop(ut)
	for u,v in pairs(CurrentAction) do
		if v.ut.Name == ut.Name then
			table.remove(CurrentAction,u)
			return
		end
	end
end

function StopAll()
	for u,action in pairs(CurrentAction) do
		action.ut.Color.a = 0
		Finish(action)
	end
	CurrentAction = {}
end

function Act(action)

	
	local coeff = (OPTime.GetTime() - action.starttime) / action.length
	
	if action.ut == nil then
		return false
	end
	
	if not action.out then
		action.ut.Color[action.channel] = 255 * (1-coeff)
	else
		action.ut.Color[action.channel] = 255 * (coeff)
	end
	
	ApplyToChildren(action.ut,SetColorChild)
	
		
end

function SetColorChild(ut)
	--OPLog.Log(ut.Parent.Color.a)
	if ut.Color ~= nil then
		ut.Color.a = ut.Parent.Color.a
	end
	--OPLog.Log("changement couleur "..ut.Name..ut.Color)
end

function CheckEnd(action)
    if action.ut == nil then
        Finish(action)
		return
    end
		
	if (OPTime.GetTime() - action.starttime) > action.length then
		if action.out then
			action.ut.Color[action.channel]=255 
		else
			action.ut.Color[action.channel]=0 
		end
		
		ApplyToChildren(action.ut,SetColorChild)
		
		return true
	end	
    

	return false
end

function Finish(action)
	if action.utnotify~=nil then
		action.utnotify.OnFaderEnd(action.ut,action.out)
	end
end
