Kaynağa Gözat

Cleanup drag-instantiate code. Use mouse raycast position if possible.

Lasse Öörni 9 yıl önce
ebeveyn
işleme
e048afa496

+ 4 - 15
bin/Data/Scripts/Editor/EditorResourceBrowser.as

@@ -1001,7 +1001,6 @@ void HandleBrowserFileDragEnd(StringHash eventType, VariantMap& eventData)
     if (element !is null)
     if (element !is null)
         return;
         return;
 
 
-    Node@ createdNode = null;
     if (browserDragFile.resourceType == RESOURCE_TYPE_MATERIAL)
     if (browserDragFile.resourceType == RESOURCE_TYPE_MATERIAL)
     {
     {
         StaticModel@ model = cast<StaticModel>(GetDrawableAtMousePostion());
         StaticModel@ model = cast<StaticModel>(GetDrawableAtMousePostion());
@@ -1012,11 +1011,11 @@ void HandleBrowserFileDragEnd(StringHash eventType, VariantMap& eventData)
     }
     }
     else if (browserDragFile.resourceType == RESOURCE_TYPE_PREFAB)
     else if (browserDragFile.resourceType == RESOURCE_TYPE_PREFAB)
     {
     {
-        createdNode = LoadNode(browserDragFile.GetFullPath());
+        LoadNode(browserDragFile.GetFullPath(), null, true);
     }
     }
     else if (browserDragFile.resourceType == RESOURCE_TYPE_MODEL)
     else if (browserDragFile.resourceType == RESOURCE_TYPE_MODEL)
     {
     {
-        createdNode = CreateNode(REPLICATED);
+        Node@ createdNode = CreateNode(REPLICATED, true);
         Model@ model = cache.GetResource("Model", browserDragFile.resourceKey);
         Model@ model = cache.GetResource("Model", browserDragFile.resourceKey);
         if (model.skeleton.numBones > 0)
         if (model.skeleton.numBones > 0)
         {
         {
@@ -1028,18 +1027,8 @@ void HandleBrowserFileDragEnd(StringHash eventType, VariantMap& eventData)
             StaticModel@ sm = createdNode.CreateComponent("StaticModel");
             StaticModel@ sm = createdNode.CreateComponent("StaticModel");
             sm.model = model;
             sm.model = model;
         }
         }
-    }
-
-    if (createdNode !is null)
-    {
-        Drawable@ drawable = GetFirstDrawable(createdNode);
-        if (drawable !is null)
-        {
-            BoundingBox aabb = drawable.worldBoundingBox;
-            Vector3 aabbBottomCenter(aabb.center.x, aabb.min.y, aabb.center.z);
-            Vector3 offset = aabbBottomCenter - createdNode.worldPosition;
-            createdNode.worldPosition = createdNode.worldPosition - offset;
-        }
+        
+        AdjustNodePositionByAABB(createdNode);
     }
     }
 
 
     browserDragFile = null;
     browserDragFile = null;

+ 21 - 19
bin/Data/Scripts/Editor/EditorScene.as

@@ -289,14 +289,14 @@ bool SaveSceneWithExistingName()
         return SaveScene(editorScene.fileName);
         return SaveScene(editorScene.fileName);
 }
 }
 
 
-Node@ CreateNode(CreateMode mode)
+Node@ CreateNode(CreateMode mode, bool raycastToMouse = false)
 {
 {
     Node@ newNode = null;
     Node@ newNode = null;
     if (editNode !is null)
     if (editNode !is null)
         newNode = editNode.CreateChild("", mode);
         newNode = editNode.CreateChild("", mode);
     else
     else
         newNode = editorScene.CreateChild("", mode);
         newNode = editorScene.CreateChild("", mode);
-    newNode.worldPosition = GetNewNodePosition();
+    newNode.worldPosition = GetNewNodePosition(raycastToMouse);
 
 
     // Create an undo action for the create
     // Create an undo action for the create
     CreateNodeAction action;
     CreateNodeAction action;
@@ -352,7 +352,7 @@ void CreateLoadedComponent(Component@ component)
     FocusComponent(component);
     FocusComponent(component);
 }
 }
 
 
