Pārlūkot izejas kodu

Better preference defaults, initial window creation, and general preference handling

Josh Engebretson 10 gadi atpakaļ
vecāks
revīzija
9330f73857

+ 3 - 2
CMakeLists.txt

@@ -19,7 +19,8 @@ if (ATOMIC_DEV_BUILD)
     add_definitions("-DATOMIC_DEV_BUILD=1")
     add_definitions("-DATOMIC_DEV_BUILD=1")
 endif()
 endif()
 
 
-add_definitions("-DATOMIC_SOURCE_BUILD=1")
+# Disable until https://github.com/AtomicGameEngine/AtomicGameEngine/issues/554 is addressed
+# add_definitions("-DATOMIC_SOURCE_BUILD=1")
 
 
 # this is here as QtCreator is having trouble picking up #include <Atomic/*> without it
 # this is here as QtCreator is having trouble picking up #include <Atomic/*> without it
 include_directories(${CMAKE_SOURCE_DIR}/Source ${CMAKE_SOURCE_DIR}/Source/AtomicEditor/Source)
 include_directories(${CMAKE_SOURCE_DIR}/Source ${CMAKE_SOURCE_DIR}/Source/AtomicEditor/Source)
@@ -67,7 +68,7 @@ endif()
 find_program(CLDOC cldoc)
 find_program(CLDOC cldoc)
 if(CLDOC)
 if(CLDOC)
 
 
-   add_custom_target(docs 
+   add_custom_target(docs
     DEPENDS AtomicEngineDocs
     DEPENDS AtomicEngineDocs
     )
     )
 endif()
 endif()

+ 53 - 18
Script/AtomicEditor/editor/Preferences.ts

