Browse Source

Merge pull request #611 from rsredsq/RED-EDITOR-599

Center window if it's outside of desktop resolution
JoshEngebretson 10 years ago
parent
commit
be7b3c50d6

+ 1 - 1
Script/AtomicEditor/ui/Shortcuts.ts

@@ -30,7 +30,7 @@ class Shortcuts extends Atomic.ScriptObject {
         } else {
             var playerWindow = Preferences.getInstance().playerWindow;
             if (playerWindow) {
-                if ((playerWindow.monitor + 1) > Atomic.graphics.getMonitorsNumber()) {
+                if ((playerWindow.monitor + 1) > Atomic.graphics.getNumMonitors()) {
                     //will use default settings if monitor is not available
                     var args = "--resizable";
                     Atomic.editorMode.playProject(args, debug);

+ 7 - 1
Source/Atomic/Graphics/Direct3D11/D3D11Graphics.cpp

@@ -2796,7 +2796,7 @@ int Graphics::GetCurrentMonitor()
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
 }
 
-int Graphics::GetMonitorsNumber()
+int Graphics::GetNumMonitors()
 {
     return SDL_GetNumVideoDisplays();
 }
@@ -2807,6 +2807,12 @@ bool Graphics::GetMaximized()
 
     return SDL_GetWindowFlags(impl_->window_) & SDL_WINDOW_MAXIMIZED;
 }
+IntVector2 Graphics::GetMonitorResolution(int monitorId) const
+{
+    SDL_DisplayMode mode;
+    SDL_GetDesktopDisplayMode(monitorId, &mode);
+    return IntVector2(mode.w, mode.h);
+}
 // ATOMIC END
 
 }

+ 3 - 1
Source/Atomic/Graphics/Direct3D11/D3D11Graphics.h

@@ -503,9 +503,11 @@ public:
     /// Return the current monitor number
     int GetCurrentMonitor();
     /// Return the available monitors number
-    int GetMonitorsNumber();
+    int GetNumMonitors();
     /// Return true if window is maximized
     bool GetMaximized();
+    /// Return monitor resolution
+    IntVector2 GetMonitorResolution(int monitorId) const;
     // ATOMIC END
 
 private:

+ 8 - 1
Source/Atomic/Graphics/Direct3D9/D3D9Graphics.cpp

@@ -2776,7 +2776,7 @@ int Graphics::GetCurrentMonitor()
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
 }
 
-int Graphics::GetMonitorsNumber()
+int Graphics::GetNumMonitors()
 {
     return SDL_GetNumVideoDisplays();
 }
@@ -2788,6 +2788,13 @@ bool Graphics::GetMaximized()
 
     return SDL_GetWindowFlags(impl_->window_) & SDL_WINDOW_MAXIMIZED;
 }
+
+IntVector2 Graphics::GetMonitorResolution(int monitorId) const
+{
+    SDL_DisplayMode mode;
+    SDL_GetDesktopDisplayMode(monitorId, &mode);
+    return IntVector2(mode.w, mode.h);
+}
 // ATOMIC END
 
 }

+ 3 - 1
Source/Atomic/Graphics/Direct3D9/D3D9Graphics.h

@@ -496,9 +496,11 @@ public:
     /// Return the current monitor number
     int GetCurrentMonitor();
     /// Return the available monitors number
-    int GetMonitorsNumber();
+    int GetNumMonitors();
     /// Return true if window is maximized
     bool GetMaximized();
+    /// Return monitor resolution
+    IntVector2 GetMonitorResolution(int monitorId) const;
     // ATOMIC END
 
 private:

+ 7 - 1
Source/Atomic/Graphics/OpenGL/OGLGraphics.cpp

@@ -3315,7 +3315,7 @@ int Graphics::GetCurrentMonitor()
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
 }
 
-int Graphics::GetMonitorsNumber()
+int Graphics::GetNumMonitors()
 {
     return SDL_GetNumVideoDisplays();
 }
