class megatronModeInfoController extends TriggerModeLogicController
{
	private var m_ammoBarVisibility : weak< inkWidget >;
	private var m_chargeBarVisibility : weak< inkWidget >;
	private var m_fullAutoModeText : weak< inkWidget >;
	private var m_chargeModeText : weak< inkWidget >;
	private var m_fullAutoModeBG : weak< inkWidget >;
	private var m_chargeModeBG : weak< inkWidget >;
	private var m_bg1 : weak< inkWidget >;
	private var m_bg2 : weak< inkWidget >;
	private var m_vignette : weak< inkWidget >;

	protected event OnInitialize()
	{
		m_fullAutoModeText = ( ( inkWidget )( GetWidget( 'FullAutoModeText' ) ) );
		m_chargeModeText = ( ( inkWidget )( GetWidget( 'ChargeModeText' ) ) );
		m_fullAutoModeBG = ( ( inkWidget )( GetWidget( 'FullAutoModeBG' ) ) );
		m_chargeModeBG = ( ( inkWidget )( GetWidget( 'ChargeModeBG' ) ) );
		m_ammoBarVisibility = ( ( inkWidget )( GetWidget( 'ammoController/ammoBar' ) ) );
		m_chargeBarVisibility = ( ( inkWidget )( GetWidget( 'chargeController/chargeBar' ) ) );
		m_bg1 = ( ( inkWidget )( GetWidget( 'bg1' ) ) );
		m_bg2 = ( ( inkWidget )( GetWidget( 'bg2' ) ) );
		m_vignette = ( ( inkWidget )( GetWidget( 'vignette' ) ) );
	}

	public override function OnTriggerModeChanged( value : TriggerMode_Record )
	{
		var isChargeMode : Bool;
		var red, blue : Color;
		red = Color( 255, 122, 131, 255 );
		blue = Color( 127, 226, 215, 255 );
		isChargeMode = value.Type() == gamedataTriggerMode.Charge;
		m_chargeModeBG.SetVisible( isChargeMode );
		m_fullAutoModeBG.SetVisible( !( isChargeMode ) );
		m_chargeBarVisibility.SetVisible( isChargeMode );
		m_ammoBarVisibility.SetVisible( !( isChargeMode ) );
		m_bg1.SetTintColor( ( ( isChargeMode ) ? ( red ) : ( blue ) ) );
		m_bg2.SetTintColor( ( ( isChargeMode ) ? ( red ) : ( blue ) ) );
		m_vignette.SetTintColor( ( ( isChargeMode ) ? ( red ) : ( blue ) ) );
		m_chargeModeText.SetTintColor( ( ( isChargeMode ) ? ( Color( 0, 0, 0, 255 ) ) : ( blue ) ) );
		m_fullAutoModeText.SetTintColor( ( ( isChargeMode ) ? ( blue ) : ( Color( 0, 0, 0, 255 ) ) ) );
	}

}

class megatronFullAutoController extends AmmoLogicController
{
	private var m_ammoCountText : weak< inkText >;
	private var m_ammoBar : weak< inkImage >;

	protected event OnInitialize()
	{
		m_ammoCountText = ( ( inkText )( GetWidget( 'ammoCountText' ) ) );
		m_ammoBar = ( ( inkImage )( GetWidget( 'ammoBar' ) ) );
	}

	public override function OnMagazineAmmoCountChanged( value : Uint32 )
	{
		super.OnMagazineAmmoCountChanged( value );
		UpdateAmmoCount( value );
	}

	public override function OnMagazineAmmoCapacityChanged( value : Uint32 )
	{
		super.OnMagazineAmmoCapacityChanged( value );
		UpdateAmmoCount( value );
	}

	private function UpdateAmmoCount( value : Uint32 )
	{
		var fractionValue : Int32;
		var texturePath : CName;
		fractionValue = FloorF( ( ( ( Float )( m_count ) ) / ( ( Float )( m_capacity ) ) ) * 16.0 );
		texturePath = StringToName( "bar_" + IntToString( fractionValue ) );
		m_ammoBar.SetTexturePart( texturePath );
		if( value < ( ( Uint32 )( 10 ) ) )
		{
			m_ammoCountText.SetText( "0" + ( ( String )( value ) ) );
		}
		else
		{
			m_ammoCountText.SetText( ( ( String )( value ) ) );
		}
	}

}

