
.namespace Animation

DEFAULTFPS = 10

--animation à la "main"
function OnEnable()
	if self.Image ~=nil or self.startVisible==false then
		self.Flags_Visible=false
	end
	--il faut le mettre avant autoplay
	if self.Fps == 0 then
		self.Fps = DEFAULTFPS
	end
	
	local value
	--On récupère le nombre d'images courant
	value, self.nbImages=string.gsub(self.Image, "|", "||")	
	

	if self.SimpleScale then
		self.Scale_X = 2
		self.Scale_Y = 2
	end
	
	if self.Autoplay then
		self.Play()
	end
	
	--self.AlphaTestThreshold = 254 -- pour le bug de changement de résolution 10285 

end

function Play( utnotify)
	
	--on recalcule ici le self.nbImages
	local value 
	value, self.nbImages=string.gsub(self.Image, "|", "||")	
	self.Flags_Visible = true
	
	self.SetFrame(0)

	self.Color.a = 255
	
	
	
	if self.Animtimer ~= nil and self.Animtimer~= nil and not self.Loop then
		OPUnitFactory.CloseUnit(self.Animtimer)			
		self.Animtimer=nil
	end
	if self.LoopDelay>0 then
		self.Animtimer = GlobalFunctions.CreateTimer(self.Name,math.random(self.LoopDelay-self.LoopDelayDelta,self.LoopDelay+self.LoopDelayDelta),true,true, true, self)
		self.Isplaying=false
	else
		self.Animtimer = GlobalFunctions.CreateTimer(self.Name, 1/ self.Fps,true,true, false, self)
		self.Isplaying=true
	end
	self.Animtimer.Enabled=true
	self.Animtimer.Start()
	
	self.AnimNotify = utnotify
	
	--OPLog.Log(" play "..self.Name.." "..self.Animtimer.Name)
	
end

function Stop()
	--OPLog.Log("stop de "..self.Name)
	if self.Animtimer ~= nil and self.Animtimer~= nil then
		OPUnitFactory.CloseUnit(self.Animtimer)			
		self.Animtimer=nil
		-- self.Isplaying = false
	end
	self.Isplaying = false
end

function OnTimeOut(name)

	if name ~= self.Name.."WBPTimer" then
		return
	end
	
	--OPLog.Log("anim frame "..self.GetFrame().." "..self.Name.." "..self.nbImages)

	if self.Isplaying then
		self.Animtimer = OPUnitFactory.GetUnit(self.Name.."WBPTimer","WBPTimer")
		if self.GetFrame() < self.nbImages then
			self.Flags_Visible = true
			self.SetFrame(self.GetFrame()+1)
			self.fadecompteur = 0
			self.Color.a = 255

		else
				
			if self.AnimNotify and self.AnimNotify.ParentEnabled and self.AnimNotify.OnAnimationEnd then
				self.AnimNotify.OnAnimationEnd(self.Name)
			end
			if self.Loop then
				self.Play(self.AnimNotify)
			elseif self.AutoDisable then
				self.Enabled=false
			else
				Stop()
			end
			
		end
	else
		if self.Animtimer ~= nil and self.Animtimer~= nil then
			OPUnitFactory.CloseUnit(self.Animtimer)
			self.Animtimer=nil
		end
		self.Animtimer = GlobalFunctions.CreateTimer(self.Name, 1/ self.Fps,true,self.Loop, false, self)
		self.Isplaying=true
		self.Animtimer.Enabled=true
		self.Animtimer.Start()
	end
	
end

function OnDisable()
	self.Animtimer = OPUnitFactory.GetUnit(self.Name.."WBPTimer","WBPTimer")
	-- if self.Animtimer~=nil and self.Animtimer~= nil then
		-- OPUnitFactory.CloseUnit(self.Animtimer)
		-- self.Animtimer=nil
	-- end
	Stop()
end