Browse Source

Merge pull request #808 from AtomicGameEngine/TSH-ATOMIC-EDITOREXT

Project based custom editors
JoshEngebretson 9 years ago
parent
commit
72ff45d22c

+ 24 - 8
Script/AtomicEditor/hostExtensions/HostExtensionServices.ts

@@ -400,19 +400,35 @@ export class UIServicesProvider extends ServicesProvider<Editor.HostExtensions.U
     }
     }
 
 
     /**
     /**
- * Disaplays a resource slection window
- * @param  {string} windowText
- * @param  {string} importerType
- * @param  {string} resourceType
- * @param  {function} callback
- * @param  {any} retObject
- * @param  {any} args
- */
+     * Displays a resource slection window
+     * @param  {string} windowText
+     * @param  {string} importerType
+     * @param  {string} resourceType
+     * @param  {function} callback
+     * @param  {any} retObject
+     * @param  {any} args
+     */
     showResourceSelection(windowText: string, importerType: string, resourceType: string, callback: (retObject: any, args: any) => void, args: any = undefined) {
     showResourceSelection(windowText: string, importerType: string, resourceType: string, callback: (retObject: any, args: any) => void, args: any = undefined) {
 
 
         this.modalOps.showResourceSelection(windowText, importerType, resourceType, callback);
         this.modalOps.showResourceSelection(windowText, importerType, resourceType, callback);
     }
     }
 
 
+    /**
+     * Will register a custom editor for a particular file type.
+     * @param  {Editor.Extensions.ResourceEditorBuilder} editorBuilder
+     */
+    registerCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
+        this.mainFrame.resourceframe.resourceEditorProvider.registerCustomEditor(editorBuilder);
+    }
+
+    /**
+     * Will unregister a previously registered editor builder
+     * @param  {Editor.Extensions.ResourceEditorBuilder} editorBuilder
+     */
+    unregisterCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
+        this.mainFrame.resourceframe.resourceEditorProvider.unregisterCustomEditor(editorBuilder);
+    }
+
     /**
     /**
      * Called when a menu item has been clicked
      * Called when a menu item has been clicked
      * @param  {string} refId
      * @param  {string} refId

+ 12 - 1
Script/AtomicEditor/ui/ResourceEditorProvider.ts

@@ -49,10 +49,21 @@ export default class ResourceEditorProvider {
      * Register a custom editor.  These editors will override editors in the standard editor list if
      * Register a custom editor.  These editors will override editors in the standard editor list if
      * they both resolve the ```canHandleResource``` call.
      * they both resolve the ```canHandleResource``` call.
      */
      */
-    registerCustomEditor(editorBuilder) {
+    registerCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
         this.customEditorRegistry.push(editorBuilder);
         this.customEditorRegistry.push(editorBuilder);
     }
     }
 
 
+    /**
+     * Will unregister a previously registered editor builder
+     * @param  {Editor.Extensions.ResourceEditorBuilder} editorBuilder
+     */
+    unregisterCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
+        var index = this.customEditorRegistry.indexOf(editorBuilder, 0);
+        if (index > -1) {
+            this.customEditorRegistry.splice(index, 1);
+        }
+    }
+
     /**
     /**
      * Returns an editor for the provided resource type or null
      * Returns an editor for the provided resource type or null
      */
      */

+ 3 - 1
Script/AtomicEditor/ui/frames/ResourceFrame.ts

@@ -259,7 +259,9 @@ class ResourceFrame extends ScriptWidget {
     handleProjectUnloaded(data) {
     handleProjectUnloaded(data) {
 
 
       for (var i in this.editors) {
       for (var i in this.editors) {
-           this.sendEvent(EditorEvents.EditorResourceClose, { editor: this.editors[i], navigateToAvailableResource: false });
+          var editor = this.editors[i];
+           this.sendEvent(EditorEvents.EditorResourceClose, { editor: editor, navigateToAvailableResource: false });
+           editor.close();
       }
       }
 
 
     }
     }

+ 9 - 2
Script/AtomicEditor/ui/resourceEditors/AbstractTextResourceEditorBuilder.ts

@@ -35,9 +35,16 @@ export abstract class AbstractTextResourceEditorBuilder implements Editor.Extens
         return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
         return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
     }
     }
 
 
