--Blurs the screen.
--by aarbro [aarbro@hotmail.com]

HudBlur = {

	Editor = {
		Model="Objects/EXP/Editor/editor_fx.cgf",
	},

	Properties = {
		bActivated = 0,
		fFadeInTime = 1.0,		-- time for the fade in to complete, in s
		fFadeOutTime = 1.0,	-- time for the fade out to complete, in s
		fBlurAmount = 1.0,		-- strength of the blur effect (0..1)
		BlurColor = {fRed = 1.0, fGreen = 1.0, fBlue = 1.0}
	},
	
	lastTime = 0,
	fadeDir = 0,
	currBlurAmount = 0,
};
-------------------------------------------------------------------------------
function HudBlur:OnPropertyChange()
	self:OnReset();
end
-------------------------------------------------------------------------------

function HudBlur:OnInit()
	self:OnReset();
end
-------------------------------------------------------------------------------

function HudBlur:OnReset()

	self.currBlurAmount = 0;
	self:TrackColliders(1);
	self:EnableUpdate(0);
	
	if ( self.Properties.bActivated == 1 ) then
		self:Event_Activate();
	else
		self:Event_Deactivate();
	end

end
-------------------------------------------------------------------------------
function HudBlur:OnShutDown()
end
-------------------------------------------------------------------------------


function HudBlur:OnSave(stm)
end
-------------------------------------------------------------------------------

function HudBlur:OnLoad(stm)
end
-------------------------------------------------------------------------------

function HudBlur:Event_Activate()
	self.lastTime = _time;

	BroadcastEvent(self, "Activate");
	self.Properties.bActivated = 1;
	self.fadeDir = 1;
	self:EnableUpdate(1);
	
end
-------------------------------------------------------------------------------

function HudBlur:Event_Deactivate()
	self.lastTime = _time;

	BroadcastEvent(self, "Deactivate");
	self.Properties.bActivated = 0;
	self.fadeDir = -1;
	self:EnableUpdate(1);
end
-------------------------------------------------------------------------------

function HudBlur:OnUpdate()
	local dt = _time - self.lastTime;
	local percent = 0;
	
	if ( self.fadeDir == 1 ) then --fade in
		percent = dt / self.Properties.fFadeInTime;
	end
	if ( self.fadeDir == -1 ) then --fade out
		percent = -dt / self.Properties.fFadeOutTime;
	end

	self.currBlurAmount = self.currBlurAmount + percent * self.Properties.fBlurAmount;
	
	--Hud:AddMessage( Properties, 5);
	
	if (self.currBlurAmount < 0.0 ) then
		self.currBlurAmount = 0;
		System:SetScreenFx("ScreenBlur", 0);
		self:EnableUpdate(0);
		return;
	end
	if ( self.currBlurAmount >= self.Properties.fBlurAmount ) then
		self.currBlurAmount = self.Properties.fBlurAmount;
	end
	
	System:SetScreenFx("ScreenBlur", 1);

	System:SetScreenFxParamFloat("ScreenBlur", "ScreenBlurAmount", self.currBlurAmount);
	System:SetScreenFxParamFloat("ScreenBlur", "ScreenBlurColorRed", self.Properties.BlurColor.fRed);
	System:SetScreenFxParamFloat("ScreenBlur", "ScreenBlurColorGreen", self.Properties.BlurColor.fGreen);
	System:SetScreenFxParamFloat("ScreenBlur", "ScreenBlurColorBlue", self.Properties.BlurColor.fBlue);

	self.lastTime = _time;
	
end


function HudBlur:OnEnterArea()
	self:Event_Activate();
end
function HudBlur:OnLeaveArea()
	self:Event_Deactivate();
end
