-- Description: 		Customizable HUD Onscreen messages
-- Created by Michael Chimella 9March2008

--Version 1.00
--Enabled OK, text size OK, time on screen OK, text colour OK, OnEnter trigger OK, Flow Graph behaviour OK
--add text position, background, add sound, tikker tape style
--fix cannot trigger more than once if unchecked
--cannot be triggered by AI
--Want OnlyLocalPlayer for SP use, OnlyPlayers for MP and SP use

HUDInformation = {
	type = "Trigger",
	
	Properties = {
		bEnabled = 1,
		bTriggerOnce = 1,
		bOnlyPlayers = 0,   	-- only Players (SP or MP)
		bOnlyLocalPlayer = 1, -- only Local Player should trigger it
		species = -1,
		bInVehicleOnly = 0,
		
		--for HUD text messages:
		Text = {
			fMessageDuration = 10.0,
			sTextHeading = "",
			sTextLine1 = "",
			sTextLine2 = "",
			sTextLine3 = "",
			sTextLine4 = "",
			sTextLine5 = "",
			iTextHeadingSize = 3,
			iTextLine1Size = 2,
			iTextLine2Size = 2,
			iTextLine3Size = 2,
			iTextLine4Size = 2,
			iTextLine5Size = 2,
			},
		
		
		Colors = {
						clrColorHeading = {x=0, y=0, z=1},
						clrColorText = {x=1, y=0, z=0},
						},
		
	},
	
	States = { "Empty", "Occupied" },
	
	Editor={
		Model="Editor/Objects/T.cgf",
		Icon="Trigger.bmp",
		ShowBounds = 1,
	},
	
	-- Who triggered me. Variables are declared
	Who=nil,
	Enabled=1,
	Entered=0,
	triggeredIn=0,
	triggeredOut=0,
}
	
	
------------------------------------------------------------
--essentially resets the entity on property changed in RollupBar, also called at OnInit and via OnReset
function HUDInformation:OnPropertyChange()
	self.Enabled = self.Properties.bEnabled;
	self.Who = nil;
	self.Entered = 0;
	self.triggeredIn = 0;
	self.triggeredOut = 0;
	self:GotoState( "Empty" );
end

------------------------------------------------------------
function HUDInformation:OnReset()
	self:OnPropertyChange();
end
	
------------------------------------------------------------
function HUDInformation:OnInit()
	self:OnReset();
end

------------------------------------------------------------
--what property variables are saved during gamesaves
function HUDInformation:OnSave(props)
	--WriteToStream(stm,self.Properties);
	props.Who = self.Who
	--	props.Entered = self.Entered;
	props.Enabled = self.Enabled	
	props.triggeredIn = self.triggeredIn
	props.triggeredOut = self.triggeredOut	
end

------------------------------------------------------------
--what property variables are read back in on game loads, should match property saves above or will use default
function HUDInformation:OnLoad(props)
	--self.Properties=ReadFromStream(stm);
	--self:OnReset();
	self.Who = props.Who	
	self.Enabled = props.Enabled	
	self.triggeredIn = props.triggeredIn
	self.triggeredOut = props.triggeredOut
--	self.Entered = props.Entered
end

------------------------------------------------------------
function HUDInformation:OnShutDown()
end

------------------------------------------------------------
------------------------------------------------------------
function HUDInformation:Event_Enter( sender )
	
	if (self.Enabled ~= 1) then --if the trigger is not enabled nothing happens when entered
		return	
	end
	
	if (self.Properties.Text.sTextLine5 ~="") then
		CryAction.Persistant2DText(string.format(self.Properties.Text.sTextLine5),self.Properties.Text.iTextLine5Size,
		self.Properties.Colors.clrColorText,"Event_Enter",self.Properties.Text.fMessageDuration);
	end
	
	if (self.Properties.Text.sTextLine4 ~="") then
		CryAction.Persistant2DText(string.format(self.Properties.Text.sTextLine4),self.Properties.Text.iTextLine4Size,
		self.Properties.Colors.clrColorText,"Event_Enter",self.Properties.Text.fMessageDuration);
	end
	
	if (self.Properties.Text.sTextLine3 ~="") then
		CryAction.Persistant2DText(string.format(self.Properties.Text.sTextLine3),self.Properties.Text.iTextLine3Size,
		self.Properties.Colors.clrColorText,"Event_Enter",self.Properties.Text.fMessageDuration);
	end
	
	if (self.Properties.Text.sTextLine2 ~="") then
		CryAction.Persistant2DText(string.format(self.Properties.Text.sTextLine2),self.Properties.Text.iTextLine2Size,
		self.Properties.Colors.clrColorText,"Event_Enter",self.Properties.Text.fMessageDuration);
	end
	
	if (self.Properties.Text.sTextLine1 ~="") then
		CryAction.Persistant2DText(string.format(self.Properties.Text.sTextLine1),self.Properties.Text.iTextLine1Size,
		self.Properties.Colors.clrColorText,"Event_Enter",self.Properties.Text.fMessageDuration);
	end
	
	if (self.Properties.Text.sTextHeading ~= "") then
	CryAction.Persistant2DText(string.format(self.Properties.Text.sTextHeading),self.Properties.Text.iTextHeadingSize,
	self.Properties.Colors.clrColorHeading,"Event_Enter",self.Properties.Text.fMessageDuration);
	end
	
	BroadcastEvent( self,"Enter" );
	self:ActivateOutput("Entered",true);
