Skip to content

Commit

Permalink
Added property OS cursor hiding for mouse grabbing
Browse files Browse the repository at this point in the history
  • Loading branch information
vurtun committed May 23, 2016
1 parent 8e3a5c7 commit 25cfeb9
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 166 deletions.
9 changes: 9 additions & 0 deletions demo/glfw_opengl2/nuklear_glfw_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ nk_glfw3_new_frame(void)
for (i = 0; i < glfw.text_len; ++i)
nk_input_unicode(ctx, glfw.text[i]);

/* optional grabbing behavior */
if (ctx->input.mouse.grab)
glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
else if (ctx->input.mouse.ungrab)
glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

nk_input_key(ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
nk_input_key(ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
nk_input_key(ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
Expand Down Expand Up @@ -296,6 +302,9 @@ nk_glfw3_new_frame(void)

glfwGetCursorPos(win, &x, &y);
nk_input_motion(ctx, (int)x, (int)y);
if (ctx->input.mouse.grabbed)
glfwSetCursorPos(glfw.win, ctx->input.mouse.prev.x, ctx->input.mouse.prev.y);

nk_input_button(ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
nk_input_button(ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
Expand Down
8 changes: 8 additions & 0 deletions demo/glfw_opengl3/nuklear_glfw_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ nk_glfw3_new_frame(void)
for (i = 0; i < glfw.text_len; ++i)
nk_input_unicode(ctx, glfw.text[i]);

/* optional grabbing behavior */
if (ctx->input.mouse.grab)
glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
else if (ctx->input.mouse.ungrab)
glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

nk_input_key(ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
nk_input_key(ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
nk_input_key(ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
Expand Down Expand Up @@ -404,6 +410,8 @@ nk_glfw3_new_frame(void)

glfwGetCursorPos(win, &x, &y);
nk_input_motion(ctx, (int)x, (int)y);
if (ctx->input.mouse.grabbed)
glfwSetCursorPos(glfw.win, ctx->input.mouse.prev.x, ctx->input.mouse.prev.y);
nk_input_button(ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
nk_input_button(ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
Expand Down
17 changes: 16 additions & 1 deletion demo/sdl_opengl2/nuklear_sdl_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ NK_API void
nk_sdl_handle_event(SDL_Event *evt)
{
struct nk_context *ctx = &sdl.ctx;

/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
}

if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
/* key events */
int down = evt->type == SDL_KEYDOWN;
Expand Down Expand Up @@ -272,7 +284,10 @@ nk_sdl_handle_event(SDL_Event *evt)
if (evt->button.button == SDL_BUTTON_RIGHT)
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
} else if (evt->type == SDL_MOUSEMOTION) {
nk_input_motion(ctx, evt->motion.x, evt->motion.y);
if (ctx->input.mouse.grabbed) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel);
} else nk_input_motion(ctx, evt->motion.x, evt->motion.y);
} else if (evt->type == SDL_TEXTINPUT) {
nk_glyph glyph;
memcpy(glyph, evt->text.text, NK_UTF_SIZE);
Expand Down
17 changes: 16 additions & 1 deletion demo/sdl_opengl3/nuklear_sdl_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ NK_API void
nk_sdl_handle_event(SDL_Event *evt)
{
struct nk_context *ctx = &sdl.ctx;

/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
}

if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
/* key events */
int down = evt->type == SDL_KEYDOWN;
Expand Down Expand Up @@ -383,7 +395,10 @@ nk_sdl_handle_event(SDL_Event *evt)
if (evt->button.button == SDL_BUTTON_RIGHT)
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
} else if (evt->type == SDL_MOUSEMOTION) {
nk_input_motion(ctx, evt->motion.x, evt->motion.y);
if (ctx->input.mouse.grabbed) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel);
} else nk_input_motion(ctx, evt->motion.x, evt->motion.y);
} else if (evt->type == SDL_TEXTINPUT) {
nk_glyph glyph;
memcpy(glyph, evt->text.text, NK_UTF_SIZE);
Expand Down
43 changes: 35 additions & 8 deletions demo/x11/nuklear_xlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ struct XSurface {
static struct {
struct nk_context ctx;
struct XSurface *surf;
Cursor cursor;
Display *dpy;
Window root;
} xlib;

#ifndef MIN
Expand All @@ -78,18 +81,18 @@ nk_color_from_byte(const nk_byte *c)
}

static XSurface*
nk_xsurf_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
nk_xsurf_create(int screen, unsigned int w, unsigned int h)
{
XSurface *surface = (XSurface*)calloc(1, sizeof(XSurface));
surface->w = w;
surface->h = h;
surface->dpy = dpy;
surface->dpy = xlib.dpy;
surface->screen = screen;
surface->root = root;
surface->gc = XCreateGC(dpy, root, 0, NULL);
XSetLineAttributes(dpy, surface->gc, 1, LineSolid, CapButt, JoinMiter);
surface->drawable = XCreatePixmap(dpy, root, w, h,
(unsigned int)DefaultDepth(dpy, screen));
surface->root = xlib.root;
surface->gc = XCreateGC(xlib.dpy, xlib.root, 0, NULL);
XSetLineAttributes(xlib.dpy, surface->gc, 1, LineSolid, CapButt, JoinMiter);
surface->drawable = XCreatePixmap(xlib.dpy, xlib.root, w, h,
(unsigned int)DefaultDepth(xlib.dpy, screen));
return surface;
}

Expand Down Expand Up @@ -450,12 +453,21 @@ nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
font.userdata = nk_handle_ptr(xfont);
font.height = (float)xfont->height;
font.width = nk_xfont_get_text_width;
xlib.dpy = dpy;
xlib.root = root;

if (!setlocale(LC_ALL,"")) return 0;
if (!XSupportsLocale()) return 0;
if (!XSetLocaleModifiers("@im=none")) return 0;

xlib.surf = nk_xsurf_create(dpy, screen, root, w, h);
/* create invisible cursor */
{XColor dummy; char data[1] = {0};
Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
if (blank == None) return 0;
xlib.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
XFreePixmap(dpy, blank);}

xlib.surf = nk_xsurf_create(screen, w, h);
nk_init_default(&xlib.ctx, &font);
return &xlib.ctx;
}
Expand All @@ -474,6 +486,18 @@ NK_API void
nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
{
struct nk_context *ctx = &xlib.ctx;

/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
XDefineCursor(xlib.dpy, xlib.root, xlib.cursor);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
XWarpPointer(xlib.dpy, None, xlib.surf->root, 0, 0, 0, 0,
(int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
XUndefineCursor(xlib.dpy, xlib.root);
ctx->input.mouse.ungrab = 0;
}

if (evt->type == KeyPress || evt->type == KeyRelease)
{
/* Key handler */
Expand Down Expand Up @@ -543,6 +567,8 @@ nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
/* Mouse motion handler */
const int x = evt->xmotion.x, y = evt->xmotion.y;
nk_input_motion(ctx, x, y);
if (ctx->input.mouse.grabbed)
XWarpPointer(xlib.dpy, None, xlib.surf->root, 0, 0, 0, 0, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
} else if (evt->type == Expose || evt->type == ConfigureNotify) {
/* Window resize handler */
unsigned int width, height;
Expand All @@ -560,6 +586,7 @@ nk_xlib_shutdown(void)
{
nk_xsurf_del(xlib.surf);
nk_free(&xlib.ctx);
XFreeCursor(xlib.dpy, xlib.cursor);
nk_memset(&xlib, 0, sizeof(xlib));
}

Expand Down
24 changes: 24 additions & 0 deletions demo/x11_opengl2/nuklear_xlib_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static struct nk_x11 {
struct nk_x11_device ogl;
struct nk_context ctx;
struct nk_font_atlas atlas;
Cursor cursor;
Display *dpy;
Window win;
} x11;
Expand Down Expand Up @@ -202,6 +203,18 @@ NK_API void
nk_x11_handle_event(XEvent *evt)
{
struct nk_context *ctx = &x11.ctx;

/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
XDefineCursor(x11.dpy, x11.win, x11.cursor);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0,
(int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
XUndefineCursor(x11.dpy, x11.win);
ctx->input.mouse.ungrab = 0;
}

if (evt->type == KeyPress || evt->type == KeyRelease)
{
/* Key handler */
Expand Down Expand Up @@ -263,6 +276,8 @@ nk_x11_handle_event(XEvent *evt)
/* Mouse motion handler */
const int x = evt->xmotion.x, y = evt->xmotion.y;
nk_input_motion(ctx, x, y);
if (ctx->input.mouse.grabbed)
XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
} else if (evt->type == KeymapNotify)
XRefreshKeyboardMapping(&evt->xmapping);
}
Expand All @@ -275,6 +290,14 @@ nk_x11_init(Display *dpy, Window win)
if (!XSetLocaleModifiers("@im=none")) return 0;
x11.dpy = dpy;
x11.win = win;

/* create invisible cursor */
{XColor dummy; char data[1] = {0};
Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
if (blank == None) return 0;
x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
XFreePixmap(dpy, blank);}

nk_buffer_init_default(&x11.ogl.cmds);
nk_init_default(&x11.ctx, 0);
return &x11.ctx;
Expand All @@ -288,6 +311,7 @@ nk_x11_shutdown(void)
nk_free(&x11.ctx);
glDeleteTextures(1, &dev->font_tex);
nk_buffer_free(&dev->cmds);
XFreeCursor(x11.dpy, x11.cursor);
memset(&x11, 0, sizeof(x11));
}

Expand Down
25 changes: 25 additions & 0 deletions demo/x11_opengl3/nuklear_xlib_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ static struct nk_x11 {
struct nk_x11_device ogl;
struct nk_context ctx;
struct nk_font_atlas atlas;
Cursor cursor;
Display *dpy;
Window win;
} x11;
Expand Down Expand Up @@ -572,6 +573,18 @@ NK_API void
nk_x11_handle_event(XEvent *evt)
{
struct nk_context *ctx = &x11.ctx;

/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
XDefineCursor(x11.dpy, x11.win, x11.cursor);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0,
(int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
XUndefineCursor(x11.dpy, x11.win);
ctx->input.mouse.ungrab = 0;
}

if (evt->type == KeyPress || evt->type == KeyRelease)
{
/* Key handler */
Expand Down Expand Up @@ -633,6 +646,8 @@ nk_x11_handle_event(XEvent *evt)
/* Mouse motion handler */
const int x = evt->xmotion.x, y = evt->xmotion.y;
nk_input_motion(ctx, x, y);
if (ctx->input.mouse.grabbed)
XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
} else if (evt->type == KeymapNotify)
XRefreshKeyboardMapping(&evt->xmapping);
}
Expand All @@ -644,8 +659,17 @@ nk_x11_init(Display *dpy, Window win)
if (!XSupportsLocale()) return 0;
if (!XSetLocaleModifiers("@im=none")) return 0;
if (!nk_x11_device_create()) return 0;

x11.dpy = dpy;
x11.win = win;

/* create invisible cursor */
{XColor dummy; char data[1] = {0};
Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
if (blank == None) return 0;
x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
XFreePixmap(dpy, blank);}

nk_init_default(&x11.ctx, 0);
return &x11.ctx;
}
Expand All @@ -656,6 +680,7 @@ nk_x11_shutdown(void)
nk_font_atlas_clear(&x11.atlas);
nk_free(&x11.ctx);
nk_x11_device_destroy();
XFreeCursor(x11.dpy, x11.cursor);
memset(&x11, 0, sizeof(x11));
}

Expand Down
Loading

0 comments on commit 25cfeb9

Please sign in to comment.