Эх сурвалжийг харах

Merge pull request #279 from AtomicGameEngine/JME-ATOMIC-275

Support dragging assets on hierarchy frame, cleanups for node instant…
JoshEngebretson 10 жил өмнө
parent
commit
085fa7e46d

+ 52 - 37
Script/AtomicEditor/ui/frames/HierarchyFrame.ts

@@ -44,6 +44,9 @@ class HierarchyFrame extends Atomic.UIWidget {
 
         this.subscribeToEvent(EditorEvents.ActiveSceneChange, (data) => this.handleActiveSceneChanged(data));
 
+        // handle dropping on hierarchy, moving node, dropping prefabs, etc
+        this.subscribeToEvent(this.hierList.rootList, "DragEnded", (data) => this.handleDragEnded(data));
+
         this.subscribeToEvent(EditorEvents.SceneClosed, (ev: EditorEvents.SceneClosedEvent) => {
 
             if (ev.scene == this.scene) {
@@ -96,7 +99,7 @@ class HierarchyFrame extends Atomic.UIWidget {
 
             if (ev.serializable.typeName == "Node") {
 
-                var node = <Atomic.Node> ev.serializable;
+                var node = <Atomic.Node>ev.serializable;
 
                 var itemID = this.nodeIDToItemID[node.id];
 
@@ -166,7 +169,7 @@ class HierarchyFrame extends Atomic.UIWidget {
         // clear selected node
         this.sendEvent(EditorEvents.ActiveNodeChange, { node: null });
 
-        this.scene = <Atomic.Scene> data.scene;
+        this.scene = <Atomic.Scene>data.scene;
 
         this.populate();
 
@@ -215,40 +218,6 @@ class HierarchyFrame extends Atomic.UIWidget {
                     var dragObject = new Atomic.UIDragObject(node, node.name.length ? "Node: " + node.name : "Node: (Anonymous)");
                     this.hierList.rootList.dragObject = dragObject;
 
-                    // handle dropping on hierarchy, moving node
-                    this.subscribeToEvent(this.hierList.rootList, "DragEnded", (ev: Atomic.DragEndedEvent) => {
-
-                        var dragNode = <Atomic.Node> ev.dragObject.object;
-
-                        var dropNode: Atomic.Node = this.scene.getNode(Number(this.hierList.hoverItemID));
-
-                        if (!dropNode) {
-                            return;
-                        }
-
-                        // can't drop on self
-                        if (dragNode == dropNode) {
-                            return;
-                        }
-
-                        // check if dropping on child of ourselves
-                        var parent = dropNode.parent;
-
-                        while (parent) {
-
-                            if (parent == dragNode) {
-                                return;
-                            }
-
-                            parent = parent.parent;
-
-                        }
-
-                        // move it
-                        dropNode.addChild(dragNode);
-
-                    });
-
                 }
 
             }
@@ -283,7 +252,7 @@ class HierarchyFrame extends Atomic.UIWidget {
 
             if (id == "hierList_") {
 
-                var list = <Atomic.UISelectList> data.target;
+                var list = <Atomic.UISelectList>data.target;
 
                 var selectedId = Number(list.selectedItemID);
                 var node = this.scene.getNode(selectedId);
@@ -390,6 +359,52 @@ class HierarchyFrame extends Atomic.UIWidget {
 
     }
 
+    handleDragEnded(ev: Atomic.DragEndedEvent) {
+
+        var typeName = ev.dragObject.object.typeName;
+
+        var dropNode: Atomic.Node = this.scene.getNode(Number(this.hierList.hoverItemID));
+
+        if (!dropNode) return;
+
+        if (typeName == "Node") {
+
+            var dragNode = <Atomic.Node>ev.dragObject.object;
+
+            if (dragNode.scene != this.scene) {
+                return;
+            }
+
+            // can't drop on self
+            if (dragNode == dropNode) {
+                return;
+            }
+
+            // check if dropping on child of ourselves
+            var parent = dropNode.parent;
+
+            while (parent) {
+
+                if (parent == dragNode) {
+                    return;
+                }
+
+                parent = parent.parent;
+
+            }
+
+            // move it
+            dropNode.addChild(dragNode);
+
+        } else if (typeName == "Asset") {
+
+            var asset = <ToolCore.Asset>ev.dragObject.object;
+            asset.instantiateNode(dropNode, asset.name);
+
+        }
+
+    }
+
 }
 
 export = HierarchyFrame;

+ 1 - 68
Source/AtomicEditor/Editors/SceneEditor3D/SceneView3D.cpp

@@ -35,14 +35,6 @@
 
 #include <ToolCore/Assets/Asset.h>
 #include <ToolCore/Assets/AssetDatabase.h>
-#include <ToolCore/Assets/ModelImporter.h>
-#include <ToolCore/Assets/PrefabImporter.h>
-#include <ToolCore/Assets/SpriterImporter.h>
-#include <ToolCore/Assets/TextureImporter.h>
-
-#include <Atomic/Atomic2D/Sprite2D.h>
-#include <Atomic/Atomic2D/AnimationSet2D.h>
-#include <Atomic/Atomic2D/AnimatedSprite2D.h>
 
 #include "../../EditorMode/AEEditorEvents.h"
 
@@ -483,64 +475,7 @@ void SceneView3D::HandleDragEnterWidget(StringHash eventType, VariantMap& eventD
     if (object->GetType() == Asset::GetTypeStatic())
     {
         Asset* asset = (Asset*) object;
-        AssetImporter* importer = asset->GetImporter();
-
-        if (!importer)
-            return;
-
-        StringHash importerType = importer->GetType();
-
-        if (importerType == PrefabImporter::GetTypeStatic())
-        {
-            dragNode_ = scene_->CreateChild(asset->GetName());
-            PrefabComponent* pc = dragNode_->CreateComponent<PrefabComponent>();
-            pc->SetPrefabGUID(asset->GetGUID());
-        }
-        else if (importerType == ModelImporter::GetTypeNameStatic())
-        {
-            dragNode_ = scene_->CreateChild();
-
-            SharedPtr<File> file(new File(context_, asset->GetCachePath()));
-            SharedPtr<XMLFile> xml(new XMLFile(context_));
-
-            if (!xml->Load(*file))
-                return;
-
-            dragNode_->LoadXML(xml->GetRoot());
-            dragNode_->SetName(asset->GetName());
-        }
-        else if (importerType == SpriterImporter::GetTypeNameStatic())
-        {
-            AnimationSet2D* animationSet = GetSubsystem<ResourceCache>()->GetResource<AnimationSet2D>(asset->GetPath());
-
-            String animationName;
-
-            if (animationSet && animationSet->GetNumAnimations())
-            {
-                animationName = animationSet->GetAnimation(0)->GetName();
-            }
-
-            dragNode_ = scene_->CreateChild(asset->GetName());
-
-            AnimatedSprite2D* sprite = dragNode_->CreateComponent<AnimatedSprite2D>();
-
-            if (!animationName.Length())
-                sprite->SetAnimationSet(animationSet);
-            else
-                sprite->SetAnimation(animationSet, animationName);
-
-        }
-        else if (importerType == TextureImporter::GetTypeNameStatic())
-        {
-            dragNode_ = scene_->CreateChild(asset->GetName());
-
-            Sprite2D* spriteGraphic = GetSubsystem<ResourceCache>()->GetResource<Sprite2D>(asset->GetPath());
-
-            StaticSprite2D* sprite = dragNode_->CreateComponent<StaticSprite2D>();
-
-            sprite->SetSprite(spriteGraphic);
-        }
-
+        dragNode_ = asset->InstantiateNode(scene_, asset->GetName());
 
         if (dragNode_.NotNull())
         {
@@ -549,8 +484,6 @@ void SceneView3D::HandleDragEnterWidget(StringHash eventType, VariantMap& eventD
             UpdateDragNode(pos.x_, pos.y_);
         }
 
-
-
         //LOGINFOF("Dropped %s : %s on SceneView3D", asset->GetPath().CString(), asset->GetGUID().CString());
     }
 

+ 2 - 2
Source/ThirdParty/TurboBadger/image/tb_image_manager.cpp

@@ -143,7 +143,7 @@ TBImage TBImageManager::GetImage(const char *filename)
 			m_frag_manager.FreeFragment(fragment);
 			image_rep = nullptr;
 		}
-		TBDebugOut(image_rep ? "TBImageManager - Loaded new image.\n" : "TBImageManager - Loading image failed.\n");
+        // TBDebugOut(image_rep ? "TBImageManager - Loaded new image.\n" : "TBImageManager - Loading image failed.\n");
 	}
 	return TBImage(image_rep);
 }
@@ -158,7 +158,7 @@ void TBImageManager::RemoveImageRep(TBImageRep *image_rep)
 	}
 	m_image_rep_hash.Remove(image_rep->hash_key);
 	image_rep->image_manager = nullptr;
-	TBDebugOut("TBImageManager - Removed image.\n");
+    //TBDebugOut("TBImageManager - Removed image.\n");
 }
 
 void TBImageManager::OnContextLost()

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

@@ -361,4 +361,16 @@ Resource* Asset::GetResource(const String &typeName)
     return 0;
 }
 
+Node* Asset::InstantiateNode(Node* parent, const String& name)
+{
+    if (!parent)
+        return 0;
+
+    if (importer_)
+        return importer_->InstantiateNode(parent, name);
+
+    return 0;
+
+}
+
 }

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

@@ -3,6 +3,7 @@
 
 #include <Atomic/Core/Object.h>
 #include <Atomic/Resource/Resource.h>
+#include <Atomic/Scene/Node.h>
 
 #include "AssetImporter.h"
 
@@ -68,6 +69,9 @@ public:
     // save .asset
     bool Save();
 
+    /// Instantiate a node from the asset
+    Node* InstantiateNode(Node* parent, const String& name);
+
 
 private:
 

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

@@ -3,6 +3,7 @@
 
 #include <Atomic/Core/Object.h>
 #include <Atomic/Resource/JSONFile.h>
+#include <Atomic/Scene/Node.h>
 
 using namespace Atomic;
 
@@ -38,6 +39,9 @@ public:
 
     bool RequiresCacheFile() const { return requiresCacheFile_; }
 
+    /// Instantiate a node from the asset
+    virtual Node* InstantiateNode(Node* parent, const String& name) { return 0; }
+
 protected:
 
     virtual bool Import() { return true; }

+ 16 - 0
Source/ToolCore/Assets/ModelImporter.cpp

@@ -11,6 +11,7 @@
 #include <Atomic/Atomic3D/Model.h>
 
 #include <Atomic/Resource/ResourceCache.h>
+#include <Atomic/Resource/XMLFile.h>
 
 #include "../Import/OpenAssetImporter.h"
 
@@ -367,5 +368,20 @@ Resource* ModelImporter::GetResource(const String& typeName)
 
 }
 
+Node* ModelImporter::InstantiateNode(Node* parent, const String& name)
+{
+    SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
+    SharedPtr<XMLFile> xml(new XMLFile(context_));
+
+    if (!xml->Load(*file))
+        return 0;
+
+    Node* node = parent->CreateChild(name);
+
+    node->LoadXML(xml->GetRoot());
+    node->SetName(asset_->GetName());
+
+    return node;
+}
 
 }

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

