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 "Vec2.hpp"
8
9#include <functional>
10#include <memory>
11#include <vector>
12#if defined(__has_include) && __has_include(<optional>)
13#include <optional>
14using std::optional;
15#else
16#include <experimental/optional>
18#endif
19
20namespace jngl {
21
22namespace key {
24enum KeyType : uint8_t {
25 Left,
26 Up,
27 Right,
28 Down,
29 PageUp,
30 PageDown,
31 Home,
32 End,
33 BackSpace,
34 Tab,
35 Clear,
36 Return,
37 Pause,
38 Escape,
39 Delete,
40 ControlL,
41 ControlR,
42 CapsLock,
43 AltL,
44 AltR,
45
48
51
52 Space,
53 ShiftL,
54 ShiftR,
55 F1,
56 F2,
57 F3,
58 F4,
59 F5,
60 F6,
61 F7,
62 F8,
63 F9,
64 F10,
65 F11,
66 F12,
67 Any
68};
69} // namespace key
70
73
75enum KeyboardType : uint8_t { Default, Numpad };
76
79
82
84void setKeyPressed(const std::string& key, bool);
85
88
91
93bool keyDown(char key);
94
112bool keyDown(const std::string& key);
113
116
118bool keyPressed(char key);
119
122bool keyPressed(const std::string& key);
123
124void setRelativeMouseMode(bool relative);
125
126bool getRelativeMouseMode();
127
129void setMouseVisible(bool visible);
130
136
141
145
151
157
160[[deprecated("Use jngl::getMousePos() instead")]] int getMouseX();
161
164[[deprecated("Use jngl::getMousePos() instead")]] int getMouseY();
165
166namespace mouse {
168enum Button : uint8_t { Left, Middle, Right };
169} // namespace mouse
170
173
175bool mouseDown(mouse::Button button = mouse::Left);
176
178void setMouseDown(mouse::Button button, bool);
179
181bool mousePressed(mouse::Button button = mouse::Left);
182
185
187void setMouse(Vec2 position);
188
189namespace controller {
191enum Button : uint8_t {
194
197
200
203
206
209
212
215
216 A,
217 B,
218 X,
219 Y,
220
223
226
227 LeftTrigger,
228 RightTrigger,
229
232
235
236 DpadUp,
237 DpadDown,
238 DpadLeft,
239 DpadRight,
240 LeftStick,
241 RightStick,
242 Last
243};
244} // namespace controller
245
246class Controller;
247
253
258void onControllerChanged(std::function<void()> callback);
259
262
263} // namespace jngl
Contains jngl::Vec2 class.
Two-dimensional vector.
Definition Vec2.hpp:36
KeyType
Keyboard keys.
Definition input.hpp:24
@ SuperL
Left Windows key.
Definition input.hpp:47
@ SuperR
Right Windows key.
Definition input.hpp:50
Button
Mouse buttons.
Definition input.hpp:168
@ RightStickY
y axis of right stick, -1 to 1
Definition input.hpp:211
@ RightStickYInverse
-RightStickY
Definition input.hpp:214
@ Back
Back, Menu, or Share button.
Definition input.hpp:234
@ LeftButton
Left shoulder button.
Definition input.hpp:222
@ RightStickXInverse
-RightStickX
Definition input.hpp:208
@ LeftStickYInverse
-LeftStickY
Definition input.hpp:202
@ LeftStickXInverse
-LeftStickX
Definition input.hpp:196
@ LeftStickX
x axis of left stick, -1 to 1
Definition input.hpp:193
@ RightStickX
x axis of right stick, -1 to 1
Definition input.hpp:205
@ Start
Start, or Options button.
Definition input.hpp:231
@ LeftStickY
y axis of left stick, -1 to 1
Definition input.hpp:199
@ RightButton
Right shoulder button.
Definition input.hpp:225
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:75
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.
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.