InputImpl.hpp
1// Copyright 2020 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#ifndef SMK_INPUTIMPL_H_
5#define SMK_INPUTIMPL_H_
6
7#include <smk/Input.hpp>
8#include <unordered_set>
9#include <vector>
10
11#ifdef __EMSCRIPTEN__
12 #include <emscripten.h>
13#endif
14
15namespace smk {
16
17class InputImpl : public Input {
18 public:
19 void Update(GLFWwindow* window);
20#ifdef __EMSCRIPTEN__
21 void OnTouchEvent(int eventType, const EmscriptenTouchEvent* keyEvent);
22#endif
23 void OnScrollEvent(glm::vec2 offset);
24 void OnCharacterTyped(wchar_t character);
25
26 class CharacterListenerImpl;
27 void BindCharacterListener(CharacterListenerImpl*);
28 void UnBindCharacterListener(CharacterListenerImpl*);
29
30 // Input override:
31 bool IsKeyPressed(int key) override;
32 bool IsKeyReleased(int key) override;
33 bool IsKeyHold(int key) override;
34 bool IsMousePressed(int key) override;
35 bool IsMouseReleased(int key) override;
36 glm::vec2 mouse() const override;
37 std::map<FingerID, Touch>& touches() override;
38 bool IsMouseHeld(int key) override;
39 bool IsCursorHeld() override;
40 bool IsCursorPressed() override;
41 bool IsCursorReleased() override;
42 glm::vec2 cursor() const override;
43 glm::vec2 scroll_offset() const override;
44 CharacterListener MakeCharacterListener() override;
45
46 private:
47 // Keyboard.
48 std::map<int, std::pair<int, int>> key_state_;
49
50 // Mouse.
51 std::map<int, std::pair<int, int>> mouse_state_;
52 glm::vec2 mouse_ = {0.f, 0.f};
53
54 // Touch.
55 std::map<FingerID, Touch> touches_;
56
57 // Cursor
58 glm::vec2 cursor_ = {0, 0};
59 bool cursor_press_ = false;
60 bool cursor_press_previous_ = false;
61 bool touching_ = true;
62
63 // Scroll
64 glm::vec2 scroll_ = {0.f, 0.f};
65 glm::vec2 scroll_old_ = {0.f, 0.f};
66
67 // CharacterListener
68 std::unordered_set<CharacterListenerImpl*> character_listeners_;
69};
70
71} // namespace smk
72#endif /* end of include guard: SMK_INPUTIMPL_H_ */
bool IsKeyHold(int key) override
Whether a keyboard button is down or not.
Definition: InputImpl.cpp:104
bool IsKeyReleased(int key) override
Whether a keyboard button is released or not.
Definition: InputImpl.cpp:99
glm::vec2 cursor() const override
The cursor position.
Definition: InputImpl.cpp:144
bool IsMousePressed(int key) override
Whether a mouse button is pressed or not.
Definition: InputImpl.cpp:109
bool IsCursorHeld() override
Whether the cursor is down or not.
Definition: InputImpl.cpp:132
std::map< FingerID, Touch > & touches() override
The touch states.
Definition: InputImpl.cpp:123
bool IsCursorPressed() override
Whether the cursor is pressed or not.
Definition: InputImpl.cpp:136
glm::vec2 scroll_offset() const override
The mouse/touchpad scrolling offset since the last frame.
Definition: InputImpl.cpp:147
bool IsCursorReleased() override
Whether the cursor is released or not.
Definition: InputImpl.cpp:140
bool IsKeyPressed(int key) override
Whether a keyboard button is pressed or not.
Definition: InputImpl.cpp:94
bool IsMouseReleased(int key) override
Whether a mouse button is released or not.
Definition: InputImpl.cpp:114
bool IsMouseHeld(int key) override
Whether a mouse button is down or not.
Definition: InputImpl.cpp:127
glm::vec2 mouse() const override
The mouse position.
Definition: InputImpl.cpp:119
A class for querying input state (keyboard, mouse, touch). The Input class is tied to a smk::Window....
Definition: Input.hpp:29