Input.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_INPUT_HPP
6#define SMK_INPUT_HPP
7
8#include <glm/glm.hpp>
9#include <map>
10#include <memory>
11#include <smk/Touch.hpp>
12#include <vector>
13
14struct GLFWwindow;
15struct EmscriptenTouchEvent;
16
17namespace smk {
18
19/// @brief A class for querying input state (keyboard, mouse, touch).
20/// The Input class is tied to a smk::Window. You need to call regularly
21/// smk::Screen::PoolEvent to update the input state.
22///
23/// To know keyboard and mouse buttons identifer, you need to refer to the GLFW
24/// definitions:
25/// * [keyboard buttons](https://www.glfw.org/docs/latest/group__keys.html)
26/// * [mouse buttons](https://www.glfw.org/docs/latest/group__buttons.html)
27///
28/// @see smk::Window::PoolEvents()
29class Input {
30 public:
31 virtual ~Input() {}
32 // Keyboard ------------------------------------------------------------------
33
34 /// @brief Whether a keyboard button is down or not.
35 /// @return true whenever a keyboard button is hold.
36 /// @param key The keyboard button.
37 virtual bool IsKeyHold(int key) = 0;
38 /// @brief Whether a keyboard button is pressed or not.
39 /// @return true whenever a keyboard button is pressed.
40 /// @param key The keyboard button.
41 virtual bool IsKeyPressed(int key) = 0;
42 /// @brief Whether a keyboard button is released or not.
43 /// @return true whenever a keyboard button is released
44 /// @param key The keyboard button.
45 virtual bool IsKeyReleased(int key) = 0;
46
47 // Mouse ---------------------------------------------------------------------
48
49 /// @brief Whether a mouse button is down or not.
50 /// @return true whenever a mouse button is hold.
51 /// @param key The mouse button.
52 virtual bool IsMouseHeld(int key) = 0;
53 /// @brief Whether a mouse button is pressed or not.
54 /// @return true whenever a mouse button is pressed.
55 /// @param key The mouse button.
56 virtual bool IsMousePressed(int key) = 0;
57 /// @brief Whether a mouse button is released or not.
58 /// @return true whenever a mouse button is released
59 /// @param key The mouse button.
60 virtual bool IsMouseReleased(int key) = 0;
61 /// @brief The mouse position.
62 /// @return the mouse position.
63 virtual glm::vec2 mouse() const = 0;
64
65 // Touch.
66 using FingerID = int;
67
68 /// @brief The touch states.
69 /// @return the touches states.
70 virtual std::map<FingerID, Touch>& touches() = 0;
71
72 // Cursor --------------------------------------------------------------------
73 // A cursor is either the mouse or a touch. This is choosen smartly.
74
75 /// @brief Whether the cursor is down or not.
76 /// @return true whenever the cursor is down.
77 virtual bool IsCursorHeld() = 0;
78 /// @brief Whether the cursor is pressed or not
79 /// @return true whenever the cursor is pressed.
80 virtual bool IsCursorPressed() = 0;
81 /// @brief Whether the cursor is released or not
82 /// @return true whenever the cursor is released.
83 virtual bool IsCursorReleased() = 0;
84 /// @brief The cursor position.
85 /// @return the cursor position.
86 virtual glm::vec2 cursor() const = 0;
87
88 // Scroll -------------------------------------------------------------------
89
90 /// @brief The mouse/touchpad scrolling offset since the last frame.
91 /// @return the scrolling offset.
92 virtual glm::vec2 scroll_offset() const = 0;
93
94 // Character listener --------------------------------------------------------
95 //
96 /// @brief Receive characters typed from the keyboard.
97 /// All the keyboard modifiers are applied (Shift, Ctrl, Alt, ...). Useful for
98 /// implementing input boxes.
99 ///
100 /// Usage:
101 /// -----
102 /// ~~~cpp
103 /// // Initialization
104 /// auto listener = input.MakeCharacterListener();
105 /// ~~~
106 ///
107 /// ~~~cpp
108 /// // Main loop.
109 /// wchar_t character;
110 /// while(listener->Receive(&character))
111 /// character_typed += character;
112 /// ~~~
114 public:
115 virtual bool Receive(wchar_t* in) = 0;
116 virtual ~CharacterListenerInterface() = default;
117 };
118 using CharacterListener = std::unique_ptr<CharacterListenerInterface>;
119 virtual CharacterListener MakeCharacterListener() = 0;
120};
121
122} // namespace smk
123
124#endif /* end of include guard: SMK_INPUT_HPP */
Receive characters typed from the keyboard. All the keyboard modifiers are applied (Shift,...
Definition: Input.hpp:113
A class for querying input state (keyboard, mouse, touch). The Input class is tied to a smk::Window....
Definition: Input.hpp:29
virtual bool IsMousePressed(int key)=0
Whether a mouse button is pressed or not.
virtual bool IsCursorHeld()=0
Whether the cursor is down or not.
virtual bool IsKeyPressed(int key)=0
Whether a keyboard button is pressed or not.
virtual glm::vec2 scroll_offset() const =0
The mouse/touchpad scrolling offset since the last frame.
virtual glm::vec2 mouse() const =0
The mouse position.
virtual bool IsCursorPressed()=0
Whether the cursor is pressed or not.
virtual glm::vec2 cursor() const =0
The cursor position.
virtual bool IsCursorReleased()=0
Whether the cursor is released or not.
virtual std::map< FingerID, Touch > & touches()=0
The touch states.
virtual bool IsMouseHeld(int key)=0
Whether a mouse button is down or not.
virtual bool IsKeyReleased(int key)=0
Whether a keyboard button is released or not.
virtual bool IsMouseReleased(int key)=0
Whether a mouse button is released or not.
virtual bool IsKeyHold(int key)=0
Whether a keyboard button is down or not.