@@ -67,6 +67,9 @@ public:
 
     AnimationImportInfo* GetAnimationInfo(unsigned index) { return animationInfo_[index]; }
 
+    /// Instantiate a node from the asset
+    Node* InstantiateNode(Node* parent, const String& name);
+
 protected:
 
     bool Import();

+ 7 - 0
Source/ToolCore/Assets/PrefabImporter.cpp

@@ -172,5 +172,12 @@ bool PrefabImporter::SaveSettingsInternal()
     return true;
 }
 
+Node* PrefabImporter::InstantiateNode(Node* parent, const String& name)
+{
+    Node* node = parent->CreateChild(asset_->GetName());
+    PrefabComponent* pc = node->CreateComponent<PrefabComponent>();
+    pc->SetPrefabGUID(asset_->GetGUID());
+    return node;
+}
 
 }

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

@@ -24,6 +24,9 @@ public:
 
     virtual bool Preload();
 
+    /// Instantiate a node from the asset
+    Node* InstantiateNode(Node* parent, const String& name);
+
 protected:
 
     bool Import();

+ 28 - 0
Source/ToolCore/Assets/SpriterImporter.cpp

@@ -2,7 +2,9 @@
 #include <Atomic/Resource/ResourceCache.h>
 #include <Atomic/Resource/Image.h>
 
