Toast Engine
Loading...
Searching...
No Matches
input_system.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "action.hpp"
12#include "keycodes.hpp"
13#include "value.hpp"
14
15#include <glm/glm.hpp>
16#include <toast/events/listener.hpp>
17#include <unordered_map>
18#include <unordered_set>
19
20struct SDL_Gamepad;
21
22namespace input {
23
24class TOAST_API InputSystem {
25public:
26 InputSystem();
27 ~InputSystem();
28
29 InputSystem(const InputSystem&) = delete;
30 auto operator=(const InputSystem&) -> InputSystem& = delete;
31
32 [[nodiscard]]
33 static auto get() noexcept -> InputSystem&;
34
35 void tick();
36 void reloadActions();
37 void setViewport(glm::vec2 position, glm::vec2 size) noexcept;
38
39 [[nodiscard]]
40 auto lastInputDevice() const noexcept -> Device;
41
42 [[nodiscard]]
43 auto lastInputName() const noexcept -> std::string_view;
44
45 [[nodiscard]]
46 auto mousePosition() const noexcept -> glm::vec2;
47
48 [[nodiscard]]
49 auto mouseDelta() const noexcept -> glm::vec2;
50
51 [[nodiscard]]
52 auto activeGamepadId() const noexcept -> uint32_t;
53
54private:
55 void subscribeEvents();
56 void refreshGamepads();
57 void noteLastInput(Device device, std::string name);
58
59 [[nodiscard]]
60 auto activeGamepad() const noexcept -> SDL_Gamepad*;
61 [[nodiscard]]
62 auto sample(const KeyCode& key) -> InputSample;
63
64 static inline InputSystem* instance = nullptr;
65
66 event::Listener m_listener;
67 std::vector<std::unique_ptr<Action>> m_actions;
68
69 std::unordered_set<int> m_keys_down;
70 std::unordered_set<int> m_mouse_down;
71 ModifierKey m_mods = ModifierKey::none;
72
73 glm::vec2 m_mouse_position {0.0f};
74 glm::vec2 m_prev_mouse_position {0.0f};
75 glm::vec2 m_mouse_delta {0.0f};
76 glm::vec2 m_scroll {0.0f};
77
78 glm::vec2 m_viewport_position {0.0f};
79 glm::vec2 m_viewport_size {0.0f};
80
81 std::unordered_map<uint32_t, SDL_Gamepad*> m_gamepads;
82
83 Device m_last_device = Device::none;
84 std::string m_last_name;
85
86 std::chrono::steady_clock::time_point m_last_tick;
87 bool m_has_last_tick = false;
88};
89
90}
Maps a group of binds to a function and a value.
manages the lifetime, subscription, and unsubscription of callbacks
Definition listener.hpp:39
auto activeGamepadId() const noexcept -> uint32_t
Definition input_system.cpp:227
auto mousePosition() const noexcept -> glm::vec2
Definition input_system.cpp:356
auto lastInputDevice() const noexcept -> Device
Definition input_system.cpp:348
auto mouseDelta() const noexcept -> glm::vec2
Definition input_system.cpp:360
auto lastInputName() const noexcept -> std::string_view
Definition input_system.cpp:352
ModifierKey
Bit flags for the keyboard modifier keys held during an input.
Definition keycodes.hpp:28
Device
Physical device a keycode belongs to.
Definition keycodes.hpp:18
One frame of raw input for a single keycode.
Definition value.hpp:130
A parsed keycode resolved to a device, kind and numeric code.
Definition keycodes.hpp:63
Value type shared by triggers, modifiers and actionsg.