class megatronChargeController extends ChargeLogicController
{
	private var m_chargeBar : weak< inkImage >;

	protected event OnInitialize()
	{
		m_chargeBar = ( ( inkImage )( GetWidget( 'chargeBar' ) ) );
	}

	public override function OnChargeChanged( value : Float )
	{
		m_chargeBar.SetSize( Vector2( 57.0, ( ( Float )( value ) ) * 218.0 ) );
	}

}

class megatronCrosshairGameController extends inkGameController
{
	private var m_bulletSpreedBlackboardId : CallbackHandle;
	private var m_crosshairStateBlackboardId : CallbackHandle;
	private var m_leftPart : weak< inkImage >;
	private var m_rightPart : weak< inkImage >;
	private var m_nearCenterPart : weak< inkImage >;
	private var m_farCenterPart : weak< inkImage >;
	private var m_bufferedSpread : Vector2;
	private var m_orgSideSize : Vector2;
	editable var m_minSpread : Float;
	default m_minSpread = 120;
	editable var m_gameplaySpreadMultiplier : Float;
	default m_gameplaySpreadMultiplier = 1;
	private var m_crosshairState : gamePSMCrosshairStates;

	protected event OnInitialize()
	{
		var weaponBB : IBlackboard;
		m_leftPart = ( ( inkImage )( GetWidget( 'Panel/NearPlane/left' ) ) );
		m_rightPart = ( ( inkImage )( GetWidget( 'Panel/NearPlane/right' ) ) );
		m_nearCenterPart = ( ( inkImage )( GetWidget( 'Panel/NearPlane/center' ) ) );
		m_farCenterPart = ( ( inkImage )( GetWidget( 'Panel/FarPlane/center' ) ) );
		m_leftPart.SetMargin( inkMargin( -( ( ( Float )( m_minSpread ) ) + ( m_gameplaySpreadMultiplier * m_bufferedSpread.X ) ), 0.0, 0.0, 0.0 ) );
		m_rightPart.SetMargin( inkMargin( ( ( Float )( m_minSpread ) ) + ( m_gameplaySpreadMultiplier * m_bufferedSpread.X ), 0.0, 0.0, 0.0 ) );
		m_orgSideSize = m_leftPart.GetSize();
		weaponBB = GetUIActiveWeaponBlackboard();
		m_bulletSpreedBlackboardId = weaponBB.RegisterListenerVector2( GetAllBlackboardDefs().UI_ActiveWeaponData.BulletSpread, this, 'OnBulletSpreadChanged' );
		OnBulletSpreadChanged( weaponBB.GetVector2( GetAllBlackboardDefs().UI_ActiveWeaponData.BulletSpread ) );
		m_crosshairState = gamePSMCrosshairStates.Default;
	}

	protected event OnUninitialize()
	{
		var weaponBB : IBlackboard;
		weaponBB = GetUIActiveWeaponBlackboard();
		weaponBB.UnregisterListenerVector2( GetAllBlackboardDefs().UI_ActiveWeaponData.BulletSpread, m_bulletSpreedBlackboardId );
		m_leftPart.SetSize( m_orgSideSize );
		m_rightPart.SetSize( m_orgSideSize );
	}

	protected event OnPlayerAttach( playerGameObject : GameObject )
	{
		RegisterPSMListeners( playerGameObject );
	}

	protected event OnPlayerDetach( playerGameObject : GameObject )
	{
		UnregisterPSMListeners( playerGameObject );
	}

