Browse Source

Code cleanup for monitor preferences

rsredsq 10 years ago
parent
commit
3ea9b753ff

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

@@ -2796,7 +2796,7 @@ int Graphics::GetCurrentMonitor()
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
 }
 }
 
 
-int Graphics::GetMonitorsNumber()
+int Graphics::GetNumMonitors()
 {
 {
     return SDL_GetNumVideoDisplays();
     return SDL_GetNumVideoDisplays();
 }
 }

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

@@ -503,7 +503,7 @@ public:
     /// Return the current monitor number
     /// Return the current monitor number
     int GetCurrentMonitor();
     int GetCurrentMonitor();
     /// Return the available monitors number
     /// Return the available monitors number
-    int GetMonitorsNumber();
+    int GetNumMonitors();
     /// Return true if window is maximized
     /// Return true if window is maximized
     bool GetMaximized();
     bool GetMaximized();
     /// Return monitor resolution
     /// Return monitor resolution

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

@@ -2776,7 +2776,7 @@ int Graphics::GetCurrentMonitor()
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
 }
 }
 
 
-int Graphics::GetMonitorsNumber()
+int Graphics::GetNumMonitors()
 {
 {
     return SDL_GetNumVideoDisplays();
     return SDL_GetNumVideoDisplays();
 }
 }

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

@@ -496,7 +496,7 @@ public:
     /// Return the current monitor number
     /// Return the current monitor number
     int GetCurrentMonitor();
     int GetCurrentMonitor();
     /// Return the available monitors number
     /// Return the available monitors number
-    int GetMonitorsNumber();
+    int GetNumMonitors();
     /// Return true if window is maximized
     /// Return true if window is maximized
     bool GetMaximized();
     bool GetMaximized();
     /// Return monitor resolution
     /// Return monitor resolution

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

@@ -3315,7 +3315,7 @@ int Graphics::GetCurrentMonitor()
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
     return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
 }
 }
 
 
-int Graphics::GetMonitorsNumber()
+int Graphics::GetNumMonitors()
 {
 {
     return SDL_GetNumVideoDisplays();
     return SDL_GetNumVideoDisplays();
 }
 }

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

@@ -528,7 +528,7 @@ public:
     /// Return the current monitor number
     /// Return the current monitor number
     int GetCurrentMonitor();
     int GetCurrentMonitor();
     /// Return the available monitors number
     /// Return the available monitors number
-    int GetMonitorsNumber();
+    int GetNumMonitors();
     /// Return true if window is maximized
     /// Return true if window is maximized
     bool GetMaximized();
     bool GetMaximized();
     /// Return monitor resolution
     /// Return monitor resolution

+ 87 - 68
Source/AtomicEditor/Application/AEEditorCommon.cpp

@@ -177,69 +177,29 @@ bool AEEditorCommon::CreateDefaultPreferences(String& path, JSONValue& prefs)
     root["recentProjects"] = JSONArray();
     root["recentProjects"] = JSONArray();
 
 
     JSONValue editorWindow;
     JSONValue editorWindow;
-    editorWindow["x"] = 0;
-    editorWindow["y"] = 0;
-    editorWindow["width"] = 0;
-    editorWindow["height"] = 0;
-    editorWindow["monitor"] = 0;
-    editorWindow["maximized"] = true;
+    GetDefaultWindowPreferences(editorWindow);
 
 
     JSONValue playerWindow;
     JSONValue playerWindow;
-    playerWindow["x"] = 0;
-    playerWindow["y"] = 0;
-    playerWindow["width"] = 0;
-    playerWindow["height"] = 0;
-    playerWindow["monitor"] = 0;
-    playerWindow["maximized"] = false;
+    GetDefaultWindowPreferences(playerWindow);
 
 
     root["editorWindow"] = editorWindow;
     root["editorWindow"] = editorWindow;
     root["playerWindow"] = playerWindow;
     root["playerWindow"] = playerWindow;
 
 
-    SharedPtr<File> file(new File(context_, path, FILE_WRITE));
-
-    if (!file->IsOpen())
-    {
-        LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
-        return false;
-    }
-
-    jsonFile->Save(*file, "   ");
-
     prefs = root;
     prefs = root;
 
 