@@ -3326,6 +3326,12 @@ bool Graphics::GetMaximized()
 
     return SDL_GetWindowFlags(impl_->window_) & SDL_WINDOW_MAXIMIZED;
 }
+IntVector2 Graphics::GetMonitorResolution(int monitorId) const
+{
+    SDL_DisplayMode mode;
+    SDL_GetDesktopDisplayMode(monitorId, &mode);
+    return IntVector2(mode.w, mode.h);
+}
 // ATOMIC END
 
 }

+ 3 - 1
Source/Atomic/Graphics/OpenGL/OGLGraphics.h

@@ -528,9 +528,11 @@ public:
     /// Return the current monitor number
     int GetCurrentMonitor();
     /// Return the available monitors number
-    int GetMonitorsNumber();
+    int GetNumMonitors();
     /// Return true if window is maximized
     bool GetMaximized();
+    /// Return monitor resolution
+    IntVector2 GetMonitorResolution(int monitorId) const;
     // ATOMIC END
 
 private:

+ 100 - 31
Source/AtomicEditor/Application/AEEditorCommon.cpp

@@ -7,6 +7,7 @@
 
 #include <Atomic/Engine/Engine.h>
 #include <Atomic/Input/Input.h>
+#include <Atomic/Graphics/Graphics.h>
 
 #include <Atomic/IPC/IPC.h>
 
@@ -55,6 +56,8 @@ AEEditorCommon::AEEditorCommon(Context* context) :
 
 void AEEditorCommon::Start()
 {
+    ValidateWindow();
+
     Input* input = GetSubsystem<Input>();
     input->SetMouseVisible(true);
 
@@ -174,46 +177,103 @@ bool AEEditorCommon::CreateDefaultPreferences(String& path, JSONValue& prefs)
     root["recentProjects"] = JSONArray();
 
     JSONValue editorWindow;
-    editorWindow["x"] = 0;
-    editorWindow["y"] = 0;
-    editorWindow["width"] = 0;
-    editorWindow["height"] = 0;
-    editorWindow["monitor"] = 0;
-    editorWindow["maximized"] = true;
+    GetDefaultWindowPreferences(editorWindow, true);
 
     JSONValue playerWindow;
-    playerWindow["x"] = 0;
-    playerWindow["y"] = 0;
-    playerWindow["width"] = 0;
-    playerWindow["height"] = 0;
-    playerWindow["monitor"] = 0;
-    playerWindow["maximized"] = false;
+    GetDefaultWindowPreferences(playerWindow, false);
 
     root["editorWindow"] = editorWindow;
     root["playerWindow"] = playerWindow;
 
-    SharedPtr<File> file(new File(context_, path, FILE_WRITE));
+    prefs = root;
 
-    if (!file->IsOpen())
+    SavePreferences(prefs);
+
+    return true;
+}
+
+bool AEEditorCommon::ReadPreferences()
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+    String path = GetPreferencesPath();
+
+    JSONValue prefs;
+
+    LoadPreferences(prefs);
+
+    if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
     {
-        LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
-        return false;
+        if (!CreateDefaultPreferences(path, prefs))
+            return false;
     }
 
-    jsonFile->Save(*file, "   ");
+    JSONValue& editorWindow = prefs["editorWindow"];
 
-    prefs = root;
+    engineParameters_["WindowPositionX"] = editorWindow["x"].GetUInt();
+    engineParameters_["WindowPositionY"] = editorWindow["y"].GetUInt();
+    engineParameters_["WindowWidth"] = editorWindow["width"].GetUInt();
+    engineParameters_["WindowHeight"] = editorWindow["height"].GetUInt();
+    engineParameters_["WindowMaximized"] = editorWindow["maximized"].GetBool();
 
     return true;
 }
 
