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
This commit is contained in:
Joshua Woodie 2021-10-09 16:15:31 -04:00
parent b6efafdf6e
commit 362915ead2
3 changed files with 72 additions and 6 deletions

View File

@ -1684,10 +1684,6 @@ typedef void (* GLFWwindowcontentscalefun)(GLFWwindow* window, float xscale, flo
* may add more actions. * may add more actions.
* @param[in] mods Bit field describing which [modifier keys](@ref mods) were * @param[in] mods Bit field describing which [modifier keys](@ref mods) were
* held down. * 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 input_mouse_button
* @sa @ref glfwSetMouseButtonCallback * @sa @ref glfwSetMouseButtonCallback
@ -1697,7 +1693,32 @@ typedef void (* GLFWwindowcontentscalefun)(GLFWwindow* window, float xscale, flo
* *
* @ingroup input * @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. /*! @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); 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. /*! @brief Sets the cursor position callback.
* *
* This function sets the cursor position callback of the specified window, * This function sets the cursor position callback of the specified window,

View File

@ -336,7 +336,9 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods)
window->mouseButtons[button] = (char) action; window->mouseButtons[button] = (char) action;
if (window->callbacks.mouseButton) 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 // Notifies shared code of a cursor motion event
@ -890,6 +892,17 @@ GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* handle,
return cbfun; 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, GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle,
GLFWcursorposfun cbfun) GLFWcursorposfun cbfun)
{ {

View File

@ -419,6 +419,7 @@ struct _GLFWwindow
GLFWframebuffersizefun fbsize; GLFWframebuffersizefun fbsize;
GLFWwindowcontentscalefun scale; GLFWwindowcontentscalefun scale;
GLFWmousebuttonfun mouseButton; GLFWmousebuttonfun mouseButton;
GLFWmousebuttonposfun mouseButtonPos;
GLFWcursorposfun cursorPos; GLFWcursorposfun cursorPos;
GLFWcursorenterfun cursorEnter; GLFWcursorenterfun cursorEnter;
GLFWscrollfun scroll; GLFWscrollfun scroll;