+#include <Atomic/Atomic2D/Sprite2D.h>
 #include <Atomic/Atomic2D/AnimationSet2D.h>
+#include <Atomic/Atomic2D/AnimatedSprite2D.h>
 
 #include "Asset.h"
 #include "AssetDatabase.h"
@@ -61,6 +63,32 @@ Resource* SpriterImporter::GetResource(const String& typeName)
 
 }
 
+Node* SpriterImporter::InstantiateNode(Node* parent, const String& name)
+{
+
+    AnimationSet2D* animationSet = (AnimationSet2D*) GetResource();
+
+    if (!animationSet)
+        return 0;
+
+    String animationName;
+
+    if (animationSet->GetNumAnimations())
+    {
+        animationName = animationSet->GetAnimation(0)->GetName();
+    }
 
+    Node* node = parent->CreateChild(name);
+
+    AnimatedSprite2D* sprite = node->CreateComponent<AnimatedSprite2D>();
+
+    if (!animationName.Length())
+        sprite->SetAnimationSet(animationSet);
+    else
+        sprite->SetAnimation(animationSet, animationName);
+
+    return node;
+
+}
 
 }

+ 3 - 1
Source/ToolCore/Assets/SpriterImporter.h

@@ -17,7 +17,9 @@ public:
 
     virtual void SetDefaults();
 
