-
Notifications
You must be signed in to change notification settings - Fork 6
Input
In Salty Engine, there is one class for handling Input, and two ways of handling it.
- the Listener-Based event system
- 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.
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.
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);
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.
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()`
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
viaInput#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
viaInput#getAbsoluteCursorPosition()
, you would want to use this only for e.g. ui, as theUIElements
do not move with the camera - You can access a
Transform
of the mouse (with the relative position and the size 1 by 1) viaInput#getCursor()
- You can access the same kind of
Transform
only with the absolute position as described above viaInput#getAbsoluteCursor()
- And finally, you can access whether the mouse is currently down or not via
Input#isMouseDown()
and if the mouse currently drag viaInput#isMouseDrags()