-bool AEEditorCommon::ReadPreferences()
+void AEEditorCommon::ValidateWindow()
+{
+    Graphics* graphics = GetSubsystem<Graphics>();
+    IntVector2 windowPosition = graphics->GetWindowPosition();
+    int monitors = graphics->GetNumMonitors();
+    IntVector2 maxResolution;
+
+    for (int i = 0; i < monitors; i++)
+    {
+        IntVector2 monitorResolution = graphics->GetMonitorResolution(i);
+        maxResolution += monitorResolution;
+    }
+
+    if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || (windowPosition.x_ + graphics->GetWidth()) < 0 || (windowPosition.y_ + graphics->GetHeight()) < 0)
+    {
+        JSONValue prefs;
+
+        if (!LoadPreferences(prefs))
+            return;
+
+        bool editor = context_->GetEditorContext();
+
+        JSONValue window;
+        GetDefaultWindowPreferences(window, editor);
+
+        prefs[editor ? "editorWindow" : "playerWindow"] = window;
+
+        //Setting the mode to 0 width/height will use engine defaults for window size and layout
+        graphics->SetMode(0, 0, graphics->GetFullscreen(), graphics->GetBorderless(), graphics->GetResizable(), graphics->GetVSync(), graphics->GetTripleBuffer(), graphics->GetMultiSample(), editor);
+
+        SavePreferences(prefs);
+    }
+}
+
+void AEEditorCommon::GetDefaultWindowPreferences(JSONValue& windowPrefs, bool maximized)
+{
+    windowPrefs["x"] = 0;
+    windowPrefs["y"] = 0;
+    windowPrefs["width"] = 0;
+    windowPrefs["height"] = 0;
+    windowPrefs["monitor"] = 0;
+    windowPrefs["maximized"] = maximized;
+}
+
+String AEEditorCommon::GetPreferencesPath()
 {
     FileSystem* fileSystem = GetSubsystem<FileSystem>();
     String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
     path += "prefs.json";
+    return path;
+}
 
-    JSONValue prefs;
+bool AEEditorCommon::LoadPreferences(JSONValue& prefs)
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+    String path = GetPreferencesPath();
 
     if (!fileSystem->FileExists(path))
     {
@@ -236,22 +296,31 @@ bool AEEditorCommon::ReadPreferences()
             prefs = jsonFile->GetRoot();
         }
 
+        file->Close();
     }
 
-    if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
-    {
-        if (!CreateDefaultPreferences(path, prefs))
-            return false;
-    }
+    return true;
+}
 
-    JSONValue& editorWindow = prefs["editorWindow"];
+bool AEEditorCommon::SavePreferences(JSONValue& prefs)
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+    String path = GetPreferencesPath();
 
-    engineParameters_["WindowPositionX"] = editorWindow["x"].GetUInt();
-    engineParameters_["WindowPositionY"] = editorWindow["y"].GetUInt();
-    engineParameters_["WindowWidth"] = editorWindow["width"].GetUInt();
-    engineParameters_["WindowHeight"] = editorWindow["height"].GetUInt();
-    engineParameters_["WindowMaximized"] = editorWindow["maximized"].GetBool();
+    SharedPtr<File> file(new File(context_, path, FILE_WRITE));
+    SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
+
+    jsonFile->GetRoot() = prefs;
 
+    if (!file->IsOpen())
+    {
+        LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
+        return false;
+    }
+
+    jsonFile->Save(*file, "   ");
+    file->Close();
+    
     return true;
 }
 

+ 6 - 2
Source/AtomicEditor/Application/AEEditorCommon.h

@@ -39,11 +39,15 @@ protected:
 
     bool CreateDefaultPreferences(String& path, JSONValue& prefs);
     bool ReadPreferences();
-    
+    void ValidateWindow();
+
     SharedPtr<JSVM> vm_;
 
 private:
-
+    void GetDefaultWindowPreferences(JSONValue& windowPrefs, bool maximized);
+    String GetPreferencesPath();
+    bool LoadPreferences(JSONValue& prefs);
+    bool SavePreferences(JSONValue& prefs);
 };
 
 }