class hudSightseeingBinocularsController extends CustomAnimationsHudGameController
{
	private var pitch_min : Float;
	default pitch_min = -360;
	private var pitch_max : Float;
	default pitch_max = 360;
	private var yaw_min : Float;
	default yaw_min = -640;
	private var yaw_max : Float;
	default yaw_max = 640;
	private var tele_min : Float;
	default tele_min = -360;
	private var tele_max : Float;
	default tele_max = 360;
	private var tele_scale : Float;
	default tele_scale = .75;
	private var max_zoom_level : Float;
	default max_zoom_level = 4;
	private editable var m_background : inkCanvasRef;
	private var m_psmBlackboard : weak< IBlackboard >;
	private var m_tcsBlackboard : weak< IBlackboard >;
	private var m_PSM_BBID : CallbackHandle;
	private var m_tcs_BBID : CallbackHandle;
	private var m_deviceChain_BBID : CallbackHandle;
	private var m_currentZoom : Float;
	private var m_controlledObjectRef : weak< GameObject >;
	private var m_alpha_fadein : inkAnimDef;
	private var m_AnimProxy : inkAnimProxy;
	private var m_AnimOptions : inkAnimOptions;
	private var m_ownerObject : weak< GameObject >;
	private var m_maxZoomLevel : Int32;

	protected event OnInitialize()
	{
		var delayInitialize : DelayedHUDInitializeEvent;
		var alphaInterpolator : inkAnimTransparency;
		m_root = ( ( inkCompoundWidget )( GetRootWidget() ) );
		m_ownerObject = ( ( GameObject )( GetOwnerEntity() ) );
		m_tcsBlackboard = GetBlackboardSystem().Get( GetAllBlackboardDefs().DeviceTakeControl );
		if( m_tcsBlackboard )
		{
			m_tcs_BBID = m_tcsBlackboard.RegisterDelayedListenerEntityID( GetAllBlackboardDefs().DeviceTakeControl.ActiveDevice, this, 'OnChangeControlledDevice' );
			m_deviceChain_BBID = m_tcsBlackboard.RegisterListenerVariant( GetAllBlackboardDefs().DeviceTakeControl.DevicesChain, this, 'OnTakeControllOverDevice' );
			OnChangeControlledDevice( m_tcsBlackboard.GetEntityID( GetAllBlackboardDefs().DeviceTakeControl.ActiveDevice ) );
			OnTakeControllOverDevice( m_tcsBlackboard.GetVariant( GetAllBlackboardDefs().DeviceTakeControl.DevicesChain ) );
		}
		UpdateRulers();
		m_alpha_fadein = new inkAnimDef;
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetDuration( 1.0 );
		alphaInterpolator.SetStartTransparency( 1.0 );
		alphaInterpolator.SetEndTransparency( 1.0 );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		m_alpha_fadein.AddInterpolator( alphaInterpolator );
		m_AnimOptions.playReversed = false;
		m_AnimOptions.executionDelay = 0.0;
		m_AnimOptions.loopType = inkanimLoopType.Cycle;
		m_AnimOptions.loopInfinite = true;
		m_AnimProxy = m_root.PlayAnimationWithOptions( m_alpha_fadein, m_AnimOptions );
		m_AnimProxy.RegisterToCallback( inkanimEventType.OnEndLoop, this, 'OnEndLoop' );
		GetPlayerControlledObject().RegisterInputListener( this );
		delayInitialize = new DelayedHUDInitializeEvent;
		GameInstance.GetDelaySystem( GetPlayerControlledObject().GetGame() ).DelayEvent( GetPlayerControlledObject(), delayInitialize, 0.1 );
	}

