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#include <SDL_keycode.h>
9
10namespace StevEngine {
14 enum CursorMode {
15 Free,
16 Confined,
17 Locked
18 };
19
20 typedef int32_t Button;
24 enum MouseButton : Button {
25 LEFT = -SDL_BUTTON_LEFT,
26 RIGHT = -SDL_BUTTON_RIGHT,
27 MIDDLE = -SDL_BUTTON_MIDDLE,
28 EXTRA1 = -SDL_BUTTON_X1,
29 EXTRA2 = -SDL_BUTTON_X2,
30 SCROLL_UP = EXTRA2 - 1,
31 SCROLL_DOWN = SCROLL_UP - 1,
32 };
33
34
42 public:
46 void Init();
47
52 EventManager* GetEvents() { return &events; };
53
59 bool IsKeyPressed(Button key) const;
60
66 void ForcePressKey(Button key, bool value);
67
72 Utilities::Vector2 GetMousePosition() const { return mousePosition; }
73
78 Utilities::Vector2 GetMouseDelta() const { return mouseDelta; }
79
80 #ifdef StevEngine_SHOW_WINDOW
81 CursorMode cursorMode = Free;
82 bool cursorVisible = true;
83 #endif
84
85 private:
86 EventManager events;
87 std::unordered_map<Button, bool> inputMap;
88 Utilities::Vector2 mousePosition;
89 Utilities::Vector2 mouseDelta;
90 double mouseWheelDelta;
91
92 void HandleSDLEvent(const SDLEvent event);
93 void Update(double deltaTime);
94 void ResetMouseDelta();
95 };
96
98 extern InputManager inputManager;
99
100
101 //Input events
105 class InputKeyDownEvent : public Event {
106 public:
112 const std::string GetEventType() const override { return GetStaticEventType(); };
113 static const std::string GetStaticEventType() { return "InputKeyDownEvent"; }
114 Button key;
115 };
116
120 class InputKeyUpEvent : public Event {
121 public:
127 const std::string GetEventType() const override { return GetStaticEventType(); };
128 static const std::string GetStaticEventType() { return "InputKeyUpEvent"; }
129 Button key;
130 };
131
135 class InputMouseMoveEvent : public Event {
136 public:
144 InputMouseMoveEvent(int x, int y, int deltaX, int deltaY) : x(x), y(y), deltaX(deltaX), deltaY(deltaY) {}
145 const std::string GetEventType() const override { return GetStaticEventType(); };
146 static const std::string GetStaticEventType() { return "InputMouseMoveEvent"; }
147 int x, y;
148 int deltaX, deltaY;
149 };
150
155 public:
161 const std::string GetEventType() const override { return GetStaticEventType(); };
162 static const std::string GetStaticEventType() { return "InputMouseWheelEvent"; }
163 float value;
164 };
165
170 public:
176 const std::string GetEventType() const override { return GetStaticEventType(); };
177 static const std::string GetStaticEventType() { return "InputMouseButtonDownEvent"; }
178 MouseButton button;
179 };
180
185 public:
191 const std::string GetEventType() const override { return GetStaticEventType(); };
192 static const std::string GetStaticEventType() { return "InputMouseButtonUpEvent"; }
193 MouseButton button;
194 };
195}
196#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:112
Button key
Keycode of pressed key.
Definition InputSystem.hpp:114
InputKeyDownEvent(Button key)
Create key down event.
Definition InputSystem.hpp:111
Button key
Keycode of released key.
Definition InputSystem.hpp:129
InputKeyUpEvent(Button key)
Create key up event.
Definition InputSystem.hpp:126
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:127
Handles input processing and management.
Definition InputSystem.hpp:41
bool cursorVisible
Whether cursor is visible.
Definition InputSystem.hpp:82
void Init()
Initialize input system.
Definition InputSystem.cpp:56
Utilities::Vector2 GetMousePosition() const
Get current mouse position.
Definition InputSystem.hpp:72
Utilities::Vector2 GetMouseDelta() const
Get mouse movement since last frame.
Definition InputSystem.hpp:78
CursorMode cursorMode
Current cursor behavior mode.
Definition InputSystem.hpp:81
void ForcePressKey(Button key, bool value)
Force key press state.
Definition InputSystem.cpp:71
bool IsKeyPressed(Button key) const
Check if key is currently pressed.
Definition InputSystem.cpp:63
EventManager * GetEvents()
Get input events manager.
Definition InputSystem.hpp:52
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:176
InputMouseButtonDownEvent(MouseButton button)
Create mouse button down event.
Definition InputSystem.hpp:175
MouseButton button
ID of pressed button.
Definition InputSystem.hpp:178
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:191
MouseButton button
ID of released button.
Definition InputSystem.hpp:193
InputMouseButtonUpEvent(MouseButton button)
Create mouse button up event.
Definition InputSystem.hpp:190
int deltaY
Mouse movement delta.
Definition InputSystem.hpp:148
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:145
InputMouseMoveEvent(int x, int y, int deltaX, int deltaY)
Create mouse move event.
Definition InputSystem.hpp:144
int y
Current mouse position.
Definition InputSystem.hpp:147
float value
Scroll amount and direction.
Definition InputSystem.hpp:163
InputMouseWheelEvent(float value)
Create mouse wheel event.
Definition InputSystem.hpp:160
const std::string GetEventType() const override
Get type identifier for this event.
Definition InputSystem.hpp:161
Event containing raw SDL event.
Definition EngineEvents.hpp:67
2D vector class
Definition Vector2.hpp:13