Browse Source

Editor preferences reworked. Deleted native code that contains a Preferences logic. Use Preferences.ts for loading/saving preferences now. A bit preferences format remapped.

rsredsq 10 years ago
parent
commit
ab70f58420

+ 6 - 7
Script/AtomicEditor/editor/Editor.ts

@@ -5,6 +5,7 @@ import AssetImport = require("../assets/AssetImport");
 import PlayMode = require("../ui/playmode/PlayMode");
 import EditorLicense = require("./EditorLicense");
 import EditorEvents = require("./EditorEvents");
+import Preferences = require("../utils/Preferences");
 
 class Editor extends Atomic.ScriptObject {
 
@@ -26,6 +27,9 @@ class Editor extends Atomic.ScriptObject {
 
         this.editorLicense = new EditorLicense();
 
+        var prefs = new Preferences();
+        prefs.read();
+
         EditorUI.initialize();
 
         this.playMode = new PlayMode();
@@ -41,14 +45,10 @@ class Editor extends Atomic.ScriptObject {
         this.subscribeToEvent("ExitRequested", (data) => this.handleExitRequested(data));
 
         this.subscribeToEvent("ProjectLoaded", (data) => {
-
-            Atomic.editorMode.preferences.registerRecentProject(data.projectPath);
-
+            prefs.registerRecentProject(data.projectPath);
         })
 
         this.parseArguments();
-
-
     }
 
     handleEditorLoadProject(event: EditorEvents.LoadProjectEvent): boolean {
@@ -69,7 +69,6 @@ class Editor extends Atomic.ScriptObject {
     }
 
     handleEditorCloseProject(event) {
-
         var system = ToolCore.getToolSystem();
 
         if (system.project) {
@@ -110,7 +109,7 @@ class Editor extends Atomic.ScriptObject {
 
     // event handling
     handleExitRequested(data) {
-
+        Preferences.getInstance().write();
         EditorUI.shutdown();
 
     }

+ 2 - 1
Script/AtomicEditor/tsconfig.json

@@ -57,6 +57,7 @@
         "./ui/modal/ResourceSelection.ts",
         "./ui/modal/UIResourceOps.ts",
         "./ui/playmode/PlayMode.ts",
-        "./ui/playmode/PlayerOutput.ts"
+        "./ui/playmode/PlayerOutput.ts",
+        "./utils/Preferences.ts"
     ]
 }

+ 7 - 7
Script/AtomicEditor/ui/WelcomeFrame.ts

@@ -2,6 +2,7 @@
 import EditorEvents = require("../editor/EditorEvents");
 import EditorUI = require("./EditorUI");
 import ScriptWidget = require("./ScriptWidget");
+import Preferences = require("../utils/Preferences");
 
 class WelcomeFrame extends ScriptWidget {
 
@@ -28,7 +29,6 @@ class WelcomeFrame extends ScriptWidget {
         this.subscribeToEvent(EditorEvents.CloseProject, () => {
             this.updateRecentProjects();
         });
-
     }
 
     handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
@@ -69,9 +69,9 @@ class WelcomeFrame extends ScriptWidget {
             }
 
             if (id == "recentProjectsContextMenu") {
-                var pref = Atomic.editorMode.getPreferences();
+                var prefs = Preferences.getInstance();
                 if (ev.refid == "clear recent projects") {
-                    pref.deleteRecentProjects();
+                    prefs.deleteRecentProjects();
                     this.updateRecentProjects();
                 }
             }
@@ -82,10 +82,10 @@ class WelcomeFrame extends ScriptWidget {
 
         this.recentList.deleteAllItems();
         
-        // prune any that don't exist
-        Atomic.editorMode.preferences.updateRecentFiles();
-
-        this.recent = Atomic.editorMode.getPreferences().getRecentProjects();
+        var prefs = Preferences.getInstance();
+        prefs.updateRecentProjects();
+        
+        this.recent = prefs.recentProjects;
 
         for (var i in this.recent) {
             this.recentList.addRootItem(this.recent[i], "Folder.icon", i);

+ 132 - 0
Script/AtomicEditor/utils/Preferences.ts

@@ -0,0 +1,132 @@
+
+class Preferences {
+
+    private fileSystem: Atomic.FileSystem;
+
+    private static instance: Preferences;
+    private _androidSDKPath: string;
+    private _jdkRootPath: string;
+    private _antPath: string;
+
+    private _recentProjects: [string];
+
+    constructor() {
+        this.fileSystem = Atomic.getFileSystem();
+        Preferences.instance = this;
+
+        this._recentProjects = [""];
+    }
+
+    registerRecentProject(path: string): void {
+        var index = this._recentProjects.indexOf(path);
+        if (index >= 0) {
+            this._recentProjects.splice(index, 1);
+        }
+        this._recentProjects.unshift(path);
+        this.updateRecentProjects();
+    }
+
+    unRegisterRecentProject(path: string): void {
+        var index = this._recentProjects.indexOf(path);
+        if (index >= 0) {
+            this._recentProjects.splice(index, 1);
+        }
+        this.updateRecentProjects();
+    }
+
+    updateRecentProjects(): void {
+        for (var i in this._recentProjects) {
+            var path = this._recentProjects[i];
+            if (!this.fileSystem.exists(path)) {
+                this._recentProjects.splice(i, 1);
+            }
+        }
+    }
+
+    deleteRecentProjects(): void {
+        this._recentProjects.length = 0;
+    }
+
+    getPreferencesFullPath(): string {
+        var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
+        filePath += "prefs.json";
+        return filePath;
+    }
+
+    read(): void {
+        var filePath = this.getPreferencesFullPath();
+        var jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
+        if (!jsonFile.isOpen()) return;
+        var prefs = <PreferencesFormat> JSON.parse(jsonFile.readText());
+        console.log(prefs);
+        this._recentProjects = prefs.recentFiles;
+        this._androidSDKPath = prefs.androidSdkPath;
+        this._jdkRootPath = prefs.jdkRootPath;
+        this._antPath = prefs.antPath;
+    }
+
+    write(): void {
+        var filePath = this.getPreferencesFullPath();
+        var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
+        if (!jsonFile.isOpen()) return;
+        var prefs = new PreferencesFormat();
+        prefs.recentFiles = this._recentProjects;
+        var graphics = Atomic.getGraphics();
+        var pos, width, height;
+        if (graphics && !graphics.getFullscreen()) {
+            pos = graphics.getWindowPosition();
+            width = graphics.getWidth();
+            height = graphics.getHeight();
+        }
+        prefs.window = { x: pos[0], y: pos[1], width: width, height: height };
+        prefs.androidSdkPath = this._androidSDKPath;
+        prefs.jdkRootPath = this._jdkRootPath;
+        prefs.antPath = this._antPath;
+        jsonFile.writeString(JSON.stringify(prefs, null, 2));
+    }
+
+    get recentProjects(): [string] {
+        return this._recentProjects;
+    }
+
+    get androidSdkPath(): string {
+        return Atomic.addTrailingSlash(this._androidSDKPath);
+    }
+
+    set androidSdkPath(path: string) {
+        this._androidSDKPath = path;
+        this.write()
+    }
+
+    get jdkRootPath(): string {
+        return Atomic.addTrailingSlash(this._jdkRootPath);
+    }
+
+    set jdkRootPath(path: string) {
+        this._jdkRootPath = path;
+        this.write();
+    }
+
+    get antPath(): string {
+        return Atomic.addTrailingSlash(this._antPath);
+    }
+
+    set andPath(path: string) {
+        this._antPath = path;
+        this.write();
+    }
+
+    static getInstance(): Preferences {
+        return Preferences.instance;
+    }
+}
+
+class PreferencesFormat {
+    recentFiles: [string];
+    androidSdkPath: string;
+    jdkRootPath: string;
+    antPath: string;
+    window: { x: number, y: number, width: number, height: number };
+}
+
+export = Preferences;

+ 2 - 1
Script/Packages/Atomic/IO.json

@@ -9,7 +9,8 @@
 	},
 	"typescript_decl" : {
 		"File" : [
-			"readText():string;"
+			"readText():string;",
+            "writeString(text:string):void;"
 		],
 		"FileSystem" : [
 			"scanDir(pathName:string, filter:string, flags:number, recursive:boolean);"

+ 1 - 1
Script/Packages/Editor/Editor.json

@@ -3,5 +3,5 @@
 	"sources" : ["Source/AtomicEditor/Application", "Source/AtomicEditor/Utils",
 							"Source/AtomicEditor/EditorMode", "Source/AtomicEditor/PlayerMode",
 							 "Source/AtomicEditor/Editors", "Source/AtomicEditor/Editors/SceneEditor3D"],
-	"classes" : ["EditorMode", "PlayerMode", "FileUtils", "AEPreferences", "ResourceEditor", "JSResourceEditor", "SceneEditor3D", "SceneView3D"]
+	"classes" : ["EditorMode", "PlayerMode", "FileUtils", "ResourceEditor", "JSResourceEditor", "SceneEditor3D", "SceneView3D"]
 }

+ 1 - 0
Script/TypeScript/Atomic.d.ts

@@ -7797,6 +7797,7 @@ declare module Atomic {
       // Unlike FileSystem.Copy this copy works when the source file is in a package file
       copy(srcFile: File): boolean;
       readText():string;
+      writeString(text:string):void;
 
    }
 

+ 0 - 28
Script/TypeScript/Editor.d.ts

@@ -35,31 +35,6 @@ declare module Editor {
 //----------------------------------------------------
 
 
-   export class AEPreferences extends Atomic.AObject {
-
-      recentProjects: string[];
-      androidSDKPath: string;
-      antPath: string;
-      jDKRootPath: string;
-
-      // Construct.
-      constructor();
-
-      registerRecentProject(fullpath: string): void;
-      getRecentProjects(): string[];
-      deleteRecentProjects(): void;
-      setAndroidSDKPath(path: string): void;
-      setAntPath(path: string): void;
-      setJDKRootPath(path: string): void;
-      getAndroidSDKPath(): string;
-      getJDKRootPath(): string;
-      getAntPath(): string;
-      read(): void;
-      write(): void;
-      updateRecentFiles(write?: boolean): void;
-
-   }
-
    export class FileUtils extends Atomic.AObject {
 
       // Construct.
@@ -74,13 +49,10 @@ declare module Editor {
 
    export class EditorMode extends Atomic.AObject {
 
-      preferences: AEPreferences;
-
       // Construct.
       constructor();
 
       playProject(): boolean;
-      getPreferences(): AEPreferences;
 
    }
 

+ 0 - 3
Source/AtomicEditor/Application/AEEditorApp.cpp

@@ -22,7 +22,6 @@
 #include "../EditorMode/AEEditorMode.h"
 
 #include "AEEditorApp.h"
-#include "AEPreferences.h"
 
 using namespace ToolCore;
 
@@ -90,8 +89,6 @@ void AEEditorApp::Setup()
 {
     context_->SetEditorContext(true);
 
-    context_->RegisterSubsystem(new AEPreferences(context_));
-
     ToolEnvironment* env = new ToolEnvironment(context_);
     context_->RegisterSubsystem(env);
 

+ 0 - 2
Source/AtomicEditor/Application/AEEditorApp.h

@@ -13,8 +13,6 @@ namespace Atomic
 namespace AtomicEditor
 {
 
-class AEPreferences;
-
 class AEEditorApp : public AEEditorCommon
 {
     OBJECT(AEEditorApp);

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

@@ -13,8 +13,6 @@ namespace Atomic
 namespace AtomicEditor
 {
 
-class AEPreferences;
-
 class AEEditorCommon : public Application
 {
     OBJECT(AEEditorCommon);

+ 0 - 292
Source/AtomicEditor/Application/AEPreferences.cpp

@@ -1,292 +0,0 @@
-// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
-// Please see LICENSE.md in repository root for license information
-// https://github.com/AtomicGameEngine/AtomicGameEngine
-
-#include <rapidjson/document.h>
-#include "rapidjson/prettywriter.h"
-#include "rapidjson/filestream.h"
-
-#include <Atomic/Core/Context.h>
-#include <Atomic/IO/FileSystem.h>
-#include <Atomic/IO/Log.h>
-#include <Atomic/IO/File.h>
-#include <Atomic/Graphics/Graphics.h>
-
-#include "../EditorMode/AEEditorEvents.h"
-#include "AEPreferences.h"
-
-using namespace rapidjson;
-
-namespace AtomicEditor
-{
-
-AEPreferences::AEPreferences(Context* context) :
-    Object(context)
-{
-    context->RegisterSubsystem(this);
-
-    SubscribeToEvent(E_EDITORSHUTDOWN, HANDLER(AEPreferences, HandleEditorShutdown));
-
-    Read();
-}
-
-AEPreferences::~AEPreferences()
-{
-
-}
-
-String AEPreferences::GetPreferencesFullPath()
-{
-    FileSystem* fs = GetSubsystem<FileSystem>();
-    String filepath = fs->GetAppPreferencesDir("AtomicEditor", "Preferences");
-    filepath += "prefs.json";
-    return filepath;
-}
-
-void AEPreferences::Clear()
-{
-    recentProjects_.Clear();
-}
-
-void AEPreferences::Read()
-{
-    rapidjson::Document document;
-
-    String filepath = GetPreferencesFullPath();
-
-    File jsonFile(context_, filepath);
-
-    if (!jsonFile.IsOpen())
-        return;
-
-    String json;
-    jsonFile.ReadText(json);
-
-    if (!json.Length())
-        return;
-
-    if (document.Parse<0>(json.CString()).HasParseError())
-    {
-        LOGERRORF("Could not parse JSON data from %s", filepath.CString());
-        return;
-    }
-
-    Clear();
-
-    const Value::Member* recent_files = document.FindMember("recent_files");
-    if (recent_files && recent_files->value.IsArray())
-    {
-        for (Value::ConstValueIterator itr = recent_files->value.Begin(); itr != recent_files->value.End(); itr++)
-        {
-             if (!(*itr).IsString())
-                 continue;
-
-            String path(itr->GetString());
-            recentProjects_.Push(path.CString());
-        }
-    }
-
-    const Value::Member* android_sdk_path = document.FindMember("android_sdk_path");
-    if (android_sdk_path && android_sdk_path->value.IsString())
-        androidSDKPath_ = android_sdk_path->value.GetString();
-
-    const Value::Member* jdk_root_path = document.FindMember("jdk_root_path");
-    if (jdk_root_path && jdk_root_path->value.IsString())
-        jdkRootPath_ = jdk_root_path->value.GetString();
-
-    const Value::Member* ant_path = document.FindMember("ant_path");
-    if (ant_path && ant_path->value.IsString())
-        antPath_ = ant_path->value.GetString();
-
-    UpdateRecentFiles(false);
-
-}
-
-void AEPreferences::Write()
-{
-    String filepath = GetPreferencesFullPath();
-
-    FILE* file = fopen(filepath.CString(), "w");
-
-    if (!file)
-        return;
-
-    Graphics* graphics = GetSubsystem<Graphics>();
-
-    IntVector2 pos(-1, -1);
-
-    int width = -1;
-    int height = -1;
-
-    if (graphics && !graphics->GetFullscreen())
-    {
-        pos = graphics->GetWindowPosition();
-        width = graphics->GetWidth();
-        height = graphics->GetHeight();
-    }
-
-    rapidjson::FileStream s(file);
-    rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
-
-    writer.StartObject();
-
-    // recent files
-    writer.String("recent_files");
-    writer.StartArray();
-    for (unsigned i = 0; i < recentProjects_.Size(); i++)
-        writer.String(recentProjects_[i].CString());
-    writer.EndArray();
-
-    writer.String("android_sdk_path");
-    writer.String(androidSDKPath_.CString());
-
-    writer.String("jdk_root_path");
-    writer.String(jdkRootPath_.CString());
-
-    writer.String("ant_path");
-    writer.String(antPath_.CString());
-
-    writer.String("window_pos_x");
-    writer.Int(pos.x_);
-    writer.String("window_pos_y");
-    writer.Int(pos.y_);
-    writer.String("window_width");
-    writer.Int(width);
-    writer.String("window_height");
-    writer.Int(height);
-
-    writer.EndObject();
-
-    fclose(file);
-
-}
-
-void AEPreferences::UpdateRecentFiles(bool write)
-{
-    FileSystem* fileSystem = GetSubsystem<FileSystem>();
-    Vector<String> recentProjects;
-
-    for (unsigned i = 0; i < recentProjects_.Size(); i++)
-    {
-        String path = recentProjects_[i];
-
-        if (!fileSystem->FileExists(path))
-            continue;
-
-
-        recentProjects.Push(path);
-
-        if (recentProjects.Size() == 10)
-            break;
-    }
-
-    recentProjects_ = recentProjects;
-
-    if (write)
-        Write();
-}
-
-void AEPreferences::RegisterRecentProject(const String& fullpath)
-{
-    if (recentProjects_.Contains(fullpath))
-    {
-        recentProjects_.Remove(fullpath);
-    }
-
-    recentProjects_.Insert(0, fullpath);
-
-
-    UpdateRecentFiles();
-
-}
-
-void AEPreferences::DeleteRecentProjects() 
-{
-    if (recentProjects_.Size() <= 0) return;
-    recentProjects_.Clear();
-    UpdateRecentFiles(true);
-}
-
-bool AEPreferences::ReadStartupPrefs(Context *context, StartupPreferences& prefs)
-{
-
-    FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
-    String filepath = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
-    filepath += "prefs.json";
-
-    if (!fileSystem->FileExists(filepath))
-        return false;
-
-    SharedPtr<File> file(new File(context, filepath, FILE_READ));
-
-    if (!file->IsOpen())
-        return false;
-
-    String json;
-    file->ReadText(json);
-
-    if (!json.Length())
-        return false;
-
-    rapidjson::Document document;
-
-    if (document.Parse<0>(json.CString()).HasParseError())
-    {
-        return false;
-    }
-
-    bool success = true;
-
-    const Value::Member* imember = document.FindMember("window_pos_x");
-    if (imember && imember->value.IsInt())
-    {
-        prefs.windowPos.x_ = imember->value.GetInt();
-    }
-    else
-    {
-        success = false;
-    }
-
-    imember = document.FindMember("window_pos_y");
-    if (imember && imember->value.IsInt())
-    {
-        prefs.windowPos.y_ = imember->value.GetInt();
-    }
-    else
-    {
-        success = false;
-    }
-
-    imember = document.FindMember("window_width");
-    if (imember && imember->value.IsInt())
-    {
-        prefs.windowWidth = imember->value.GetInt();
-    }
-    else
-    {
-        success = false;
-    }
-
-    imember = document.FindMember("window_height");
-    if (imember && imember->value.IsInt())
-    {
-        prefs.windowHeight = imember->value.GetInt();
-    }
-    else
-    {
-        success = false;
-    }
-
-    if (prefs.windowHeight < 128 || prefs.windowWidth < 128)
-        return false;
-
-    return success;
-
-}
-
-void AEPreferences::HandleEditorShutdown(StringHash eventType, VariantMap& eventData)
-{
-    context_->RemoveSubsystem(GetType());
-}
-
-}

+ 0 - 69
Source/AtomicEditor/Application/AEPreferences.h

@@ -1,69 +0,0 @@
-// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
-// Please see LICENSE.md in repository root for license information
-// https://github.com/AtomicGameEngine/AtomicGameEngine
-
-#pragma once
-
-#include <Atomic/Core/Object.h>
-#include <Atomic/IO/FileSystem.h>
-
-using namespace Atomic;
-
-namespace AtomicEditor
-{
-
-class AEPreferences : public Object
-{
-
-    OBJECT(AEPreferences);
-
-public:
-
-    struct StartupPreferences
-    {
-        IntVector2 windowPos;
-        int windowWidth;
-        int windowHeight;
-    };
-
-    /// Construct.
-    AEPreferences(Context* context);
-    /// Destruct.
-    ~AEPreferences();
-
-    void RegisterRecentProject(const String& fullpath);
-    const Vector<String>& GetRecentProjects() { return recentProjects_; }
-    void DeleteRecentProjects();
-
-    void SetAndroidSDKPath(const String& path) { androidSDKPath_ = path; Write(); }
-    void SetAntPath(const String& path) { antPath_ = path; Write(); }
-    void SetJDKRootPath(const String& path) { jdkRootPath_ = path; Write(); }
-
-    String GetAndroidSDKPath() { return AddTrailingSlash(androidSDKPath_); }
-    String GetJDKRootPath() { return AddTrailingSlash(jdkRootPath_); }
-    String GetAntPath() { return AddTrailingSlash(antPath_); }
-
-    void Read();
-    void Write();
-
-    static bool ReadStartupPrefs(Context* context, StartupPreferences& prefs);
-
-    void UpdateRecentFiles(bool write = true);
-
-private:
-
-     void HandleEditorShutdown(StringHash eventType, VariantMap& eventData);
-
-    void Clear();
-    String GetPreferencesFullPath();
-
-    String androidSDKPath_;
-    String jdkRootPath_;
-    String antPath_;
-
-    String lastProjectFullPath_;
-    Vector<String> recentProjects_;    
-};
-
-
-}

+ 0 - 8
Source/AtomicEditor/EditorMode/AEEditorMode.cpp

@@ -11,8 +11,6 @@
 
 #include <AtomicJS/Javascript/JSIPCEvents.h>
 
-#include "../Application/AEPreferences.h"
-
 #include "AEEditorMode.h"
 
 using namespace ToolCore;
@@ -31,12 +29,6 @@ EditorMode::~EditorMode()
 
 }
 
-AEPreferences* EditorMode::GetPreferences()
-{
-    return GetSubsystem<AEPreferences>();
-
-}
-
 void EditorMode::HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData)
 {
     VariantMap startupData;

+ 0 - 6
Source/AtomicEditor/EditorMode/AEEditorMode.h

@@ -15,8 +15,6 @@ using namespace Atomic;
 namespace AtomicEditor
 {
 
-class AEPreferences;
-
 /// EditorMode subsystem
 class EditorMode : public Object
 {
@@ -30,10 +28,6 @@ public:
 
     bool PlayProject();
 
-
-    AEPreferences* GetPreferences();
-
-
 private:
 
     void HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData);

+ 0 - 2
Source/AtomicEditor/Javascript/AEEditorJS.cpp

@@ -1,8 +1,6 @@
 
 #include <AtomicJS/Javascript/JSVM.h>
 
-#include "../Application/AEPreferences.h"
-
 #include "../EditorMode/AEEditorMode.h"
 
 using namespace Atomic;