コード例 #1
0
        private void Spawn(AssetItem item, SceneGraphNode hit, ref Vector3 hitLocation)
        {
            // TODO: refactor this and dont use ContentDomain but only asset Type for matching

            if (item is BinaryAssetItem binaryAssetItem)
            {
                if (binaryAssetItem.Type == typeof(ParticleSystem))
                {
                    var particleSystem = FlaxEngine.Content.LoadAsync <ParticleSystem>(item.ID);
                    var actor          = ParticleEffect.New();
                    actor.Name           = item.ShortName;
                    actor.ParticleSystem = particleSystem;
                    actor.Position       = PostProcessSpawnedActorLocation(actor, ref hitLocation);
                    Spawn(actor);

                    return;
                }
            }

            switch (item.ItemDomain)
            {
            case ContentDomain.Material:
            {
                if (hit is StaticModelNode.EntryNode meshNode)
                {
                    var material = FlaxEngine.Content.LoadAsync <MaterialBase>(item.ID);
                    using (new UndoBlock(Undo, meshNode.Model, "Change material"))
                        meshNode.Entry.Material = material;
                }
                else if (hit is BoxBrushNode.SideLinkNode brushSurfaceNode)
                {
                    var material = FlaxEngine.Content.LoadAsync <MaterialBase>(item.ID);
                    using (new UndoBlock(Undo, brushSurfaceNode.Brush, "Change material"))
                        brushSurfaceNode.Surface.Material = material;
                }

                break;
            }

            case ContentDomain.Model:
            {
                if (item.TypeName == typeof(SkinnedModel).FullName)
                {
                    var model = FlaxEngine.Content.LoadAsync <SkinnedModel>(item.ID);
                    var actor = AnimatedModel.New();
                    actor.Name         = item.ShortName;
                    actor.SkinnedModel = model;
                    actor.Position     = PostProcessSpawnedActorLocation(actor, ref hitLocation);
                    Spawn(actor);
                }
                else
                {
                    var model = FlaxEngine.Content.LoadAsync <Model>(item.ID);
                    var actor = StaticModel.New();
                    actor.Name     = item.ShortName;
                    actor.Model    = model;
                    actor.Position = PostProcessSpawnedActorLocation(actor, ref hitLocation);
                    Spawn(actor);
                }

                break;
            }

            case ContentDomain.Audio:
            {
                var clip  = FlaxEngine.Content.LoadAsync <AudioClip>(item.ID);
                var actor = AudioSource.New();
                actor.Name     = item.ShortName;
                actor.Clip     = clip;
                actor.Position = PostProcessSpawnedActorLocation(actor, ref hitLocation);
                Spawn(actor);

                break;
            }

            case ContentDomain.Prefab:
            {
                var prefab = FlaxEngine.Content.LoadAsync <Prefab>(item.ID);
                var actor  = PrefabManager.SpawnPrefab(prefab, null);
                actor.Name     = item.ShortName;
                actor.Position = PostProcessSpawnedActorLocation(actor, ref hitLocation);
                Spawn(actor);

                break;
            }

            default: throw new ArgumentOutOfRangeException();
            }
        }
