Browse Source

Added a more interesting example of a custom editor

Shaddock Heath 9 years ago
parent
commit
7bc2f8f32d

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

@@ -76,7 +76,7 @@ var CustomEditorBuilder = { }
  * Returns true if this builder can generate an editor for this resource type
  * Returns true if this builder can generate an editor for this resource type
  */
  */
 CustomEditorBuilder.canHandleResource = function(resourcePath) {
 CustomEditorBuilder.canHandleResource = function(resourcePath) {
-    return resourcePath.indexOf("custom.editorjs.txt") > 0;
+    return resourcePath.indexOf("custom.editorjs.json") > 0;
 }
 }
 
 
 /**
 /**

+ 1 - 1
EditorPluginExample/Resources/EditorData/TSExample.plugin.js

@@ -6,7 +6,7 @@ var CustomEditorBuilder = (function () {
     function CustomEditorBuilder() {
     function CustomEditorBuilder() {
     }
     }
     CustomEditorBuilder.prototype.canHandleResource = function (resourcePath) {
     CustomEditorBuilder.prototype.canHandleResource = function (resourcePath) {
-        return resourcePath.indexOf("custom.editor.txt") > 0;
+        return resourcePath.indexOf("custom.editor.json") > 0;
     };
     };
     CustomEditorBuilder.prototype.getNormalizedPath = function (path) {
     CustomEditorBuilder.prototype.getNormalizedPath = function (path) {
         var RESOURCES_MARKER = "resources/";
         var RESOURCES_MARKER = "resources/";

+ 1 - 1
EditorPluginExample/Resources/EditorData/TSExample.plugin.ts

@@ -10,7 +10,7 @@ class CustomEditorBuilder implements Editor.Extensions.ResourceEditorBuilder {
          * Returns true if this builder can generate an editor for this resource type
          * Returns true if this builder can generate an editor for this resource type
          */
          */
         canHandleResource(resourcePath: string) {
         canHandleResource(resourcePath: string) {
-            return resourcePath.indexOf("custom.editor.txt") > 0;
+            return resourcePath.indexOf("custom.editor.json") > 0;
         }
         }
 
 
         /**
         /**

+ 85 - 15
EditorPluginExample/Resources/EditorData/customEditor.html

@@ -10,16 +10,79 @@
 <body>
 <body>
 
 
     <h1>Custom Editor Instance</h1>
     <h1>Custom Editor Instance</h1>
-    <p>This is an example of creating a custom editor.  You can edit the text below and if you hit save, it will replace the source file.</p>
-    <textarea id="source" rows="10" cols="80"></textarea>
+    <p>This is an example of creating a custom editor. You can edit the elements below and if you hit save, it will update the source file.</p>
+
+    <div>
+        <div style="float:left">
+            <select style="width:190px;height:400px" id="npc_list" size="10"></select>
+        </div>
+        <div style="margin-left:200px">
+            <p>Name
+                <br/>
+                <input type="text" id="npc_name" />
+            </p>
+            <p>Conversation Text
+                <br/>
+                <textarea id="convo_text" rows="10" cols="80"></textarea>
+            </p>
+
+            <div>
+                <label>
+                    <input type="checkbox" value="quest_giver" id="quest">&nbsp;Quest Giver?
+                </label>
+            </div>
+        </div>
+    </div>
 
 
     <script>
     <script>
-        var code;
+        var filename;
+        var structure;
+        var currentObject;
+
+        //get a reference to the elements
+        var controls = {
+            list: document.getElementById("npc_list"),
+            npcName: document.getElementById("npc_name"),
+            convoText: document.getElementById("convo_text"),
+            questCheck: document.getElementById("quest")
+        };
+
+        controls.convoText.addEventListener("input", (event) => {
+            if (currentObject) {
+                atomicQueryPromise("editorChange");
+                currentObject.convo = controls.convoText.value;
+            }
+            event.stopPropagation();
+        });
+
+        controls.npcName.addEventListener("input", (event) => {
+            if (currentObject) {
+                atomicQueryPromise("editorChange");
+                currentObject.name = controls.npcName.value;
+                controls.list.options[controls.list.selectedIndex].text = currentObject.name;
+            }
+            event.stopPropagation();
+        });
+
+        controls.questCheck.addEventListener("click", (event) => {
+            if (currentObject) {
+                atomicQueryPromise("editorChange");
+                currentObject.quest_giver = event.currentTarget.checked;
+            }
+            event.stopPropagation();
+        });
+
+        controls.list.addEventListener("change", (event) => {
+            var selName = controls.list.options[controls.list.selectedIndex].text;
+
+            currentObject = structure.npclist.find((el) => el.name == selName);
+            if (currentObject) {
+                controls.convoText.value = currentObject.convo;
+                controls.npcName.value = currentObject.name;
+                controls.questCheck.checked = currentObject.quest_giver;
+            }
+            event.stopPropagation();
 
 
-        //get a reference to the element
-        var txt = document.getElementById("source");
-        txt.addEventListener("input", (event) => {
-            atomicQueryPromise("editorChange");
         });
         });
 
 
         /**
         /**
@@ -33,7 +96,7 @@
                 var queryMessage = message;
                 var queryMessage = message;
 
 
                 // if message is coming in as an object then let's stringify it
                 // if message is coming in as an object then let's stringify it
-                if (typeof (message) != "string") {
+                if (typeof(message) != "string") {
                     queryMessage = JSON.stringify(message);
                     queryMessage = JSON.stringify(message);
                 }
                 }
 
 
@@ -41,7 +104,10 @@
                     request: queryMessage,
                     request: queryMessage,
                     persistent: false,
                     persistent: false,
                     onSuccess: resolve,
                     onSuccess: resolve,
-                    onFailure: (error_code, error_message) => reject({ error_code: error_code, error_message: error_message })
+                    onFailure: (error_code, error_message) => reject({
+                        error_code: error_code,
+                        error_message: error_message
+                    })
                 });
                 });
             });
             });
         }
         }
@@ -66,18 +132,22 @@
 
 
         // Functions exposed to the host editor.  These
         // Functions exposed to the host editor.  These
         // are hooked in here so that they are available immediately from the host
         // are hooked in here so that they are available immediately from the host
-        // and when called will bring in the interop as a promise and call it once
-        // it has been loaded
-        function HOST_loadCode(url) {
-            getResource(url).then((code)=>{
-                txt.value = code;
+        function HOST_loadCode(codeUrl) {
+            filename = codeUrl.replace("atomic://", "");
+            getResource(codeUrl).then((code) => {
+                structure = JSON.parse(code);
+                for (var i = 0; i < structure.npclist.length; i++) {
+                    var opt = document.createElement("OPTION");
+                    controls.list.options.add(opt);
+                    opt.text = structure.npclist[i].name;
+                }
             });
             });
         }
         }
 
 
         function HOST_saveCode() {
         function HOST_saveCode() {
             atomicQueryPromise({
             atomicQueryPromise({
                 message: "editorSaveCode",
                 message: "editorSaveCode",
-                payload: txt.value
+                payload: JSON.stringify(structure, null, 2)
             });
             });
         }
         }
 
 

+ 19 - 0
EditorPluginExample/Resources/Scripts/custom.editor.json

@@ -0,0 +1,19 @@
+{
+  "npclist": [
+    {
+      "name": "Paul",
+      "quest_giver": false,
+      "convo": "Fear is the mind killer."
+    },
+    {
+      "name": "Jessica",
+      "quest_giver": true,
+      "convo": "My son lives!"
+    },
+    {
+      "name": "Chani",
+      "quest_giver": true,
+      "convo": "Tell me of the waters of your homeworld, Usul."
+    }
+  ]
+}

+ 5 - 0
EditorPluginExample/Resources/Scripts/custom.editor.json.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "cb8047ec5b5b44ad0351e4724d46c16d",
+	"JSONImporter": null
+}

+ 0 - 1
EditorPluginExample/Resources/Scripts/custom.editor.txt

@@ -1 +0,0 @@
-# this is a file that will be loaded into a custom editor

+ 0 - 5
EditorPluginExample/Resources/Scripts/custom.editor.txt.asset

@@ -1,5 +0,0 @@
-{
-	"version": 1,
-	"guid": "fa08111fb2e263d4e7792e7188d76e65",
-	"TextImporter": {}
-}

+ 14 - 0
EditorPluginExample/Resources/Scripts/custom.editorjs.json

@@ -0,0 +1,14 @@
+{
+  "npclist": [
+    {
+      "name": "Vlad",
+      "quest_giver": false,
+      "convo": "You must squeeze them."
+    },
+    {
+      "name": "Feyd",
+      "quest_giver": false,
+      "convo": "Who is the little one, a pet perhaps? Will she deserve my special attentions?"
+    }
+  ]
+}

+ 5 - 0
EditorPluginExample/Resources/Scripts/custom.editorjs.json.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "fbd0ca31f7ad8ba64481d5d7071da56c",
+	"JSONImporter": null
+}

+ 0 - 1
EditorPluginExample/Resources/Scripts/custom.editorjs.txt

@@ -1 +0,0 @@
-# this is a file that will be loaded into a custom editor from javascript

+ 0 - 5
EditorPluginExample/Resources/Scripts/custom.editorjs.txt.asset

@@ -1,5 +0,0 @@
-{
-	"version": 1,
-	"guid": "43539d72330826b91d3112a10408a5ad",
-	"TextImporter": {}
-}