// InputSystem_GLFW.cpp
#include "InputSystem.h"
#include <GLFW/glfw3.h>
#include <imgui.h>
// Store the GLFW window pointer
static GLFWwindow* gWindow = nullptr;
static bool mouseCaptureMode = false;
// GLFW callback functions
static void glfwMouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
if (button >= 0 && button < 3) {
InputState& state = InputSystem::GetInputState();
state.mouseButtons[button] = action == GLFW_PRESS;
}
}
static void glfwCursorPosCallback(GLFWwindow* window, double xpos, double ypos) {
InputState& state = InputSystem::GetInputState();
// Calculate delta from last position
glm::vec2 newPos(static_cast<float>(xpos), static_cast<float>(ypos));
state.cursorDelta = newPos - state.cursorPosition;
state.cursorPosition = newPos;
}
static void glfwScrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
InputState& state = InputSystem::GetInputState();
state.scrollDelta = static_cast<float>(yoffset);
}
static void glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
// Map GLFW keys to our input actions
if (action == GLFW_PRESS || action == GLFW_RELEASE) {
bool pressed = (action == GLFW_PRESS);
// Toggle mouse capture mode with Escape key
if (key == GLFW_KEY_ESCAPE && pressed) {
mouseCaptureMode = !mouseCaptureMode;
if (mouseCaptureMode) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} else {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
// Map other keys to actions
// ...
}
}
void InputSystem::Initialize(GLFWwindow* window) {
gWindow = window;
// Set up GLFW callbacks
glfwSetMouseButtonCallback(window, glfwMouseButtonCallback);
glfwSetCursorPosCallback(window, glfwCursorPosCallback);
glfwSetScrollCallback(window, glfwScrollCallback);
glfwSetKeyCallback(window, glfwKeyCallback);
// Initially capture the cursor for camera control
mouseCaptureMode = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
void InputSystem::Update(float deltaTime) {
// Poll for input events
glfwPollEvents();
// Update key states for continuous actions (like movement)
if (glfwGetKey(gWindow, GLFW_KEY_W) == GLFW_PRESS) {
if (auto it = actionCallbacks.find(InputAction::MOVE_FORWARD); it != actionCallbacks.end()) {
it->second(deltaTime);
}
}
// ... other keys ...
// Reset delta values after processing
inputState.resetDeltas();
}
bool InputSystem::IsImGuiCapturingKeyboard() {
return ImGui::GetIO().WantCaptureKeyboard;
}
bool InputSystem::IsImGuiCapturingMouse() {
return ImGui::GetIO().WantCaptureMouse;
}