From 362915ead2d1cf988824628918751b9976fa02f3 Mon Sep 17 00:00:00 2001 From: Joshua Woodie Date: Sat, 9 Oct 2021 16:15:31 -0400 Subject: [PATCH] Add new mouse button callback function In many cases it can be useful to get the cursor position along with the mouse buttons. This commit adds a new callback that specifies position along with buttons. Resolves: #752 --- include/GLFW/glfw3.h | 62 ++++++++++++++++++++++++++++++++++++++++---- src/input.c | 15 ++++++++++- src/internal.h | 1 + 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index c5436371..b16f5ebd 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1684,10 +1684,6 @@ typedef void (* GLFWwindowcontentscalefun)(GLFWwindow* window, float xscale, flo * may add more actions. * @param[in] mods Bit field describing which [modifier keys](@ref mods) were * held down. - * @param[in] xpos The x-coordinate of the cursor, relative to the left edge of - * the content area. - * @param[in] ypos The y-coordinate of the cursor, relative to the top edge of - * the content area. * * @sa @ref input_mouse_button * @sa @ref glfwSetMouseButtonCallback @@ -1697,7 +1693,32 @@ typedef void (* GLFWwindowcontentscalefun)(GLFWwindow* window, float xscale, flo * * @ingroup input */ -typedef void (* GLFWmousebuttonfun)(GLFWwindow* window, int button, int action, int mods, double xpos, double ypos); +typedef void (* GLFWmousebuttonfun)(GLFWwindow* window, int button, int action, int mods); + +/*! @brief The function pointer type for mouse button & position callbacks. + * This is the function pointer type for mouse button & position callback functions. + * A mouse button callback function has the following signature: + * @code + * void function_name(GLFWwindow* window, int button, int action, int mods, double xpos, double ypos) + * @endcode + * + * @param[in] window The window that received the vent. + * @param[in] button The [mouse button](@ref buttons) that was pressed or + * released. + * @param[in] action One of 'GLFW_PRESS' or 'GLFW_RELEASE'. Future releases + * may add more actions. + * @param[in] mods Bit field describing which [modifier keys](@ref mods) were + * held down. + * @param[in] xpos The cursor x-position relative to the left edge of the + * content area. + * @param[in] xpos The cursor y-position relative to the right edge of the + * content area. + * + * @since Added in version 4.0 + * + * @ingroup input + */ +typedef void (* GLFWmousebuttonposfun)(GLFWwindow* window, int button, int action, int mods, double xpos, double ypos); /*! @brief The function pointer type for cursor position callbacks. * @@ -5038,6 +5059,37 @@ GLFWAPI GLFWcharmodsfun glfwSetCharModsCallback(GLFWwindow* window, GLFWcharmods */ GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun callback); +/*! @brief Sets the mouse button & position callback. + * + * This function sets the mouse button and position callback of the specified window, which + * is called when a mouse button is pressed or released. + * + * For more information see the @ref glfwSetMouseButtonCallback. + * + * When the callback is called it also provides the cursor position relative + * to the window content area. + * + * @param[in] window The window whose callback to set. + * @param[in] callback The new callback, or `NULL` to remove the currently set + * callback. + * @return The previously set callback, or `NULL` if no callback was set or the + * library had not been [initialized](@ref intro_init). + * + * @callback_signature + * @code + * void function_name(GLFWwindow* window, int button, int action, int mods, double xpos, double ypos) + * @endcode + * For more information about the callback parameters, see the + * [function pointer type](@ref GLFWmousebutonposfun). + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED. + * + * @thread_safety This function must only be called from the main thread. + * + * @ingroup input + */ +GLFWAPI GLFWmousebuttonposfun glfwSetMouseButtonPosCallback(GLFWwindow* window, GLFWmousebuttonposfun callback); + /*! @brief Sets the cursor position callback. * * This function sets the cursor position callback of the specified window, diff --git a/src/input.c b/src/input.c index 948d47de..6ece9545 100644 --- a/src/input.c +++ b/src/input.c @@ -336,7 +336,9 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods) window->mouseButtons[button] = (char) action; if (window->callbacks.mouseButton) - window->callbacks.mouseButton((GLFWwindow*) window, button, action, mods, window->virtualCursorPosX, window->virtualCursorPosY); + window->callbacks.mouseButton((GLFWwindow*) window, button, action, mods); + if (window->callbacks.mouseButtonPos) + window->callbacks.mouseButtonPos((GLFWwindow*) window, button, action, mods, window->virtualCursorPosX, window->virtualCursorPosY); } // Notifies shared code of a cursor motion event @@ -890,6 +892,17 @@ GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* handle, return cbfun; } +GLFWAPI GLFWmousebuttonposfun glfwSetMouseButtonPosCallback(GLFWwindow *handle, + GLFWmousebuttonposfun cbfun) +{ + _GLFWwindow* window = (_GLFWwindow*) handle; + assert(window != NULL); + + _GLFW_REQUIRE_INIT_OR_RETURN(NULL); + _GLFW_SWAP_POINTERS(window->callbacks.mouseButtonPos, cbfun); + return cbfun; +} + GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle, GLFWcursorposfun cbfun) { diff --git a/src/internal.h b/src/internal.h index f41db3c3..d7c9344e 100644 --- a/src/internal.h +++ b/src/internal.h @@ -419,6 +419,7 @@ struct _GLFWwindow GLFWframebuffersizefun fbsize; GLFWwindowcontentscalefun scale; GLFWmousebuttonfun mouseButton; + GLFWmousebuttonposfun mouseButtonPos; GLFWcursorposfun cursorPos; GLFWcursorenterfun cursorEnter; GLFWscrollfun scroll;