Skip to content

Commit

Permalink
GLFW backends: Properly convert mouse position to pixel coordinates, …
Browse files Browse the repository at this point in the history
…see #521
  • Loading branch information
mikke89 committed Oct 16, 2023
1 parent f43ee36 commit 836e6c4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Backends/RmlUi_Backend_GLFW_GL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions Backends/RmlUi_Backend_GLFW_GL3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions Backends/RmlUi_Backend_GLFW_VK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions Backends/RmlUi_Platform_GLFW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion Backends/RmlUi_Platform_GLFW.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 836e6c4

Please sign in to comment.