Browse Source

New E_WINDOWPOS event when SDL notifies window movement. Support for setting initial window position beforecreation.

Jonne Nauha 11 years ago
parent
commit
03d9532b4a

+ 35 - 6
Source/Engine/Graphics/Direct3D9/D3D9Graphics.cpp

@@ -251,6 +251,7 @@ Graphics::Graphics(Context* context) :
     externalWindow_(0),
     width_(0),
     height_(0),
+    position_(M_MAX_INT,M_MAX_INT),
     multiSample_(1),
     fullscreen_(false),
     borderless_(false),
@@ -364,6 +365,8 @@ void Graphics::SetWindowPosition(const IntVector2& position)
 {
     if (impl_->window_)
         SDL_SetWindowPosition(impl_->window_, position.x_, position.y_);
+    else
+        position_ = position; // Sets as initial position for OpenWindow()
 }
 
 void Graphics::SetWindowPosition(int x, int y)
@@ -2024,12 +2027,9 @@ bool Graphics::IsInitialized() const
 
 IntVector2 Graphics::GetWindowPosition() const
 {
-    IntVector2 ret(IntVector2::ZERO);
-    
     if (impl_->window_)
-        SDL_GetWindowPosition(impl_->window_, &ret.x_, &ret.y_);
-    
-    return ret;
+        return position_;
+    return IntVector2::ZERO;
 }
 
 PODVector<IntVector2> Graphics::GetResolutions() const
@@ -2223,6 +2223,30 @@ void Graphics::WindowResized()
     SendEvent(E_SCREENMODE, eventData);
 }
 
+void Graphics::WindowMoved()
+{
+    if (!impl_->device_ || !impl_->window_)
+        return;
+
+    int newX, newY;
+
+    SDL_GetWindowPosition(impl_->window_, &newX, &newY);
+    if (newX == position_.x_ && newY == position_.y_)
+        return;
+
+    position_.x_ = newX;
+    position_.y_ = newY;
+
+    LOGDEBUGF("Window was moved to %d,%d", position_.x_, position_.y_);
+
+    using namespace WindowPos;
+
+    VariantMap& eventData = GetEventDataMap();
+    eventData[P_X] = position_.x_;
+    eventData[P_Y] = position_.y_;
+    SendEvent(E_WINDOWPOS, eventData);
+}
+
 void Graphics::Maximize()
 {
     if (!impl_->window_)
@@ -2453,7 +2477,10 @@ bool Graphics::OpenWindow(int width, int height, bool resizable, bool borderless
         if (borderless)
             flags |= SDL_WINDOW_BORDERLESS;
 
-        impl_->window_ = SDL_CreateWindow(windowTitle_.CString(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, flags);
+        impl_->window_ = SDL_CreateWindow(windowTitle_.CString(),
+             (position_.x_ != M_MAX_INT ? position_.x_ : SDL_WINDOWPOS_UNDEFINED), (position_.y_ != M_MAX_INT ? position_.y_ : SDL_WINDOWPOS_UNDEFINED),
+             width, height, flags
+        );
     }
     else
         impl_->window_ = SDL_CreateWindowFrom(externalWindow_, 0);
@@ -2464,6 +2491,8 @@ bool Graphics::OpenWindow(int width, int height, bool resizable, bool borderless
         return false;
     }
 
+    SDL_GetWindowPosition(impl_->window_, &position_.x_, &position_.y_);
+
     CreateWindowIcon();
 
     return true;

+ 6 - 2
Source/Engine/Graphics/Direct3D9/D3D9Graphics.h

@@ -88,9 +88,9 @@ public:
     void SetWindowTitle(const String& windowTitle);
     /// Set window icon.
     void SetWindowIcon(Image* windowIcon);
-    /// Set window position.
+    /// Set window position. Sets initial position if window is not created yet.
     void SetWindowPosition(const IntVector2& position);
-    /// Set window position.
+    /// Set window position. Sets initial position if window is not created yet.
     void SetWindowPosition(int x, int y);
     /// Set screen mode. Return true if successful.
     bool SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool vsync, bool tripleBuffer, int multiSample);
@@ -374,6 +374,8 @@ public:
     
     /// Window was resized through user interaction. Called by Input subsystem.
     void WindowResized();
+    /// Window was moved through user interaction. Called by Input subsystem.
+    void WindowMoved();
     /// Maximize the Window.
     void Maximize();
     /// Minimize the Window.
@@ -460,6 +462,8 @@ private:
     int width_;
     /// Window height.
     int height_;
+    /// Window position.
+    IntVector2 position_;
     /// Multisampling mode.
     int multiSample_;
     /// Fullscreen flag.

+ 7 - 0
Source/Engine/Graphics/GraphicsEvents.h

@@ -37,6 +37,13 @@ EVENT(E_SCREENMODE, ScreenMode)
     PARAM(P_BORDERLESS, Borderless);        // bool
 }
 
+/// Window position changed.
+EVENT(E_WINDOWPOS, WindowPos)
+{
+    PARAM(P_X, X);                          // int
+    PARAM(P_Y, Y);                          // int
+}
+
 /// Graphics features checked.
 EVENT(E_GRAPHICSFEATURES, GraphicsFeatures)
 {