- made ungrab a second function - WM_cursor_ungrab
authorCampbell Barton <ideasman42@gmail.com>
Wed, 7 Oct 2009 21:19:35 +0000 (21:19 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 7 Oct 2009 21:19:35 +0000 (21:19 +0000)
- ungrab can restore the position of the mouse clamped to the window bounds (much nicer for transform)

intern/ghost/GHOST_C-api.h
intern/ghost/GHOST_IWindow.h
intern/ghost/intern/GHOST_C-api.cpp
intern/ghost/intern/GHOST_Window.cpp
intern/ghost/intern/GHOST_Window.h
intern/ghost/intern/GHOST_WindowX11.cpp
intern/ghost/intern/GHOST_WindowX11.h
source/blender/editors/interface/interface_handlers.c
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_cursors.c
source/blender/windowmanager/intern/wm_event_system.c

index 35391d3f4cf94232f8fce4cfd46ae0ebbc9b75d1..00d2cdb1e3b62d75f754346a47a11715da01e245 100644 (file)
@@ -376,7 +376,7 @@ extern GHOST_TSuccess GHOST_SetCursorPosition(GHOST_SystemHandle systemhandle,
  * @return     Indication of success.
  */
 extern GHOST_TSuccess GHOST_SetCursorGrab(GHOST_WindowHandle windowhandle,
-                                                                                 int grab, int warp);
+                                                                                 int grab, int warp, int restore);
 
 /***************************************************************************************
  ** Access to mouse button and keyboard states.
index 44ddf9a7cfb651a9e457009385a918fc0e4b055e..993b41a4d4fd282c398b367fb148fe3926ecd451 100644 (file)
@@ -271,7 +271,7 @@ public:
         * @param       grab The new grab state of the cursor.
         * @return      Indication of success.
         */
-       virtual GHOST_TSuccess setCursorGrab(bool grab, bool warp) { return GHOST_kSuccess; };
+       virtual GHOST_TSuccess setCursorGrab(bool grab, bool warp, bool restore) { return GHOST_kSuccess; };
 
 };
 
index b86c4703ea2b2ede3f1c9dc0c211fada26456cfa..e225ad4fd90a719c4efb9d13469f1d159395f5f5 100644 (file)
@@ -355,11 +355,11 @@ GHOST_TSuccess GHOST_SetCursorPosition(GHOST_SystemHandle systemhandle,
 
 
 GHOST_TSuccess GHOST_SetCursorGrab(GHOST_WindowHandle windowhandle,
-                                                                  int grab, int warp)
+                                                                  int grab, int warp, int restore)
 {
        GHOST_IWindow* window = (GHOST_IWindow*) windowhandle;
        
-       return window->setCursorGrab(grab?true:false, warp?true:false);
+       return window->setCursorGrab(grab?true:false, warp?true:false, restore?true:false);
 }
 
 
index 531674607d13dba6fd5d65d6c402a4e230be1778..94feb83e003b6d4a54d5386ceb4387ad10e3f1c1 100644 (file)
@@ -98,12 +98,12 @@ GHOST_TSuccess GHOST_Window::setCursorVisibility(bool visible)
        }
 }
 
