Class GLFWInputSystem

java.lang.Object
com.blackrook.gloop.glfw.GLFWInputSystem

public class GLFWInputSystem extends Object
An input event filtering system, meant to be used as a "one-stop" management class.

Annotated input objects are attached to this system as listeners and have their fields altered and methods called when events are sent through via window events and other device polling like joysticks/gamepads.

Since the Main thread is the thread responsible for carrying out callbacks and other listeners via a window, all input objects should handle their event consumption as lightly as possible so that other event processing doesn't get held up. If actions need to be taken on value changes via mouse/keyboard events, it might be worth processing in a parallel thread and kicked off by a listener that listens for event dispatch completion.

Author:
Matthew Tropiano
  • Constructor Details

    • GLFWInputSystem

      public GLFWInputSystem()
      Creates a new input system.
  • Method Details

    • addInputObject

      public void addInputObject(Object inputObject)
      Adds an object that is annotated for receiving input events to this input system. Adding or removing an object from this system in thread-safe.
      Parameters:
      inputObject - the input object to add.
      Throws:
      InputSetupException - if an object's annotations are not set up properly.
    • removeInputObject

      public void removeInputObject(Object inputObject)
      Removes an object that is annotated for receiving input events from this input system. Adding or removing an object from this system in thread-safe.
      Parameters:
      inputObject - the input object to remove.
    • addJoystickInputObject

      public void addJoystickInputObject(int jid, Object inputObject)
      Adds a joystick-specific object that is annotated for receiving input events to this input system. Adding or removing an object from this system is thread-safe. Adding a joystick where a JID is already used will remove the old one.
      Parameters:
      jid - the joystick id.
      inputObject - the input object to add.
      Throws:
      InputSetupException - if an object's annotations are not set up properly.
    • addJoystickInputObject

      public void addJoystickInputObject(int jid, GLFWInputSystem.JoystickInputParameters parameters, Object inputObject)
      Adds a joystick-specific object that is annotated for receiving input events to this input system. Adding or removing an object from this system is thread-safe. Adding a joystick where a JID is already used will remove the old one.
      Parameters:
      jid - the joystick id.
      parameters - the joystick parameters.
      inputObject - the input object to add.
      Throws:
      InputSetupException - if an object's annotations are not set up properly.
    • removeJoystickInputObject

      public void removeJoystickInputObject(int jid)
      Removes a joystick-specific object that is annotated for receiving input events to this input system. Adding or removing an object from this system in thread-safe.
      Parameters:
      jid - the joystick id.
      Throws:
      InputSetupException - if an object's annotations are not set up properly.
    • attachToWindow

      public void attachToWindow(GLFWWindow window)
      Attaches this input system to a window. This listens for key/mouse events.

      Essentially just attaches the window and input listener of this system to the window.

      Parameters:
      window - the window to accept events from.
    • detachFromWindow

      public void detachFromWindow(GLFWWindow window)
      Detaches this input system from a window.

      Essentially just detaches the window and input listener of this system from the window.

      Parameters:
      window - the window to remove from.
    • addJoystickListener

      public void addJoystickListener(GLFWInputSystem.JoystickConnectionListener listener)
      Adds a joystick listener to this input system to listen for joystick connections and disconnections. Usually, an annotated input object is added on connection, and removed on disconnect. This method is thread safe.

      Joystick support does not need to be enabled for this to be called.

      Parameters:
      listener - the listener to add.
    • removeJoystickListener

      public void removeJoystickListener(GLFWInputSystem.JoystickConnectionListener listener)
      Removes a joystick listener from this input system. This method is thread safe.

      Joystick support does not need to be enabled for this to be called.

      Parameters:
      listener - the listener to remove.
    • enableJoysticks

      public void enableJoysticks()
      This method must be called in order to prep joysticks for polling. If joysticks are already enabled, this does nothing.

      This will fire connection events to GLFWInputSystem.JoystickConnectionListeners for each joystick found at enable so that they can be attached as input listeners.

      This must only be called from the main thread.

    • disableJoysticks

      public void disableJoysticks()
      This method must be called in order to prep joysticks for polling. If joysticks are already enabled, this does nothing.
    • pollJoysticks

      public void pollJoysticks()
      Polls all connected joysticks.

      This must only be called from the main thread.

    • fireKeyEvent

      public boolean fireKeyEvent(KeyType type, boolean pressed)
      Fires a key event in this system.
      Parameters:
      type - the key type.
      pressed - true if pressed, false if released.
      Returns:
      true if the event was handled by an object, false if not.
    • fireKeyTypedEvent

      public boolean fireKeyTypedEvent(char c)
      Fires a key typed event in this system.
      Parameters:
      c - the character typed.
      Returns:
      true if the event was handled by an object, false if not.
    • fireMouseButtonEvent

      public boolean fireMouseButtonEvent(MouseButtonType type, boolean pressed)
      Fires a mouse button event in this system.
      Parameters:
      type - the mouse button type.
      pressed - true if pressed, false if released.
      Returns:
      true if the event was handled by an object, false if not.
    • fireMouseAxisEvent

      public boolean fireMouseAxisEvent(MouseAxisType type, double amount)
      Fires a mouse axis event in this system.
      Parameters:
      type - the mouse axis type.
      amount - the amount moved.
      Returns:
      true if the event was handled by an object, false if not.
    • fireMousePositionEvent

      public boolean fireMousePositionEvent(MouseAxisType type, double value)
      Fires a mouse position event in this system.
      Parameters:
      type - the mouse axis type.
      value - the mouse position value.
      Returns:
      true if the event was handled by an object, false if not.
    • fireMouseScrollEvent

      public boolean fireMouseScrollEvent(MouseScrollType type, double amount)
      Fires a mouse scroll event in this system.
      Parameters:
      type - the mouse axis type.
      amount - the scroll amount.
      Returns:
      true if the event was handled by an object, false if not.
    • fireJoystickButtonEvent

      public boolean fireJoystickButtonEvent(int jid, JoystickButtonType type, boolean pressed)
      Fires a joystick button event in this system.
      Parameters:
      jid - the joystick id to fire to.
      type - the joystick button type.
      pressed - true if pressed, false if released.
      Returns:
      true if the event was handled by an object, false if not.
    • fireJoystickAxisEvent

      public boolean fireJoystickAxisEvent(int jid, JoystickAxisType type, double amount)
      Fires a joystick axis event in this system.
      Parameters:
      jid - the joystick id to fire to.
      type - the joystick axis type.
      amount - the amount moved.
      Returns:
      true if the event was handled by an object, false if not.
    • fireJoystickHatEvent

      public boolean fireJoystickHatEvent(int jid, int index, JoystickHatType type)
      Fires a joystick hat event in this system.
      Parameters:
      jid - the joystick id to fire to.
      index - the hat index on the device.
      type - the joystick hat type.
      Returns:
      true if the event was handled by an object, false if not.
    • fireJoystickDirectionEvent

      public boolean fireJoystickDirectionEvent(int jid, JoystickAxisType type, JoystickDirectionType direction)
      Fires a joystick direction event in this system.
      Parameters:
      jid - the joystick id to fire to.
      type - the joystick axis type.
      direction - the joystick hat direction type.
      Returns:
      true if the event was handled by an object, false if not.