-Node@ LoadNode(const String&in fileName, Node@ parent = null)
+Node@ LoadNode(const String&in fileName, Node@ parent = null, bool raycastToMouse = false)
 {
 {
     if (fileName.empty)
     if (fileName.empty)
         return null;
         return null;
@@ -375,11 +375,7 @@ Node@ LoadNode(const String&in fileName, Node@ parent = null)
     // Before instantiating, add object's resource path if necessary
     // Before instantiating, add object's resource path if necessary
     SetResourcePath(GetPath(fileName), true, true);
     SetResourcePath(GetPath(fileName), true, true);
 
 
-    Ray cameraRay = camera.GetScreenRay(0.5, 0.5); // Get ray at view center
-    Vector3 position, normal;
-    GetSpawnPosition(cameraRay, newNodeDistance, position, normal, 0, true);
-
-    Node@ newNode = InstantiateNodeFromFile(file, position, Quaternion(), 1, parent, instantiateMode);
+    Node@ newNode = InstantiateNodeFromFile(file, GetNewNodePosition(raycastToMouse), Quaternion(), 1, parent, instantiateMode);
     if (newNode !is null)
     if (newNode !is null)
     {
     {
         FocusNode(newNode);
         FocusNode(newNode);
@@ -414,17 +410,8 @@ Node@ InstantiateNodeFromFile(File@ file, const Vector3& position, const Quatern
     if (newNode !is null)
     if (newNode !is null)
     {
     {
         newNode.scale = newNode.scale * scaleMod;
         newNode.scale = newNode.scale * scaleMod;
-        if (alignToAABBBottom)
-        {
-            Drawable@ drawable = GetFirstDrawable(newNode);
-            if (drawable !is null)
-            {
-                BoundingBox aabb = drawable.worldBoundingBox;
-                Vector3 aabbBottomCenter(aabb.center.x, aabb.min.y, aabb.center.z);
-                Vector3 offset = aabbBottomCenter - newNode.worldPosition;
-                newNode.worldPosition = newNode.worldPosition - offset;
-            }
-        }
+        
+        AdjustNodePositionByAABB(newNode);
 
 
         // Create an undo action for the load
         // Create an undo action for the load
         CreateNodeAction action;
         CreateNodeAction action;
@@ -441,6 +428,21 @@ Node@ InstantiateNodeFromFile(File@ file, const Vector3& position, const Quatern
     return newNode;
     return newNode;
 }
 }
 
 
+void AdjustNodePositionByAABB(Node@ newNode)
+{
+    if (alignToAABBBottom)
+    {
+        Drawable@ drawable = GetFirstDrawable(newNode);
+        if (drawable !is null)
+        {
+            BoundingBox aabb = drawable.worldBoundingBox;
+            Vector3 aabbBottomCenter(aabb.center.x, aabb.min.y, aabb.center.z);
+            Vector3 offset = aabbBottomCenter - newNode.worldPosition;
+            newNode.worldPosition = newNode.worldPosition - offset;
+        }
+    }
+}
+
 bool SaveNode(const String&in fileName)
 bool SaveNode(const String&in fileName)
 {
 {
     if (fileName.empty)
     if (fileName.empty)

+ 2 - 2
bin/Data/Scripts/Editor/EditorView.as

@@ -1877,13 +1877,13 @@ void ViewRaycast(bool mouseClick)
     }
     }
 }
 }
 
 
-Vector3 GetNewNodePosition()
+Vector3 GetNewNodePosition(bool raycastToMouse = false)
 {
 {
     if (newNodeMode == NEW_NODE_IN_CENTER)
     if (newNodeMode == NEW_NODE_IN_CENTER)
         return Vector3(0, 0, 0);
         return Vector3(0, 0, 0);
     if (newNodeMode == NEW_NODE_RAYCAST)
     if (newNodeMode == NEW_NODE_RAYCAST)
     {
     {
-        Ray cameraRay = camera.GetScreenRay(0.5, 0.5);
+        Ray cameraRay = raycastToMouse ? GetActiveViewportCameraRay() : camera.GetScreenRay(0.5, 0.5);
         Vector3 position, normal;
         Vector3 position, normal;
         if (GetSpawnPosition(cameraRay, camera.farClip, position, normal, 0, false))
         if (GetSpawnPosition(cameraRay, camera.farClip, position, normal, 0, false))
             return position;
             return position;