-GHOST_TSuccess GHOST_Window::setCursorGrab(bool grab, bool warp)
+GHOST_TSuccess GHOST_Window::setCursorGrab(bool grab, bool warp, bool restore)
 {
        if(m_cursorGrabbed == grab)
                return GHOST_kSuccess;
 
-       if (setWindowCursorGrab(grab, warp)) {
+       if (setWindowCursorGrab(grab, warp, restore)) {
                m_cursorGrabbed = grab;
                return GHOST_kSuccess;
        }
index 36e4bac6dae1b54f0bcd9ed321ede74b84a83d8e..786918716c51ce57cf69dcc74861cce248dd45d6 100644 (file)
@@ -175,7 +175,7 @@ public:
         * @param       grab The new grab state of the cursor.
         * @return      Indication of success.
         */
-       virtual GHOST_TSuccess setCursorGrab(bool grab, bool warp);
+       virtual GHOST_TSuccess setCursorGrab(bool grab, bool warp, bool restore);
 
        /**
         * Sets the window "modified" status, indicating unsaved changes
@@ -247,7 +247,7 @@ protected:
         * Sets the cursor grab on the window using
         * native window system calls.
         */
-       virtual GHOST_TSuccess setWindowCursorGrab(bool grab, bool warp) { return GHOST_kSuccess; };
+       virtual GHOST_TSuccess setWindowCursorGrab(bool grab, bool warp, bool restore) { return GHOST_kSuccess; };
        
        /**
         * Sets the cursor shape on the window using
index c2dc1048ea0db362f1d9db5f67ababe2ef25caca..d197b5343526f08961d7b5fa3aa970937824daea 100644 (file)
@@ -1400,7 +1400,7 @@ setWindowCursorVisibility(
        GHOST_TSuccess
 GHOST_WindowX11::
 setWindowCursorGrab(
-       bool grab, bool warp
+       bool grab, bool warp, bool restore
 ){
        if(grab) {
                if(warp) {
@@ -1416,7 +1416,27 @@ setWindowCursorGrab(
                if(m_cursorWarp) { /* are we exiting warp */
                        setWindowCursorVisibility(true);
                        /* Almost works without but important otherwise the mouse GHOST location can be incorrect on exit */
-                       m_system->setCursorPosition(m_cursorWarpInitPos[0], m_cursorWarpInitPos[1]);
+                       if(restore) {
+                               GHOST_Rect bounds;
+                               GHOST_TInt32 x_new, y_new, x_rel, y_rel;
+
+                               getClientBounds(bounds);
+
+                               x_new= m_cursorWarpInitPos[0]+m_cursorWarpAccumPos[0];
+                               y_new= m_cursorWarpInitPos[1]+m_cursorWarpAccumPos[1];
+
+                               screenToClient(x_new, y_new, x_rel, y_rel);
+
+                               if(x_rel < 0)           x_new = (x_new-x_rel) + 2;
+                               if(y_rel < 0)           y_new = (y_new-y_rel) + 2;
+                               if(x_rel > bounds.getWidth())   x_new -= (x_rel-bounds.getWidth()) + 2;
+                               if(y_rel > bounds.getHeight())  y_new -= (y_rel-bounds.getHeight()) + 2;
+                               m_system->setCursorPosition(x_new, y_new);
+
+                       }
+                       else {
+                               m_system->setCursorPosition(m_cursorWarpInitPos[0], m_cursorWarpInitPos[1]);
+                       }
 
                        setCursorWarpAccum(0, 0);
                        m_cursorWarp= false;
index 08fba3e2be8a15ae525fa879fda213cbce5ec7e6..eb0689ab4105c9b99abe401cd56e800183c8339f 100644 (file)
@@ -256,7 +256,7 @@ protected:
         */
                GHOST_TSuccess 
        setWindowCursorGrab(
-               bool grab, bool warp
+               bool grab, bool warp, bool restore
        );
 
        /**
index 385a0eec0403a183160179bd5b6e03aec8fe7b26..8dea3fa1fc3214d8c5dfb36eb2991ecd0bf989b4 100644 (file)
@@ -3709,12 +3709,12 @@ static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState s
        /* number editing */
        if(state == BUTTON_STATE_NUM_EDITING) {
                if(ui_is_a_warp_but(but))
-                       WM_cursor_grab(CTX_wm_window(C), 1, 1);
+                       WM_cursor_grab(CTX_wm_window(C), TRUE);
                ui_numedit_begin(but, data);
        } else if(data->state == BUTTON_STATE_NUM_EDITING) {
                ui_numedit_end(but, data);
                if(ui_is_a_warp_but(but))
-                       WM_cursor_grab(CTX_wm_window(C), 0, -1);
+                       WM_cursor_ungrab(CTX_wm_window(C), FALSE);
        }
        /* menu open */
        if(state == BUTTON_STATE_MENU_OPEN)
index 321afec51b72d83d95a831b83ed194dad2e5c98f..e7f5bb3b2917d75c23aa73d9876ffa89c9439ed1 100644 (file)
@@ -75,7 +75,8 @@ void          WM_cursor_set           (struct wmWindow *win, int curs);
 void           WM_cursor_modal         (struct wmWindow *win, int curs);
 void           WM_cursor_restore       (struct wmWindow *win);
 void           WM_cursor_wait          (int val);
-void           WM_cursor_grab          (struct wmWindow *win, int val, int warp);
+void           WM_cursor_grab(struct wmWindow *win, int warp);
+void           WM_cursor_ungrab(wmWindow *win, int restore);
 void           WM_timecursor           (struct wmWindow *win, int nr);
 
 void           *WM_paint_cursor_activate(struct wmWindowManager *wm, int (*poll)(struct bContext *C), void (*draw)(struct bContext *C, int, int, void *customdata), void *customdata);
index d14cde56083edfa32ae30037e3716736b75855ce..e33132d18b980495c4198e577bdd62dbb6129697 100644 (file)
@@ -163,10 +163,16 @@ void WM_cursor_wait(int val)
        }
 }
 
-void WM_cursor_grab(wmWindow *win, int val, int warp)
+void WM_cursor_grab(wmWindow *win, int warp)
 {
        if(win)
-               GHOST_SetCursorGrab(win->ghostwin, val, warp);
+               GHOST_SetCursorGrab(win->ghostwin, 1, warp, -1);
+}
+
+void WM_cursor_ungrab(wmWindow *win, int restore)
+{
+       if(win)
+               GHOST_SetCursorGrab(win->ghostwin, 0, -1, restore);
 }
 
 /* afer this you can call restore too */
index 54841f0e06342fad63bd815775c76608b8a69f2d..086fdd3665bed614ad8b3357772c2aa0277e5ca5 100644 (file)
@@ -442,7 +442,7 @@ static int wm_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event, P
                else if(retval & OPERATOR_RUNNING_MODAL) {
                        /* grab cursor during blocking modal ops (X11) */
                        if(ot->flag & OPTYPE_BLOCKING)
-                               WM_cursor_grab(CTX_wm_window(C), 1, (U.uiflag & USER_CONTINUOUS_MOUSE));
+                               WM_cursor_grab(CTX_wm_window(C), (U.uiflag & USER_CONTINUOUS_MOUSE));
                }
                else
                        WM_operator_free(op);
@@ -637,7 +637,7 @@ void WM_event_remove_handlers(bContext *C, ListBase *handlers)
                                CTX_wm_region_set(C, region);
                        }
 
-                       WM_cursor_grab(CTX_wm_window(C), 0, -1);
+                       WM_cursor_ungrab(CTX_wm_window(C), TRUE);
                        WM_operator_free(handler->op);
                }
                else if(handler->ui_remove) {
@@ -835,7 +835,7 @@ static int wm_handler_operator_call(bContext *C, ListBase *handlers, wmEventHand
                        
                        /* remove modal handler, operator itself should have been cancelled and freed */
                        if(retval & (OPERATOR_CANCELLED|OPERATOR_FINISHED)) {
-                               WM_cursor_grab(CTX_wm_window(C), 0, -1);
+                               WM_cursor_ungrab(CTX_wm_window(C), TRUE);
 
                                BLI_remlink(handlers, handler);
                                wm_event_free_handler(handler);