-    Resource* GetResource(const String& typeName);
+    Resource* GetResource(const String& typeName = String::EMPTY);
+
+    Node* InstantiateNode(Node* parent, const String& name);
 
 protected:
 

+ 18 - 0
Source/ToolCore/Assets/TextureImporter.cpp

@@ -1,6 +1,8 @@
 
 #include <Atomic/Resource/ResourceCache.h>
 #include <Atomic/Resource/Image.h>
+#include <Atomic/Atomic2D/Sprite2D.h>
+#include <Atomic/Atomic2D/StaticSprite2D.h>
 
 #include "Asset.h"
 #include "AssetDatabase.h"
@@ -79,5 +81,21 @@ Resource* TextureImporter::GetResource(const String& typeName)
 
 }
 
+Node* TextureImporter::InstantiateNode(Node* parent, const String& name)
+{
+
+    Node* node = parent->CreateChild(name);
+
+    Sprite2D* spriteGraphic = GetSubsystem<ResourceCache>()->GetResource<Sprite2D>(asset_->GetPath());
+
+    StaticSprite2D* sprite = node->CreateComponent<StaticSprite2D>();
+
+    sprite->SetSprite(spriteGraphic);
+
+    return node;
+
+}
+
+
 
 }

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

@@ -18,6 +18,7 @@ public:
     virtual void SetDefaults();
 
     Resource* GetResource(const String& typeName = String::EMPTY);
+    Node* InstantiateNode(Node* parent, const String& name);
 
 protected: