Browse Source

Support moving assets via drag/drop

Josh Engebretson 10 years ago
parent
commit
dfbd3289ac

+ 17 - 2
Script/AtomicEditor/ui/frames/ProjectFrame.ts

@@ -255,8 +255,11 @@ class ProjectFrame extends ScriptWidget {
 
             if (data.target.id == "contentcontainerscroll" || container.isAncestorOf(data.target)) {
 
-                asset = this.currentFolder;
+                if (data.target["asset"])
+                  asset = <ToolCore.Asset> data.target["asset"];
 
+                if (!asset || !asset.isFolder)
+                  asset = this.currentFolder;
             }
 
         }
@@ -279,6 +282,7 @@ class ProjectFrame extends ScriptWidget {
             return;
 
         var dragObject = data.dragObject;
+
         if (dragObject.object && dragObject.object.typeName == "Node") {
 
             var node = <Atomic.Node>dragObject.object;
@@ -293,7 +297,6 @@ class ProjectFrame extends ScriptWidget {
             else {
                 var destFilename = Atomic.addTrailingSlash(asset.path);
                 destFilename += node.name + ".prefab";
-
                 var file = new Atomic.File(destFilename, Atomic.FILE_WRITE);
                 node.saveXML(file);
                 file.close();
@@ -303,6 +306,16 @@ class ProjectFrame extends ScriptWidget {
 
             return;
 
+        } else if (dragObject.object && dragObject.object.typeName == "Asset") {
+
+            var dragAsset = <ToolCore.Asset> dragObject.object;
+
+            // get the folder we dragged on
+            var destPath = Atomic.addTrailingSlash(asset.path);
+
+            dragAsset.move(destPath + dragAsset.name + dragAsset.extension);
+
+            return false;
         }
 
         // dropped some files?
@@ -426,12 +439,14 @@ class ProjectFrame extends ScriptWidget {
         image.rect = [0, 0, 12, 12];
         image.gravity = Atomic.UI_GRAVITY_RIGHT;
         blayout.addChild(image);
+        image["asset"] = asset;
 
         button.id = asset.guid;
         button.layoutParams = lp;
         button.fontDescription = fd;
         button.text = asset.name + asset.extension;
         button.skinBg = "TBButton.flat";
+        button["asset"] = asset;
         blayout['assetButton'] = button;
         blayout.addChild(button);
 

+ 7 - 0
Script/TypeScript/AtomicWork.d.ts

@@ -253,6 +253,13 @@ declare module ToolCore {
 
     }
 
+    export interface AssetMovedEvent {
+
+        asset: Asset;
+        oldPath: string;
+
+    }
+
 
     export interface PlatformChangedEvent {
 

+ 21 - 0
Source/ToolCore/Assets/Asset.cpp

@@ -367,6 +367,27 @@ bool Asset::SetPath(const String& path)
 
 }
 
+bool Asset::Move(const String& newPath)
+{
+    if (importer_.Null())
+        return false;
+
+    String oldPath = path_;
+
+    bool result = importer_->Move(newPath);
+
+    if (result)
+    {
+        VariantMap eventData;
+        eventData[AssetMoved::P_ASSET] = this;
+        eventData[AssetMoved::P_OLDPATH] = oldPath;
+        SendEvent(E_ASSETMOVED, eventData);
+    }
+
+    return result;
+
+}
+
 bool Asset::Rename(const String& newName)
 {
     if (importer_.Null())

+ 3 - 0
Source/ToolCore/Assets/Asset.h

@@ -73,6 +73,9 @@ public:
     /// Rename the asset, which depending on the asset type may be nontrivial
     bool Rename(const String& newName);
 
+    /// Move the asset, which depending on the asset type may be nontrivial
+    bool Move(const String& newPath);
+
     bool IsFolder() const { return isFolder_; }
 
     // load .asset

+ 7 - 0
Source/ToolCore/Assets/AssetEvents.h

@@ -33,4 +33,11 @@ EVENT(E_ASSETRENAMED, AssetRenamed)
     PARAM(P_ASSET, Asset);                  // asset ptr
 }
 
+EVENT(E_ASSETMOVED, AssetMoved)
+{
+    PARAM(P_ASSET, Asset);                  // asset ptr
+    PARAM(P_OLDPATH, OldPath);                  // string
+
+}
+
 }

+ 30 - 9
Source/ToolCore/Assets/AssetImporter.cpp

@@ -53,28 +53,30 @@ bool AssetImporter::SaveSettingsInternal(JSONValue& jsonRoot)
     return true;
 }
 
-bool AssetImporter::Rename(const String& newName)
+bool AssetImporter::Move(const String& newPath)
 {
-    String pathName, fileName, ext;
-
-    SplitPath(asset_->path_, pathName, fileName, ext);
-
-    String newPath = pathName + newName + ext;
-
     FileSystem* fs = GetSubsystem<FileSystem>();
 
-    if (fs->FileExists(newPath) || fs->DirExists(newPath))
+    if (newPath == asset_->path_)
         return false;
 
-    // rename asset first, ahead of the filesystem watcher
     String oldPath = asset_->path_;
+    String oldName = asset_->name_;
+
+    String pathName, newName, ext;
 
+    SplitPath(newPath, pathName, newName, ext);
+
+    // rename asset first, ahead of the filesystem watcher, so the assetdatabase doesn't see a new asset
     asset_->name_ = newName;
     asset_->path_ = newPath;
 
     // first rename the .asset file
     if (!fs->Rename(oldPath + ".asset", newPath + ".asset"))
     {
+        asset_->name_ = oldName;
+        asset_->path_ = oldPath;
+
         LOGERRORF("Unable to rename asset: %s to %s", GetNativePath(oldPath + ".asset").CString(), GetNativePath(newPath + ".asset").CString());
         return false;
     }
@@ -82,6 +84,9 @@ bool AssetImporter::Rename(const String& newName)
     // now rename the asset file itself
     if (!fs->Rename(oldPath, newPath))
     {
+        asset_->name_ = oldName;
+        asset_->path_ = oldPath;
+
         // restore .asset
         fs->Rename(newPath + ".asset", oldPath + ".asset");
 
@@ -90,6 +95,22 @@ bool AssetImporter::Rename(const String& newName)
     }
 
     return true;
+}
+
+bool AssetImporter::Rename(const String& newName)
+{
+    String pathName, fileName, ext;
+
+    SplitPath(asset_->path_, pathName, fileName, ext);
+
+    String newPath = pathName + newName + ext;
+
+    FileSystem* fs = GetSubsystem<FileSystem>();
+
+    if (fs->FileExists(newPath) || fs->DirExists(newPath))
+        return false;
+
+    return Move(newPath);
 
 }
 

+ 1 - 0
Source/ToolCore/Assets/AssetImporter.h

@@ -49,6 +49,7 @@ public:
     virtual Node* InstantiateNode(Node* parent, const String& name) { return 0; }
 
     virtual bool Rename(const String& newName);
+    virtual bool Move(const String& newPath);
 
 protected: