Browse Source

GLFW backends: Properly convert mouse position to pixel coordinates, see #521

Michael Ragazzon 2 years ago
parent
commit
836e6c44c9

+ 2 - 2
Backends/RmlUi_Backend_GLFW_GL2.cpp

@@ -230,8 +230,8 @@ static void SetupCallbacks(GLFWwindow* window)
 	glfwSetCursorEnterCallback(window, [](GLFWwindow* /*window*/, int entered) { RmlGLFW::ProcessCursorEnterCallback(data->context, entered); });
 
 	// Mouse input
-	glfwSetCursorPosCallback(window, [](GLFWwindow* /*window*/, double xpos, double ypos) {
-		RmlGLFW::ProcessCursorPosCallback(data->context, xpos, ypos, data->glfw_active_modifiers);
+	glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
+		RmlGLFW::ProcessCursorPosCallback(data->context, window, xpos, ypos, data->glfw_active_modifiers);
 	});
 
 	glfwSetMouseButtonCallback(window, [](GLFWwindow* /*window*/, int button, int action, int mods) {

+ 2 - 2
Backends/RmlUi_Backend_GLFW_GL3.cpp

@@ -242,8 +242,8 @@ static void SetupCallbacks(GLFWwindow* window)
 	glfwSetCursorEnterCallback(window, [](GLFWwindow* /*window*/, int entered) { RmlGLFW::ProcessCursorEnterCallback(data->context, entered); });
 
 	// Mouse input
-	glfwSetCursorPosCallback(window, [](GLFWwindow* /*window*/, double xpos, double ypos) {
-		RmlGLFW::ProcessCursorPosCallback(data->context, xpos, ypos, data->glfw_active_modifiers);
+	glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
+		RmlGLFW::ProcessCursorPosCallback(data->context, window, xpos, ypos, data->glfw_active_modifiers);
 	});
 
 	glfwSetMouseButtonCallback(window, [](GLFWwindow* /*window*/, int button, int action, int mods) {

+ 2 - 2
Backends/RmlUi_Backend_GLFW_VK.cpp

@@ -270,8 +270,8 @@ static void SetupCallbacks(GLFWwindow* window)
 	glfwSetCursorEnterCallback(window, [](GLFWwindow* /*window*/, int entered) { RmlGLFW::ProcessCursorEnterCallback(data->context, entered); });
 
 	// Mouse input
-	glfwSetCursorPosCallback(window, [](GLFWwindow* /*window*/, double xpos, double ypos) {
-		RmlGLFW::ProcessCursorPosCallback(data->context, xpos, ypos, data->glfw_active_modifiers);
+	glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
+		RmlGLFW::ProcessCursorPosCallback(data->context, window, xpos, ypos, data->glfw_active_modifiers);
 	});
 
 	glfwSetMouseButtonCallback(window, [](GLFWwindow* /*window*/, int button, int action, int mods) {

+ 14 - 2
Backends/RmlUi_Platform_GLFW.cpp

@@ -30,6 +30,7 @@
 #include <RmlUi/Core/Context.h>
 #include <RmlUi/Core/Input.h>
 #include <RmlUi/Core/Log.h>
+#include <RmlUi/Core/Math.h>
 #include <RmlUi/Core/StringUtilities.h>
 #include <RmlUi/Core/SystemInterface.h>
 #include <GLFW/glfw3.h>
@@ -135,12 +136,23 @@ bool RmlGLFW::ProcessCursorEnterCallback(Rml::Context* context, int entered)
 	return result;
 }
 
-bool RmlGLFW::ProcessCursorPosCallback(Rml::Context* context, double xpos, double ypos, int mods)
+bool RmlGLFW::ProcessCursorPosCallback(Rml::Context* context, GLFWwindow* window, double xpos, double ypos, int mods)
 {
 	if (!context)
 		return true;
 
-	bool result = context->ProcessMouseMove(int(xpos), int(ypos), RmlGLFW::ConvertKeyModifiers(mods));
+	using Rml::Vector2i;
+	using Vector2d = Rml::Vector2<double>;
+
+	Vector2i window_size, framebuffer_size;
+	glfwGetWindowSize(window, &window_size.x, &window_size.y);
+	glfwGetFramebufferSize(window, &framebuffer_size.x, &framebuffer_size.y);
+
+	// Convert from mouse position in GLFW screen coordinates to framebuffer coordinates (pixels) used by RmlUi.
+	const Vector2d mouse_pos = Vector2d(xpos, ypos) * (Vector2d(window_size) / Vector2d(framebuffer_size));
+	const Vector2i mouse_pos_round = {int(Rml::Math::Round(mouse_pos.x)), int(Rml::Math::Round(mouse_pos.y))};
+
+	bool result = context->ProcessMouseMove(mouse_pos_round.x, mouse_pos_round.y, RmlGLFW::ConvertKeyModifiers(mods));
 	return result;
 }
 

+ 1 - 1
Backends/RmlUi_Platform_GLFW.h

@@ -70,7 +70,7 @@ namespace RmlGLFW {
 bool ProcessKeyCallback(Rml::Context* context, int key, int action, int mods);
 bool ProcessCharCallback(Rml::Context* context, unsigned int codepoint);
 bool ProcessCursorEnterCallback(Rml::Context* context, int entered);
-bool ProcessCursorPosCallback(Rml::Context* context, double xpos, double ypos, int mods);
+bool ProcessCursorPosCallback(Rml::Context* context, GLFWwindow* window, double xpos, double ypos, int mods);
 bool ProcessMouseButtonCallback(Rml::Context* context, int button, int action, int mods);
 bool ProcessScrollCallback(Rml::Context* context, double yoffset, int mods);
 void ProcessFramebufferSizeCallback(Rml::Context* context, int width, int height);