changes to ghost/sdl
authorCampbell Barton <ideasman42@gmail.com>
Wed, 13 Jul 2011 00:31:08 +0000 (00:31 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 13 Jul 2011 00:31:08 +0000 (00:31 +0000)
- mouse coords made absolute
- window position set
- building with SDL 1.2 gives an error.

intern/ghost/intern/GHOST_DisplayManagerSDL.h
intern/ghost/intern/GHOST_SystemSDL.cpp
intern/ghost/intern/GHOST_SystemSDL.h
intern/ghost/intern/GHOST_WindowSDL.cpp
intern/ghost/intern/GHOST_WindowSDL.h

index 5019d1b135179f03097769daaf620d82ef3b3be5..0afa964b3c034d0db6432656076f3f954437acdd 100644 (file)
@@ -35,6 +35,10 @@ extern "C" {
        #include "SDL.h"
 }
 
+#if !SDL_VERSION_ATLEAST(1, 3, 0)
+#  error "SDL 1.3 or newer is needed to build with Ghost"
+#endif
+
 class GHOST_SystemSDL;
 
 class GHOST_DisplayManagerSDL : public GHOST_DisplayManager
index 33dcbc01307c465765e88d92e35aa737e19ff627..59900e6794026d507ba314491799a8ffe6d6e788 100644 (file)
@@ -279,10 +279,57 @@ GHOST_SystemSDL::processEvent(SDL_Event *sdl_event)
        case SDL_MOUSEMOTION:
                {
                        SDL_MouseMotionEvent &sdl_sub_evt= sdl_event->motion;
-                       GHOST_WindowSDL *window= findGhostWindow(SDL_GetWindowFromID(sdl_sub_evt.windowID));
+                       SDL_Window *sdl_win= SDL_GetWindowFromID(sdl_sub_evt.windowID);
+                       GHOST_WindowSDL *window= findGhostWindow(sdl_win);
                        assert(window != NULL);
 
-                       g_event= new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, sdl_sub_evt.x, sdl_sub_evt.y);
+                       int x_win, y_win;
+                       SDL_GetWindowPosition(sdl_win, &x_win, &y_win);
+
+                       GHOST_TInt32 x_root= sdl_sub_evt.x + x_win;
+                       GHOST_TInt32 y_root= sdl_sub_evt.y + y_win;
+
+#if 0
+                       if(window->getCursorGrabMode() != GHOST_kGrabDisable && window->getCursorGrabMode() != GHOST_kGrabNormal)
+                       {
+                               GHOST_TInt32 x_new= x_root;
+                               GHOST_TInt32 y_new= y_root;
+                               GHOST_TInt32 x_accum, y_accum;
+                               GHOST_Rect bounds;
+
+                               /* fallback to window bounds */
+                               if(window->getCursorGrabBounds(bounds)==GHOST_kFailure)
+                                       window->getClientBounds(bounds);
+
+                               /* could also clamp to screen bounds
+                                * wrap with a window outside the view will fail atm  */
+                               bounds.wrapPoint(x_new, y_new, 8); /* offset of one incase blender is at screen bounds */
+                               window->getCursorGrabAccum(x_accum, y_accum);
+
+                               // cant use setCursorPosition because the mouse may have no focus!
+                               if(x_new != x_root || y_new != y_root) {
+                                       if (1 ) { //xme.time > m_last_warp) {
+                                               /* when wrapping we don't need to add an event because the
+                                                * setCursorPosition call will cause a new event after */
+                                               SDL_WarpMouseInWindow(sdl_win, x_new - x_win, y_new - y_win); /* wrap */
+                                               window->setCursorGrabAccum(x_accum + (x_root - x_new), y_accum + (y_root - y_new));
+                                               // m_last_warp= lastEventTime(xme.time);
+                                       } else {
+                                               // setCursorPosition(x_new, y_new); /* wrap but don't accumulate */
+                                               SDL_WarpMouseInWindow(sdl_win, x_new - x_win, y_new - y_win);
+                                       }
+
+                                       g_event = new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, x_new, y_new);
+                               }
+                               else {
+                                       g_event = new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, x_root + x_accum, y_root + y_accum);
+                               }
+                       }
+                       else
+#endif
+                       {
+                               g_event= new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, x_root, y_root);
+                       }
                        break;
                }
        case SDL_MOUSEBUTTONUP:
