From d0840bdea14fbf3abab21d3094a5b2811e99acee Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 9 Oct 2011 17:10:40 +0200 Subject: [PATCH] Added more input functions to clarify internal API. --- src/cocoa_window.m | 33 +++++----------------- src/internal.h | 10 +++++-- src/win32_window.c | 45 ++++++++---------------------- src/window.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ src/x11_window.c | 56 ++++++++++--------------------------- 5 files changed, 109 insertions(+), 104 deletions(-) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 93e37089..136a4378 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -67,11 +67,8 @@ NSRect contentRect = [window->NS.window contentRectForFrameRect:[window->NS.window frame]]; - window->width = contentRect.size.width; - window->height = contentRect.size.height; - if (_glfwLibrary.windowSizeCallback) - _glfwLibrary.windowSizeCallback(window, window->width, window->height); + _glfwInputWindowSize(window, contentRect.size.width, contentRect.size.height); } - (void)windowDidMove:(NSNotification *)notification @@ -87,24 +84,17 @@ mainScreenHeight - contentRect.origin.y - mainScreenOrigin.y - window->height); - window->positionX = flippedPos.x; - window->positionY = flippedPos.y; + _glfwInputWindowPos(window, flippedPos.x, flippedPos.y); } - (void)windowDidMiniaturize:(NSNotification *)notification { - window->iconified = GL_TRUE; - - if (_glfwLibrary.windowIconifyCallback) - _glfwLibrary.windowIconifyCallback(window, window->iconified); + _glfwInputWindowIconify(window, GL_TRUE); } - (void)windowDidDeminiaturize:(NSNotification *)notification { - window->iconified = GL_FALSE; - - if (_glfwLibrary.windowIconifyCallback) - _glfwLibrary.windowIconifyCallback(window, window->iconified); + _glfwInputWindowIconify(window, GL_FALSE); } - (void)windowDidBecomeKey:(NSNotification *)notification @@ -349,24 +339,15 @@ static int convertMacKeyCode(unsigned int macKeyCode) - (void)mouseMoved:(NSEvent *)event { if (window->cursorMode == GLFW_CURSOR_CAPTURED) - { - window->mousePosX += [event deltaX]; - window->mousePosY += [event deltaY]; - } + _glfwInputCursorMotion(window, [event deltaX], [event deltaY]); else { NSPoint p = [event locationInWindow]; // Cocoa coordinate system has origin at lower left - window->mousePosX = p.x; - window->mousePosY = [[window->NS.window contentView] bounds].size.height - p.y; - } + p.y = [[window->NS.window contentView] bounds].size.height - p.y; - if (_glfwLibrary.mousePosCallback) - { - _glfwLibrary.mousePosCallback(window, - window->mousePosX, - window->mousePosY); + _glfwInputCursorMotion(window, p.x, p.y); } } diff --git a/src/internal.h b/src/internal.h index 2d38912c..bfe24fb0 100644 --- a/src/internal.h +++ b/src/internal.h @@ -339,12 +339,18 @@ void _glfwSetError(int error, const char* description); // Window management (window.c) void _glfwSetDefaultWindowHints(void); -// Input handling (window.c) +// WIndow event notification +void _glfwInputWindowFocus(_GLFWwindow* window, GLboolean activated); +void _glfwInputWindowPos(_GLFWwindow* window, int x, int y); +void _glfwInputWindowSize(_GLFWwindow* window, int width, int height); +void _glfwInputWindowIconify(_GLFWwindow* window, int iconified); + +// Input event notification void _glfwInputKey(_GLFWwindow* window, int key, int action); void _glfwInputChar(_GLFWwindow* window, int character); void _glfwInputScroll(_GLFWwindow* window, int x, int y); void _glfwInputMouseClick(_GLFWwindow* window, int button, int action); -void _glfwInputWindowFocus(_GLFWwindow* window, GLboolean activated); +void _glfwInputCursorMotion(_GLFWwindow* window, int x, int y); // OpenGL context helpers (opengl.c) int _glfwStringInExtensionString(const char* string, const GLubyte* extensions); diff --git a/src/win32_window.c b/src/win32_window.c index e975235b..6343f902 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -852,15 +852,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, } _glfwInputWindowFocus(window, active); - - if (iconified != window->iconified) - { - window->iconified = iconified; - - if (_glfwLibrary.windowIconifyCallback) - _glfwLibrary.windowIconifyCallback(window, window->iconified); - } - + _glfwInputWindowIconify(window, iconified); return 0; } @@ -1006,32 +998,27 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (newMouseX != window->Win32.oldMouseX || newMouseY != window->Win32.oldMouseY) { + int x, y; + if (window->cursorMode == GLFW_CURSOR_CAPTURED) { if (_glfwLibrary.activeWindow != window) return 0; - window->mousePosX += newMouseX - - window->Win32.oldMouseX; - window->mousePosY += newMouseY - - window->Win32.oldMouseY; + x += newMouseX - window->Win32.oldMouseX; + y += newMouseY - window->Win32.oldMouseY; } else { - window->mousePosX = newMouseX; - window->mousePosY = newMouseY; + x = newMouseX; + x = newMouseY; } window->Win32.oldMouseX = newMouseX; window->Win32.oldMouseY = newMouseY; window->Win32.cursorCentered = GL_FALSE; - if (_glfwLibrary.mousePosCallback) - { - _glfwLibrary.mousePosCallback(window, - window->mousePosX, - window->mousePosY); - } + _glfwInputCursorMotion(window, x, y); } return 0; @@ -1053,9 +1040,6 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, case WM_SIZE: { - window->width = LOWORD(lParam); - window->height = HIWORD(lParam); - // If window is in cursor capture mode, update clipping rect if (window->cursorMode == GLFW_CURSOR_CAPTURED) { @@ -1064,21 +1048,12 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, ClipCursor(&ClipWindowRect); } - if (_glfwLibrary.windowSizeCallback) - { - _glfwLibrary.windowSizeCallback(window, - window->width, - window->height); - } - + _glfwInputWindowSize(window, LOWORD(lParam), HIWORD(lParam)); return 0; } case WM_MOVE: { - window->positionX = LOWORD(lParam); - window->positionY = HIWORD(lParam); - // If window is in cursor capture mode, update clipping rect if (window->cursorMode == GLFW_CURSOR_CAPTURED) { @@ -1086,6 +1061,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (GetWindowRect(window->Win32.handle, &ClipWindowRect)) ClipCursor(&ClipWindowRect); } + + _glfwInputWindowPos(window, LOWORD(lParam), HIWORD(lParam)); return 0; } diff --git a/src/window.c b/src/window.c index 06c32b8d..73d32114 100644 --- a/src/window.c +++ b/src/window.c @@ -182,6 +182,28 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action) } +//======================================================================== +// Register cursor moves +//======================================================================== + +void _glfwInputCursorMotion(_GLFWwindow* window, int x, int y) +{ + if (window->cursorMode == GLFW_CURSOR_CAPTURED) + { + window->mousePosX += x; + window->mousePosY += y; + } + else + { + window->mousePosX = x; + window->mousePosY = y; + } + + if (_glfwLibrary.mousePosCallback) + _glfwLibrary.mousePosCallback(window, x, y); +} + + //======================================================================== // Register window focus events //======================================================================== @@ -227,6 +249,53 @@ void _glfwInputWindowFocus(_GLFWwindow* window, GLboolean activated) } +//======================================================================== +// Register window position events +//======================================================================== + +void _glfwInputWindowPos(_GLFWwindow* window, int x, int y) +{ + if (window->positionX == x && window->positionY == y) + return; + + window->positionX = x; + window->positionY = y; +} + + +//======================================================================== +// Register window size events +//======================================================================== + +void _glfwInputWindowSize(_GLFWwindow* window, int width, int height) +{ + if (window->width == width && window->height == height) + return; + + window->width = width; + window->height = height; + + if (_glfwLibrary.windowSizeCallback) + _glfwLibrary.windowSizeCallback(window, width, height); +} + + +//======================================================================== +// Register window size events +//======================================================================== + +void _glfwInputWindowIconify(_GLFWwindow* window, int iconified) +{ + if (window->iconified == iconified) + return; + + window->iconified = iconified; + + if (_glfwLibrary.windowIconifyCallback) + _glfwLibrary.windowIconifyCallback(window, iconified); +} + + ////////////////////////////////////////////////////////////////////////// ////// GLFW public API ////// ////////////////////////////////////////////////////////////////////////// diff --git a/src/x11_window.c b/src/x11_window.c index 962da7fb..c08b9b36 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -1194,33 +1194,27 @@ static void processSingleEvent(void) event.xmotion.y != window->X11.cursorPosY) { // The mouse cursor was moved and we didn't do it + int x, y; if (window->cursorMode == GLFW_CURSOR_CAPTURED) { if (_glfwLibrary.activeWindow != window) break; - window->mousePosX += event.xmotion.x - - window->X11.cursorPosX; - window->mousePosY += event.xmotion.y - - window->X11.cursorPosY; + x = event.xmotion.x - window->X11.cursorPosX; + y = event.xmotion.y - window->X11.cursorPosY; } else { - window->mousePosX = event.xmotion.x; - window->mousePosY = event.xmotion.y; + x = event.xmotion.x; + x = event.xmotion.y; } window->X11.cursorPosX = event.xmotion.x; window->X11.cursorPosY = event.xmotion.y; window->X11.cursorCentered = GL_FALSE; - if (_glfwLibrary.mousePosCallback) - { - _glfwLibrary.mousePosCallback(window, - window->mousePosX, - window->mousePosY); - } + _glfwInputCursorMotion(window, x, y); } break; @@ -1236,27 +1230,13 @@ static void processSingleEvent(void) return; } - if (event.xconfigure.width != window->width || - event.xconfigure.height != window->height) - { - // The window was resized + _glfwInputWindowSize(window, + event.xconfigure.width, + event.xconfigure.height); - window->width = event.xconfigure.width; - window->height = event.xconfigure.height; - if (_glfwLibrary.windowSizeCallback) - { - _glfwLibrary.windowSizeCallback(window, - window->width, - window->height); - } - } - - if (event.xconfigure.x != window->positionX || - event.xconfigure.y != window->positionY) - { - window->positionX = event.xconfigure.x; - window->positionY = event.xconfigure.y; - } + _glfwInputWindowPos(window, + event.xconfigure.x, + event.xconfigure.y); break; } @@ -1305,11 +1285,7 @@ static void processSingleEvent(void) return; } - window->iconified = GL_FALSE; - - if (_glfwLibrary.windowIconifyCallback) - _glfwLibrary.windowIconifyCallback(window, window->iconified); - + _glfwInputWindowIconify(window, GL_FALSE); break; } @@ -1323,11 +1299,7 @@ static void processSingleEvent(void) return; } - window->iconified = GL_TRUE; - - if (_glfwLibrary.windowIconifyCallback) - _glfwLibrary.windowIconifyCallback(window, window->iconified); - + _glfwInputWindowIconify(window, GL_TRUE); break; }