class PersistentDotSettingsListener extends ConfigVarListener
{
	private var m_controller : weak< CrosshairGameControllerPersistentDot >;

	public function RegisterController( controller : CrosshairGameControllerPersistentDot )
	{
		m_controller = controller;
	}

	public override function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		m_controller.OnVarModified( groupPath, varName, varType, reason );
	}

}

class CrosshairGameControllerPersistentDot extends inkHUDGameController
{
	private var m_settings : UserSettings;
	private var m_settingsListener : PersistentDotSettingsListener;
	private var m_groupPath : CName;
	default m_groupPath = '/accessibility/interface';
	private var m_isAiming : Bool;
	private var m_psmUpperBodyStateCallback : CallbackHandle;

	protected event OnInitialize()
	{
		m_settings = GetSystemRequestsHandler().GetUserSettings();
		m_settingsListener = new PersistentDotSettingsListener;
		m_settingsListener.RegisterController( this );
		m_settingsListener.Register( m_groupPath );
		UpdateRootVisibility();
	}

	protected event OnPlayerAttach( player : GameObject )
	{
		var psmBlackboard : IBlackboard;
		psmBlackboard = GetPSMBlackboard( player );
		m_psmUpperBodyStateCallback = psmBlackboard.RegisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.UpperBody, this, 'OnPSMUpperBodyStateChanged' );
	}

	protected event OnPlayerDetach( player : GameObject )
	{
		var psmBlackboard : IBlackboard;
		psmBlackboard = GetPSMBlackboard( player );
		if( m_psmUpperBodyStateCallback )
		{
			psmBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.UpperBody, m_psmUpperBodyStateCallback );
		}
	}

	protected event OnPSMUpperBodyStateChanged( value : Int32 )
	{
		m_isAiming = value == ( ( Int32 )( gamePSMUpperBodyStates.Aim ) );
		UpdateRootVisibility();
	}

	public function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		if( varName == 'PersistentCenterDot' )
		{
			UpdateRootVisibility();
		}
	}

	private function UpdateRootVisibility()
	{
		var configVar : ConfigVarBool;
		configVar = ( ( ConfigVarBool )( m_settings.GetVar( m_groupPath, 'PersistentCenterDot' ) ) );
		GetRootWidget().SetVisible( configVar.GetValue() && !( m_isAiming ) );
	}

}

