Browse Source

Fixed mouse dragging behavior

Fixed a bug inside the library and demos causing wrong dragging
behavior for `nk_property_xxx`.
vurtun 9 years ago
parent
commit
1ac8021979

+ 4 - 2
demo/glfw_opengl2/nuklear_glfw_gl2.h

@@ -302,8 +302,11 @@ nk_glfw3_new_frame(void)
 
 
     glfwGetCursorPos(win, &x, &y);
     glfwGetCursorPos(win, &x, &y);
     nk_input_motion(ctx, (int)x, (int)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);
         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_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_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
@@ -326,4 +329,3 @@ void nk_glfw3_shutdown(void)
 }
 }
 
 
 #endif
 #endif
-

+ 6 - 2
demo/glfw_opengl3/nuklear_glfw_gl3.h

@@ -389,7 +389,7 @@ nk_glfw3_new_frame(void)
                                     glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);
                                     glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);
 
 
     if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == 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_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_PASTE, glfwGetKey(win, GLFW_KEY_P) == GLFW_PRESS);
         nk_input_key(ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == 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);
     glfwGetCursorPos(win, &x, &y);
     nk_input_motion(ctx, (int)x, (int)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);
         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_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_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);
     nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);

+ 0 - 1
demo/sdl_opengl2/nuklear_sdl_gl2.h

@@ -230,7 +230,6 @@ nk_sdl_handle_event(SDL_Event *evt)
         SDL_WarpMouseInWindow(sdl.win, x, y);
         SDL_WarpMouseInWindow(sdl.win, x, y);
         ctx->input.mouse.ungrab = 0;
         ctx->input.mouse.ungrab = 0;
     }
     }
-
     if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
     if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
         /* key events */
         /* key events */
         int down = evt->type == SDL_KEYDOWN;
         int down = evt->type == SDL_KEYDOWN;

+ 0 - 1
demo/sdl_opengl3/nuklear_sdl_gl3.h

@@ -341,7 +341,6 @@ nk_sdl_handle_event(SDL_Event *evt)
         SDL_WarpMouseInWindow(sdl.win, x, y);
         SDL_WarpMouseInWindow(sdl.win, x, y);
         ctx->input.mouse.ungrab = 0;
         ctx->input.mouse.ungrab = 0;
     }
     }
-
     if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
     if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
         /* key events */
         /* key events */
         int down = evt->type == SDL_KEYDOWN;
         int down = evt->type == SDL_KEYDOWN;

+ 10 - 7
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;
     if (!XSetLocaleModifiers("@im=none")) return 0;
 
 
     /* create invisible cursor */
     /* create invisible cursor */
-    {XColor dummy; char data[1] = {0};
+    {static XColor dummy; char data[1] = {0};
     Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
     Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
     if (blank == None) return 0;
     if (blank == None) return 0;
     xlib.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 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);
         XDefineCursor(xlib.dpy, xlib.root, xlib.cursor);
         ctx->input.mouse.grab = 0;
         ctx->input.mouse.grab = 0;
     } else if (ctx->input.mouse.ungrab) {
     } 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);
             (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
         XUndefineCursor(xlib.dpy, xlib.root);
         XUndefineCursor(xlib.dpy, xlib.root);
         ctx->input.mouse.ungrab = 0;
         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_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_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_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 {
         else {
             if (*code == 'c' && (evt->xkey.state & ControlMask))
             if (*code == 'c' && (evt->xkey.state & ControlMask))
                 nk_input_key(ctx, NK_KEY_COPY, down);
                 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 */
         /* Mouse motion handler */
         const int x = evt->xmotion.x, y = evt->xmotion.y;
         const int x = evt->xmotion.x, y = evt->xmotion.y;
         nk_input_motion(ctx, x, 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) {
     } else if (evt->type == Expose || evt->type == ConfigureNotify) {
         /* Window resize handler */
         /* Window resize handler */
         unsigned int width, height;
         unsigned int width, height;

+ 21 - 11
demo/x11_opengl2/nuklear_xlib_gl2.h

@@ -210,7 +210,7 @@ nk_x11_handle_event(XEvent *evt)
         ctx->input.mouse.grab = 0;
         ctx->input.mouse.grab = 0;
     } else if (ctx->input.mouse.ungrab) {
     } else if (ctx->input.mouse.ungrab) {
         XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0,
         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);
         XUndefineCursor(x11.dpy, x11.win);
         ctx->input.mouse.ungrab = 0;
         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);
                 nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down);
             else if (*code == 'e' && (evt->xkey.state & ControlMask))
             else if (*code == 'e' && (evt->xkey.state & ControlMask))
                 nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down);
                 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);
         XFree(code);
