Exemplo n.º 1
0
        /// <inheritdoc />
        public override bool OnKeyDown(Keys key)
        {
            // Navigate backward
            if (key == Keys.Backspace)
            {
                OnNavigateBack?.Invoke();
                return(true);
            }

            // Select all
            if (key == Keys.A && ParentWindow.GetKey(Keys.Control))
            {
                SelectAll();
                return(true);
            }

            // Check if sth is selected
            if (HasSelection)
            {
                // Delete selection
                if (key == Keys.Delete)
                {
                    OnDelete?.Invoke(_selection);
                    return(true);
                }

                // Open
                if (key == Keys.Return && _selection.Count == 1)
                {
                    OnOpen?.Invoke(_selection[0]);
                    return(true);
                }

                // Duplicate
                if (key == Keys.D && ParentWindow.GetKey(Keys.Control))
                {
                    DuplicateSelection();
                    return(true);
                }

                // Movement with arrows
                {
                    var         root   = _selection[0];
                    Vector2     size   = root.Size;
                    Vector2     offset = Vector2.Minimum;
                    ContentItem item   = null;
                    if (key == Keys.ArrowUp)
                    {
                        offset = new Vector2(0, -size.Y);
                    }
                    else if (key == Keys.ArrowDown)
                    {
                        offset = new Vector2(0, size.Y);
                    }
                    else if (key == Keys.ArrowRight)
                    {
                        offset = new Vector2(size.X, 0);
                    }
                    else if (key == Keys.ArrowLeft)
                    {
                        offset = new Vector2(-size.X, 0);
                    }
                    if (offset != Vector2.Minimum)
                    {
                        item = GetChildAt(root.Location + size / 2 + offset) as ContentItem;
                    }
                    if (item != null)
                    {
                        OnItemClick(item);
                        return(true);
                    }
                }
            }

            return(base.OnKeyDown(key));
        }
Exemplo n.º 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 = ParentWindow.GetKey(Keys.Control) == false;
                var  singleObject      = _dragActors.Objects.Count == 1;
                if (singleObject)
                {
                    var targetActor = _dragActors.Objects[0].Actor;
                    using (new UndoBlock(Editor.Instance.Undo, targetActor, "Change actor parent"))
                    {
                        targetActor.SetParent(newParent, worldPositionLock);
                        targetActor.OrderInParent = newOrder;
                    }
                }
                else
                {
                    var targetActors = _dragActors.Objects.ConvertAll(x => x.Actor);
                    using (new UndoMultiBlock(Editor.Instance.Undo, targetActors, "Change actors parent"))
                    {
                        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:
                    {
                        // Create actor
                        var model = FlaxEngine.Content.LoadAsync <Model>(item.ID);
                        var actor = ModelActor.New();
                        actor.StaticFlags = Actor.StaticFlags;
                        actor.Name        = item.ShortName;
                        actor.Model       = model;
                        actor.Transform   = Actor.Transform;

                        // Spawn
                        Editor.Instance.SceneEditing.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
                            Editor.Instance.SceneEditing.Spawn(actor, Actor);
                        }

                        break;
                    }

                    case ContentDomain.Prefab:
                    {
                        throw new NotImplementedException("Spawning prefabs");
                    }
                    }
                }

                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
                    Editor.Instance.SceneEditing.Spawn(actor, Actor);
                }

                result = DragDropEffect.Move;
            }

            // Clear cache
            _dragActors?.OnDragDrop();
            _dragAssets?.OnDragDrop();
            _dragActorType?.OnDragDrop();

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

            return(result);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        protected override void OnLeftMouseButtonUp()
        {
            // Skip if was controlling mouse or mouse is not over the area
            if (_prevInput.IsControllingMouse || !Bounds.Contains(ref _viewMousePos))
            {
                return;
            }

            if (TransformGizmo.IsActive)
            {
                // Ensure player is not moving objects
                if (TransformGizmo.ActiveAxis != TransformGizmo.Axis.None)
                {
                    return;
                }
            }
            else
            {
                // For now just pick objects in transform gizmo mode
                return;
            }

            // Get mouse ray and try to hit any object
            var   ray     = MouseRay;
            float closest = float.MaxValue;
            var   hit     = Editor.Instance.Scene.Root.RayCast(ref ray, ref closest);

            // Update selection
            var sceneEditing = Editor.Instance.SceneEditing;

            if (hit != null)
            {
                // For child actor nodes (mesh, link or sth) we need to select it's owning actor node first or any other child node (but not a child actor)
                if (hit is ActorChildNode actorChildNode)
                {
                    var  parentNode         = actorChildNode.ParentNode;
                    bool canChildBeSelected = sceneEditing.Selection.Contains(parentNode);
                    if (!canChildBeSelected)
                    {
                        for (int i = 0; i < parentNode.ChildNodes.Count; i++)
                        {
                            if (sceneEditing.Selection.Contains(parentNode.ChildNodes[i]))
                            {
                                canChildBeSelected = true;
                                break;
                            }
                        }
                    }

                    if (!canChildBeSelected)
                    {
                        // Select parent
                        hit = parentNode;
                    }
                }

                bool addRemove  = ParentWindow.GetKey(Keys.Control);
                bool isSelected = sceneEditing.Selection.Contains(hit);

                if (addRemove)
                {
                    if (isSelected)
                    {
                        sceneEditing.Deselect(hit);
                    }
                    else
                    {
                        sceneEditing.Select(hit, true);
                    }
                }
                else
                {
                    sceneEditing.Select(hit);
                }
            }
            else
            {
                sceneEditing.Deselect();
            }

            // Keep focus
            Focus();

            base.OnLeftMouseButtonUp();
        }