@@ -60,24 +60,30 @@ class Preferences {
     read(): void {
     read(): void {
         var filePath = this.getPreferencesFullPath();
         var filePath = this.getPreferencesFullPath();
         var jsonFile;
         var jsonFile;
-        //check if file doesn't exists, create an empty JSON file
+
+        //check if file doesn't exist, create default json
         if (!this.fileSystem.fileExists(filePath)) {
         if (!this.fileSystem.fileExists(filePath)) {
-            jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
-            jsonFile.writeString("{}");
-            jsonFile.close();
+            this.useDefaultConfig();
+            this.write();
+            return;
         }
         }
+
         //Read file
         //Read file
         jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
         jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
+
         var prefs;
         var prefs;
+
         try {
         try {
-          prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
-        } catch (e){
-          console.log("Editor preference file invalid, regenerating default configuration");
-          prefs = null;
-          this.useDefaultConfig();
+
+            prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
+
+        } catch (e) {
+            console.log("Editor preference file invalid, regenerating default configuration");
+            this.useDefaultConfig();
+            this.write();
         }
         }
+
         if (prefs) {
         if (prefs) {
-            if (!prefs.recentProjects) prefs.recentProjects = [""];
             this._prefs = prefs;
             this._prefs = prefs;
         }
         }
 
 
@@ -90,30 +96,29 @@ class Preferences {
         jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
         jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
     }
     }
 
 
-    saveEditorWindowData(windowData:WindowData) {
+    saveEditorWindowData(windowData: WindowData) {
         this._prefs.editorWindow = windowData;
         this._prefs.editorWindow = windowData;
         this.write();
         this.write();
     }
     }
 
 
-    savePlayerWindowData(windowData:WindowData) {
+    savePlayerWindowData(windowData: WindowData) {
         this._prefs.playerWindow = windowData;
         this._prefs.playerWindow = windowData;
         this.write();
         this.write();
     }
     }
 
 
-    useDefaultConfig():void {
+    useDefaultConfig(): void {
         this._prefs = new PreferencesFormat();
         this._prefs = new PreferencesFormat();
-        if (!this._prefs.recentProjects) this._prefs.recentProjects = [""];
     }
     }
 
 
-    get editorWindow():WindowData {
+    get editorWindow(): WindowData {
         return this._prefs.editorWindow;
         return this._prefs.editorWindow;
     }
     }
 
 
-    get playerWindow():WindowData {
+    get playerWindow(): WindowData {
         return this._prefs.playerWindow;
         return this._prefs.playerWindow;
     }
     }
 
 
-    get recentProjects(): [string] {
+    get recentProjects(): string[] {
         return this._prefs.recentProjects;
         return this._prefs.recentProjects;
     }
     }
 
 
@@ -132,7 +137,37 @@ interface WindowData {
 }
 }
 
 
 class PreferencesFormat {
 class PreferencesFormat {
-    recentProjects: [string];
+
+    constructor() {
+
+        this.setDefault();
+    }
+
+    setDefault() {
+
+        this.recentProjects = [];
+
+        this.editorWindow = {
+            x: 0,
+            y: 0,
+            width: 0,
+            height: 0,
+            monitor: 0,
+            maximized: true
+        }
+
+        this.playerWindow = {
+            x: 0,
+            y: 0,
+            width: 0,
+            height: 0,
+            monitor: 0,
+            maximized: true
+        }
+
+    }
+
+    recentProjects: string[];
     editorWindow: WindowData;
     editorWindow: WindowData;
     playerWindow: WindowData;
     playerWindow: WindowData;
 }
 }

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

@@ -365,7 +365,7 @@ bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless,
     // If zero in fullscreen, use desktop mode
     // If zero in fullscreen, use desktop mode
     if (!width || !height)
     if (!width || !height)
     {
     {
-        if (fullscreen || borderless)
+        if (fullscreen || borderless || maximize)
         {
         {
             SDL_DisplayMode mode;
             SDL_DisplayMode mode;
             SDL_GetDesktopDisplayMode(0, &mode);
             SDL_GetDesktopDisplayMode(0, &mode);

+ 1 - 15
Source/AtomicEditor/Application/AEEditorApp.cpp

@@ -109,22 +109,8 @@ void AEEditorApp::Setup()
 
 
 #endif // ATOMIC_DEV_BUILD
 #endif // ATOMIC_DEV_BUILD
 
 
-    String prefsPath = filesystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
-    prefsPath += "prefs.json";
+    ReadPreferences();
 
 
-    JSONValue editorWindow;
-
-    if (ReadPreferences(prefsPath, editorWindow, "editorWindow"))
-    {
-        if (editorWindow.IsObject())
-        {
-            engineParameters_["WindowPositionX"] = editorWindow.Get("x").GetUInt();
-            engineParameters_["WindowPositionY"] = editorWindow.Get("y").GetUInt();
-            engineParameters_["WindowWidth"] = editorWindow.Get("width").GetUInt();
-            engineParameters_["WindowHeight"] = editorWindow.Get("height").GetUInt();
-            engineParameters_["WindowMaximized"] = editorWindow.Get("maximized").GetBool();
-        }
-    }
 }
 }
 
 
 void AEEditorApp::Stop()
 void AEEditorApp::Stop()

+ 71 - 14
Source/AtomicEditor/Application/AEEditorCommon.cpp

@@ -156,34 +156,91 @@ void AEEditorCommon::Stop()
 #endif
 #endif
 }
 }
 
 
-
-bool AEEditorCommon::ReadPreferences(String& path, JSONValue& prefs, const String& propertyName)
+void AEEditorCommon::CreateDefaultPreferences(String& path, JSONValue& prefs)
 {
 {
-    if (!path.EndsWith(".json"))
-        path.Append(".json");
+    // Note there is some duplication here with the editor's
+    // TypeScript preference code, this is due to the preferences for
+    // the editor window needing to be available at window creation time
+    // It could be better to split this all out to a native, scriptable
+    // preferences object
 
 
-    SharedPtr<File> file(new File(context_, path, FILE_READ));
+    LOGINFOF("Creating default Atomic Editor preferences: %s", path.CString());
 
 
     SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
     SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
 
 
-    if (!jsonFile->BeginLoad(*file))
-        return false;
+    JSONValue& root = jsonFile->GetRoot();
+
+    root.Clear();
+    root["recentProjects"] = JSONArray();
+
+    JSONValue editorWindow;
+    editorWindow["x"] = 0;
+    editorWindow["y"] = 0;
+    editorWindow["width"] = 0;
+    editorWindow["height"] = 0;
+    editorWindow["monitor"] = 0;
+    editorWindow["maximized"] = true;
+
+    JSONValue playerWindow;
+    playerWindow["x"] = 0;
+    playerWindow["y"] = 0;
+    playerWindow["width"] = 0;
+    playerWindow["height"] = 0;
+    playerWindow["monitor"] = 0;
+    playerWindow["maximized"] = true;
+
+    root["editorWindow"] = editorWindow;
+    root["playerWindow"] = playerWindow;
+
+    SharedPtr<File> file(new File(context_, path, FILE_WRITE));
+
+    jsonFile->Save(*file, "   ");
+
+    prefs = root;
+
+}
+
+bool AEEditorCommon::ReadPreferences()
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+    String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
+    path += "prefs.json";
 
 
-    JSONValue root = jsonFile->GetRoot();
+    JSONValue prefs;
 
 
-    if (propertyName.Length() > 0)
+    if (!fileSystem->FileExists(path))
+    {
+        CreateDefaultPreferences(path, prefs);
+    }
+    else
     {
     {
-        if (root.Contains(propertyName))
+        SharedPtr<File> file(new File(context_, path, FILE_READ));
+        SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
+
+        if (!jsonFile->BeginLoad(*file))
         {
         {
-            prefs = root.Get(propertyName);
+            file->Close();
+            CreateDefaultPreferences(path, prefs);
         }
         }
+        else
+        {
+            prefs = jsonFile->GetRoot();
+        }
+
     }
     }
-    else
+
+    if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
     {
     {
-        prefs = root;
+        CreateDefaultPreferences(path, prefs);
     }
     }
 
 
-    file->Close();
+    JSONValue& editorWindow = prefs["editorWindow"];
+
+    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;
     return true;
 }
 }

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

@@ -37,7 +37,8 @@ public:
 
 
 protected:
 protected:
 
 
-    bool ReadPreferences(String& path, JSONValue& prefs, const String& propertyName = "");
+    void CreateDefaultPreferences(String& path, JSONValue& prefs);
+    bool ReadPreferences();
     
     
     SharedPtr<JSVM> vm_;
     SharedPtr<JSVM> vm_;