<commit_before>﻿using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Nez.Systems;
using System.Runtime.CompilerServices;


namespace Nez
{
	public static class Input
	{
		public static Emitter<InputEventType, InputEvent> Emitter;

		public static GamePadData[] GamePads;
		public const float DEFAULT_DEADZONE = 0.1f;

		internal static Vector2 _resolutionScale;
		internal static Point _resolutionOffset;
		static KeyboardState _previousKbState;
		static KeyboardState _currentKbState;
		static MouseState _previousMouseState;
		static MouseState _currentMouseState;
		static internal FastList<VirtualInput> _virtualInputs = new FastList<VirtualInput>();
		static int _maxSupportedGamePads;

		/// <summary>
		/// the TouchInput details when on a device that supports touch
		/// </summary>
		public static TouchInput Touch;

		/// <summary>
		/// set by the Scene and used to scale mouse input for cases where the Scene render target is a different size
		/// than the backbuffer. This situation basically results in mouse coordinates in screen space instead of
		/// in the render target coordinate system;
		/// </summary>
		public static Vector2 ResolutionScale => _resolutionScale;

		/// <summary>
		/// set by the Scene and used to get mouse input from raw screen coordinates to render target coordinates. Any
		/// SceneResolutionPolicy that can result in letterboxing could potentially have an offset (basically, the
		/// letterbox portion of the render).
		/// </summary>
		/// <returns></returns>
		public static Vector2 ResolutionOffset => _resolutionOffset.ToVector2();

		/// <summary>
		/// gets/sets the maximum supported gamepads
		/// </summary>
		/// <value></value>
		public static int MaxSupportedGamePads
		{
			get { return _maxSupportedGamePads; }
			set
			{
#if FNA
				_maxSupportedGamePads = Mathf.Clamp( value, 1, 8 );
#else
				_maxSupportedGamePads = Mathf.Clamp(value, 1, GamePad.MaximumGamePadCount);
#endif
				GamePads = new GamePadData[_maxSupportedGamePads];
				for (var i = 0; i < _maxSupportedGamePads; i++)
					GamePads[i] = new GamePadData((PlayerIndex) i);
			}
		}


		static Input()
		{
			Emitter = new Emitter<InputEventType, InputEvent>();
			Touch = new TouchInput();

			_previousKbState = new KeyboardState();
			_currentKbState = Keyboard.GetState();

			_previousMouseState = new MouseState();
			_currentMouseState = Mouse.GetState();

			MaxSupportedGamePads = 1;
		}


		public static void Update()
		{
			Touch.Update();

			_previousKbState = _currentKbState;
			_currentKbState = Keyboard.GetState();

			_previousMouseState = _currentMouseState;
			_currentMouseState = Mouse.GetState();

			for (var i = 0; i < _maxSupportedGamePads; i++)
				GamePads[i].Update();

			for (var i = 0; i < _virtualInputs.Length; i++)
				_virtualInputs.Buffer[i].Update();
		}

		/// <summary>
		/// this takes into account the SceneResolutionPolicy and returns the value scaled to the RenderTargets coordinates
		/// </summary>
		/// <value>The scaled position.</value>
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static Vector2 ScaledPosition(Vector2 position)
		{
			var scaledPos = new Vector2(position.X - _resolutionOffset.X, position.Y - _resolutionOffset.Y);
			return scaledPos * _resolutionScale;
		}

		/// <summary>
		/// to be used with great care! This lets you override the current MouseState. This is useful
		/// when the Nez render is embedded in a larger window so that mouse coordinates can be translated
		/// to Nez space from the outer window coordinates and for simulating mouse input.
		/// </summary>
		/// <param name="state"></param>
		public static void SetCurrentMouseState(MouseState state)
		{
			_currentMouseState = state;
		}


		/// <summary>
		/// useful for simulating keyboard input
		/// </summary>
		/// <param name="state">State.</param>
		public static void SetCurrentKeyboardState(KeyboardState state)
		{
			_currentKbState = state;
		}

		#region Keyboard

		/// <summary>
		/// returns the previous KeyboardState from the last frame
		/// </summary>
		/// <value></value>
		public static KeyboardState PreviousKeyboardState => _previousKbState;

		/// <summary>
		/// returns the KeyboardState from this frame
		/// </summary>
		/// <value></value>
		public static KeyboardState CurrentKeyboardState => _currentKbState;