@@ -276,8 +282,11 @@ nk_x11_handle_event(XEvent *evt)
         /* Mouse motion handler */
         /* Mouse motion handler */
         const int x = evt->xmotion.x, y = evt->xmotion.y;
         const int x = evt->xmotion.x, y = evt->xmotion.y;
         nk_input_motion(ctx, x, 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)
     } else if (evt->type == KeymapNotify)
         XRefreshKeyboardMapping(&evt->xmapping);
         XRefreshKeyboardMapping(&evt->xmapping);
 }
 }
@@ -285,14 +294,15 @@ nk_x11_handle_event(XEvent *evt)
 NK_API struct nk_context*
 NK_API struct nk_context*
 nk_x11_init(Display *dpy, Window win)
 nk_x11_init(Display *dpy, Window win)
 {
 {
+    x11.dpy = dpy;
+    x11.win = win;
+
     if (!setlocale(LC_ALL,"")) return 0;
     if (!setlocale(LC_ALL,"")) return 0;
     if (!XSupportsLocale()) return 0;
     if (!XSupportsLocale()) return 0;
     if (!XSetLocaleModifiers("@im=none")) return 0;
     if (!XSetLocaleModifiers("@im=none")) return 0;
-    x11.dpy = dpy;
-    x11.win = win;
 
 
     /* create invisible cursor */
     /* create invisible cursor */
-    {XColor dummy; char data[1] = {0};
+    {static XColor dummy; char data[1] = {0};
     Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
     Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
     if (blank == None) return 0;
     if (blank == None) return 0;
     x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
     x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);

+ 17 - 8
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);
                 nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down);
             else if (*code == 'e' && (evt->xkey.state & ControlMask))
             else if (*code == 'e' && (evt->xkey.state & ControlMask))
                 nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down);
                 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);
         XFree(code);
@@ -646,8 +652,11 @@ nk_x11_handle_event(XEvent *evt)
         /* Mouse motion handler */
         /* Mouse motion handler */
         const int x = evt->xmotion.x, y = evt->xmotion.y;
         const int x = evt->xmotion.x, y = evt->xmotion.y;
         nk_input_motion(ctx, x, 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)
     } else if (evt->type == KeymapNotify)
         XRefreshKeyboardMapping(&evt->xmapping);
         XRefreshKeyboardMapping(&evt->xmapping);
 }
 }
@@ -664,7 +673,7 @@ nk_x11_init(Display *dpy, Window win)
     x11.win = win;
     x11.win = win;
 
 
     /* create invisible cursor */
     /* create invisible cursor */
-    {XColor dummy; char data[1] = {0};
+    {static XColor dummy; char data[1] = {0};
     Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
     Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
     if (blank == None) return 0;
     if (blank == None) return 0;
     x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
     x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);

+ 2 - 5
nuklear.h

@@ -9961,10 +9961,6 @@ nk_input_end(struct nk_context *ctx)
         in->mouse.ungrab = 0;
         in->mouse.ungrab = 0;
         in->mouse.grab = 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
 NK_API void
@@ -9976,7 +9972,8 @@ nk_input_motion(struct nk_context *ctx, int x, int y)
     in = &ctx->input;
     in = &ctx->input;
     in->mouse.pos.x = (float)x;
     in->mouse.pos.x = (float)x;
     in->mouse.pos.y = (float)y;
     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
 NK_API void