Skip to content

Commit adac16d

Browse files
committed
improve the mouse movement
1 parent 12bab6c commit adac16d

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

src/core/EventManager.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace reactor {
66

77
enum class EventType {
88
MouseMoved,
9+
MouseButtonPressed,
10+
MouseButtonReleased,
911
KeyPressed,
1012
KeyReleased,
1113
};
@@ -16,6 +18,10 @@ struct Event {
1618
struct {
1719
double x, y;
1820
} mouseMove;
21+
struct {
22+
int button;
23+
double x, y;
24+
} mouseButton;
1925
struct {
2026
int key;
2127
} keyboard;

src/core/OrbitController.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@ void OrbitController::onEvent(const Event &event) {
2222
float max_elev = glm::radians(85.0f);
2323
m_elevation = glm::clamp(m_elevation, -max_elev, max_elev);
2424
updateCamera();
25+
m_lastX = event.mouseMove.x;
26+
m_lastY = event.mouseMove.y;
27+
}
28+
break;
29+
case EventType::MouseButtonPressed:
30+
if (event.mouseButton.button == 0) {
31+
m_rotating = true;
32+
}
33+
break;
34+
case EventType::MouseButtonReleased:
35+
if (event.mouseButton.button == 0) {
36+
m_rotating = false;
2537
}
26-
m_lastX = event.mouseMove.x;
27-
m_lastY = event.mouseMove.y;
2838
break;
2939
case EventType::KeyPressed:
3040
break;

src/core/OrbitController.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class OrbitController final : public IEventListener {
1717
float m_azimuth = 0.0f;
1818
float m_elevation = 0.0f;
1919

20-
bool m_rotating = true;
20+
bool m_rotating = false;
2121
double m_lastX = 0.0;
2222
double m_lastY = 0.0;
2323

src/core/Window.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ m_width(width), m_height(height), m_title(title), m_eventManager(eventManager) {
2424
glfwSetFramebufferSizeCallback(m_window, framebufferResizeCallback);
2525

2626
glfwSetKeyCallback(m_window, keyCallback);
27-
glfwSetCursorPosCallback(m_window, mouseButtonCallback);
27+
glfwSetCursorPosCallback(m_window, cursorPosCallback);
28+
glfwSetMouseButtonCallback(m_window, mouseButtonCallback);
2829
}
2930

3031
// The destructor cleans up GLFW resources.
@@ -83,9 +84,28 @@ m_width(width), m_height(height), m_title(title), m_eventManager(eventManager) {
8384
}
8485

8586
}
86-
void Window::mouseButtonCallback(GLFWwindow *window, double xpos, double ypos) {
87+
88+
void Window::mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
89+
auto* windowInstance = static_cast<Window*>(glfwGetWindowUserPointer(window));
90+
if (!windowInstance) return;
91+
92+
Event event{};
93+
if (action == GLFW_PRESS) {
94+
event.type = EventType::MouseButtonPressed;
95+
event.mouseButton.button = button;
96+
windowInstance->m_eventManager.post(event);
97+
} else if (action == GLFW_RELEASE) {
98+
event.type = EventType::MouseButtonReleased;
99+
event.mouseButton.button = button;
100+
}
101+
102+
windowInstance->m_eventManager.post(event);
103+
}
104+
105+
void Window::cursorPosCallback(GLFWwindow *window, double xpos, double ypos) {
87106
auto* windowInstance = static_cast<Window*>(glfwGetWindowUserPointer(window));
88107
if (!windowInstance) return;
108+
89109
Event event{};
90110
event.type = EventType::MouseMoved;
91111
event.mouseMove.x = xpos;

src/core/Window.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ namespace reactor {
5252
// This is the static callback function that GLFW will call on resize events.
5353
static void framebufferResizeCallback(GLFWwindow* window, int width, int height);
5454
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
55-
static void mouseButtonCallback(GLFWwindow* window, double xpos, double ypos);
55+
static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
56+
static void cursorPosCallback(GLFWwindow* window, double xpos, double ypos);
5657

5758
GLFWwindow* m_window = nullptr;
5859
EventManager& m_eventManager;

src/imgui/Imgui.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ void Imgui::createFrame() {
8585
ImGui::NewFrame();
8686
//ImGui::ShowDemoWindow();
8787
ImGui::Begin("Composite");
88-
ImGui::SliderFloat("Exposure", &m_exposure, 0.0, 1.0);
89-
ImGui::SliderFloat("Contrast", &m_contrast, 0.0, 1.0);
90-
ImGui::SliderFloat("Saturation", &m_saturation, 0.0, 1.0);
88+
ImGui::SliderFloat("Exposure", &m_exposure, 0.0, 2.0);
89+
ImGui::SliderFloat("Contrast", &m_contrast, 0.0, 2.0);
90+
ImGui::SliderFloat("Saturation", &m_saturation, 0.0, 2.0);
9191

9292
ImGui::End();
9393
}

src/vulkan/VulkanRenderer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ VulkanRenderer::VulkanRenderer(const RendererConfig &config) : m_config(config)
2828
m_camera = std::make_unique<Camera>();
2929
m_orbitController = std::make_unique<OrbitController>(*m_camera);
3030
m_eventManager->subscribe(EventType::MouseMoved, m_orbitController.get());
31+
m_eventManager->subscribe(EventType::MouseButtonPressed, m_orbitController.get());
32+
m_eventManager->subscribe(EventType::MouseButtonReleased, m_orbitController.get());
3133
}
3234

3335
void VulkanRenderer::createCoreVulkanObjects() {

0 commit comments

Comments
 (0)