From 703fe88fa68a3fec983b4c2b18fc8fbec33af3c5 Mon Sep 17 00:00:00 2001 From: connellpaxton Date: Tue, 30 Jan 2024 16:20:07 -0500 Subject: [PATCH] Cleaned up input with IMGUI while using mouse look --- Input/Input.cpp | 16 +++++++++++++++- Input/Input.hpp | 2 ++ Renderer/Renderer.hpp | 2 ++ UI/UI.cpp | 2 +- pléascach.cpp | 6 ++++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Input/Input.cpp b/Input/Input.cpp index 868a8d8..6136f43 100644 --- a/Input/Input.cpp +++ b/Input/Input.cpp @@ -67,11 +67,16 @@ Input::Input(INPUT_PTR in) : in(in) { } }); }); +} - glfwSetInputMode(in, GLFW_CURSOR, GLFW_CURSOR_DISABLED); +void Input::setCursor(bool enabled) { + glfwSetInputMode(in, GLFW_CURSOR, enabled? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED); } void Input::handleMovementKeys(Renderer& ren) { + if (ImGui::GetIO().WantCaptureKeyboard) + return; + const auto forward = glm::vec3(glm::cos(ren.cam.phi), 0.0, glm::sin(ren.cam.phi)); const auto right = glm::cross(forward, glm::vec3(0.0, 1.0, 0.0)); const auto speed = glfwGetKey(in, GLFW_KEY_LEFT_SHIFT)? 2.0f : 1.0f; @@ -129,6 +134,15 @@ void Input::handleCursorMovement(Renderer& ren, double x, double y) { int rel_mouse_x = static_cast(x) - last_mouse.x; int rel_mouse_y = static_cast(y) - last_mouse.y; + auto& io = ImGui::GetIO(); + if (io.WantCaptureMouse) + return; + + if (!ren.capture_mouse) { + io.AddMousePosEvent(x, y); + return; + } + ren.cam.phi += rel_mouse_x / 100.0; ren.cam.theta += rel_mouse_y / 100.0; diff --git a/Input/Input.hpp b/Input/Input.hpp index c57047a..28413ee 100644 --- a/Input/Input.hpp +++ b/Input/Input.hpp @@ -72,5 +72,7 @@ struct Input { void handleMovementKeys(Renderer& ren); void handleCursorMovement(Renderer& ren, double x, double y); + void setCursor(bool enabled); + bool shouldClose(); }; \ No newline at end of file diff --git a/Renderer/Renderer.hpp b/Renderer/Renderer.hpp index 832c701..bdada14 100644 --- a/Renderer/Renderer.hpp +++ b/Renderer/Renderer.hpp @@ -61,4 +61,6 @@ struct Renderer { std::unique_ptr ui; Camera cam {}; + + bool capture_mouse = false; }; diff --git a/UI/UI.cpp b/UI/UI.cpp index c8d088f..342cc4f 100644 --- a/UI/UI.cpp +++ b/UI/UI.cpp @@ -91,7 +91,7 @@ void UI::newFrame() { ImGui::Text("FPS: %f", info.fps); ImGui::SliderAngle("Theta", &info.cam.theta, 0.01, 179.9); - ImGui::SliderAngle("Phi", &info.cam.phi); + ImGui::SliderAngle("Phi", &info.cam.phi, 0.0, 360.0, "%.0f def"); ImGui::SliderFloat("cam.x", &info.cam.pos.x, -1e2, 1e2); ImGui::SliderFloat("cam.y", &info.cam.pos.y, -1e2, 1e2); ImGui::SliderFloat("cam.z", &info.cam.pos.z, -1e2, 1e2); diff --git a/pléascach.cpp b/pléascach.cpp index 12568a1..87b786a 100644 --- a/pléascach.cpp +++ b/pléascach.cpp @@ -40,6 +40,12 @@ int main(int argc, char* argv[]) { break; case InputEvent::Tag::eBUTTON: break; + case InputEvent::Tag::eKEY: + if (event.key.key == GLFW_KEY_ESCAPE && event.key.state == GLFW_PRESS) { + ren.capture_mouse = !ren.capture_mouse; + in->setCursor(!ren.capture_mouse); + } + break; } }