Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
InputsManager.cpp
Go to the documentation of this file.
1#include "InputsManager.h"
2#include "system/message.h"
3#include <algorithm>
4#include <string>
5
7{
8 name = "InputsManager";
9
10 SYSTEM_LOG << "InputsManager created and Initialized\n";
11}
12
14{
15 Shutdown();
16 SYSTEM_LOG << "InputsManager destroyed\n";
17}
18
20 SYSTEM_LOG << "[InputsManager][Info] Initializing new input system...\n";
21
22 // Load engine configuration
23 if (!InputConfigLoader::Get().LoadEngineConfig(configPath)) {
24 SYSTEM_LOG << "[InputsManager][Warning] Failed to load engine config, using defaults\n";
25 }
26
27 // Load input configuration
28 std::string inputConfigPath = "Config/Inputs.json"; // Default path
29 if (!InputConfigLoader::Get().LoadInputConfig(inputConfigPath)) {
30 SYSTEM_LOG << "[InputsManager][Warning] Failed to load input config, creating defaults\n";
31
32 // Create default profiles
33 auto keyboardProfile = std::make_shared<InputProfile>("default_keyboard", InputDeviceType::KeyboardMouse);
34 keyboardProfile->InitializeDefaults();
37
38 auto gamepadProfile = std::make_shared<InputProfile>("default_gamepad", InputDeviceType::Joystick);
39 gamepadProfile->InitializeDefaults();
42
43 // Create default action map
45 gameplayMap.AddAction("move_up");
46 gameplayMap.AddAction("move_down");
47 gameplayMap.AddAction("move_left");
48 gameplayMap.AddAction("move_right");
49 gameplayMap.AddAction("jump");
50 gameplayMap.AddAction("shoot");
51 gameplayMap.AddAction("interact");
53 }
54
55 // Try to load user overrides
56 InputConfigLoader::Get().LoadProfileOverride("Config/Inputs.user.json");
57
58 // Initialize context manager
60
61 // Register keyboard-mouse device
64
65 // Joysticks are registered by HandleEvent when they connect
66
67 SYSTEM_LOG << "[InputsManager][Info] Input system initialized successfully\n";
69}
70
72{
73 // Forward to individual managers
77
78 // Register joysticks with new device manager when they connect
79 if (ev->type == SDL_EVENT_JOYSTICK_ADDED) {
80 SDL_JoystickID joyID = ev->jdevice.which;
82 if (joystick) {
83 const char* name = SDL_GetJoystickName(joystick);
86 SYSTEM_LOG << "[InputsManager][Info] Joystick connected and registered: " << (name ? name : "Unknown") << " (ID: " << joyID << ")\n";
87 }
88 }
89 else if (ev->type == SDL_EVENT_JOYSTICK_REMOVED) {
90 SDL_JoystickID joyID = ev->jdevice.which;
92 SYSTEM_LOG << "[InputsManager][Info] Joystick disconnected and unregistered (ID: " << joyID << ")\n";
93 }
94}
95
96//-------------------------------------------------------------
97// set a strin with the status and info of all connected devices (joysticks, keyboard and mouse)
98// state of connectivity, bouds to player ID etc...
99// the returned string is stored internally and updated at each call and will be use by the PanelManager InputsInspector panel
101{
102 m_devicesStatus.str(string());
103 m_devicesStatus << "---- InputsManager Devices Status ----\r\n";
104 // Joysticks
106 m_devicesStatus << "Connected Joysticks: " << joysticks.size() << "\r\n";
107 m_devicesStatus << "Available Unassigned Joysticks: " << GetAvailableJoystickCount() << "\r\n";
108 m_devicesStatus << "Available Assigned Joysticks: " << (GetConnectedJoysticksCount() - GetAvailableJoystickCount()) << "\r\n";
109 for (auto jid : joysticks)
110 {
111 m_devicesStatus << " - Joystick ID=" << jid;
112 // find which player is bound to this joystick
113 short boundPlayerID = -1;
114 for (auto& kv : m_playerBindings)
115 {
116 if (kv.second == jid) { boundPlayerID = kv.first; break; }
117 }
118 if (boundPlayerID >= 0)
119 m_devicesStatus << " -> Bound to Player " << boundPlayerID << "\r\n";
120 else
121 m_devicesStatus << " -> Not bound to any player\r\n";
122 }
123 // Keyboard
124 m_devicesStatus << "Keyboard: ";
126 {
127 m_devicesStatus << "Assigned to Player ";
128 // find which player is bound to keyboard
129 short boundPlayerID = -1;
130 for (auto& kv : m_playerBindings)
131 {
132 if (kv.second == SDL_JoystickID(-1)) { boundPlayerID = kv.first; break; }
133 }
134 if (boundPlayerID >= 0)
135 m_devicesStatus << boundPlayerID << "\r\n";
136 else
137 m_devicesStatus << "(error: assigned but no player?)\r\n";
138 }
139 else
140 {
141 m_devicesStatus << "Not assigned\r\n";
142 }
143 // Mouse
144 m_devicesStatus << "Mouse: Connected\r\n"; // assume always connected for now
145 return m_devicesStatus.str();
146}
147//-------------------------------------------------------------
148// Input Context Stack
150{
151 m_contextStack.push_back(ctx);
152 SYSTEM_LOG << "InputsManager: Pushed context " << static_cast<int>(ctx) << ", stack size: " << m_contextStack.size() << "\n";
153}
154
156{
157 if (m_contextStack.size() > 1) // Keep at least one context
158 {
159 m_contextStack.pop_back();
160 SYSTEM_LOG << "InputsManager: Popped context, stack size: " << m_contextStack.size() << "\n";
161 }
162 else
163 {
164 SYSTEM_LOG << "InputsManager: Cannot pop last context (stack would be empty)\n";
165 }
166}
167
172//-------------------------------------------------------------
173// Input Entity Cache
175{
176 // Check if already registered
178 {
179 if (existing == e) return;
180 }
181 m_inputEntities.push_back(e);
182 SYSTEM_LOG << "InputsManager: Registered input entity " << e << "\n";
183}
184
186{
187 auto it = std::find(m_inputEntities.begin(), m_inputEntities.end(), e);
188 if (it != m_inputEntities.end())
189 {
190 m_inputEntities.erase(it);
191 SYSTEM_LOG << "InputsManager: Unregistered input entity " << e << "\n";
192 }
193}
194
195const std::vector<EntityID>& InputsManager::GetInputEntities() const
196{
197 return m_inputEntities;
198}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
InputContext
static InputConfigLoader & Get()
bool LoadProfileOverride(const std::string &path)
static InputContextManager & Get()
void RegisterDevice(const InputDeviceSlot &slot)
void AddProfile(std::shared_ptr< InputProfile > profile)
static InputDeviceManager & Get()
void LogDeviceStatus() const
void UnregisterDevice(int deviceIndex)
void AddActionMap(const ActionMap &actionMap)
void SetDefaultProfile(InputDeviceType deviceType, const std::string &profileName)
void InitializeInputSystem(const std::string &configPath="Config/olympe-config.json")
std::ostringstream m_devicesStatus
void PushContext(InputContext ctx)
std::unordered_map< short, SDL_JoystickID > m_playerBindings
virtual void HandleEvent(const SDL_Event *ev)
virtual ~InputsManager()
void RegisterInputEntity(EntityID e)
int GetConnectedJoysticksCount() const
InputContext GetActiveContext() const
int GetAvailableJoystickCount() const
string GetDevicesStatusUpdate()
void UnregisterInputEntity(EntityID e)
std::vector< EntityID > m_inputEntities
const std::vector< EntityID > & GetInputEntities() const
std::vector< InputContext > m_contextStack
std::string name
static JoystickManager & Get()
void HandleEvent(const SDL_Event *ev)
std::vector< SDL_JoystickID > GetConnectedJoysticks()
static KeyboardManager & Get()
void HandleEvent(const SDL_Event *ev)
void HandleEvent(const SDL_Event *ev)
static MouseManager & Get()
#define SYSTEM_LOG