diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index 2de5b409..2c55b136 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -389,7 +389,7 @@ typedef void (* GLFWwindowfocusfun)(GLFWwindow,int); typedef void (* GLFWwindowiconifyfun)(GLFWwindow,int); typedef void (* GLFWmousebuttonfun)(GLFWwindow,int,int); typedef void (* GLFWmouseposfun)(GLFWwindow,int,int); -typedef void (* GLFWmousewheelfun)(GLFWwindow,int); +typedef void (* GLFWscrollfun)(GLFWwindow,int,int); typedef void (* GLFWkeyfun)(GLFWwindow,int,int); typedef void (* GLFWcharfun)(GLFWwindow,int); @@ -443,13 +443,12 @@ GLFWAPI int glfwGetKey(GLFWwindow window, int key); GLFWAPI int glfwGetMouseButton(GLFWwindow window, int button); GLFWAPI void glfwGetMousePos(GLFWwindow window, int* xpos, int* ypos); GLFWAPI void glfwSetMousePos(GLFWwindow window, int xpos, int ypos); -GLFWAPI int glfwGetMouseWheel(GLFWwindow window); -GLFWAPI void glfwSetMouseWheel(GLFWwindow window, int pos); +GLFWAPI void glfwGetScrollOffset(GLFWwindow window, int* x, int* y); GLFWAPI void glfwSetKeyCallback(GLFWwindow window, GLFWkeyfun cbfun); GLFWAPI void glfwSetCharCallback(GLFWwindow window, GLFWcharfun cbfun); GLFWAPI void glfwSetMouseButtonCallback(GLFWwindow window, GLFWmousebuttonfun cbfun); GLFWAPI void glfwSetMousePosCallback(GLFWwindow window, GLFWmouseposfun cbfun); -GLFWAPI void glfwSetMouseWheelCallback(GLFWwindow window, GLFWmousewheelfun cbfun); +GLFWAPI void glfwSetScrollCallback(GLFWwindow window, GLFWscrollfun cbfun); /* Joystick input */ GLFWAPI int glfwGetJoystickParam(int joy, int param); diff --git a/readme.html b/readme.html index dbc63445..06292c66 100644 --- a/readme.html +++ b/readme.html @@ -276,6 +276,7 @@ version of GLFW.

  • Renamed glfw.h to glfw3.h to avoid conflicts with 2.x series
  • Renamed GLFW_WINDOW token to GLFW_WINDOWED
  • Replaced ad hoc build system with CMake
  • +
  • Replaced mouse wheel interface with two-dimensional scrolling interface
  • Made Unicode character input unaffected by GLFW_KEY_REPEAT
  • Removed event auto-polling and the GLFW_AUTO_POLL_EVENTS window enable
  • Removed the entire threading API
  • diff --git a/src/input.c b/src/input.c index 9164e148..c5ad0c7b 100644 --- a/src/input.c +++ b/src/input.c @@ -148,26 +148,10 @@ GLFWAPI void glfwSetMousePos(GLFWwindow window, int xpos, int ypos) //======================================================================== -// Returns the mouse wheel "position" for the specified window +// Returns the scroll offset for the specified window //======================================================================== -GLFWAPI int glfwGetMouseWheel(GLFWwindow window) -{ - if (!_glfwInitialized) - { - _glfwSetError(GLFW_NOT_INITIALIZED); - return 0; - } - - return window->wheelPos; -} - - -//======================================================================== -// Sets the mouse wheel "position" for the specified window -//======================================================================== - -GLFWAPI void glfwSetMouseWheel(GLFWwindow window, int pos) +GLFWAPI void glfwGetScrollOffset(GLFWwindow window, int* x, int* y) { if (!_glfwInitialized) { @@ -175,7 +159,11 @@ GLFWAPI void glfwSetMouseWheel(GLFWwindow window, int pos) return; } - window->wheelPos = pos; + if (x) + *x = window->scrollX; + + if (y) + *y = window->scrollY; } @@ -250,10 +238,10 @@ GLFWAPI void glfwSetMousePosCallback(GLFWwindow window, GLFWmouseposfun cbfun) //======================================================================== -// Set callback function for mouse wheel +// Set callback function for scroll events //======================================================================== -GLFWAPI void glfwSetMouseWheelCallback(GLFWwindow window, GLFWmousewheelfun cbfun) +GLFWAPI void glfwSetScrollCallback(GLFWwindow window, GLFWscrollfun cbfun) { if (!_glfwInitialized) { @@ -262,11 +250,11 @@ GLFWAPI void glfwSetMouseWheelCallback(GLFWwindow window, GLFWmousewheelfun cbfu } // Set callback function - window->mouseWheelCallback = cbfun; + window->scrollCallback = cbfun; // Call the callback function to let the application know the current - // mouse wheel position + // scroll offset if (cbfun) - cbfun(window, window->wheelPos); + cbfun(window, window->scrollX, window->scrollY); } diff --git a/src/internal.h b/src/internal.h index 7c3d0187..d33b94f2 100644 --- a/src/internal.h +++ b/src/internal.h @@ -152,7 +152,7 @@ typedef struct _GLFWwindow GLFWwindowiconifyfun windowIconifyCallback; GLFWmousebuttonfun mouseButtonCallback; GLFWmouseposfun mousePosCallback; - GLFWmousewheelfun mouseWheelCallback; + GLFWscrollfun scrollCallback; GLFWkeyfun keyCallback; GLFWcharfun charCallback; @@ -172,7 +172,7 @@ typedef struct _GLFWwindow GLboolean stickyMouseButtons; GLboolean keyRepeat; int mousePosX, mousePosY; - int wheelPos; + int scrollX, scrollY; char mouseButton[GLFW_MOUSE_BUTTON_LAST + 1]; char key[GLFW_KEY_LAST + 1]; diff --git a/src/window.c b/src/window.c index 62622d80..288f252d 100644 --- a/src/window.c +++ b/src/window.c @@ -91,7 +91,8 @@ void clearInputState(_GLFWwindow* window) window->mousePosY = 0; // Set mouse wheel position to 0 - window->wheelPos = 0; + window->scrollX = 0; + window->scrollY = 0; // The default is to use non sticky keys and mouse buttons window->stickyKeys = GL_FALSE; @@ -163,6 +164,20 @@ void _glfwInputChar(_GLFWwindow* window, int character) } +//======================================================================== +// Register scroll events +//======================================================================== + +void _glfwInputScroll(_GLFWwindow* window, int x, int y) +{ + window->scrollX += x; + window->scrollY += y; + + if (window->scrollCallback) + window->scrollCallback(window, x, y); +} + + //======================================================================== // Register mouse button clicks //======================================================================== diff --git a/src/x11/x11_window.c b/src/x11/x11_window.c index 2d3db8e8..9a304c75 100644 --- a/src/x11/x11_window.c +++ b/src/x11/x11_window.c @@ -41,6 +41,9 @@ #define _NET_WM_STATE_ADD 1 #define _NET_WM_STATE_TOGGLE 2 +// Additional mouse button names for XButtonEvent +#define Button6 6 +#define Button7 7 ////////////////////////////////////////////////////////////////////////// ////// GLFW internal API ////// @@ -1094,17 +1097,15 @@ static void processSingleEvent(void) // XFree86 3.3.2 and later translates mouse wheel up/down into // mouse button 4 & 5 presses else if (event.xbutton.button == Button4) - { - window->wheelPos++; // To verify: is this up or down? - if (window->mouseWheelCallback) - window->mouseWheelCallback(window, window->wheelPos); - } + _glfwInputScroll(window, 0, 1); else if (event.xbutton.button == Button5) - { - window->wheelPos--; - if (window->mouseWheelCallback) - window->mouseWheelCallback(window, window->wheelPos); - } + _glfwInputScroll(window, 0, -1); + + else if (event.xbutton.button == Button6) + _glfwInputScroll(window, -1, 0); + else if (event.xbutton.button == Button7) + _glfwInputScroll(window, 1, 0); + break; } @@ -1741,8 +1742,15 @@ void _glfwPlatformPollEvents(void) { _GLFWwindow* window; + for (window = _glfwLibrary.windowListHead; window; window = window->next) + { + window->scrollX = 0; + window->scrollY = 0; + } + // Flag that the cursor has not moved - if (window = _glfwLibrary.cursorLockWindow) + window = _glfwLibrary.cursorLockWindow; + if (window) window->X11.mouseMoved = GL_FALSE; // Process all pending events @@ -1750,7 +1758,8 @@ void _glfwPlatformPollEvents(void) processSingleEvent(); // Did we get mouse movement in fully enabled hidden cursor mode? - if (window = _glfwLibrary.cursorLockWindow) + window = _glfwLibrary.cursorLockWindow; + if (window) { if (window->X11.mouseMoved && window->X11.pointerHidden) { diff --git a/tests/events.c b/tests/events.c index 26c1dfea..4ba11449 100644 --- a/tests/events.c +++ b/tests/events.c @@ -218,9 +218,9 @@ static void mouse_position_callback(GLFWwindow window, int x, int y) printf("%08x at %0.3f: Mouse position: %i %i\n", counter++, glfwGetTime(), x, y); } -static void mouse_wheel_callback(GLFWwindow window, int position) +static void scroll_callback(GLFWwindow window, int x, int y) { - printf("%08x at %0.3f: Mouse wheel: %i\n", counter++, glfwGetTime(), position); + printf("%08x at %0.3f: Scroll: %i %i\n", counter++, glfwGetTime(), x, y); } static void key_callback(GLFWwindow window, int key, int action) @@ -310,7 +310,7 @@ int main(void) glfwSetWindowIconifyCallback(window, window_iconify_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetMousePosCallback(window, mouse_position_callback); - glfwSetMouseWheelCallback(window, mouse_wheel_callback); + glfwSetScrollCallback(window, scroll_callback); glfwSetKeyCallback(window, key_callback); glfwSetCharCallback(window, char_callback);