@@ -346,10 +393,15 @@ GHOST_TSuccess
 GHOST_SystemSDL::getCursorPosition(GHOST_TInt32& x,
                                    GHOST_TInt32& y) const
 {
+       int x_win, y_win;
+       SDL_Window *win= SDL_GetMouseFocus();
+       SDL_GetWindowPosition(win, &x_win, &y_win);
+
        int xi, yi;
        SDL_GetMouseState(&xi, &yi);
-       x= xi;
-       y= yi;
+       x= xi + x_win;
+       y= yi + x_win;
+
        return GHOST_kSuccess;
 }
 
@@ -357,8 +409,11 @@ GHOST_TSuccess
 GHOST_SystemSDL::setCursorPosition(GHOST_TInt32 x,
                                    GHOST_TInt32 y)
 {
-       // SDL_SendMouseMotion(SDL, SDL_FALSE, x, y); // NOT EXPOSED
-       SDL_WarpMouseInWindow(NULL, x, y);
+       int x_win, y_win;
+       SDL_Window *win= SDL_GetMouseFocus();
+       SDL_GetWindowPosition(win, &x_win, &y_win);
+
+       SDL_WarpMouseInWindow(win, x - x_win, y - y_win);
        return GHOST_kSuccess;
 }
 
index 7aea1b6694a3c591f6477bcc0d925582d9e9db72..63141ad807b78e1e3c25dc106f083cf046664c59 100644 (file)
@@ -40,6 +40,11 @@ extern "C" {
        #include "SDL.h"
 }
 
+#if !SDL_VERSION_ATLEAST(1, 3, 0)
+#  error "SDL 1.3 or newer is needed to build with Ghost"
+#endif
+
+
 class GHOST_WindowSDL;
 
 