	protected function RegisterPSMListeners( playerPuppet : GameObject )
	{
		var playerStateMachineBlackboard : IBlackboard;
		playerStateMachineBlackboard = GetPSMBlackboard( playerPuppet );
		if( playerStateMachineBlackboard )
		{
			m_crosshairStateBlackboardId = playerStateMachineBlackboard.RegisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.Crosshair, this, 'OnPSMCrosshairStateChanged' );
		}
	}

	protected function UnregisterPSMListeners( playerPuppet : GameObject )
	{
		var playerStateMachineBlackboard : IBlackboard;
		playerStateMachineBlackboard = GetPSMBlackboard( playerPuppet );
		if( playerStateMachineBlackboard )
		{
			playerStateMachineBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.Crosshair, m_crosshairStateBlackboardId );
		}
	}

	protected event OnBulletSpreadChanged( spread : Vector2 )
	{
		m_bufferedSpread = spread;
		m_leftPart.SetMargin( inkMargin( -( ( ( Float )( m_minSpread ) ) + ( m_gameplaySpreadMultiplier * spread.X ) ), 0.0, 0.0, 0.0 ) );
		m_rightPart.SetMargin( inkMargin( ( ( Float )( m_minSpread ) ) + ( m_gameplaySpreadMultiplier * spread.X ), 0.0, 0.0, 0.0 ) );
	}

	protected event OnPSMCrosshairStateChanged( value : Int32 )
	{
		var oldState, newState : gamePSMCrosshairStates;
		oldState = m_crosshairState;
		newState = ( ( gamePSMCrosshairStates )( value ) );
		m_crosshairState = newState;
		OnCrosshairStateChange( oldState, newState );
	}

	private virtual function OnCrosshairStateChange( oldState : gamePSMCrosshairStates, newState : gamePSMCrosshairStates )
	{
		switch( newState )
		{
			case gamePSMCrosshairStates.HipFire:
				OnState_HipFire();
			break;
			case gamePSMCrosshairStates.Aim:
				OnState_Aim();
			break;
			case gamePSMCrosshairStates.Reload:
				OnState_Reload();
			break;
			case gamePSMCrosshairStates.Sprint:
				OnState_Sprint();
			break;
		}
	}

	public function ColapseCrosshair( full : Bool, duration : Float )
	{
		var anim : inkAnimDef;
		var marginInterpolator : inkAnimMargin;
		var alphaInterpolator : inkAnimTransparency;
		anim = new inkAnimDef;
		marginInterpolator = new inkAnimMargin;
		marginInterpolator.SetStartMargin( m_leftPart.GetMargin() );
		marginInterpolator.SetEndMargin( inkMargin( 0.0, 0.0, 0.0, 0.0 ) );
		marginInterpolator.SetDuration( duration );
		marginInterpolator.SetType( inkanimInterpolationType.Linear );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( marginInterpolator );
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetStartTransparency( m_leftPart.GetOpacity() );
		alphaInterpolator.SetEndTransparency( 0.0 );
		alphaInterpolator.SetDuration( duration );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( alphaInterpolator );
		m_leftPart.PlayAnimation( anim );
		anim = new inkAnimDef;
		marginInterpolator = new inkAnimMargin;
		marginInterpolator.SetStartMargin( m_rightPart.GetMargin() );
		marginInterpolator.SetEndMargin( inkMargin( 0.0, 0.0, 0.0, 0.0 ) );
		marginInterpolator.SetDuration( duration );
		marginInterpolator.SetType( inkanimInterpolationType.Linear );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( marginInterpolator );
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetStartTransparency( m_rightPart.GetOpacity() );
		alphaInterpolator.SetEndTransparency( 0.0 );
		alphaInterpolator.SetDuration( duration );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( alphaInterpolator );
		m_rightPart.PlayAnimation( anim );
		if( full )
		{
			anim = new inkAnimDef;
			alphaInterpolator = new inkAnimTransparency;
			alphaInterpolator.SetStartTransparency( m_nearCenterPart.GetOpacity() );
			alphaInterpolator.SetEndTransparency( 0.0 );
			alphaInterpolator.SetDuration( duration );
			alphaInterpolator.SetType( inkanimInterpolationType.Linear );
			alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
			anim.AddInterpolator( alphaInterpolator );
			m_nearCenterPart.PlayAnimation( anim );
			m_farCenterPart.PlayAnimation( anim );
		}
	}

	public function ExpandCrosshair( full : Bool, duration : Float )
	{
		var anim : inkAnimDef;
		var marginInterpolator : inkAnimMargin;
		var alphaInterpolator : inkAnimTransparency;
		anim = new inkAnimDef;
		marginInterpolator = new inkAnimMargin;
		marginInterpolator.SetStartMargin( inkMargin( 0.0, 0.0, 0.0, 0.0 ) );
		marginInterpolator.SetEndMargin( inkMargin( -( m_minSpread ) - ( m_bufferedSpread.X * m_gameplaySpreadMultiplier ), 0.0, 0.0, 0.0 ) );
		marginInterpolator.SetDuration( duration );
		marginInterpolator.SetType( inkanimInterpolationType.Linear );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( marginInterpolator );
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetStartTransparency( m_leftPart.GetOpacity() );
		alphaInterpolator.SetEndTransparency( 1.0 );
		alphaInterpolator.SetDuration( duration );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( alphaInterpolator );
		m_leftPart.PlayAnimation( anim );
		anim = new inkAnimDef;
		marginInterpolator = new inkAnimMargin;
		marginInterpolator.SetStartMargin( inkMargin( 0.0, 0.0, 0.0, 0.0 ) );
		marginInterpolator.SetEndMargin( inkMargin( m_minSpread + ( m_bufferedSpread.X * m_gameplaySpreadMultiplier ), 0.0, 0.0, 0.0 ) );
		marginInterpolator.SetDuration( duration );
		marginInterpolator.SetType( inkanimInterpolationType.Linear );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( marginInterpolator );
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetStartTransparency( m_rightPart.GetOpacity() );
		alphaInterpolator.SetEndTransparency( 1.0 );
		alphaInterpolator.SetDuration( duration );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( alphaInterpolator );
		m_rightPart.PlayAnimation( anim );
		if( full )
		{
			anim = new inkAnimDef;
			alphaInterpolator = new inkAnimTransparency;
			alphaInterpolator.SetStartTransparency( m_nearCenterPart.GetOpacity() );
			alphaInterpolator.SetEndTransparency( 1.0 );
			alphaInterpolator.SetDuration( duration );
			alphaInterpolator.SetType( inkanimInterpolationType.Linear );
			alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
			anim.AddInterpolator( alphaInterpolator );
			m_nearCenterPart.PlayAnimation( anim );
			m_farCenterPart.PlayAnimation( anim );
		}
	}

	public function OnState_HipFire()
	{
		var anim : inkAnimDef;
		var sizeInterpolator : inkAnimSize;
		anim = new inkAnimDef;
		sizeInterpolator = new inkAnimSize;
		sizeInterpolator.SetStartSize( Vector2( m_orgSideSize.X / 2.0, m_orgSideSize.Y / 2.0 ) );
		sizeInterpolator.SetEndSize( m_orgSideSize );
		sizeInterpolator.SetDuration( 0.1 );
		sizeInterpolator.SetType( inkanimInterpolationType.Linear );
		sizeInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( sizeInterpolator );
		m_leftPart.PlayAnimation( anim );
		m_rightPart.PlayAnimation( anim );
		ExpandCrosshair( false, 0.1 );
	}

	private function OnState_Aim()
	{
		var anim : inkAnimDef;
		var sizeInterpolator : inkAnimSize;
		anim = new inkAnimDef;
		sizeInterpolator = new inkAnimSize;
		sizeInterpolator.SetStartSize( m_orgSideSize );
		sizeInterpolator.SetEndSize( Vector2( m_orgSideSize.X / 2.0, m_orgSideSize.Y / 2.0 ) );
		sizeInterpolator.SetDuration( 0.1 );
		sizeInterpolator.SetType( inkanimInterpolationType.Linear );
		sizeInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		anim.AddInterpolator( sizeInterpolator );
		m_leftPart.PlayAnimation( anim );
		m_rightPart.PlayAnimation( anim );
		ExpandCrosshair( false, 0.1 );
	}

	private function OnState_Reload()
	{
		ColapseCrosshair( false, 0.25 );
	}

	public function OnState_Sprint()
	{
		ColapseCrosshair( false, 0.1 );
	}

	public function GetUIActiveWeaponBlackboard() : IBlackboard
	{
		return GetBlackboardSystem().Get( GetAllBlackboardDefs().UI_ActiveWeaponData );
	}

}

