mirror of
https://github.com/glfw/glfw.git
synced 2024-11-11 13:03:52 +00:00
Cursor mode cleanup.
This commit is contained in:
parent
54b8d0d3c8
commit
e0a26aa62f
@ -41,9 +41,9 @@ static void centerCursor(_GLFWwindow *window)
|
||||
|
||||
// Update the cursor to match the specified cursor mode
|
||||
//
|
||||
static void setModeCursor(_GLFWwindow* window, int mode)
|
||||
static void setModeCursor(_GLFWwindow* window)
|
||||
{
|
||||
if (mode == GLFW_CURSOR_NORMAL)
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL)
|
||||
[[NSCursor arrowCursor] set];
|
||||
else
|
||||
[(NSCursor*) _glfw.ns.cursor set];
|
||||
@ -472,7 +472,7 @@ static int translateKey(unsigned int key)
|
||||
|
||||
- (void)cursorUpdate:(NSEvent *)event
|
||||
{
|
||||
setModeCursor(window, window->cursorMode);
|
||||
setModeCursor(window);
|
||||
}
|
||||
|
||||
- (void)mouseDown:(NSEvent *)event
|
||||
@ -1084,7 +1084,7 @@ void _glfwPlatformWaitEvents(void)
|
||||
|
||||
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
|
||||
{
|
||||
setModeCursor(window, window->cursorMode);
|
||||
setModeCursor(window);
|
||||
|
||||
if (window->monitor)
|
||||
{
|
||||
@ -1102,11 +1102,11 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
|
||||
}
|
||||
}
|
||||
|
||||
void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
|
||||
void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
|
||||
{
|
||||
setModeCursor(window, mode);
|
||||
setModeCursor(window);
|
||||
|
||||
if (mode == GLFW_CURSOR_DISABLED)
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
{
|
||||
CGAssociateMouseAndMouseCursorPosition(false);
|
||||
centerCursor(window);
|
||||
|
@ -70,7 +70,7 @@ static void setCursorMode(_GLFWwindow* window, int newMode)
|
||||
_glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0);
|
||||
}
|
||||
|
||||
_glfwPlatformSetCursorMode(window, newMode);
|
||||
_glfwPlatformApplyCursorMode(window);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -364,12 +364,11 @@ const char* _glfwPlatformGetVersionString(void);
|
||||
*/
|
||||
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos);
|
||||
|
||||
/*! @brief Sets up the specified cursor mode for the specified window.
|
||||
* @param[in] window The window whose cursor mode to change.
|
||||
* @param[in] mode The desired cursor mode.
|
||||
/*! @brief Applies the cursor mode of the specified window to the system.
|
||||
* @param[in] window The window whose cursor mode to apply.
|
||||
* @ingroup platform
|
||||
*/
|
||||
void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode);
|
||||
void _glfwPlatformApplyCursorMode(_GLFWwindow* window);
|
||||
|
||||
/*! @copydoc glfwGetMonitors
|
||||
* @ingroup platform
|
||||
|
@ -45,7 +45,7 @@ static void updateClipRect(_GLFWwindow* window)
|
||||
ClipCursor(&clipRect);
|
||||
}
|
||||
|
||||
// Hide mouse cursor
|
||||
// Hide the mouse cursor
|
||||
//
|
||||
static void hideCursor(_GLFWwindow* window)
|
||||
{
|
||||
@ -67,9 +67,9 @@ static void hideCursor(_GLFWwindow* window)
|
||||
}
|
||||
}
|
||||
|
||||
// Capture mouse cursor
|
||||
// Disable the mouse cursor
|
||||
//
|
||||
static void captureCursor(_GLFWwindow* window)
|
||||
static void disableCursor(_GLFWwindow* window)
|
||||
{
|
||||
if (!window->win32.cursorHidden)
|
||||
{
|
||||
@ -81,9 +81,9 @@ static void captureCursor(_GLFWwindow* window)
|
||||
SetCapture(window->win32.handle);
|
||||
}
|
||||
|
||||
// Show mouse cursor
|
||||
// Restores the mouse cursor
|
||||
//
|
||||
static void showCursor(_GLFWwindow* window)
|
||||
static void restoreCursor(_GLFWwindow* window)
|
||||
{
|
||||
POINT pos;
|
||||
|
||||
@ -410,7 +410,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
||||
// The window was defocused (or iconified, see above)
|
||||
|
||||
if (window->cursorMode != GLFW_CURSOR_NORMAL)
|
||||
showCursor(window);
|
||||
restoreCursor(window);
|
||||
|
||||
if (window->monitor)
|
||||
{
|
||||
@ -428,10 +428,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
||||
{
|
||||
// The window was focused
|
||||
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
captureCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_HIDDEN)
|
||||
hideCursor(window);
|
||||
if (window->cursorMode != GLFW_CURSOR_NORMAL)
|
||||
_glfwPlatformApplyCursorMode(window);
|
||||
|
||||
if (window->monitor)
|
||||
_glfwSetVideoMode(window->monitor, &window->videoMode);
|
||||
@ -678,10 +676,10 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
||||
|
||||
case WM_SIZE:
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED &&
|
||||
_glfw.focusedWindow == window)
|
||||
if (_glfw.focusedWindow == window)
|
||||
{
|
||||
updateClipRect(window);
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
updateClipRect(window);
|
||||
}
|
||||
|
||||
_glfwInputFramebufferSize(window, LOWORD(lParam), HIWORD(lParam));
|
||||
@ -691,10 +689,10 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
||||
|
||||
case WM_MOVE:
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED &&
|
||||
_glfw.focusedWindow == window)
|
||||
if (_glfw.focusedWindow == window)
|
||||
{
|
||||
updateClipRect(window);
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
updateClipRect(window);
|
||||
}
|
||||
|
||||
// NOTE: This cannot use LOWORD/HIWORD recommended by MSDN, as
|
||||
@ -713,12 +711,14 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
|
||||
|
||||
case WM_SETCURSOR:
|
||||
{
|
||||
if (window->cursorMode != GLFW_CURSOR_NORMAL &&
|
||||
_glfw.focusedWindow == window &&
|
||||
LOWORD(lParam) == HTCLIENT)
|
||||
if (_glfw.focusedWindow == window && LOWORD(lParam) == HTCLIENT)
|
||||
{
|
||||
SetCursor(NULL);
|
||||
return TRUE;
|
||||
if (window->cursorMode == GLFW_CURSOR_HIDDEN ||
|
||||
window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
{
|
||||
SetCursor(NULL);
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
@ -1113,7 +1113,7 @@ void _glfwPlatformPollEvents(void)
|
||||
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, 0, GLFW_RELEASE, mods);
|
||||
}
|
||||
|
||||
// Did the cursor move in an focused window that has captured the cursor
|
||||
// Did the cursor move in an focused window that has disabled the cursor
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED &&
|
||||
!window->win32.cursorCentered)
|
||||
{
|
||||
@ -1142,18 +1142,18 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos)
|
||||
window->win32.oldCursorY = (int) ypos;
|
||||
}
|
||||
|
||||
void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
|
||||
void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
|
||||
{
|
||||
switch (mode)
|
||||
switch (window->cursorMode)
|
||||
{
|
||||
case GLFW_CURSOR_NORMAL:
|
||||
showCursor(window);
|
||||
restoreCursor(window);
|
||||
break;
|
||||
case GLFW_CURSOR_HIDDEN:
|
||||
hideCursor(window);
|
||||
break;
|
||||
case GLFW_CURSOR_DISABLED:
|
||||
captureCursor(window);
|
||||
disableCursor(window);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -80,8 +80,6 @@ typedef struct _GLFWwindowX11
|
||||
|
||||
// Various platform specific internal variables
|
||||
GLboolean overrideRedirect; // True if window is OverrideRedirect
|
||||
GLboolean cursorGrabbed; // True if cursor is currently grabbed
|
||||
GLboolean cursorHidden; // True if cursor is currently hidden
|
||||
|
||||
// Cached position and size used to filter out duplicate events
|
||||
int width, height;
|
||||
|
@ -317,62 +317,30 @@ static GLboolean createWindow(_GLFWwindow* window,
|
||||
return GL_TRUE;
|
||||
}
|
||||
|
||||
// Hide cursor
|
||||
// Hide the mouse cursor
|
||||
//
|
||||
static void hideCursor(_GLFWwindow* window)
|
||||
{
|
||||
// Un-grab cursor (in windowed mode only; in fullscreen mode we still
|
||||
// want the cursor grabbed in order to confine the cursor to the window
|
||||
// area)
|
||||
if (window->x11.cursorGrabbed && window->monitor == NULL)
|
||||
{
|
||||
XUngrabPointer(_glfw.x11.display, CurrentTime);
|
||||
window->x11.cursorGrabbed = GL_FALSE;
|
||||
}
|
||||
|
||||
if (!window->x11.cursorHidden)
|
||||
{
|
||||
XDefineCursor(_glfw.x11.display, window->x11.handle, _glfw.x11.cursor);
|
||||
window->x11.cursorHidden = GL_TRUE;
|
||||
}
|
||||
XUngrabPointer(_glfw.x11.display, CurrentTime);
|
||||
XDefineCursor(_glfw.x11.display, window->x11.handle, _glfw.x11.cursor);
|
||||
}
|
||||
|
||||
// Capture cursor
|
||||
// Disable the mouse cursor
|
||||
//
|
||||
static void captureCursor(_GLFWwindow* window)
|
||||
static void disableCursor(_GLFWwindow* window)
|
||||
{
|
||||
if (!window->x11.cursorGrabbed)
|
||||
{
|
||||
if (XGrabPointer(_glfw.x11.display, window->x11.handle, True,
|
||||
ButtonPressMask | ButtonReleaseMask |
|
||||
PointerMotionMask, GrabModeAsync, GrabModeAsync,
|
||||
window->x11.handle, _glfw.x11.cursor, CurrentTime) ==
|
||||
GrabSuccess)
|
||||
{
|
||||
window->x11.cursorGrabbed = GL_TRUE;
|
||||
}
|
||||
}
|
||||
XGrabPointer(_glfw.x11.display, window->x11.handle, True,
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
|
||||
GrabModeAsync, GrabModeAsync,
|
||||
window->x11.handle, _glfw.x11.cursor, CurrentTime);
|
||||
}
|
||||
|
||||
// Show cursor
|
||||
// Restores the mouse cursor
|
||||
//
|
||||
static void showCursor(_GLFWwindow* window)
|
||||
static void restoreCursor(_GLFWwindow* window)
|
||||
{
|
||||
// Un-grab cursor (in windowed mode only; in fullscreen mode we still
|
||||
// want the cursor grabbed in order to confine the cursor to the window
|
||||
// area)
|
||||
if (window->x11.cursorGrabbed && window->monitor == NULL)
|
||||
{
|
||||
XUngrabPointer(_glfw.x11.display, CurrentTime);
|
||||
window->x11.cursorGrabbed = GL_FALSE;
|
||||
}
|
||||
|
||||
// Show cursor
|
||||
if (window->x11.cursorHidden)
|
||||
{
|
||||
XUndefineCursor(_glfw.x11.display, window->x11.handle);
|
||||
window->x11.cursorHidden = GL_FALSE;
|
||||
}
|
||||
XUngrabPointer(_glfw.x11.display, CurrentTime);
|
||||
XUndefineCursor(_glfw.x11.display, window->x11.handle);
|
||||
}
|
||||
|
||||
// Enter fullscreen mode
|
||||
@ -640,18 +608,12 @@ static void processEvent(XEvent *event)
|
||||
|
||||
case EnterNotify:
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_HIDDEN)
|
||||
hideCursor(window);
|
||||
|
||||
_glfwInputCursorEnter(window, GL_TRUE);
|
||||
break;
|
||||
}
|
||||
|
||||
case LeaveNotify:
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_HIDDEN)
|
||||
showCursor(window);
|
||||
|
||||
_glfwInputCursorEnter(window, GL_FALSE);
|
||||
break;
|
||||
}
|
||||
@ -763,7 +725,7 @@ static void processEvent(XEvent *event)
|
||||
_glfwInputWindowFocus(window, GL_TRUE);
|
||||
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
captureCursor(window);
|
||||
disableCursor(window);
|
||||
|
||||
break;
|
||||
}
|
||||
@ -773,7 +735,7 @@ static void processEvent(XEvent *event)
|
||||
_glfwInputWindowFocus(window, GL_FALSE);
|
||||
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
showCursor(window);
|
||||
restoreCursor(window);
|
||||
|
||||
break;
|
||||
}
|
||||
@ -1202,18 +1164,18 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
|
||||
0,0,0,0, (int) x, (int) y);
|
||||
}
|
||||
|
||||
void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
|
||||
void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
|
||||
{
|
||||
switch (mode)
|
||||
switch (window->cursorMode)
|
||||
{
|
||||
case GLFW_CURSOR_NORMAL:
|
||||
showCursor(window);
|
||||
restoreCursor(window);
|
||||
break;
|
||||
case GLFW_CURSOR_HIDDEN:
|
||||
hideCursor(window);
|
||||
break;
|
||||
case GLFW_CURSOR_DISABLED:
|
||||
captureCursor(window);
|
||||
disableCursor(window);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user