StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
InputSystem.hpp
1#pragma once
2#ifdef StevEngine_INPUTS
3#include "main/EventSystem.hpp"
4#include "main/EngineEvents.hpp"
5#include "utilities/Vector2.hpp"
6
7#include <SDL.h>
8
9namespace StevEngine {
13 enum CursorMode {
14 Free,
15 Confined,
16 Locked
17 };
18
26 public:
30 void Init();
31
36 EventManager* GetEvents() { return &events; };
37
43 bool IsKeyPressed(SDL_Keycode key) const;
44
50 void ForcePressKey(SDL_Keycode key, bool value);
51
57 bool IsMouseButtonPressed(Uint8 button) const;
58
63 Utilities::Vector2 GetMousePosition() const { return mousePosition; }
64
69 Utilities::Vector2 GetMouseDelta() const { return mouseDelta; }
70
71 #ifdef StevEngine_SHOW_WINDOW
72 CursorMode cursorMode = Free;
73 bool cursorVisible = true;
74 #endif
75
76 private:
77 EventManager events;
78 std::unordered_map<SDL_Keycode, bool> inputMap;
79 std::unordered_map<Uint8, bool> mouseInputMap;
80 Utilities::Vector2 mousePosition;
81 Utilities::Vector2 mouseDelta;
82 double mouseWheelDelta;
83
84 void HandleSDLEvent(const SDLEvent event);
85 void Update(double deltaTime);
86 void ResetMouseDelta();
87 };
88
90 extern InputManager inputManager;
91
92
93 //Input events
97 class InputKeyDownEvent : public Event {
98 public:
103 InputKeyDownEvent(SDL_Keycode key) : key(key) {}
104 const std::string GetEventType() const override { return GetStaticEventType(); };
105 static const std::string GetStaticEventType() { return "InputKeyDownEvent"; }
106 SDL_Keycode key;
107 };
108
112 class InputKeyUpEvent : public Event {
113 public:
118 InputKeyUpEvent(SDL_Keycode key) : key(key) {}
119 const std::string GetEventType() const override { return GetStaticEventType(); };
120 static const std::string GetStaticEventType() { return "InputKeyUpEvent"; }
121 SDL_Keycode key;
122 };
123
127 class InputMouseMoveEvent : public Event {
128 public:
136 InputMouseMoveEvent(int x, int y, int deltaX, int deltaY) : x(x), y(y), deltaX(deltaX), deltaY(deltaY) {}
137 const std::string GetEventType() const override { return GetStaticEventType(); };
138 static const std::string GetStaticEventType() { return "InputMouseMoveEvent"; }
139 int x, y;
140 int deltaX, deltaY;
141 };
142
147 public:
153 const std::string GetEventType() const override { return GetStaticEventType(); };
154 static const std::string GetStaticEventType() { return "InputMouseWheelEvent"; }
155 float value;
156 };
157
162 public:
168 const std::string GetEventType() const override { return GetStaticEventType(); };
169 static const std::string GetStaticEventType() { return "InputMouseButtonDownEvent"; }
170 Uint8 button;
171 };
172
177 public:
183 const std::string GetEventType() const override { return GetStaticEventType(); };
184 static const std::string GetStaticEventType() { return "InputMouseButtonUpEvent"; }
185 Uint8 button;
186 };
187}
188#endif
Manages event subscriptions and publishing.
Definition EventSystem.hpp:74
Base class for all engine events.
Definition EventSystem.hpp:12
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:104
SDL_Keycode key
Keycode of pressed key.
Definition InputSystem.hpp:106
InputKeyDownEvent(SDL_Keycode key)
Create key down event.
Definition InputSystem.hpp:103
InputKeyUpEvent(SDL_Keycode key)
Create key up event.
Definition InputSystem.hpp:118
SDL_Keycode key
Keycode of released key.
Definition InputSystem.hpp:121
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:119
Handles input processing and management.
Definition InputSystem.hpp:25
void ForcePressKey(SDL_Keycode key, bool value)
Force key press state.
Definition InputSystem.cpp:57
bool cursorVisible
Whether cursor is visible.
Definition InputSystem.hpp:73
void Init()
Initialize input system.
Definition InputSystem.cpp:43
Utilities::Vector2 GetMousePosition() const
Get current mouse position.
Definition InputSystem.hpp:63
Utilities::Vector2 GetMouseDelta() const
Get mouse movement since last frame.
Definition InputSystem.hpp:69
CursorMode cursorMode
Current cursor behavior mode.
Definition InputSystem.hpp:72
bool IsKeyPressed(SDL_Keycode key) const
Check if key is currently pressed.
Definition InputSystem.cpp:49
bool IsMouseButtonPressed(Uint8 button) const
Check if mouse button is currently pressed.
Definition InputSystem.cpp:63
EventManager * GetEvents()
Get input events manager.
Definition InputSystem.hpp:36
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:168
Uint8 button
ID of pressed button.
Definition InputSystem.hpp:170
InputMouseButtonDownEvent(Uint8 button)
Create mouse button down event.
Definition InputSystem.hpp:167
Uint8 button
ID of released button.
Definition InputSystem.hpp:185
InputMouseButtonUpEvent(Uint8 button)
Create mouse button up event.
Definition InputSystem.hpp:182
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:183
int deltaY
Mouse movement delta.
Definition InputSystem.hpp:140
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:137
InputMouseMoveEvent(int x, int y, int deltaX, int deltaY)
Create mouse move event.
Definition InputSystem.hpp:136
int y
Current mouse position.
Definition InputSystem.hpp:139
float value
Scroll amount and direction.
Definition InputSystem.hpp:155
InputMouseWheelEvent(float value)
Create mouse wheel event.
Definition InputSystem.hpp:152
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:153
Event containing raw SDL event.
Definition EngineEvents.hpp:67
2D vector class
Definition Vector2.hpp:13