<commit_before>﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;


namespace Nez
{
	public class GamePadData
	{
		/// <summary>
		/// toggles inverting the left sticks vertical value
		/// </summary>
		public bool isLeftStickVertcialInverted = false;

		/// <summary>
		/// toggles inverting the right sticks vertical value
		/// </summary>
		public bool isRightStickVertcialInverted = false;

		public GamePadDeadZone deadZone = GamePadDeadZone.IndependentAxes;

		PlayerIndex _playerIndex;
		GamePadState _previousState;
		GamePadState _currentState;
		float _rumbleTime;


		internal GamePadData( PlayerIndex playerIndex )
		{
			_playerIndex = playerIndex;
			_previousState = new GamePadState();
			_currentState = GamePad.GetState( _playerIndex );
		}


		public void update()
		{
			_previousState = _currentState;
			_currentState = GamePad.GetState( _playerIndex, deadZone );

			// check for controller connects/disconnects
			if( _previousState.IsConnected != _currentState.IsConnected )
			{
				var data = new InputEvent {
					gamePadIndex = (int)_playerIndex
				};
				Input.emitter.emit( _currentState.IsConnected ? InputEventType.GamePadConnected : InputEventType.GamePadDisconnected, data );
			}

			if( _rumbleTime > 0f )
			{
				_rumbleTime -= Time.deltaTime;
				if( _rumbleTime <= 0f )
					GamePad.SetVibration( _playerIndex, 0, 0 );
			}
		}


		public void setVibration( float left, float right, float duration )
		{
			_rumbleTime = duration;
			GamePad.SetVibration( _playerIndex, left, right );
		}


		public void stopVibration()
		{
			GamePad.SetVibration( _playerIndex, 0, 0 );
			_rumbleTime = 0f;
		}


		/// <summary>
		/// returns true if this game pad is connected
		/// </summary>
		/// <returns><c>true</c>, if connected was ised, <c>false</c> otherwise.</returns>
		public bool isConnected()
		{
			return _currentState.IsConnected;
		}


		#region Buttons

		/// <summary>
		/// only true if down this frame
		/// </summary>
		/// <returns><c>true</c>, if button pressed was ised, <c>false</c> otherwise.</returns>
		/// <param name="button">Button.</param>
		public bool isButtonPressed( Buttons button )
		{
			return _currentState.IsButtonDown( button ) && !_previousState.IsButtonDown( button );
		}


		/// <summary>
		/// true the entire time the button is down
		/// </summary>
		/// <returns><c>true</c>, if button down was ised, <c>false</c> otherwise.</returns>
		/// <param name="button">Button.</param>
		public bool isButtonDown( Buttons button )
		{
			return _currentState.IsButtonDown( button );
		}


		/// <summary>
		/// true only the frame the button is released
		/// </summary>
		/// <returns><c>true</c>, if button released was ised, <c>false</c> otherwise.</returns>
		/// <param name="button">Button.</param>
		public bool isButtonReleased( Buttons button )
		{
			return !_currentState.IsButtonDown( button ) && _previousState.IsButtonDown( button );
		}

		#endregion


		#region Sticks

		public Vector2 getLeftStick()
		{
			var res = _currentState.ThumbSticks.Left;

			if( isLeftStickVertcialInverted )
				res.Y = -res.Y;

			return res;
		}


		public Vector2 getLeftStick( float deadzone )
		{
			var res = _currentState.ThumbSticks.Left;

			if( res.LengthSquared() < deadzone * deadzone )
				res = Vector2.Zero;
			else if( isLeftStickVertcialInverted )
				res.Y = -res.Y;

			return res;
		}


		public Vector2 getRightStick()
		{
			var res = _currentState.ThumbSticks.Right;

			if( isRightStickVertcialInverted )
				res.Y = -res.Y;

			return res;
		}


		public Vector2 getRightStick( float deadzone )
		{
			var res = _currentState.ThumbSticks.Right;

			if( res.LengthSquared() < deadzone * deadzone )
				res = Vector2.Zero;
			else if( isRightStickVertcialInverted )
				res.Y = -res.Y;

			return res;
		}

		#endregion


		#region Sticks as Buttons

		public bool isLeftStickLeft( float deadzone = Input.DEFAULT_DEADZONE )
		{
			return _currentState.ThumbSticks.Left.X < -deadzone;
		}


