VEGO-Engine  0.1
Loading...
Searching...
No Matches
InputManager.h
1#pragma once
2
3#include <SDL3/SDL_events.h>
4#include <SDL3/SDL_init.h>
5#include <SDL3/SDL.h>
6#include <map>
7#include <string>
8#include <functional>
9#include <vector>
10#include <iostream>
11
14class InputManager {
15public:
17 enum class Key
18 {
19 UP,
20 DOWN,
21 LEFT,
22 RIGHT,
23 SPACE,
24 ENTER,
25 ESCAPE,
26 TAB,
27 BACKSPACE,
28 DELETE,
29 HOME,
30 END,
31 PAGE_UP,
32 PAGE_DOWN,
33 INSERT,
34 CAPS_LOCK,
35 LEFT_SHIFT,
36 RIGHT_SHIFT,
37 LEFT_CTRL,
38 RIGHT_CTRL,
39 LEFT_ALT,
40 RIGHT_ALT,
41 F1,
42 F2,
43 F3,
44 F4,
45 F5,
46 F6,
47 F7,
48 F8,
49 F9,
50 F10,
51 F11,
52 F12,
53 A,
54 B,
55 C,
56 D,
57 E,
58 F,
59 G,
60 H,
61 I,
62 J,
63 K,
64 L,
65 M,
66 N,
67 O,
68 P,
69 Q,
70 R,
71 S,
72 T,
73 U,
74 V,
75 W,
76 X,
77 Y,
78 Z,
79 NUM_0,
80 NUM_1,
81 NUM_2,
82 NUM_3,
83 NUM_4,
84 NUM_5,
85 NUM_6,
86 NUM_7,
87 NUM_8,
88 NUM_9,
89 LEFT_BRACKET,
90 RIGHT_BRACKET,
91 SEMICOLON,
92 APOSTROPHE,
93 COMMA,
94 PERIOD,
95 SLASH,
96 BACKSLASH,
97 GRAVE
98 };
99
104 struct InputAction {
105 std::string name;
106 std::vector<Key> bindings;
107 std::function<void(bool)> callback;
108 };
109
110 InputManager();
111 ~InputManager();
112
113 void init(); // see if necessary
114 void processEvents();
120 void registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void(bool)> callback, const std::string& context = "Default");
121
124 void setActiveContext(const std::string& context);
125
127 std::string getActiveContext() const;
128
129 //void rebindAction(const std::string& actionName, const std::vector<Key>& newBindings, const std::string& context);
130 //void removeBindings(const std::string& actionName, const std::string& context);
131 //std::vector<Key> getBindings(const std::string& actionName, const std::string& context) const;
132 std::vector<InputAction*> getActionsByKey(const Key key) const;
133
134 SDL_AppResult handleEvent(SDL_EventType type, SDL_Event* const event);
135
136 void initKeyMap();
137private:
138 // TODO: flesh this out to avoid loops in process actions
139 // additionally to actionsByContext, not instead
140 std::map<std::string, std::map<Key, std::vector<InputAction*>>> actionsByContextAndKey;
141
142 std::map<Key, SDL_Scancode> keyMap;
143 std::string activeContext;
144};
145
146std::ostream& operator<<(std::ostream& os, InputManager::Key key);
147std::ostream& operator<<(std::ostream& os, const InputManager::InputAction& action);
148std::ostream& operator<<(std::ostream& os, const InputManager::InputAction* action);
149std::ostream& operator<<(std::ostream& os, const std::vector<InputManager::InputAction>& actions);
150std::ostream& operator<<(std::ostream& os, const std::vector<InputManager::InputAction*>& actions);
void setActiveContext(const std::string &context)
set the active context, is "Default" by default
Definition InputManager.cpp:271
std::string getActiveContext() const
Get the active context.
Definition InputManager.cpp:275
Key
A list of every key that can be bound to an action.
Definition InputManager.h:18
void registerAction(const std::string &actionName, const std::vector< Key > &keys, std::function< void(bool)> callback, const std::string &context="Default")
Register an action with a name, key bindings, and a callback function.
Definition InputManager.cpp:251
InputAction struct to represent an action and its bindings.
Definition InputManager.h:104