	protected event OnUninitialize()
	{
		m_tcsBlackboard.UnregisterListenerEntityID( GetAllBlackboardDefs().DeviceTakeControl.ActiveDevice, m_tcs_BBID );
		m_tcsBlackboard.UnregisterListenerVariant( GetAllBlackboardDefs().DeviceTakeControl.DevicesChain, m_deviceChain_BBID );
		m_AnimProxy.UnregisterFromCallback( inkanimEventType.OnEndLoop, this, 'OnEndLoop' );
		m_AnimProxy.Stop();
		TakeOverControlSystem.CreateInputHint( GetPlayerControlledObject().GetGame(), false );
	}

	protected event OnAction( action : ListenerAction, consumer : ListenerActionConsumer )
	{
		UpdateRulers();
	}

	protected event OnEndLoop( proxy : inkAnimProxy )
	{
		UpdateRulers();
	}

	protected event OnTakeControllOverDevice( value : Variant ) {}

	private function UpdateRulers()
	{
		var data : CameraRotationData;
		var euAngles : EulerAngles;
		var pitchPt : Float;
		var yawPt : Float;
		var m_yawMargin : Float;
		var m_pitchMargin : Float;
		data = ( ( SensorDevice )( m_controlledObjectRef ) ).GetRotationData();
		euAngles = ( ( SensorDevice )( m_controlledObjectRef ) ).GetRotationFromSlotRotation();
		if( data.m_maxPitch == 0.0 )
		{
			data.m_maxPitch = 360.0;
		}
		pitchPt = euAngles.Pitch / AbsF( data.m_maxPitch - data.m_minPitch );
		m_pitchMargin = AbsF( pitch_max - pitch_min ) * pitchPt;
		if( data.m_maxYaw == 0.0 )
		{
			data.m_maxYaw = 360.0;
		}
		yawPt = -( euAngles.Yaw ) / AbsF( data.m_maxYaw - data.m_minYaw );
		m_yawMargin = AbsF( yaw_max - yaw_min ) * yawPt;
		inkCanvasRef.SetMargin( m_background, m_yawMargin, m_pitchMargin, 0.0, 0.0 );
	}

	protected event OnPlayerAttach( playerPuppet : GameObject )
	{
		m_psmBlackboard = GetPSMBlackboard( playerPuppet );
		if( m_psmBlackboard )
		{
			m_PSM_BBID = m_psmBlackboard.RegisterDelayedListenerFloat( GetAllBlackboardDefs().PlayerStateMachine.ZoomLevel, this, 'OnZoomChange' );
			m_maxZoomLevel = m_psmBlackboard.GetInt( GetAllBlackboardDefs().PlayerStateMachine.MaxZoomLevel );
		}
	}

	protected event OnPlayerDetach( playerPuppet : GameObject )
	{
		m_psmBlackboard.UnregisterDelayedListener( GetAllBlackboardDefs().PlayerStateMachine.ZoomLevel, m_PSM_BBID );
		GameInstance.GetQuestsSystem( GetPlayerControlledObject().GetGame() ).SetFact( 'sightseeing_binculars_ui_turned_off', 1 );
	}

	protected event OnChangeControlledDevice( value : EntityID )
	{
		if( m_ownerObject )
		{
			m_controlledObjectRef = m_ownerObject.GetTakeOverControlSystem().GetControlledObject();
			if( m_controlledObjectRef.GetPSClassName() == 'SniperNestControllerPS' )
			{
				m_root.SetVisible( false );
			}
			else
			{
				m_root.SetVisible( true );
			}
		}
		ResolveState();
	}

	private function ResolveState()
	{
		var stateName : CName;
		var ownerObject : GameObject;
		if( m_controlledObjectRef )
		{
			ownerObject = m_controlledObjectRef.GetOwner();
			if( ownerObject && ( ownerObject.IsDrone() || ownerObject.IsVehicle() ) )
			{
				stateName = 'Drone';
			}
			else if( EntityID.IsDynamic( m_controlledObjectRef.GetEntityID() ) )
			{
				stateName = 'Drone';
			}
			else
			{
				stateName = 'Default';
			}
		}
		else
		{
			stateName = 'Default';
		}
		if( m_root )
		{
			m_root.SetState( stateName );
		}
	}

}