-    getEditor(resourceFrame: Atomic.UIWidget, resourcePath: string, tabContainer: Atomic.UITabContainer): Editor.ResourceEditor {
+    /**
+     * Returns the URL to load into the web view.  Override this to return a custom url
+     * @return {string}
+     */
+    getEditorUrl(): string {
+        return `atomic://${ToolCore.toolEnvironment.toolDataDir}CodeEditor/Editor.html`;
+    }
 
 
-        const editor = new Editor.JSResourceEditor(resourcePath, tabContainer);
+    getEditor(resourceFrame: Atomic.UIWidget, resourcePath: string, tabContainer: Atomic.UITabContainer): Editor.ResourceEditor {
+        const editor = new Editor.JSResourceEditor(resourcePath, tabContainer, this.getEditorUrl());
 
 
         // one time subscriptions waiting for the web view to finish loading.  This event
         // 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
         // actually hits the editor instance before we can hook it, so listen to it on the

+ 11 - 0
Script/TypeScript/EditorWork.d.ts

@@ -323,6 +323,17 @@ declare module Editor.HostExtensions {
         showModalError(windowText: string, message: string);
         showModalError(windowText: string, message: string);
         showResourceSelection(windowText: string, importerType: string, resourceType: string, callback: (retObject: any, args: any) => void, args?: any);
         showResourceSelection(windowText: string, importerType: string, resourceType: string, callback: (retObject: any, args: any) => void, args?: any);
 
 
+        /**
+         * Register a custom editor.  These editors will override editors in the standard editor list if
+         * they both resolve the ```canHandleResource``` call.
+         */
+        registerCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder);
+
+        /**
+         * Will unregister a previously registered editor builder
+         * @param  {Editor.Extensions.ResourceEditorBuilder} editorBuilder
+         */
+        unregisterCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder);
     }
     }
 }
 }
 
 

+ 9 - 14
Source/AtomicEditor/Editors/JSResourceEditor.cpp

@@ -51,7 +51,7 @@ using namespace ToolCore;
 namespace AtomicEditor
 namespace AtomicEditor
 {
 {
 
 
-JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UITabContainer *container) :
+JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UITabContainer *container, const String &editorUrl) :
     ResourceEditor(context, fullpath, container)
     ResourceEditor(context, fullpath, container)
 {
 {
 
 
@@ -67,17 +67,7 @@ JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UI
 
 
     layout->AddChild(c);
     layout->AddChild(c);
 
 
-    ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
-    String codeEditorDir = tenv->GetToolDataDir();
-    codeEditorDir += "CodeEditor/Editor.html";
-
-#ifdef ATOMIC_PLATFORM_OSX
-    String url = "file://" + codeEditorDir;
-#else
-    String url = "file:///" + codeEditorDir;
-#endif
-
-    webView_ = new UIWebView(context_, url);
+    webView_ = new UIWebView(context_, editorUrl);
     webClient_ = webView_->GetWebClient();
     webClient_ = webView_->GetWebClient();
     messageHandler_ = new WebMessageHandler(context_);
     messageHandler_ = new WebMessageHandler(context_);
     webClient_->AddMessageHandler(messageHandler_);
     webClient_->AddMessageHandler(messageHandler_);
@@ -158,8 +148,13 @@ void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventD
             {
             {
                 String code = jvalue["payload"].GetString();
                 String code = jvalue["payload"].GetString();
                 String fn = jvalue["filename"].GetString();
                 String fn = jvalue["filename"].GetString();
-                // TODO: determine if we are absolute path or partial path
-                File file(context_, fn, FILE_WRITE);
+
+                // NOTE: We only want to be able save into the resource directory, so parse out the file path and append
+                // it to the resource directory
+                ToolSystem* tsys = GetSubsystem<ToolSystem>();
+                String fullFilePath = tsys->GetProject()->GetProjectPath() + getNormalizedPath(fn);
+
+                File file(context_, fullFilePath, FILE_WRITE);
                 file.Write((void*) code.CString(), code.Length());
                 file.Write((void*) code.CString(), code.Length());
                 file.Close();
                 file.Close();
             }
             }

+ 2 - 2
Source/AtomicEditor/Editors/JSResourceEditor.h

@@ -43,7 +43,7 @@ class JSResourceEditor: public ResourceEditor
 
 
 public:
 public:
 
 
-    JSResourceEditor(Context* context, const String& fullpath, UITabContainer* container);
+    JSResourceEditor(Context* context, const String& fullpath, UITabContainer* container, const String& editorUrl);
 
 
     virtual ~JSResourceEditor();
     virtual ~JSResourceEditor();
 
 
@@ -74,7 +74,7 @@ private:
     void HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData);
     void HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData);
     void HandleDeleteResourceNotification(StringHash eventType, VariantMap& eventData);
     void HandleDeleteResourceNotification(StringHash eventType, VariantMap& eventData);
     void HandleProjectUnloadedNotification(StringHash eventType, VariantMap& eventData);
     void HandleProjectUnloadedNotification(StringHash eventType, VariantMap& eventData);
-    
+
     SharedPtr<UIWebView> webView_;
     SharedPtr<UIWebView> webView_;
     WeakPtr<WebClient> webClient_;
     WeakPtr<WebClient> webClient_;
     WeakPtr<WebMessageHandler> messageHandler_;
     WeakPtr<WebMessageHandler> messageHandler_;