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
12class InputManager {
13public:
14 enum class EventType {
15 KeyDown,
16 KeyUp
17 };
18
19 enum class Key
20 {
21 UP,
22 DOWN,
23 LEFT,
24 RIGHT,
25 SPACE,
26 ENTER,
27 ESCAPE,
28 TAB,
29 BACKSPACE,
30 DELETE,
31 HOME,
32 END,
33 PAGE_UP,
34 PAGE_DOWN,
35 INSERT,
36 CAPS_LOCK,
37 LEFT_SHIFT,
38 RIGHT_SHIFT,
39 LEFT_CTRL,
40 RIGHT_CTRL,
41 LEFT_ALT,
42 RIGHT_ALT,
43 F1,
44 F2,
45 F3,
46 F4,
47 F5,
48 F6,
49 F7,
50 F8,
51 F9,
52 F10,
53 F11,
54 F12,
55 A,
56 B,
57 C,
58 D,
59 E,
60 F,
61 G,
62 H,
63 I,
64 J,
65 K,
66 L,
67 M,
68 N,
69 O,
70 P,
71 Q,
72 R,
73 S,
74 T,
75 U,
76 V,
77 W,
78 X,
79 Y,
80 Z,
81 NUM_0,
82 NUM_1,
83 NUM_2,
84 NUM_3,
85 NUM_4,
86 NUM_5,
87 NUM_6,
88 NUM_7,
89 NUM_8,
90 NUM_9,
91 LEFT_BRACKET,
92 RIGHT_BRACKET,
93 SEMICOLON,
94 APOSTROPHE,
95 COMMA,
96 PERIOD,
97 SLASH,
98 BACKSLASH,
99 GRAVE
100 };
101
102 struct InputAction {
103 std::string name;
104 std::vector<Key> bindings;
105 std::function<void(bool)> callback;
106 };
107
108 InputManager();
109 ~InputManager();
110
111 void init(); // see if necessary
112 void processEvents();
113 void registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void(bool)> callback, const std::string& context);
114
115 void setActiveContext(const std::string& context);
116 std::string getActiveContext() const;
117
118 //void rebindAction(const std::string& actionName, const std::vector<Key>& newBindings, const std::string& context);
119 //void removeBindings(const std::string& actionName, const std::string& context);
120 //std::vector<Key> getBindings(const std::string& actionName, const std::string& context) const;
121 std::vector<InputAction*> getActionsByKey(const Key key) const;
122
123 SDL_AppResult handleEvent(SDL_EventType type, SDL_Event* const event);
124
125 void initKeyMap();
126private:
127 // TODO: flesh this out to avoid loops in process actions
128 // additionally to actionsByContext, not instead
129 std::map<std::string, std::map<Key, std::vector<InputAction*>>> actionsByContextAndKey;
130
131 std::map<Key, SDL_Scancode> keyMap;
132 std::string activeContext;
133};
134
135std::ostream& operator<<(std::ostream& os, InputManager::Key key);
136std::ostream& operator<<(std::ostream& os, const InputManager::InputAction& action);
137std::ostream& operator<<(std::ostream& os, const InputManager::InputAction* action);
138std::ostream& operator<<(std::ostream& os, const std::vector<InputManager::InputAction>& actions);
139std::ostream& operator<<(std::ostream& os, const std::vector<InputManager::InputAction*>& actions);
Definition InputManager.h:102