Tesseract-Engine/src/Engine/InputManager.cpp

93 lines
2.7 KiB
C++
Raw Normal View History

2025-01-01 07:36:53 +00:00
// InputManager.cpp
#include "InputManager.h"
2025-01-06 18:43:18 +00:00
#include <algorithm> // For std::clamp
2025-01-01 07:36:53 +00:00
2025-01-06 18:43:18 +00:00
// Constructor
InputManager::InputManager()
: m_MouseDeltaX(0.0f), m_MouseDeltaY(0.0f), m_ScrollDelta(0.0f)
{
Initialize();
}
2025-01-01 07:36:53 +00:00
2025-01-06 18:43:18 +00:00
// Initialize method to set up state vectors
void InputManager::Initialize() {
// Initialize key states
m_KeyStates.resize(GLFW_KEY_LAST + 1, false);
m_PreviousKeyStates.resize(GLFW_KEY_LAST + 1, false);
2025-01-01 07:36:53 +00:00
2025-01-06 18:43:18 +00:00
// Initialize mouse button states
m_MouseButtonStates.resize(GLFW_MOUSE_BUTTON_LAST + 1, false);
m_PreviousMouseButtonStates.resize(GLFW_MOUSE_BUTTON_LAST + 1, false);
}
// Update method to poll input states
void InputManager::Update(GLFWwindow* window) {
if (!window) {
2025-01-01 07:36:53 +00:00
return;
}
2025-01-06 18:43:18 +00:00
// Update previous key states
m_PreviousKeyStates = m_KeyStates;
// Update current key states
for (int key = 0; key <= GLFW_KEY_LAST; ++key) {
2025-01-01 07:36:53 +00:00
m_KeyStates[key] = glfwGetKey(window, key) == GLFW_PRESS;
}
2025-01-06 18:43:18 +00:00
// Update previous mouse button states
m_PreviousMouseButtonStates = m_MouseButtonStates;
// Update current mouse button states
for (int button = 0; button <= GLFW_MOUSE_BUTTON_LAST; ++button) {
m_MouseButtonStates[button] = glfwGetMouseButton(window, button) == GLFW_PRESS;
}
// Reset mouse deltas and scroll delta for this frame
m_MouseDeltaX = 0.0f;
m_MouseDeltaY = 0.0f;
m_ScrollDelta = 0.0f;
2025-01-01 07:36:53 +00:00
}
2025-01-06 18:43:18 +00:00
// Keyboard input query
bool InputManager::IsKeyPressed(KeyCode key) const {
2025-01-01 07:36:53 +00:00
int keyInt = static_cast<int>(key);
2025-01-06 18:43:18 +00:00
if (keyInt >= 0 && keyInt <= GLFW_KEY_LAST) {
2025-01-01 07:36:53 +00:00
return m_KeyStates[keyInt];
}
return false;
}
2025-01-06 18:43:18 +00:00
// Mouse button input query
bool InputManager::IsMouseButtonPressed(MouseButton button) const {
int buttonInt = static_cast<int>(button);
if (buttonInt >= 0 && buttonInt <= GLFW_MOUSE_BUTTON_LAST) {
return m_MouseButtonStates[buttonInt];
}
return false;
}
// Mouse button just pressed (edge detection)
bool InputManager::IsMouseButtonJustPressed(MouseButton button) const {
int buttonInt = static_cast<int>(button);
if (buttonInt >= 0 && buttonInt <= GLFW_MOUSE_BUTTON_LAST) {
return m_MouseButtonStates[buttonInt] && !m_PreviousMouseButtonStates[buttonInt];
}
return false;
}
// Mouse button just released (edge detection)
bool InputManager::IsMouseButtonJustReleased(MouseButton button) const {
int buttonInt = static_cast<int>(button);
if (buttonInt >= 0 && buttonInt <= GLFW_MOUSE_BUTTON_LAST) {
return !m_MouseButtonStates[buttonInt] && m_PreviousMouseButtonStates[buttonInt];
}
return false;
}
// Reset deltas after handling input
void InputManager::ResetDeltas() {
m_MouseDeltaX = 0.0f;
m_MouseDeltaY = 0.0f;
m_ScrollDelta = 0.0f;
}