コード例 #2
0
        /// <inheritdoc />
        protected override DragDropEffect OnDragDropHeader(DragData data)
        {
            var result = DragDropEffect.None;

            Actor myActor = Actor;
            Actor newParent;
            int   newOrder = -1;

            // Check if has no actor (only for Root Actor)
            if (myActor == null)
            {
                // Append to the last scene
                var scenes = SceneManager.Scenes;
                if (scenes == null || scenes.Length == 0)
                {
                    throw new InvalidOperationException("No scene loaded.");
                }
                newParent = scenes[scenes.Length - 1];
            }
            else
            {
                newParent = myActor;

                // Use drag positioning to change target parent and index
                if (DragOverMode == DragItemPositioning.Above)
                {
                    if (myActor.HasParent)
                    {
                        newParent = myActor.Parent;
                        newOrder  = myActor.OrderInParent;
                    }
                }
                else if (DragOverMode == DragItemPositioning.Below)
                {
                    if (myActor.HasParent)
                    {
                        newParent = myActor.Parent;
                        newOrder  = myActor.OrderInParent + 1;
                    }
                }
            }
            if (newParent == null)
            {
                throw new InvalidOperationException("Missing parent actor.");
            }

            // Drag actors
            if (_dragActors != null && _dragActors.HasValidDrag)
            {
                bool worldPositionLock = Root.GetKey(Keys.Control) == false;
                var  singleObject      = _dragActors.Objects.Count == 1;
                if (singleObject)
                {
                    var targetActor  = _dragActors.Objects[0].Actor;
                    var customAction = targetActor.HasPrefabLink ? new ReparentAction(targetActor) : null;
                    using (new UndoBlock(ActorNode.Root.Undo, targetActor, "Change actor parent", customAction))
                    {
                        targetActor.SetParent(newParent, worldPositionLock);
                        targetActor.OrderInParent = newOrder;
                    }
                }
                else
                {
                    var targetActors = _dragActors.Objects.ConvertAll(x => x.Actor);
                    var customAction = targetActors.Any(x => x.HasPrefabLink) ? new ReparentAction(targetActors) : null;
                    using (new UndoMultiBlock(ActorNode.Root.Undo, targetActors, "Change actors parent", customAction))
                    {
                        for (int i = 0; i < targetActors.Count; i++)
                        {
                            var targetActor = targetActors[i];
                            targetActor.SetParent(newParent, worldPositionLock);
                            targetActor.OrderInParent = newOrder;
                        }
                    }
                }

                result = DragDropEffect.Move;
            }
            // Drag assets
            else if (_dragAssets != null && _dragAssets.HasValidDrag)
            {
                for (int i = 0; i < _dragAssets.Objects.Count; i++)
                {
                    var item = _dragAssets.Objects[i];

                    switch (item.ItemDomain)
                    {
                    case ContentDomain.Model:
                    {
                        if (item.TypeName == typeof(SkinnedModel).FullName)
                        {
                            // Create actor
                            var model = FlaxEngine.Content.LoadAsync <SkinnedModel>(item.ID);
                            var actor = AnimatedModel.New();
                            actor.StaticFlags  = Actor.StaticFlags;
                            actor.Name         = item.ShortName;
                            actor.SkinnedModel = model;
                            actor.Transform    = Actor.Transform;

                            // Spawn
                            ActorNode.Root.Spawn(actor, Actor);
                        }
                        else
                        {
                            // Create actor
                            var model = FlaxEngine.Content.LoadAsync <Model>(item.ID);
                            var actor = StaticModel.New();
                            actor.StaticFlags = Actor.StaticFlags;
                            actor.Name        = item.ShortName;
                            actor.Model       = model;
                            actor.Transform   = Actor.Transform;

                            // Spawn
                            ActorNode.Root.Spawn(actor, Actor);
                        }

                        break;
                    }

                    case ContentDomain.Other:
                    {
                        if (item.TypeName == typeof(CollisionData).FullName)
                        {
                            // Create actor
                            var actor = MeshCollider.New();
                            actor.StaticFlags   = Actor.StaticFlags;
                            actor.Name          = item.ShortName;
                            actor.CollisionData = FlaxEngine.Content.LoadAsync <CollisionData>(item.ID);
                            actor.Transform     = Actor.Transform;

                            // Spawn
                            ActorNode.Root.Spawn(actor, Actor);
                        }
                        else if (item.TypeName == typeof(ParticleSystem).FullName)
                        {
                            // Create actor
                            var actor = ParticleEffect.New();
                            actor.StaticFlags    = Actor.StaticFlags;
                            actor.Name           = item.ShortName;
                            actor.ParticleSystem = FlaxEngine.Content.LoadAsync <ParticleSystem>(item.ID);
                            actor.Transform      = Actor.Transform;

                            // Spawn
                            ActorNode.Root.Spawn(actor, Actor);
                        }
                        else if (item.TypeName == typeof(SceneAnimation).FullName)
                        {
                            // Create actor
                            var actor = SceneAnimationPlayer.New();
                            actor.StaticFlags = Actor.StaticFlags;
                            actor.Name        = item.ShortName;
                            actor.Animation   = FlaxEngine.Content.LoadAsync <SceneAnimation>(item.ID);
                            actor.Transform   = Actor.Transform;

                            // Spawn
                            ActorNode.Root.Spawn(actor, Actor);
                        }

                        break;
                    }

                    case ContentDomain.Audio:
                    {
                        // Create actor
                        var actor = AudioSource.New();
                        actor.StaticFlags = Actor.StaticFlags;
                        actor.Name        = item.ShortName;
                        actor.Clip        = FlaxEngine.Content.LoadAsync <AudioClip>(item.ID);
                        actor.Transform   = Actor.Transform;

                        // Spawn
                        ActorNode.Root.Spawn(actor, Actor);

                        break;
                    }

                    case ContentDomain.Prefab:
                    {
                        // Create prefab instance
                        var prefab = FlaxEngine.Content.LoadAsync <Prefab>(item.ID);
                        var actor  = PrefabManager.SpawnPrefab(prefab, null);
                        actor.StaticFlags = Actor.StaticFlags;
                        actor.Name        = item.ShortName;
                        actor.Transform   = Actor.Transform;

                        // Spawn
                        ActorNode.Root.Spawn(actor, Actor);

                        break;
                    }
                    }
                }

                result = DragDropEffect.Move;
            }
            // Drag actor type
            else if (_dragActorType != null && _dragActorType.HasValidDrag)
            {
                for (int i = 0; i < _dragActorType.Objects.Count; i++)
                {
                    var item = _dragActorType.Objects[i];

                    // Create actor
                    var actor = FlaxEngine.Object.New(item) as Actor;
                    if (actor == null)
                    {
                        Editor.LogWarning("Failed to spawn actor of type " + item.FullName);
                        continue;
                    }
                    actor.StaticFlags = Actor.StaticFlags;
                    actor.Name        = item.Name;
                    actor.Transform   = Actor.Transform;

                    // Spawn
                    ActorNode.Root.Spawn(actor, Actor);
                }

                result = DragDropEffect.Move;
            }

            // Clear cache
            _dragHandlers.OnDragDrop(null);

            // Check if scene has been modified
            if (result != DragDropEffect.None)
            {
                var node = SceneGraphFactory.FindNode(newParent.ID) as ActorNode;
                node?.TreeNode.Expand();
            }

            return(result);
        }