index 9d4263f6311fc740135fa291bafb8d4474f6bedf..a97f2877f10249c11af186ee2fae8e572f231e37 100644 (file)
@@ -50,8 +50,8 @@ GHOST_WindowSDL::GHOST_WindowSDL(GHOST_SystemSDL *system,
       m_sdl_custom_cursor(NULL)
 {
        m_sdl_win= SDL_CreateWindow(title,
-                                   10,
-                                   10,
+                                   left,
+                                   top,
                                    width,
                                    height,
                                    SDL_WINDOW_RESIZABLE|SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN);
@@ -285,6 +285,26 @@ GHOST_WindowSDL::setClientSize(GHOST_TUns32 width,
        return GHOST_kSuccess;
 }
 
+void
+GHOST_WindowSDL::screenToClient( GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST_TInt32& outX, GHOST_TInt32& outY ) const
+{
+       /* XXXSDL_WEAK_ABS_COORDS */
+       int x_win, y_win;
+       SDL_GetWindowPosition(m_sdl_win, &x_win, &y_win);
+
+       outX = inX - x_win;
+       outY = inY - y_win;
+}
+void
+GHOST_WindowSDL::clientToScreen( GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST_TInt32& outX, GHOST_TInt32& outY ) const
+{
+       /* XXXSDL_WEAK_ABS_COORDS */
+       int x_win, y_win;
+       SDL_GetWindowPosition(m_sdl_win, &x_win, &y_win);
+
+       outX = inX + x_win;
+       outY = inY + y_win;
+}
 
 /* mouse cursor */
 static unsigned char sdl_std_cursor_mask_xterm[]= {0xef,0x01,0xff,0x01,0xff,0x01,0x7c,0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x7c,0x00,0xff,0x01,0xff,0x01,0xef,0x01,};
index 12ff0266e8cf587fe862b49e77b306fc78c35fd5..1b2bd35e057aa480d001943030eed885b7932017 100644 (file)
@@ -37,6 +37,10 @@ extern "C" {
        #include "SDL.h"
 }
 
+#if !SDL_VERSION_ATLEAST(1, 3, 0)
+#  error "SDL 1.3 or newer is needed to build with Ghost"
+#endif
+
 class STR_String;
 
 class GHOST_WindowSDL : public GHOST_Window
@@ -55,10 +59,8 @@ public:
 
        GHOST_WindowSDL(GHOST_SystemSDL *system,
                        const STR_String& title,
-                       GHOST_TInt32 left,
-                       GHOST_TInt32 top,
-                       GHOST_TUns32 width,
-                       GHOST_TUns32 height,
+                       GHOST_TInt32 left, GHOST_TInt32 top,
+                       GHOST_TUns32 width, GHOST_TUns32 height,
                        GHOST_TWindowState state,
                        const GHOST_TEmbedderWindowID parentWindow,
                        GHOST_TDrawingContextType type,
@@ -70,8 +72,8 @@ public:
 
        /* SDL spesific */
        SDL_Window *
-       getSDLWindow(
-       ){
+       getSDLWindow()
+       {
                return m_sdl_win;
        }
 
@@ -88,37 +90,74 @@ public:
                m_invalid_window = false;
        }
 
-       bool getValid( ) const
+       bool getValid() const
        {
                return (m_sdl_win != NULL);
        }
 
+       void getWindowBounds(GHOST_Rect& bounds) const;
+       void getClientBounds(GHOST_Rect& bounds) const;
+
 protected:
        GHOST_TSuccess installDrawingContext(GHOST_TDrawingContextType type);
        GHOST_TSuccess removeDrawingContext();
 
-       GHOST_TSuccess setWindowCursorGrab(GHOST_TGrabCursorMode mode);
-       GHOST_TSuccess setWindowCursorShape(GHOST_TStandardCursor shape);
-       GHOST_TSuccess setWindowCustomCursorShape(GHOST_TUns8 bitmap[16][2], GHOST_TUns8 mask[16][2], int hotX, int hotY);
-       GHOST_TSuccess setWindowCustomCursorShape(GHOST_TUns8 *bitmap, GHOST_TUns8 *mask, int sizex, int sizey, int hotX, int hotY, int fg_color, int bg_color);
-       GHOST_TSuccess setWindowCursorVisibility(bool visible);
-
-       void setTitle(const STR_String& title);
-       void getTitle(STR_String& title) const;
-       void getWindowBounds( GHOST_Rect& bounds ) const;
-       void getClientBounds( GHOST_Rect& bounds ) const;
-       GHOST_TSuccess setClientWidth(GHOST_TUns32 width);
-       GHOST_TSuccess setClientHeight(GHOST_TUns32 height);
-       GHOST_TSuccess setClientSize(GHOST_TUns32 width, GHOST_TUns32 height);
-
-       /* TODO */
-       void screenToClient( GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST_TInt32& outX, GHOST_TInt32& outY ) const { outX = inX; outY = inY; }
-       void clientToScreen( GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST_TInt32& outX, GHOST_TInt32& outY ) const { outX = inX; outY = inY; }
-
-       GHOST_TSuccess swapBuffers();
-       GHOST_TSuccess activateDrawingContext();
-       GHOST_TSuccess setState(GHOST_TWindowState state);
-       GHOST_TWindowState getState() const;
+       GHOST_TSuccess
+       setWindowCursorGrab(GHOST_TGrabCursorMode mode);
+
+       GHOST_TSuccess
+       setWindowCursorShape(GHOST_TStandardCursor shape);
+
+       GHOST_TSuccess
+       setWindowCustomCursorShape(GHOST_TUns8 bitmap[16][2],
+                                  GHOST_TUns8 mask[16][2],
+                                  int hotX, int hotY);
+
+       GHOST_TSuccess
+       setWindowCustomCursorShape(GHOST_TUns8 *bitmap,
+                                  GHOST_TUns8 *mask,
+                                  int sizex, int sizey,
+                                  int hotX, int hotY,
+                                  int fg_color, int bg_color);
+
+       GHOST_TSuccess
+       setWindowCursorVisibility(bool visible);
+
+       void
+       setTitle(const STR_String& title);
+
+       void
+       getTitle(STR_String& title) const;
+
+       GHOST_TSuccess
+       setClientWidth(GHOST_TUns32 width);
+
+       GHOST_TSuccess
+       setClientHeight(GHOST_TUns32 height);
+
+       GHOST_TSuccess
+       setClientSize(GHOST_TUns32 width,
+                     GHOST_TUns32 height);
+
+       void
+       screenToClient(GHOST_TInt32 inX, GHOST_TInt32 inY,
+                      GHOST_TInt32& outX, GHOST_TInt32& outY) const;
+
+       void
+       clientToScreen(GHOST_TInt32 inX, GHOST_TInt32 inY,
+                      GHOST_TInt32& outX, GHOST_TInt32& outY) const;
+
+       GHOST_TSuccess
+       swapBuffers();
+
+       GHOST_TSuccess
+       activateDrawingContext();
+
+       GHOST_TSuccess
+       setState(GHOST_TWindowState state);
+
+       GHOST_TWindowState
+       getState() const;
 
        GHOST_TSuccess setOrder(GHOST_TWindowOrder order) { return GHOST_kSuccess; } // TODO