		/// <summary>
		/// true only the frame the stick passes the deadzone in the direction
		/// </summary>
		/// <returns><c>true</c>, if left stick left pressed was ised, <c>false</c> otherwise.</returns>
		/// <param name="deadzone">Deadzone.</param>
		public bool isLeftStickLeftPressed( float deadzone = Input.DEFAULT_DEADZONE )
		{
			return _currentState.ThumbSticks.Left.X < -deadzone && _previousState.ThumbSticks.Left.X > -deadzone;
		}
<commit_msg>fix spelling of vertical in GamePadData (#470)<commit_after>﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;


namespace Nez
{
	public class GamePadData
	{
		/// <summary>
		/// toggles inverting the left sticks vertical value
		/// </summary>
		public bool isLeftStickVerticalInverted = false;

		/// <summary>
		/// toggles inverting the right sticks vertical value
		/// </summary>
		public bool isRightStickVerticalInverted = false;

		public GamePadDeadZone deadZone = GamePadDeadZone.IndependentAxes;

		PlayerIndex _playerIndex;
		GamePadState _previousState;
		GamePadState _currentState;
		float _rumbleTime;


		internal GamePadData( PlayerIndex playerIndex )
		{
			_playerIndex = playerIndex;
			_previousState = new GamePadState();
			_currentState = GamePad.GetState( _playerIndex );
		}


		public void update()
		{
			_previousState = _currentState;
			_currentState = GamePad.GetState( _playerIndex, deadZone );

			// check for controller connects/disconnects
			if( _previousState.IsConnected != _currentState.IsConnected )
			{
				var data = new InputEvent {
					gamePadIndex = (int)_playerIndex
				};
				Input.emitter.emit( _currentState.IsConnected ? InputEventType.GamePadConnected : InputEventType.GamePadDisconnected, data );
			}

			if( _rumbleTime > 0f )
			{
				_rumbleTime -= Time.deltaTime;
				if( _rumbleTime <= 0f )
					GamePad.SetVibration( _playerIndex, 0, 0 );
			}
		}


		public void setVibration( float left, float right, float duration )
		{
			_rumbleTime = duration;
			GamePad.SetVibration( _playerIndex, left, right );
		}


		public void stopVibration()
		{
			GamePad.SetVibration( _playerIndex, 0, 0 );
			_rumbleTime = 0f;
		}


		/// <summary>
		/// returns true if this game pad is connected
		/// </summary>
		/// <returns><c>true</c>, if connected was ised, <c>false</c> otherwise.</returns>
		public bool isConnected()
		{
			return _currentState.IsConnected;
		}


		#region Buttons

		/// <summary>
		/// only true if down this frame
		/// </summary>
		/// <returns><c>true</c>, if button pressed was ised, <c>false</c> otherwise.</returns>
		/// <param name="button">Button.</param>
		public bool isButtonPressed( Buttons button )
		{
			return _currentState.IsButtonDown( button ) && !_previousState.IsButtonDown( button );
		}


		/// <summary>
		/// true the entire time the button is down
		/// </summary>
		/// <returns><c>true</c>, if button down was ised, <c>false</c> otherwise.</returns>
		/// <param name="button">Button.</param>
		public bool isButtonDown( Buttons button )
		{
			return _currentState.IsButtonDown( button );
		}


		/// <summary>
		/// true only the frame the button is released
		/// </summary>
		/// <returns><c>true</c>, if button released was ised, <c>false</c> otherwise.</returns>
		/// <param name="button">Button.</param>
		public bool isButtonReleased( Buttons button )
		{
			return !_currentState.IsButtonDown( button ) && _previousState.IsButtonDown( button );
		}

		#endregion


		#region Sticks

		public Vector2 getLeftStick()
		{
			var res = _currentState.ThumbSticks.Left;

			if( isLeftStickVerticalInverted )
				res.Y = -res.Y;

			return res;
		}


		public Vector2 getLeftStick( float deadzone )
		{
			var res = _currentState.ThumbSticks.Left;

			if( res.LengthSquared() < deadzone * deadzone )
				res = Vector2.Zero;
			else if( isLeftStickVerticalInverted )
				res.Y = -res.Y;

			return res;
		}


		public Vector2 getRightStick()
		{
			var res = _currentState.ThumbSticks.Right;

			if( isRightStickVerticalInverted )
				res.Y = -res.Y;

			return res;
		}


		public Vector2 getRightStick( float deadzone )
		{
			var res = _currentState.ThumbSticks.Right;

			if( res.LengthSquared() < deadzone * deadzone )
				res = Vector2.Zero;
			else if( isRightStickVerticalInverted )
				res.Y = -res.Y;

			return res;
		}

		#endregion


		#region Sticks as Buttons

		public bool isLeftStickLeft( float deadzone = Input.DEFAULT_DEADZONE )
		{
			return _currentState.ThumbSticks.Left.X < -deadzone;
		}


		/// <summary>
		/// true only the frame the stick passes the deadzone in the direction
		/// </summary>
		/// <returns><c>true</c>, if left stick left pressed was ised, <c>false</c> otherwise.</returns>
		/// <param name="deadzone">Deadzone.</param>
		public bool isLeftStickLeftPressed( float deadzone = Input.DEFAULT_DEADZONE )
		{
			return _currentState.ThumbSticks.Left.X < -deadzone && _previousState.ThumbSticks.Left.X > -deadzone;
		}

{'fraction_non_alphanumeric': 0.12054519048151044, 'fraction_numerical': 0.003239861467992403, 'mean_word_length': 3.208744710860367, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2626132', 'n_tokens_mistral': 3401, 'n_tokens_neox': 2913, 'n_words': 710}