end

------------------------------------------------------------
------------------------------------------------------------
function HUDInformation:Event_Enable( sender )
	self.Enabled = 1;
	BroadcastEvent( self,"Enable" );
	self:ActivateOutput("Enabled",true);
end

function HUDInformation:Event_Disable( sender )
	self.Enabled = 0;
	self:GotoState( "Empty" );
	BroadcastEvent( self,"Disable" );
	self:ActivateOutput("Disabled",true);
end

------------------------------------------------------------
HUDInformation.Empty =
{

	OnBeginState = function( self )	  
		self.Who = nil;
		self.Entered = 0;
	end,


	OnEndState = function( self )
	end,
	OnEnterArea = function( self,player,areaId )	  
		-- Ignore if disabled.
		if (self.Enabled ~= 1) then
			return
		elseif (self.Properties.bInVehicleOnly ~= 0 and (g_localActor.vehicleId==nil)) then
			return;	
		end
			
		if (self.Properties.bOnlyPlayers ~= 0 and player.type ~= "Player") then
			--System.Log("HUDInformation: "..self:GetName().." entered by "..player:GetName().." Discard No Player");
			return;
		end
		
		if (self.Properties.bOnlyLocalPlayer ~= 0 and player ~= g_localActor) then
			--System.Log("HUDInformation: "..self:GetName().." entered by "..player:GetName().." Discard Not Local Player");
			return;
		end
			
		--System.Log("HUDInformation: "..self:GetName().." entered by "..player:GetName().." Really");

		if (self.Who==nil) then
			self.Who = player;
			self:GotoState( "Occupied" );
		end
	end,
}

------------------------------------------------------------
HUDInformation.Occupied =
{

	OnBeginState = function( self )	  
		--if has already been triggered and bTriggerOnce is 1
		--skip the envent
		if((self.Properties.bTriggerOnce==1) and self.triggeredIn==1)then
			self.Entered = 1;
			return
		end
		self.triggeredIn=1;
		self:Event_Enter(self.Who);

		--if(self.Properties.ScriptCommand and self.Properties.ScriptCommand~="")then
			local f = loadstring(self.Properties.ScriptCommand);
			--if (f~=nil) then
				--f();
			--end
		--end
	end,
	-------------------------------------------------------------------------------
	OnEndState = function( self )
	end,


	OnLeaveArea = function( self,player,areaId )
		-- Ignore if disabled.
		if (self.Enabled ~= 1) then
			return
		elseif (self.Properties.bInVehicleOnly ~= 0 and (g_localActor.vehicleId==nil)) then
			return;	
		end
		
		if (self.Properties.bOnlyPlayers ~= 0 and player.type ~= "Player") then
			--System.Log("HUDInformation: "..self:GetName().." left by "..player:GetName().." Discard No Player");
			return;
		end
		
		if (self.Properties.bOnlyLocalPlayer ~= 0 and player ~= g_localActor) then
			--System.Log("HUDInformation: "..self:GetName().." left by "..player:GetName().." Discard Not Local Player");
			return;
		end
			
		--System.Log("HUDInformation: "..self:GetName().." left by "..player:GetName().." Really");
		
		if (self.Properties.bOnlyLocalPlayer ~= 0 and player ~= g_localActor) then
			return;
		end

		if((self.Properties.bTriggerOnce~=1) or self.triggeredOut~=1)then
			self.triggeredOut=1;
			self:Event_Leave( self.Who );
		end
		
		self.triggeredOut=1;
		self:GotoState("Empty");
	end,
}


------------------------------------------------------------
HUDInformation.FlowEvents =
{
	Inputs =
	{
		Disable = { HUDInformation.Event_Disable, "bool" },
		Enable = { HUDInformation.Event_Enable, "bool" },
		Enter = { HUDInformation.Event_Enter, "bool" },
	},
	Outputs =
	{
		Disabled = "bool",
		Enabled = "bool",
		Entered = "bool",
	},
}

	