diff --git a/demo/glfw_opengl2/nuklear_glfw_gl2.h b/demo/glfw_opengl2/nuklear_glfw_gl2.h index b43296b..1394040 100644 --- a/demo/glfw_opengl2/nuklear_glfw_gl2.h +++ b/demo/glfw_opengl2/nuklear_glfw_gl2.h @@ -302,8 +302,11 @@ nk_glfw3_new_frame(void) glfwGetCursorPos(win, &x, &y); nk_input_motion(ctx, (int)x, (int)y); - if (ctx->input.mouse.grabbed) + if (ctx->input.mouse.grabbed) { glfwSetCursorPos(glfw.win, ctx->input.mouse.prev.x, ctx->input.mouse.prev.y); + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = 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); @@ -326,4 +329,3 @@ void nk_glfw3_shutdown(void) } #endif - diff --git a/demo/glfw_opengl3/nuklear_glfw_gl3.h b/demo/glfw_opengl3/nuklear_glfw_gl3.h index eb26f87..c129e3d 100644 --- a/demo/glfw_opengl3/nuklear_glfw_gl3.h +++ b/demo/glfw_opengl3/nuklear_glfw_gl3.h @@ -389,7 +389,7 @@ nk_glfw3_new_frame(void) glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS); if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || - glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL == GLFW_PRESS)) { + glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) { nk_input_key(ctx, NK_KEY_COPY, glfwGetKey(win, GLFW_KEY_C) == GLFW_PRESS); nk_input_key(ctx, NK_KEY_PASTE, glfwGetKey(win, GLFW_KEY_P) == GLFW_PRESS); nk_input_key(ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == GLFW_PRESS); @@ -410,8 +410,12 @@ nk_glfw3_new_frame(void) glfwGetCursorPos(win, &x, &y); nk_input_motion(ctx, (int)x, (int)y); - if (ctx->input.mouse.grabbed) + if (ctx->input.mouse.grabbed) { glfwSetCursorPos(glfw.win, ctx->input.mouse.prev.x, ctx->input.mouse.prev.y); + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = 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); diff --git a/demo/sdl_opengl2/nuklear_sdl_gl2.h b/demo/sdl_opengl2/nuklear_sdl_gl2.h index 44457fb..7ae7b2b 100644 --- a/demo/sdl_opengl2/nuklear_sdl_gl2.h +++ b/demo/sdl_opengl2/nuklear_sdl_gl2.h @@ -230,7 +230,6 @@ nk_sdl_handle_event(SDL_Event *evt) 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; diff --git a/demo/sdl_opengl3/nuklear_sdl_gl3.h b/demo/sdl_opengl3/nuklear_sdl_gl3.h index 86e5f9e..7acd284 100644 --- a/demo/sdl_opengl3/nuklear_sdl_gl3.h +++ b/demo/sdl_opengl3/nuklear_sdl_gl3.h @@ -341,7 +341,6 @@ nk_sdl_handle_event(SDL_Event *evt) 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; diff --git a/demo/x11/nuklear_xlib.h b/demo/x11/nuklear_xlib.h index 49165ea..3f75d52 100644 --- a/demo/x11/nuklear_xlib.h +++ b/demo/x11/nuklear_xlib.h @@ -461,7 +461,7 @@ nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root, if (!XSetLocaleModifiers("@im=none")) return 0; /* create invisible cursor */ - {XColor dummy; char data[1] = {0}; + {static 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); @@ -492,7 +492,7 @@ nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt) 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, + XWarpPointer(xlib.dpy, None, xlib.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; @@ -512,9 +512,9 @@ nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt) else if (*code == XK_Up) nk_input_key(ctx, NK_KEY_UP, down); else if (*code == XK_Down) nk_input_key(ctx, NK_KEY_DOWN, down); else if (*code == XK_BackSpace) nk_input_key(ctx, NK_KEY_BACKSPACE, down); - else if (*code == XK_Home) nk_input_key(ctx, NK_KEY_TEXT_START, down); - else if (*code == XK_End) nk_input_key(ctx, NK_KEY_TEXT_END, down); - else if (*code == XK_Escape) nk_input_key(ctx, NK_KEY_TEXT_RESET_MODE, down); + else if (*code == XK_Home) nk_input_key(ctx, NK_KEY_TEXT_START, down); + else if (*code == XK_End) nk_input_key(ctx, NK_KEY_TEXT_END, down); + else if (*code == XK_Escape) nk_input_key(ctx, NK_KEY_TEXT_RESET_MODE, down); else { if (*code == 'c' && (evt->xkey.state & ControlMask)) nk_input_key(ctx, NK_KEY_COPY, down); @@ -567,8 +567,11 @@ 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); + if (ctx->input.mouse.grabbed) { + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; + XWarpPointer(xlib.dpy, None, xlib.surf->root, 0, 0, 0, 0, (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y); + } } else if (evt->type == Expose || evt->type == ConfigureNotify) { /* Window resize handler */ unsigned int width, height; diff --git a/demo/x11_opengl2/nuklear_xlib_gl2.h b/demo/x11_opengl2/nuklear_xlib_gl2.h index 7626e3c..4df7d56 100644 --- a/demo/x11_opengl2/nuklear_xlib_gl2.h +++ b/demo/x11_opengl2/nuklear_xlib_gl2.h @@ -210,7 +210,7 @@ nk_x11_handle_event(XEvent *evt) 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); + (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y); XUndefineCursor(x11.dpy, x11.win); ctx->input.mouse.ungrab = 0; } @@ -249,11 +249,17 @@ nk_x11_handle_event(XEvent *evt) nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down); else if (*code == 'e' && (evt->xkey.state & ControlMask)) nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down); - else if (!down) { - char buf[32]; - KeySym keysym = 0; - if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol) - nk_input_glyph(ctx, buf); + else { + if (*code == 'i') + nk_input_key(ctx, NK_KEY_TEXT_INSERT_MODE, down); + else if (*code == 'r') + nk_input_key(ctx, NK_KEY_TEXT_REPLACE_MODE, down); + if (down) { + char buf[32]; + KeySym keysym = 0; + if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol) + nk_input_glyph(ctx, buf); + } } } XFree(code); @@ -276,8 +282,11 @@ 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); + if (ctx->input.mouse.grabbed) { + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; + XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0, (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y); + } } else if (evt->type == KeymapNotify) XRefreshKeyboardMapping(&evt->xmapping); } @@ -285,14 +294,15 @@ nk_x11_handle_event(XEvent *evt) NK_API struct nk_context* nk_x11_init(Display *dpy, Window win) { - if (!setlocale(LC_ALL,"")) return 0; - if (!XSupportsLocale()) return 0; - if (!XSetLocaleModifiers("@im=none")) return 0; x11.dpy = dpy; x11.win = win; + if (!setlocale(LC_ALL,"")) return 0; + if (!XSupportsLocale()) return 0; + if (!XSetLocaleModifiers("@im=none")) return 0; + /* create invisible cursor */ - {XColor dummy; char data[1] = {0}; + {static 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); diff --git a/demo/x11_opengl3/nuklear_xlib_gl3.h b/demo/x11_opengl3/nuklear_xlib_gl3.h index 183c104..433eceb 100644 --- a/demo/x11_opengl3/nuklear_xlib_gl3.h +++ b/demo/x11_opengl3/nuklear_xlib_gl3.h @@ -619,11 +619,17 @@ nk_x11_handle_event(XEvent *evt) nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down); else if (*code == 'e' && (evt->xkey.state & ControlMask)) nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down); - else if (!down) { - char buf[32]; - KeySym keysym = 0; - if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol) - nk_input_glyph(ctx, buf); + else { + if (*code == 'i') + nk_input_key(ctx, NK_KEY_TEXT_INSERT_MODE, down); + else if (*code == 'r') + nk_input_key(ctx, NK_KEY_TEXT_REPLACE_MODE, down); + if (down) { + char buf[32]; + KeySym keysym = 0; + if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol) + nk_input_glyph(ctx, buf); + } } } XFree(code); @@ -646,8 +652,11 @@ 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); + if (ctx->input.mouse.grabbed) { + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; + XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0, (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y); + } } else if (evt->type == KeymapNotify) XRefreshKeyboardMapping(&evt->xmapping); } @@ -664,7 +673,7 @@ nk_x11_init(Display *dpy, Window win) x11.win = win; /* create invisible cursor */ - {XColor dummy; char data[1] = {0}; + {static 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); diff --git a/nuklear.h b/nuklear.h index 63cc22e..d2194ef 100644 --- a/nuklear.h +++ b/nuklear.h @@ -9961,10 +9961,6 @@ nk_input_end(struct nk_context *ctx) in->mouse.ungrab = 0; in->mouse.grab = 0; } - if (in->mouse.grabbed) { - in->mouse.pos.x = in->mouse.prev.x; - in->mouse.pos.y = in->mouse.prev.y; - } } NK_API void @@ -9976,7 +9972,8 @@ nk_input_motion(struct nk_context *ctx, int x, int y) in = &ctx->input; in->mouse.pos.x = (float)x; in->mouse.pos.y = (float)y; - in->mouse.delta = nk_vec2_sub(in->mouse.pos, in->mouse.prev); + in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x; + in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y; } NK_API void