		/// <summary>
		/// only true if down this frame and not down the previous frame
		/// </summary>
		/// <returns><c>true</c>, if key pressed was gotten, <c>false</c> otherwise.</returns>
		public static bool IsKeyPressed(Keys key)
		{
			return _currentKbState.IsKeyDown(key) && !_previousKbState.IsKeyDown(key);<commit_msg>Add SetPreviousMouseState and SetPreviousKeyboardState (#664)<commit_after>﻿using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Nez.Systems;
using System.Runtime.CompilerServices;


namespace Nez
{
	public static class Input
	{
		public static Emitter<InputEventType, InputEvent> Emitter;

		public static GamePadData[] GamePads;
		public const float DEFAULT_DEADZONE = 0.1f;

		internal static Vector2 _resolutionScale;
		internal static Point _resolutionOffset;
		static KeyboardState _previousKbState;
		static KeyboardState _currentKbState;
		static MouseState _previousMouseState;
		static MouseState _currentMouseState;
		static internal FastList<VirtualInput> _virtualInputs = new FastList<VirtualInput>();
		static int _maxSupportedGamePads;

		/// <summary>
		/// the TouchInput details when on a device that supports touch
		/// </summary>
		public static TouchInput Touch;

		/// <summary>
		/// set by the Scene and used to scale mouse input for cases where the Scene render target is a different size
		/// than the backbuffer. This situation basically results in mouse coordinates in screen space instead of
		/// in the render target coordinate system;
		/// </summary>
		public static Vector2 ResolutionScale => _resolutionScale;

		/// <summary>
		/// set by the Scene and used to get mouse input from raw screen coordinates to render target coordinates. Any
		/// SceneResolutionPolicy that can result in letterboxing could potentially have an offset (basically, the
		/// letterbox portion of the render).
		/// </summary>
		/// <returns></returns>
		public static Vector2 ResolutionOffset => _resolutionOffset.ToVector2();

		/// <summary>
		/// gets/sets the maximum supported gamepads
		/// </summary>
		/// <value></value>
		public static int MaxSupportedGamePads
		{
			get { return _maxSupportedGamePads; }
			set
			{
#if FNA
				_maxSupportedGamePads = Mathf.Clamp( value, 1, 8 );
#else
				_maxSupportedGamePads = Mathf.Clamp(value, 1, GamePad.MaximumGamePadCount);
#endif
				GamePads = new GamePadData[_maxSupportedGamePads];
				for (var i = 0; i < _maxSupportedGamePads; i++)
					GamePads[i] = new GamePadData((PlayerIndex) i);
			}
		}


		static Input()
		{
			Emitter = new Emitter<InputEventType, InputEvent>();
			Touch = new TouchInput();

			_previousKbState = new KeyboardState();
			_currentKbState = Keyboard.GetState();

			_previousMouseState = new MouseState();
			_currentMouseState = Mouse.GetState();

			MaxSupportedGamePads = 1;
		}


		public static void Update()
		{
			Touch.Update();

			_previousKbState = _currentKbState;
			_currentKbState = Keyboard.GetState();

			_previousMouseState = _currentMouseState;
			_currentMouseState = Mouse.GetState();

			for (var i = 0; i < _maxSupportedGamePads; i++)
				GamePads[i].Update();

			for (var i = 0; i < _virtualInputs.Length; i++)
				_virtualInputs.Buffer[i].Update();
		}

		/// <summary>
		/// this takes into account the SceneResolutionPolicy and returns the value scaled to the RenderTargets coordinates
		/// </summary>
		/// <value>The scaled position.</value>
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static Vector2 ScaledPosition(Vector2 position)
		{
			var scaledPos = new Vector2(position.X - _resolutionOffset.X, position.Y - _resolutionOffset.Y);
			return scaledPos * _resolutionScale;
		}

		/// <summary>
		/// to be used with great care! This lets you override the current MouseState. This is useful
		/// when the Nez render is embedded in a larger window so that mouse coordinates can be translated
		/// to Nez space from the outer window coordinates and for simulating mouse input.
		/// </summary>
		/// <param name="state"></param>
		public static void SetCurrentMouseState(MouseState state)
		{
			_currentMouseState = state;
		}

		/// <summary>
		/// useful for simulating mouse input
		/// </summary>
		/// <param name="state"></param>
		public static void SetPreviousMouseState(MouseState state)
		{
			_previousMouseState = state;
		}
		
		/// <summary>
		/// useful for simulating keyboard input
		/// </summary>
		/// <param name="state">State.</param>
		public static void SetCurrentKeyboardState(KeyboardState state)
		{
			_currentKbState = state;
		}
		
		/// <summary>
		/// useful for simulating keyboard input
		/// </summary>
		/// <param name="state">State.</param>
		public static void SetPreviousKeyboardState(KeyboardState state)
		{
			_previousKbState = state;
		}

		#region Keyboard

		/// <summary>
		/// returns the previous KeyboardState from the last frame
		/// </summary>
		/// <value></value>
		public static KeyboardState PreviousKeyboardState => _previousKbState;

		/// <summary>
		/// returns the KeyboardState from this frame
		/// </summary>
		/// <value></value>
		public static KeyboardState CurrentKeyboardState => _currentKbState;


		/// <summary>
		/// only true if down this frame and not down the previous frame
		/// </summary>
		/// <returns><c>true</c>, if key pressed was gotten, <c>false</c> otherwise.</returns>
		public static bool IsKeyPressed(Keys key)
		{
			return _currentKbState.IsKeyDown(key) && !_previousKbState.IsKeyDown(key);
{'fraction_non_alphanumeric': 0.1067500756734941, 'fraction_numerical': 0.0035314297245484815, 'mean_word_length': 3.608089260808926, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 107, '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': '3597189', 'n_tokens_mistral': 3252, 'n_tokens_neox': 2831, 'n_words': 901}