Input System

The Input System allows users to interact with the game world. It provides a consistent way to handle input events, such as keyboard presses, mouse clicks, and touch events.

Input Manager

The Input Manager is responsible for handling all input events. It uses the InputManager.cs class, which is located in the Gleed2D.Input namespace.

The InputManager class provides the following methods for handling input events:

  • GetKeyDown(KeyCode key): Returns true if the specified key is pressed.

  • GetKeyUp(KeyCode key): Returns true if the specified key is released.

  • GetKey(KeyCode key): Returns true if the specified key is currently pressed.

  • GetMouseButtonDown(int button): Returns true if the specified mouse button is pressed.

  • GetMouseButtonUp(int button): Returns true if the specified mouse button is released.

  • GetMouseButton(int button): Returns true if the specified mouse button is currently pressed.

  • GetAxis(string axis): Returns the current value of the specified axis.

  • GetTouchCount(): Returns the number of touches currently active.

  • GetTouch(int index): Returns the specified touch.

Example:

// Check if the 'A' key is pressed.
          if (InputManager.GetKeyDown(KeyCode.A)) {
              // Handle the key press.
          }
          
          // Check if the left mouse button is clicked.
          if (InputManager.GetMouseButtonDown(0)) {
              // Handle the mouse click.
          }
          
          // Get the current value of the horizontal axis.
          float horizontalAxis = InputManager.GetAxis("Horizontal");
          
          // Check if there are any touches.
          if (InputManager.GetTouchCount() > 0) {
              // Handle the touch event.
          }
          

Input Actions

Input Actions are a more advanced way of handling input events. They allow you to define custom input combinations and bindings.

The InputAction class, located in the Gleed2D.Input namespace, provides the following properties:

  • Name: The name of the action.
  • Description: A description of the action.
  • Bindings: A list of bindings for the action.

Example:

// Create a new InputAction called "Jump".
          InputAction jumpAction = new InputAction("Jump");
          
          // Add a binding for the 'Space' key.
          jumpAction.Bindings.Add(new InputBinding("Space"));
          
          // Add a binding for the 'W' key.
          jumpAction.Bindings.Add(new InputBinding("W"));
          
          // Check if the 'Jump' action is triggered.
          if (jumpAction.IsTriggered()) {
              // Handle the jump action.
          }
          

Input Bindings

Input Bindings define how input events are mapped to Input Actions.

The InputBinding class, located in the Gleed2D.Input namespace, provides the following properties:

  • Type: The type of input event.
  • Key: The key or button to bind.
  • Axis: The axis to bind.

Example:

// Create a new InputBinding for the 'Space' key.
          InputBinding spaceBinding = new InputBinding(InputType.Key, KeyCode.Space);
          
          // Create a new InputBinding for the left mouse button.
          InputBinding leftMouseBinding = new InputBinding(InputType.MouseButton, 0);
          
          // Create a new InputBinding for the horizontal axis.
          InputBinding horizontalAxisBinding = new InputBinding(InputType.Axis, "Horizontal");
          

Input Devices

The Input System supports a variety of input devices, including:

  • Keyboard
  • Mouse
  • Touch screen
  • Gamepad

Example:

// Check if the keyboard is connected.
          if (InputManager.IsKeyboardConnected()) {
              // Use the keyboard for input.
          }
          
          // Check if the mouse is connected.
          if (InputManager.IsMouseConnected()) {
              // Use the mouse for input.
          }
          
          // Check if the touch screen is connected.
          if (InputManager.IsTouchscreenConnected()) {
              // Use the touch screen for input.
          }
          
          // Check if a gamepad is connected.
          if (InputManager.IsGamepadConnected()) {
              // Use the gamepad for input.
          }
          

Input Settings

The Input System allows you to customize input settings, such as:

  • Sensitivity: The sensitivity of the mouse and other input devices.
  • Dead zone: The dead zone for analog input devices, such as joysticks.
  • Key bindings: The key bindings for the keyboard and other input devices.

Example:

// Set the mouse sensitivity.
          InputManager.MouseSensitivity = 0.5f;
          
          // Set the dead zone for the horizontal axis.
          InputManager.AxisDeadZone = 0.1f;
          
          // Set the key binding for the 'Jump' action.
          InputManager.SetKeyBinding("Jump", KeyCode.Space);
          

References