/*---------------------------------------------------------
   Selects entity and aims their eyes
---------------------------------------------------------*/
function TOOL:LeftClick( trace )

	if (self.SelectedEntity == nil) then
	
		if (!trace.Entity) then return end
		if (!trace.Entity:IsValid()) then return end
	
		self.SelectedEntity = trace.Entity
		
		self:GetWeapon():SetNetworkedEntity( 0, self.SelectedEntity )
	
	return end

	local selectedent = self.SelectedEntity
	self.SelectedEntity = nil
	self:GetWeapon():SetNetworkedEntity( 0, NULL )
	
	if (!selectedent:IsValid()) then return end
	
	selectedent:SetEyeTarget( trace.HitPos )

end

/*---------------------------------------------------------
   Makes the eyes look at the player
---------------------------------------------------------*/
function TOOL:RightClick( trace )

	self:GetWeapon():SetNetworkedEntity( 0, NULL )
	self.SelectedEntity = nil
	
	if (!trace.Entity) then return end
	if (!trace.Entity:IsValid()) then return end
	if ( CLIENT ) then return true end
	
	local pos = self:GetOwner():EyePos()
	trace.Entity:SetEyeTarget( pos )
	
end
	
if ( CLIENT ) then

	/*---------------------------------------------------------
	   Draw a box indicating the face we have selected
	---------------------------------------------------------*/
	function TOOL:DrawHUD()
	
		local selected = self:GetWeapon():GetNetworkedEntity( 0 )
		
		if (!selected:IsValid()) then return end
		
		local vEyePos = selected:EyePos()
		
		local eyeattachment = selected:LookupAttachment( "eyes" )
		if (eyeattachment == 0) then return end
		
		local attachment = selected:GetAttachment( eyeattachment )
		local scrpos = attachment.Pos:ToScreen()
		if (!scrpos.visible) then return end
		
		// Try to get each eye position.. this is a real guess and won't work on non-humans
		local Leye = (attachment.Pos + attachment.Ang:Right() * 1.5):ToScreen()
		local Reye = (attachment.Pos - attachment.Ang:Right() * 1.5):ToScreen()
		
		// Work out the side distance to give a rough headsize box..
		local player_eyes = LocalPlayer():EyeAngles()
		local side = (attachment.Pos + player_eyes:Right() * 10):ToScreen()
		local size = 4
		
		local Owner = self:GetOwner()

		// Get Target
		local tr = utilx.GetPlayerTrace( Owner, Owner:GetCursorAimVector() )
		local trace = util.TraceLine( tr )
		local scrhit = trace.HitPos:ToScreen()
		local x = scrhit.x
		local y = scrhit.y

		selected:SetEyeTarget( trace.HitPos )
		
		// Todo, make look less like ass
		
		surface.SetDrawColor( 0, 0, 0, 100 )
		surface.DrawLine( Leye.x-1, Leye.y+1, x-1, y+1 )
		surface.DrawLine( Leye.x-1, Leye.y-1, x-1, y-1 )
		surface.DrawLine( Leye.x+1, Leye.y+1, x+1, y+1 )
		surface.DrawLine( Leye.x+1, Leye.y-1, x+1, y-1 )
		surface.DrawLine( Reye.x-1, Reye.y+1, x-1, y+1 )
		surface.DrawLine( Reye.x-1, Reye.y-1, x-1, y-1 )
		surface.DrawLine( Reye.x+1, Reye.y+1, x+1, y+1 )
		surface.DrawLine( Reye.x+1, Reye.y-1, x+1, y-1 )
		
		surface.SetDrawColor( 0, 255, 0, 255 )
		surface.DrawLine( Leye.x, Leye.y, x, y )
		surface.DrawLine( Reye.x, Reye.y, x, y )
		surface.DrawLine( Leye.x, Leye.y-1, x, y-1 )
		surface.DrawLine( Reye.x, Reye.y-1, x, y-1 )
	
	end

end

