Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
edgelord edited this page Feb 22, 2019 · 1 revision

Input

In Salty Engine, there is one class for handling Input, and two ways of handling it.

  1. the Listener-Based event system
  2. the easier, more accessible non-event-based way

The pro and cons for each way should be clear, when using the event-driven, you have events and the other way is easier to use and more accessible.

1. Input

Salty Engine listens for two different kinds of input: the one from the Keyboard and from the mouse. The central class for input is de.edgelord.saltyengine.input.Input, with lots of static methods for handling input.

2. The Listener-Based way

2.1 Listening for Keyboard events

To listen for keyboard input events, you can add an implementation of de.edgelord.saltyengine.input.KeyboardInputHandler to the event queue using Input#addKeyboardInputHandler(KeyboardInputHandler). The KeyboardInputHandler has three methods available, each working with the default KeyEvent from java.awt.event:

void keyPressed(KeyEvent e);

void keyReleased(KeyEvent e);

void keyTyped(KeyEvent e);

2.2 Listening for Keyboard events

To listen for mouse input events, you can add an implementation of de.edgelord.saltyengine.MouseInputHandler to the event queue using Input#addMouseInputHandler(MouseInputHandler). The MouseInputhandler has eight methods available, each one working with the default MouseEvent from java.awt.event:

void mouseMoved(MouseEvent e);

void mouseDragged(MouseEvent e);

void mousePressed(MouseEvent e);

void mouseReleased(MouseEvent e);

void mouseClicked(MouseEvent e);

void mouseExitedScreen(MouseEvent e);

void mouseEnteredScreen(MouseEvent e);

void mouseWheelMoved(MouseEvent e);

The methods for both handlers should be self-explaining, but if not, you can look them up in the internet, they are named conventional.

3. The Non-Listener-Based way

3.1 Working with Keyboard input

You can access input left, right, up and down simply with the methods Input#isInputLeft(), Input#isInputRight() and so on.
These events are triggered by the arrow keys and WASD.

You can access any other keyboard input as an instance of `Keyboard` via `Input#getKeyboardInput()`.
You can get the state of each button with separate methods named like `Keyboard#isEnter()`, `Keyboard#isBackspace()`, `Keyboard#isNumber_0()` and `Keyboard#isKey_d()`.

Other ways of handling keyboard input without listeners: - the last typed key is stored as a char, you can access it via `Input#getLastInputKey()` - the last `KeyEvent` is stored as well, you can access it via `Input#getLastInput()` - an instance of `Directions` containing the currently inputted directions as explained above via `Input#getInput()`

3.2 Working with Mouse input

You can access states of the mouse from anywhere at any time as following:

  • You can access the mouse position, relative to the camera position as an instance of Coordinates2f via Input#getCursorPosition() - this is the position you would normally want to use
  • You can access the absolute mouse position (relative to the screen) as a Coordinates2f via Input#getAbsoluteCursorPosition(), you would want to use this only for e.g. ui, as the UIElements do not move with the camera
  • You can access a Transform of the mouse (with the relative position and the size 1 by 1) via Input#getCursor()
  • You can access the same kind of Transform only with the absolute position as described above via Input#getAbsoluteCursor()
  • And finally, you can access whether the mouse is currently down or not via Input#isMouseDown() and if the mouse currently drag via Input#isMouseDrags()