+    SavePreferences(prefs);
+
     return true;
     return true;
 }
 }
 
 
 bool AEEditorCommon::ReadPreferences()
 bool AEEditorCommon::ReadPreferences()
 {
 {
     FileSystem* fileSystem = GetSubsystem<FileSystem>();
     FileSystem* fileSystem = GetSubsystem<FileSystem>();
-    String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
-    path += "prefs.json";
+    String path = GetPreferencesPath();
 
 
     JSONValue prefs;
     JSONValue prefs;
 
 
-    if (!fileSystem->FileExists(path))
-    {
-        if (!CreateDefaultPreferences(path, prefs))
-            return false;
-    }
-    else
-    {
-        SharedPtr<File> file(new File(context_, path, FILE_READ));
-        SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
-
-        if (!jsonFile->BeginLoad(*file))
-        {
-            file->Close();
-            if (!CreateDefaultPreferences(path, prefs))
-                return false;
-        }
-        else
-        {
-            prefs = jsonFile->GetRoot();
-        }
-
-    }
+    LoadPreferences(prefs);
 
 
     if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
     if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
     {
     {
@@ -262,7 +222,7 @@ void AEEditorCommon::ValidateWindow()
 {
 {
     Graphics* graphics = GetSubsystem<Graphics>();
     Graphics* graphics = GetSubsystem<Graphics>();
     IntVector2 windowPosition = graphics->GetWindowPosition();
     IntVector2 windowPosition = graphics->GetWindowPosition();
-    int monitors = graphics->GetMonitorsNumber();
+    int monitors = graphics->GetNumMonitors();
     IntVector2 maxResolution;
     IntVector2 maxResolution;
 
 
     for (int i = 0; i < monitors; i++)
     for (int i = 0; i < monitors; i++)
@@ -273,38 +233,97 @@ void AEEditorCommon::ValidateWindow()
 
 
     if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || windowPosition.x_ < 0 || windowPosition.y_ < 0)
     if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || windowPosition.x_ < 0 || windowPosition.y_ < 0)
     {
     {
-        bool editor = this->GetTypeName() == "AEEditorApp";
+        JSONValue prefs;
+
+        if (!LoadPreferences(prefs))
+            return;
+
         JSONValue window;
         JSONValue window;
-        window["x"] = 0;
-        window["y"] = 0;
-        window["width"] = 0;
-        window["height"] = 0;
-        window["monitor"] = 0;
-        window["maximized"] = editor ? true : false;
+        GetDefaultWindowPreferences(window);
 
 
-        FileSystem* fileSystem = GetSubsystem<FileSystem>();
-        String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
-        path += "prefs.json";
+        prefs[context_->GetEditorContext() ? "editorWindow" : "playerWindow"] = window;
 
 
-        JSONValue prefs;
+        graphics->SetMode(0, 0);
+        graphics->CenterWindow();
+        if (context_->GetEditorContext())
+        {
+            graphics->Maximize();
+        }
 
 
-        SharedPtr<File> file(new File(context_, path, FILE_READWRITE));
-        SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
+        SavePreferences(prefs);
+    }
+}
 
 
-        jsonFile->BeginLoad(*file);
-        prefs = jsonFile->GetRoot();
+void AEEditorCommon::GetDefaultWindowPreferences(JSONValue& windowPrefs)
+{
+    windowPrefs["x"] = 0;
+    windowPrefs["y"] = 0;
+    windowPrefs["width"] = 0;
+    windowPrefs["height"] = 0;
+    windowPrefs["monitor"] = 0;
+    windowPrefs["maximized"] = context_->GetEditorContext() ? true : false;
+}
 
 
-        prefs[editor ? "editorWindow" : "playerWindow"] = window;
+String AEEditorCommon::GetPreferencesPath()
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+    String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
+    path += "prefs.json";
+    return path;
+}
 
 
-        jsonFile->Save(*file, "   ");
-        file->Close();
+bool AEEditorCommon::LoadPreferences(JSONValue& prefs)
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+    String path = GetPreferencesPath();
 
 
-        graphics->SetMode(0, 0);
-        if (editor)
+    if (!fileSystem->FileExists(path))
+    {
+        if (!CreateDefaultPreferences(path, prefs))
+            return false;
+    }
+    else
+    {
+        SharedPtr<File> file(new File(context_, path, FILE_READ));
+        SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
+
+        if (!jsonFile->BeginLoad(*file))
         {
         {
-            graphics->Maximize();
+            file->Close();
+            if (!CreateDefaultPreferences(path, prefs))
+                return false;
+        }
+        else
+        {
+            prefs = jsonFile->GetRoot();
         }
         }
+
+        file->Close();
+    }
+
+    return true;
+}
+
+bool AEEditorCommon::SavePreferences(JSONValue& prefs)
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+    String path = GetPreferencesPath();
+
+    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;
 }
 }
 
 
 }
 }

+ 4 - 1
Source/AtomicEditor/Application/AEEditorCommon.h

@@ -44,7 +44,10 @@ protected:
     SharedPtr<JSVM> vm_;
     SharedPtr<JSVM> vm_;
 
 
 private:
 private:
-
+    void GetDefaultWindowPreferences(JSONValue& windowPrefs);
+    String GetPreferencesPath();
+    bool LoadPreferences(JSONValue& prefs);
+    bool SavePreferences(JSONValue& prefs);
 };
 };
 
 
 }
 }