コード例 #3
0
 private void Spawn(AssetItem item, SceneGraphNode hit, ref Vector2 location, ref Vector3 hitLocation)
 {
     if (item is BinaryAssetItem binaryAssetItem)
     {
         if (binaryAssetItem.Type == typeof(ParticleSystem))
         {
             var particleSystem = FlaxEngine.Content.LoadAsync <ParticleSystem>(item.ID);
             var actor          = ParticleEffect.New();
             actor.Name           = item.ShortName;
             actor.ParticleSystem = particleSystem;
             actor.Position       = PostProcessSpawnedActorLocation(actor, ref hitLocation);
             Spawn(actor);
             return;
         }
         if (typeof(MaterialBase).IsAssignableFrom(binaryAssetItem.Type))
         {
             if (hit is StaticModelNode staticModelNode)
             {
                 var staticModel = (StaticModel)staticModelNode.Actor;
                 var ray         = ConvertMouseToRay(ref location);
                 if (staticModel.IntersectsEntry(ref ray, out _, out _, out var entryIndex))
                 {
                     var material = FlaxEngine.Content.LoadAsync <MaterialBase>(item.ID);
                     using (new UndoBlock(Undo, staticModel, "Change material"))
                         staticModel.SetMaterial(entryIndex, material);
                 }
             }
             return;
         }
         if (typeof(SkinnedModel).IsAssignableFrom(binaryAssetItem.Type))
         {
             var model = FlaxEngine.Content.LoadAsync <SkinnedModel>(item.ID);
             var actor = AnimatedModel.New();
             actor.Name         = item.ShortName;
             actor.SkinnedModel = model;
             actor.Position     = PostProcessSpawnedActorLocation(actor, ref hitLocation);
             Spawn(actor);
             return;
         }
         if (typeof(Model).IsAssignableFrom(binaryAssetItem.Type))
         {
             var model = FlaxEngine.Content.LoadAsync <Model>(item.ID);
             var actor = StaticModel.New();
             actor.Name     = item.ShortName;
             actor.Model    = model;
             actor.Position = PostProcessSpawnedActorLocation(actor, ref hitLocation);
             Spawn(actor);
             return;
         }
         if (typeof(AudioClip).IsAssignableFrom(binaryAssetItem.Type))
         {
             var clip  = FlaxEngine.Content.LoadAsync <AudioClip>(item.ID);
             var actor = AudioSource.New();
             actor.Name     = item.ShortName;
             actor.Clip     = clip;
             actor.Position = PostProcessSpawnedActorLocation(actor, ref hitLocation);
             Spawn(actor);
             return;
         }
         if (typeof(Prefab).IsAssignableFrom(binaryAssetItem.Type))
         {
             var prefab = FlaxEngine.Content.LoadAsync <Prefab>(item.ID);
             var actor  = PrefabManager.SpawnPrefab(prefab, null);
             actor.Name     = item.ShortName;
             actor.Position = PostProcessSpawnedActorLocation(actor, ref hitLocation);
             Spawn(actor);
             return;
         }
     }
 }