Browse Source

Updated to match the new way preferences are handled

Shaddock Heath 9 years ago
parent
commit
c266247d19

+ 1 - 6
EditorPluginExample/Resources/EditorData/JSExample.plugin.js

@@ -106,12 +106,7 @@ CustomEditorBuilder.getEditor = function(resourceFrame, resourcePath, tabContain
     });
 
     editor.subscribeToEvent("UserPreferencesChangedNotification", function(data) {
-        var prefsPath = ToolCore.toolSystem.project.userPrefsFullPath;
-        if (Atomic.fileSystem.fileExists(prefsPath)) {
-            // Get a reference to the web client so we can call the load preferences method
-            var webClient = editor.webView.webClient;
-            webClient.executeJavaScript('HOST_loadPreferences("atomic://' + prefsPath + '");');
-        }
+        webClient.executeJavaScript('HOST_preferencesChanged();');
     });
 
     return editor;

+ 164 - 149
EditorPluginExample/Resources/EditorData/TSExample.plugin.js

@@ -1,149 +1,164 @@
-"use strict";
-var ExamplePluginUILabel = "TS Example Plugin";
-var ExamplePluginTBPath = "EditorData/Example.tb.txt";
-var InfoboxTBPath = "EditorData/Infobox.tb.txt";
-var CustomEditorBuilder = (function () {
-    function CustomEditorBuilder() {
-    }
-    CustomEditorBuilder.prototype.canHandleResource = function (resourcePath) {
-        return resourcePath.indexOf("custom.editor.json") > 0;
-    };
-    CustomEditorBuilder.prototype.getNormalizedPath = function (path) {
-        var RESOURCES_MARKER = "resources/";
-        return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
-    };
-    CustomEditorBuilder.prototype.getEditor = function (resourceFrame, resourcePath, tabContainer) {
-        var _this = this;
-        var editorUrl = "atomic://" + ToolCore.toolSystem.project.resourcePath + "EditorData/customEditor.html";
-        var editor = new Editor.JSResourceEditor(resourcePath, tabContainer, editorUrl);
-        editor.subscribeToEvent("WebViewLoadEnd", function (data) {
-            editor.unsubscribeFromEvent("WebViewLoadEnd");
-            var webClient = editor.webView.webClient;
-            webClient.executeJavaScript("HOST_loadCode(\"atomic://" + _this.getNormalizedPath(editor.fullPath) + "\");");
-        });
-        editor.subscribeToEvent("DeleteResourceNotification", function (data) {
-            var webClient = editor.webView.webClient;
-            webClient.executeJavaScript("HOST_resourceDeleted(\"atomic://" + _this.getNormalizedPath(data.path) + "\");");
-        });
-        editor.subscribeToEvent("UserPreferencesChangedNotification", function (data) {
-            var prefsPath = ToolCore.toolSystem.project.userPrefsFullPath;
-            if (Atomic.fileSystem.fileExists(prefsPath)) {
-                var webClient = editor.webView.webClient;
-                webClient.executeJavaScript("HOST_loadPreferences(\"atomic://" + prefsPath + "\");");
-            }
-        });
-        return editor;
-    };
-    return CustomEditorBuilder;
-}());
-var customEditorBuilder = new CustomEditorBuilder();
-var TSExamplePluginService = (function () {
-    function TSExamplePluginService() {
-        var _this = this;
-        this.name = "TSExampleService";
-        this.description = "This service demonstrates plugin functionality functionality.";
-        this.serviceLocator = null;
-        this.extensionWindow = null;
-        this.lastObjectName = null;
-        this.totalUses = 0;
-        this.handleWidgetEvent = function (ev) {
-            if (!_this.extensionWindow) {
-                return;
-            }
-            if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
-                if (ev.target.id == "example_cancel") {
-                    _this.extensionWindow.hide();
-                    _this.extensionWindow = null;
-                    return true;
-                }
-                if (ev.target.id == "example_speak") {
-                    _this.serviceLocator.projectServices.setUserPreference(_this.name, "UsageCount", ++_this.totalUses);
-                    _this.helloLabel.text = "Hello " + _this.nameField.text + ", This was used " + _this.totalUses + " times.";
-                    return true;
-                }
-            }
-            return false;
-        };
-    }
-    TSExamplePluginService.prototype.initialize = function (serviceLoader) {
-        Atomic.print("TSExamplePluginService.initialize");
-        this.serviceLocator = (serviceLoader);
-        if (this.serviceLocator) {
-            this.serviceLocator.projectServices.register(this);
-            this.serviceLocator.uiServices.register(this);
-        }
-    };
-    TSExamplePluginService.prototype.projectUnloaded = function () {
-        this.serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
-        this.serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
-        this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
-        this.serviceLocator.uiServices.unregisterCustomEditor(customEditorBuilder);
-        Atomic.print("TSExamplePluginService.projectUnloaded");
-        if (this.serviceLocator) {
-            this.serviceLocator.projectServices.unregister(this);
-            this.serviceLocator.uiServices.unregister(this);
-        }
-    };
-    TSExamplePluginService.prototype.projectLoaded = function (ev) {
-        Atomic.print("TSExamplePluginService.projectLoaded");
-        this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open": ["tsexampleplugin open"] });
-        this.serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin hierarchy context"] });
-        this.serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin project context"] });
-        this.totalUses = this.serviceLocator.projectServices.getUserPreference(this.name, "UsageCount", 0);
-        this.serviceLocator.uiServices.registerCustomEditor(customEditorBuilder);
-    };
-    TSExamplePluginService.prototype.playerStarted = function () {
-        Atomic.print("TSExamplePluginService.playerStarted");
-    };
-    TSExamplePluginService.prototype.menuItemClicked = function (refId) {
-        Atomic.print("TSExamplePluginService.menuItemClicked: " + refId);
-        if (refId == "tsexampleplugin open") {
-            this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
-            this.getWidgets();
-            return true;
-        }
-        return false;
-    };
-    TSExamplePluginService.prototype.hierarchyContextItemClicked = function (node, refid) {
-        Atomic.print("TSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
-        if (refid == "tsexampleplugin hierarchy context") {
-            this.lastObjectName = "node " + node.name;
-            this.showInfobox("Hierarchy Item Selected ", "Node: '" + node.name + "' was selected.");
-            return true;
-        }
-        return false;
-    };
-    TSExamplePluginService.prototype.projectContextItemClicked = function (asset, refid) {
-        Atomic.print("TSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
-        if (refid == "tsexampleplugin project context") {
-            this.lastObjectName = "asset " + asset.name;
-            this.showInfobox("Project Asset Selected", "Asset: '" + asset.name + "' was selected.");
-            return true;
-        }
-        return false;
-    };
-    TSExamplePluginService.prototype.getWidgets = function () {
-        if (!this.extensionWindow) {
-            return;
-        }
-        this.helloLabel = this.extensionWindow.getWidget("example_hello");
-        this.nameField = this.extensionWindow.getWidget("example_name");
-        if (this.lastObjectName) {
-            this.nameField.text = this.lastObjectName;
-            this.lastObjectName = null;
-        }
-    };
-    TSExamplePluginService.prototype.showInfobox = function (title, msg) {
-        var infobox = this.serviceLocator.uiServices.showModalWindow(title, InfoboxTBPath, function (ev) {
-            if (ev.type == Atomic.UI_EVENT_TYPE_CLICK && ev.target.id == "infobox_ok") {
-                infobox.hide();
-                return true;
-            }
-        });
-        var msgLabel = infobox.getWidget("infobox_msg");
-        msgLabel.text = msg;
-    };
-    return TSExamplePluginService;
-}());
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.default = new TSExamplePluginService();
+/// <reference path="../../typings/Atomic/Atomic.d.ts" />
+"use strict";
+var ExamplePluginUILabel = "TS Example Plugin";
+var ExamplePluginTBPath = "EditorData/Example.tb.txt";
+var InfoboxTBPath = "EditorData/Infobox.tb.txt";
+var CustomEditorBuilder = (function () {
+    function CustomEditorBuilder() {
+    }
+    /**
+     * Returns true if this builder can generate an editor for this resource type
+     */
+    CustomEditorBuilder.prototype.canHandleResource = function (resourcePath) {
+        return resourcePath.indexOf("custom.editor.json") > 0;
+    };
+    /**
+     * Full path is the fully qualified path from the root of the filesystem.  In order to take advantage
+     * of the resource caching system, let's trim it down to just the path inside the resources directory
+     * including the Resources directory so that the casing is correct
+     */
+    CustomEditorBuilder.prototype.getNormalizedPath = function (path) {
+        var RESOURCES_MARKER = "resources/";
+        return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
+    };
+    /**
+     * Generates a resource editor for the provided resource type
+     * @param  resourcePath
+     * @param  tabContainer
+     */
+    CustomEditorBuilder.prototype.getEditor = function (resourceFrame, resourcePath, tabContainer) {
+        var _this = this;
+        // point to a custom page
+        var editorUrl = "atomic://" + ToolCore.toolSystem.project.resourcePath + "EditorData/customEditor.html";
+        var editor = new Editor.JSResourceEditor(resourcePath, tabContainer, editorUrl);
+        // one time subscriptions waiting for the web view to finish loading.  This event
+        // actually hits the editor instance before we can hook it, so listen to it on the
+        // frame and then unhook it
+        editor.subscribeToEvent("WebViewLoadEnd", function (data) {
+            editor.unsubscribeFromEvent("WebViewLoadEnd");
+            var webClient = editor.webView.webClient;
+            webClient.executeJavaScript("HOST_loadCode(\"atomic://" + _this.getNormalizedPath(editor.fullPath) + "\");");
+        });
+        editor.subscribeToEvent("DeleteResourceNotification", function (data) {
+            var webClient = editor.webView.webClient;
+            webClient.executeJavaScript("HOST_resourceDeleted(\"atomic://" + _this.getNormalizedPath(data.path) + "\");");
+        });
+        editor.subscribeToEvent("UserPreferencesChangedNotification", function (data) {
+            var webClient = editor.webView.webClient;
+            webClient.executeJavaScript("HOST_preferencesChanged();");
+        });
+        return editor;
+    };
+    return CustomEditorBuilder;
+}());
+var customEditorBuilder = new CustomEditorBuilder();
+var TSExamplePluginService = (function () {
+    function TSExamplePluginService() {
+        var _this = this;
+        this.name = "TSExampleService";
+        this.description = "This service demonstrates plugin functionality functionality.";
+        this.serviceLocator = null;
+        this.extensionWindow = null;
+        this.lastObjectName = null;
+        this.totalUses = 0;
+        this.handleWidgetEvent = function (ev) {
+            if (!_this.extensionWindow) {
+                return;
+            }
+            if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
+                if (ev.target.id == "example_cancel") {
+                    _this.extensionWindow.hide();
+                    _this.extensionWindow = null;
+                    return true;
+                }
+                if (ev.target.id == "example_speak") {
+                    _this.serviceLocator.projectServices.setUserPreference(_this.name, "UsageCount", ++_this.totalUses);
+                    _this.helloLabel.text = "Hello " + _this.nameField.text + ", This was used " + _this.totalUses + " times.";
+                    return true;
+                }
+            }
+            return false;
+        };
+    }
+    TSExamplePluginService.prototype.initialize = function (serviceLoader) {
+        Atomic.print("TSExamplePluginService.initialize");
+        this.serviceLocator = (serviceLoader);
+        if (this.serviceLocator) {
+            this.serviceLocator.projectServices.register(this);
+            this.serviceLocator.uiServices.register(this);
+        }
+    };
+    TSExamplePluginService.prototype.projectUnloaded = function () {
+        this.serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
+        this.serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
+        this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
+        this.serviceLocator.uiServices.unregisterCustomEditor(customEditorBuilder);
+        Atomic.print("TSExamplePluginService.projectUnloaded");
+        if (this.serviceLocator) {
+            this.serviceLocator.projectServices.unregister(this);
+            this.serviceLocator.uiServices.unregister(this);
+        }
+    };
+    TSExamplePluginService.prototype.projectLoaded = function (ev) {
+        Atomic.print("TSExamplePluginService.projectLoaded");
+        this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open": ["tsexampleplugin open"] });
+        this.serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin hierarchy context"] });
+        this.serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin project context"] });
+        this.totalUses = this.serviceLocator.projectServices.getUserPreference(this.name, "UsageCount", 0);
+        this.serviceLocator.uiServices.registerCustomEditor(customEditorBuilder);
+    };
+    TSExamplePluginService.prototype.playerStarted = function () {
+        Atomic.print("TSExamplePluginService.playerStarted");
+    };
+    TSExamplePluginService.prototype.menuItemClicked = function (refId) {
+        Atomic.print("TSExamplePluginService.menuItemClicked: " + refId);
+        if (refId == "tsexampleplugin open") {
+            this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
+            this.getWidgets();
+            return true;
+        }
+        return false;
+    };
+    TSExamplePluginService.prototype.hierarchyContextItemClicked = function (node, refid) {
+        Atomic.print("TSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
+        if (refid == "tsexampleplugin hierarchy context") {
+            this.lastObjectName = "node " + node.name;
+            this.showInfobox("Hierarchy Item Selected ", "Node: '" + node.name + "' was selected.");
+            return true;
+        }
+        return false;
+    };
+    TSExamplePluginService.prototype.projectContextItemClicked = function (asset, refid) {
+        Atomic.print("TSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
+        if (refid == "tsexampleplugin project context") {
+            this.lastObjectName = "asset " + asset.name;
+            this.showInfobox("Project Asset Selected", "Asset: '" + asset.name + "' was selected.");
+            return true;
+        }
+        return false;
+    };
+    TSExamplePluginService.prototype.getWidgets = function () {
+        if (!this.extensionWindow) {
+            return;
+        }
+        this.helloLabel = this.extensionWindow.getWidget("example_hello");
+        this.nameField = this.extensionWindow.getWidget("example_name");
+        if (this.lastObjectName) {
+            this.nameField.text = this.lastObjectName;
+            this.lastObjectName = null;
+        }
+    };
+    TSExamplePluginService.prototype.showInfobox = function (title, msg) {
+        var infobox = this.serviceLocator.uiServices.showModalWindow(title, InfoboxTBPath, function (ev) {
+            if (ev.type == Atomic.UI_EVENT_TYPE_CLICK && ev.target.id == "infobox_ok") {
+                infobox.hide();
+                return true;
+            }
+        });
+        var msgLabel = infobox.getWidget("infobox_msg");
+        msgLabel.text = msg;
+    };
+    return TSExamplePluginService;
+}());
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.default = new TSExamplePluginService();

+ 2 - 6
EditorPluginExample/Resources/EditorData/TSExample.plugin.ts

@@ -50,12 +50,8 @@ class CustomEditorBuilder implements Editor.Extensions.ResourceEditorBuilder {
             });
 
             editor.subscribeToEvent("UserPreferencesChangedNotification", (data) => {
-                let prefsPath = ToolCore.toolSystem.project.userPrefsFullPath;
-                if (Atomic.fileSystem.fileExists(prefsPath)) {
-                    // Get a reference to the web client so we can call the load preferences method
-                    const webClient = editor.webView.webClient;
-                    webClient.executeJavaScript(`HOST_loadPreferences("atomic://${prefsPath}");`);
-                }
+                const webClient = editor.webView.webClient;
+                webClient.executeJavaScript("HOST_preferencesChanged();");
             });
 
             return editor;

+ 12 - 2
EditorPluginExample/Resources/EditorData/customEditor.html

@@ -133,6 +133,12 @@
         // Functions exposed to the host editor.  These
         // are hooked in here so that they are available immediately from the host
         function HOST_loadCode(codeUrl) {
+
+            // preferences are available on the window object as stringified JSON strings
+            // in case they are needed
+            var projectPrefs = JSON.parse(window.HOST_Preferences.ProjectPreferences);
+            var applicationPrefs = JSON.parse(window.HOST_Preferences.ApplicationPreferences);
+
             filename = codeUrl.replace("atomic://", "");
             getResource(codeUrl).then((code) => {
                 structure = JSON.parse(code);
@@ -159,8 +165,12 @@
             alert("Resource Deleted: " + path);
         }
 
-        function HOST_loadPreferences(prefUrl) {
-            alert("Prefs Changed: " + prefUrl);
+        function HOST_preferencesChanged() {
+
+            // Updated preferences are available on the window object as stringified JSON strings
+            var projectPrefs = JSON.parse(window.HOST_Preferences.ProjectPreferences);
+            var applicationPrefs = JSON.parse(window.HOST_Preferences.ApplicationPreferences);
+            alert("Prefs Changed.");
         }
     </script>
 

+ 5 - 0
EditorPluginExample/Resources/typings.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "77bfb762a44705b4cb5c65a187d669b9",
+	"FolderImporter": {}
+}

+ 5 - 0
EditorPluginExample/Resources/typings/Atomic.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "9ae1203ac52f042c8601c8bdcd56b6eb",
+	"FolderImporter": {}
+}