Browse Source

Added delete, rename, and project unloaded events to the native side so it can route these events to the web view

Shaddock Heath 9 years ago
parent
commit
95858eb29c

+ 21 - 0
Source/Atomic/Resource/ResourceEvents.h

@@ -74,6 +74,27 @@ EVENT(E_RESOURCEBACKGROUNDLOADED, ResourceBackgroundLoaded)
     PARAM(P_SUCCESS, Success);                      // bool
     PARAM(P_RESOURCE, Resource);                    // Resource pointer
 }
+    
+/// Resource was renamed
+EVENT(E_RENAMERESOURCENOTIFICATION, RenameResourceNotification)
+{
+    PARAM(P_RESOURCEPATH, Path);                    // String
+    PARAM(P_NEWRESOURCEPATH, NewPath);              // String
+    PARAM(P_NEWNAME, NewName);                      // String
+    PARAM(P_RESOURCE, Asset);                       // Resource pointer
+}
+
+/// Resource was deleted
+EVENT(E_DELETERESOURCENOTIFICATION, DeleteResourceNotification)
+{
+    PARAM(P_RESOURCEPATH, Path);                    // String
+}
+
+/// Project was unloaded
+EVENT(E_PROJECTUNLOADEDNOTIFICATION, ProjecUnloadedNotification)
+{
+}
+    
 
 /// Language changed.
 EVENT(E_CHANGELANGUAGE, ChangeLanguage)

+ 44 - 8
Source/AtomicEditor/Editors/JSResourceEditor.cpp

@@ -27,6 +27,7 @@
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/ResourceCache.h>
 #include <Atomic/Resource/JSONFile.h>
+#include <Atomic/Resource/ResourceEvents.h>
 
 #include <Atomic/Core/CoreEvents.h>
 #include <AtomicJS/Javascript/JSVM.h>
@@ -84,16 +85,56 @@ JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UI
     SubscribeToEvent(webClient_, E_WEBVIEWLOADEND, HANDLER(JSResourceEditor, HandleWebViewLoadEnd));
     SubscribeToEvent(messageHandler_, E_WEBMESSAGE, HANDLER(JSResourceEditor, HandleWebMessage));
 
+    SubscribeToEvent(E_RENAMERESOURCENOTIFICATION, HANDLER(JSResourceEditor, HandleRenameResourceNotification));
+    SubscribeToEvent(E_DELETERESOURCENOTIFICATION, HANDLER(JSResourceEditor, HandleDeleteResourceNotification));
+    SubscribeToEvent(E_PROJECTUNLOADEDNOTIFICATION, HANDLER(JSResourceEditor, HandleProjectUnloadedNotification));
 
     c->AddChild(webView_->GetInternalWidget());
 
 }
-
+    
 JSResourceEditor::~JSResourceEditor()
 {
 
 }
-
+   
+String getNormalizedPath(const String& path)
+{
+    // 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.
+    const String& RESOURCES_MARKER = "resources/";
+    return path.SubstringUTF8(path.ToLower().Find(RESOURCES_MARKER));
+}
+    
+void JSResourceEditor::HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData)
+{
+    using namespace RenameResourceNotification;
+    const String& newPath = eventData[P_NEWRESOURCEPATH].GetString();
+    const String& path = eventData[P_RESOURCEPATH].GetString();
+    
+    webClient_->ExecuteJavaScript(ToString("HOST_resourceRenamed(\"%s\",\"%s\");", getNormalizedPath(path).CString(), getNormalizedPath(newPath).CString()));
+    
+    if (fullpath_.Compare(path) == 0) {
+        fullpath_ = newPath;
+        SetModified(modified_);
+    }
+}
+    
+void JSResourceEditor::HandleDeleteResourceNotification(StringHash eventType, VariantMap& eventData)
+{
+    using namespace DeleteResourceNotification;
+    const String& path = eventData[P_RESOURCEPATH].GetString();
+    
+    webClient_->ExecuteJavaScript(ToString("HOST_resourceDeleted(\"%s\");", getNormalizedPath(path).CString()));
+}
+    
+void JSResourceEditor::HandleProjectUnloadedNotification(StringHash eventType, VariantMap& eventData)
+{
+    webClient_->ExecuteJavaScript("HOST_projectUnloaded();");
+}
+    
 void JSResourceEditor::HandleWebViewLoadEnd(StringHash eventType, VariantMap& eventData)
 {
     // need to wait until we get an editor load complete message since we could
@@ -110,13 +151,8 @@ void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventD
     const String& EDITOR_SAVE_FILE = "editorSaveFile";
     const String& EDITOR_LOAD_COMPLETE = "editorLoadComplete";
     
-    const String& RESOURCES_MARKER = "resources/";
+    String normalizedPath = getNormalizedPath(fullpath_);
     
-    // 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
-    // correct.
-    String normalizedPath = fullpath_.SubstringUTF8(fullpath_.ToLower().Find(RESOURCES_MARKER));
     WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
 
     if (request == EDITOR_CHANGE)

+ 5 - 1
Source/AtomicEditor/Editors/JSResourceEditor.h

@@ -60,7 +60,7 @@ public:
     void SetFocus();
 
     bool Save();
-
+    
 private:
 
     void HandleWebViewLoadEnd(StringHash eventType, VariantMap& eventData);
@@ -68,6 +68,10 @@ private:
 
     bool BeautifyJavascript(const char* source, String& output);
 
+    void HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData);
+    void HandleDeleteResourceNotification(StringHash eventType, VariantMap& eventData);
+    void HandleProjectUnloadedNotification(StringHash eventType, VariantMap& eventData);
+    
     SharedPtr<UIWebView> webView_;
     WeakPtr<WebClient> webClient_;
     WeakPtr<WebMessageHandler> messageHandler_;

+ 17 - 1
Source/AtomicEditor/Editors/ResourceEditor.cpp

@@ -155,6 +155,8 @@ ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabCo
     container_->GetContentRoot()->AddChild(rootContentWidget_);
 
     SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
+    SubscribeToEvent(E_RENAMERESOURCENOTIFICATION, HANDLER(ResourceEditor, HandleRenameResourceNotification));
+    
 }
 
 ResourceEditor::~ResourceEditor()
@@ -178,6 +180,14 @@ void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventDa
     */
 }
 
+void ResourceEditor::HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData)
+{
+    using namespace RenameResourceNotification;
+    const String& newPath = eventData[P_NEWRESOURCEPATH].GetString();
+    fullpath_ = newPath;
+    SetModified(modified_);
+}
+    
 void ResourceEditor::RequestClose()
 {
     editorTabLayout_->RequestClose();
@@ -218,5 +228,11 @@ void ResourceEditor::SetModified(bool modified)
         button_->SetText(filename.CString());
     }
 }
-
+    
+    
+void ResourceEditor::ProjectUnloaded() {}
+    
+void ResourceEditor::Delete() {}
+    
+    
 }

+ 4 - 1
Source/AtomicEditor/Editors/ResourceEditor.h

@@ -65,6 +65,9 @@ public:
     virtual void Redo() {}
 
     virtual bool Save() { return true; }
+    
+    virtual void ProjectUnloaded();
+    virtual void Delete();
 
     UIWidget* GetRootContentWidget() { return rootContentWidget_; }
 
@@ -89,7 +92,7 @@ protected:
 private:
 
     void HandleFileChanged(StringHash eventType, VariantMap& eventData);
-
+    void HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData);
 };
 
 }