JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
input.hpp
Go to the documentation of this file.
1// Copyright 2012-2024 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include "Finally.hpp"
8#include "Vec2.hpp"
9
10#include <functional>
11#include <memory>
12#include <vector>
13#if defined(__has_include) && __has_include(<optional>)
14#include <optional>
15using std::optional;
16#else
17#include <experimental/optional>
19#endif
20
21namespace jngl {
22
23namespace key {
25enum KeyType : uint8_t {
26 Left,
27 Up,
28 Right,
29 Down,
30 PageUp,
31 PageDown,
32 Home,
33 End,
34 BackSpace,
35 Tab,
36 Clear,
37 Return,
38 Pause,
39 Escape,
40 Delete,
41 ControlL,
42 ControlR,
43 CapsLock,
44 AltL,
45 AltR,
46
49
52
53 Space,
54 ShiftL,
55 ShiftR,
56 F1,
57 F2,
58 F3,
59 F4,
60 F5,
61 F6,
62 F7,
63 F8,
64 F9,
65 F10,
66 F11,
67 F12,
68 Any
69};
70} // namespace key
71
74
76enum KeyboardType : uint8_t { Default, Numpad };
77
80
83
85void setKeyPressed(const std::string& key, bool);
86
89
92
94bool keyDown(char key);
95
113bool keyDown(const std::string& key);
114
117
119bool keyPressed(char key);
120
123bool keyPressed(const std::string& key);
124
125void setRelativeMouseMode(bool relative);
126
127bool getRelativeMouseMode();
128
130void setMouseVisible(bool visible);
131
133[[nodiscard]] Finally hideMouse();
134
140
145
149
155
161
164[[deprecated("Use jngl::getMousePos() instead")]] int getMouseX();
165
168[[deprecated("Use jngl::getMousePos() instead")]] int getMouseY();
169
170namespace mouse {
172enum Button : uint8_t { Left, Middle, Right };
173} // namespace mouse
174
177
179bool mouseDown(mouse::Button button = mouse::Left);
180
182void setMouseDown(mouse::Button button, bool);
183
185bool mousePressed(mouse::Button button = mouse::Left);
186
189
191void setMouse(Vec2 position);
192
193namespace controller {
195enum Button : uint8_t {
198
201
204
207
210
213
216
219
220 A,
221 B,
222 X,
223 Y,
224
227
230
231 LeftTrigger,
232 RightTrigger,
233
236
239
240 DpadUp,
241 DpadDown,
242 DpadLeft,
243 DpadRight,
244 LeftStick,
245 RightStick,
246 Last
247};
248} // namespace controller
249
250class Controller;
251
257
262void onControllerChanged(std::function<void()> callback);
263
266
267} // namespace jngl
Contains jngl::Finally class.
Contains jngl::Vec2 class.
Helper class which calls a function when being destroyed.
Definition Finally.hpp:22
Two-dimensional vector.
Definition Vec2.hpp:36
KeyType
Keyboard keys.
Definition input.hpp:25
@ SuperL
Left Windows key.
Definition input.hpp:48
@ SuperR
Right Windows key.
Definition input.hpp:51
Button
Mouse buttons.
Definition input.hpp:172
@ RightStickY
y axis of right stick, -1 to 1
Definition input.hpp:215
@ RightStickYInverse
-RightStickY
Definition input.hpp:218
@ Back
Back, Menu, or Share button.
Definition input.hpp:238
@ LeftButton
Left shoulder button.
Definition input.hpp:226
@ RightStickXInverse
-RightStickX
Definition input.hpp:212
@ LeftStickYInverse
-LeftStickY
Definition input.hpp:206
@ LeftStickXInverse
-LeftStickX
Definition input.hpp:200
@ LeftStickX
x axis of left stick, -1 to 1
Definition input.hpp:197
@ RightStickX
x axis of right stick, -1 to 1
Definition input.hpp:209
@ Start
Start, or Options button.
Definition input.hpp:235
@ LeftStickY
y axis of left stick, -1 to 1
Definition input.hpp:203
@ RightButton
Right shoulder button.
Definition input.hpp:229
JNGL's main namespace.
int getMouseX()
Retrieve mouse position in pixels.
KeyboardType
Whether a normal or a numbers-only keyboard should appear.
Definition input.hpp:76
double getMouseWheel()
Returns mouse wheel movement between -100 and 100 (0 if the mousewheel doesn't move)
bool keyDown(key::KeyType key)
Whether key is down.
int getMouseY()
Retrieve mouse position in pixels.
KeyboardType getKeyboardType()
Currently active type of onscreen keyboard.
std::vector< jngl::Vec2 > getTouchPositions()
Returns all positions where a finger touches the screen.
void onControllerChanged(std::function< void()> callback)
Specify a function which gets called, whenever a controller gets added or removed.
void setMouse(Vec2 position)
Moves the mouse (does nothing on iOS and Android)
std::string getTextInput()
Returns a string of characters that have been pressed since the last call to updateInput()
bool mousePressed(mouse::Button button=mouse::Left)
Whether button has been pressed since the next to last call to updateInput()
bool isMultitouch()
Returns true when there's more than one finger touching the screen.
bool mouseDown(mouse::Button button=mouse::Left)
Returns whether button is currently held down.
void setMouseDown(mouse::Button button, bool)
Overwrite what mouseDown() should return.
void setKeyboardVisible(bool)
Display onscreen keyboard for touch devices.
void setKeyboardType(KeyboardType)
Type of the onscreen keyboard.
Finally hideMouse()
Hides the mouse cursor; destroying the returned Finally object will show it again.
void setKeyPressed(const std::string &key, bool)
Overwrite what keyPressed() should return.
bool keyPressed(key::KeyType key)
Whether key has been pressed since the next to last call to updateInput()
void setMouseVisible(bool visible)
By default the mouse cursor of the OS is visible and can be hidden by passing false.
std::vector< std::shared_ptr< Controller > > getConnectedControllers()
Returns all controllers (gamepads) that are connected.
void setMousePressed(jngl::mouse::Button, bool)
Overwrite what mouseDown() should return.
Vec2 getMousePos()
Mouse position in screen coordinates.
bool isMouseVisible()
Returns whether the mouse cursor of the OS is currently visible.
optional< Vec2 > getCursorPos()
Returns the position of the mouse pointer if a mouse is connected/available.