mirror of
https://github.com/glfw/glfw.git
synced 2026-11-17 12:19:19 +00:00
Add cursor mode GLFW_CURSOR_CAPTURED
This adds a cursor mode that provides a visible cursor confined to the content area of the window. Fixes #58
This commit is contained in:
committed by
Camilla Löwy
parent
a46f829de8
commit
488008e0a2
@@ -1619,8 +1619,16 @@ void _glfwSetCursorPosCocoa(_GLFWwindow* window, double x, double y)
|
||||
void _glfwSetCursorModeCocoa(_GLFWwindow* window, int mode)
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
if (mode == GLFW_CURSOR_CAPTURED)
|
||||
{
|
||||
_glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
|
||||
"Cocoa: Captured cursor mode not yet implemented");
|
||||
}
|
||||
|
||||
if (_glfwWindowFocusedCocoa(window))
|
||||
updateCursorMode(window);
|
||||
|
||||
} // autoreleasepool
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -596,7 +596,8 @@ GLFWAPI void glfwSetInputMode(GLFWwindow* handle, int mode, int value)
|
||||
{
|
||||
if (value != GLFW_CURSOR_NORMAL &&
|
||||
value != GLFW_CURSOR_HIDDEN &&
|
||||
value != GLFW_CURSOR_DISABLED)
|
||||
value != GLFW_CURSOR_DISABLED &&
|
||||
value != GLFW_CURSOR_CAPTURED)
|
||||
{
|
||||
_glfwInputError(GLFW_INVALID_ENUM,
|
||||
"Invalid cursor mode 0x%08X",
|
||||
|
||||
+13
-2
@@ -238,7 +238,8 @@ static void applyAspectRatio(_GLFWwindow* window, int edge, RECT* area)
|
||||
//
|
||||
static void updateCursorImage(_GLFWwindow* window)
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL)
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL ||
|
||||
window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
{
|
||||
if (window->cursor)
|
||||
SetCursor(window->cursor->win32.handle);
|
||||
@@ -586,6 +587,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
disableCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
captureCursor(window);
|
||||
|
||||
window->win32.frameAction = GLFW_FALSE;
|
||||
}
|
||||
@@ -604,6 +607,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
|
||||
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
disableCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
captureCursor(window);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -612,6 +617,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
enableCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
releaseCursor();
|
||||
|
||||
if (window->monitor && window->autoIconify)
|
||||
_glfwIconifyWindowWin32(window);
|
||||
@@ -981,6 +988,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
|
||||
// resizing the window or using the window menu
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
enableCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
releaseCursor();
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -995,6 +1004,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
|
||||
// resizing the window or using the menu
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
disableCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
captureCursor(window);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -2166,7 +2177,7 @@ void _glfwSetCursorModeWin32(_GLFWwindow* window, int mode)
|
||||
disableRawMouseMotion(window);
|
||||
}
|
||||
|
||||
if (mode == GLFW_CURSOR_DISABLED)
|
||||
if (mode == GLFW_CURSOR_DISABLED || mode == GLFW_CURSOR_CAPTURED)
|
||||
captureCursor(window);
|
||||
else
|
||||
releaseCursor();
|
||||
|
||||
@@ -269,6 +269,7 @@ typedef struct _GLFWwindowWayland
|
||||
|
||||
struct zwp_relative_pointer_v1* relativePointer;
|
||||
struct zwp_locked_pointer_v1* lockedPointer;
|
||||
struct zwp_confined_pointer_v1* confinedPointer;
|
||||
|
||||
struct zwp_idle_inhibitor_v1* idleInhibitor;
|
||||
|
||||
|
||||
+53
-1
@@ -1860,6 +1860,9 @@ void _glfwDestroyWindowWayland(_GLFWwindow* window)
|
||||
if (window->wl.lockedPointer)
|
||||
zwp_locked_pointer_v1_destroy(window->wl.lockedPointer);
|
||||
|
||||
if (window->wl.confinedPointer)
|
||||
zwp_confined_pointer_v1_destroy(window->wl.confinedPointer);
|
||||
|
||||
if (window->context.destroy)
|
||||
window->context.destroy(window);
|
||||
|
||||
@@ -2538,6 +2541,43 @@ static void lockPointer(_GLFWwindow* window)
|
||||
window);
|
||||
}
|
||||
|
||||
static void confinedPointerHandleConfined(void* userData,
|
||||
struct zwp_confined_pointer_v1* confinedPointer)
|
||||
{
|
||||
}
|
||||
|
||||
static void confinedPointerHandleUnconfined(void* userData,
|
||||
struct zwp_confined_pointer_v1* confinedPointer)
|
||||
{
|
||||
}
|
||||
|
||||
static const struct zwp_confined_pointer_v1_listener confinedPointerListener =
|
||||
{
|
||||
confinedPointerHandleConfined,
|
||||
confinedPointerHandleUnconfined
|
||||
};
|
||||
|
||||
static void confinePointer(_GLFWwindow* window)
|
||||
{
|
||||
window->wl.confinedPointer =
|
||||
zwp_pointer_constraints_v1_confine_pointer(
|
||||
_glfw.wl.pointerConstraints,
|
||||
window->wl.surface,
|
||||
_glfw.wl.pointer,
|
||||
NULL,
|
||||
ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
|
||||
|
||||
zwp_confined_pointer_v1_add_listener(window->wl.confinedPointer,
|
||||
&confinedPointerListener,
|
||||
window);
|
||||
}
|
||||
|
||||
static void unconfinePointer(_GLFWwindow* window)
|
||||
{
|
||||
zwp_confined_pointer_v1_destroy(window->wl.confinedPointer);
|
||||
window->wl.confinedPointer = NULL;
|
||||
}
|
||||
|
||||
void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor)
|
||||
{
|
||||
if (!_glfw.wl.pointer)
|
||||
@@ -2553,17 +2593,29 @@ void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor)
|
||||
// Update pointer lock to match cursor mode
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
{
|
||||
if (window->wl.confinedPointer)
|
||||
unconfinePointer(window);
|
||||
if (!window->wl.lockedPointer)
|
||||
lockPointer(window);
|
||||
}
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
{
|
||||
if (window->wl.lockedPointer)
|
||||
unlockPointer(window);
|
||||
if (!window->wl.confinedPointer)
|
||||
confinePointer(window);
|
||||
}
|
||||
else if (window->cursorMode == GLFW_CURSOR_NORMAL ||
|
||||
window->cursorMode == GLFW_CURSOR_HIDDEN)
|
||||
{
|
||||
if (window->wl.lockedPointer)
|
||||
unlockPointer(window);
|
||||
else if (window->wl.confinedPointer)
|
||||
unconfinePointer(window);
|
||||
}
|
||||
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL)
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL ||
|
||||
window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
{
|
||||
if (cursor)
|
||||
setCursorImage(window, &cursor->wl);
|
||||
|
||||
+9
-3
@@ -453,7 +453,8 @@ static char* convertLatin1toUTF8(const char* source)
|
||||
//
|
||||
static void updateCursorImage(_GLFWwindow* window)
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL)
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL ||
|
||||
window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
{
|
||||
if (window->cursor)
|
||||
{
|
||||
@@ -1705,6 +1706,8 @@ static void processEvent(XEvent *event)
|
||||
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
disableCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
captureCursor(window);
|
||||
|
||||
if (window->x11.ic)
|
||||
XSetICFocus(window->x11.ic);
|
||||
@@ -1725,6 +1728,8 @@ static void processEvent(XEvent *event)
|
||||
|
||||
if (window->cursorMode == GLFW_CURSOR_DISABLED)
|
||||
enableCursor(window);
|
||||
else if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
releaseCursor();
|
||||
|
||||
if (window->x11.ic)
|
||||
XUnsetICFocus(window->x11.ic);
|
||||
@@ -2831,7 +2836,7 @@ void _glfwSetCursorModeX11(_GLFWwindow* window, int mode)
|
||||
disableRawMouseMotion(window);
|
||||
}
|
||||
|
||||
if (mode == GLFW_CURSOR_DISABLED)
|
||||
if (mode == GLFW_CURSOR_DISABLED || mode == GLFW_CURSOR_CAPTURED)
|
||||
captureCursor(window);
|
||||
else
|
||||
releaseCursor();
|
||||
@@ -3003,7 +3008,8 @@ void _glfwDestroyCursorX11(_GLFWcursor* cursor)
|
||||
|
||||
void _glfwSetCursorX11(_GLFWwindow* window, _GLFWcursor* cursor)
|
||||
{
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL)
|
||||
if (window->cursorMode == GLFW_CURSOR_NORMAL ||
|
||||
window->cursorMode == GLFW_CURSOR_CAPTURED)
|
||||
{
|
||||
updateCursorImage(window);
|
||||
XFlush(_glfw.x11.display);
|
||||
|
||||
Reference in New Issue
Block a user