// InputManager.cpp #include "InputManager.h" #include // For std::clamp // Constructor InputManager::InputManager() : m_MouseDeltaX(0.0f), m_MouseDeltaY(0.0f), m_ScrollDelta(0.0f) { Initialize(); } // 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); // 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) { return; } // Update previous key states m_PreviousKeyStates = m_KeyStates; // Update current key states for (int key = 0; key <= GLFW_KEY_LAST; ++key) { m_KeyStates[key] = glfwGetKey(window, key) == GLFW_PRESS; } // 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; } // Keyboard input query bool InputManager::IsKeyPressed(KeyCode key) const { int keyInt = static_cast(key); if (keyInt >= 0 && keyInt <= GLFW_KEY_LAST) { return m_KeyStates[keyInt]; } return false; } // Mouse button input query bool InputManager::IsMouseButtonPressed(MouseButton button) const { int buttonInt = static_cast(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(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(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; }