// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; using System.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Primitives; using osu.Framework.Input; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; using osu.Game.Graphics.UserInterface; using osu.Game.Rulesets.Edit; using osuTK; using osuTK.Input; namespace osu.Game.Screens.Edit.Compose.Components { /// /// A container which provides a "blueprint" display of items. /// Includes selection and manipulation support via a . /// public abstract class BlueprintContainer : CompositeDrawable, IKeyBindingHandler where T : class { protected DragBox DragBox { get; private set; } public Container> SelectionBlueprints { get; private set; } protected SelectionHandler SelectionHandler { get; private set; } private readonly Dictionary> blueprintMap = new Dictionary>(); [Resolved(canBeNull: true)] private IPositionSnapProvider snapProvider { get; set; } [Resolved(CanBeNull = true)] private IEditorChangeHandler changeHandler { get; set; } protected readonly BindableList SelectedItems = new BindableList(); protected BlueprintContainer() { RelativeSizeAxes = Axes.Both; } [BackgroundDependencyLoader] private void load() { SelectedItems.CollectionChanged += (selectedObjects, args) => { switch (args.Action) { case NotifyCollectionChangedAction.Add: foreach (object o in args.NewItems) SelectionBlueprints.FirstOrDefault(b => b.Item == o)?.Select(); break; case NotifyCollectionChangedAction.Remove: foreach (object o in args.OldItems) SelectionBlueprints.FirstOrDefault(b => b.Item == o)?.Deselect(); break; } }; SelectionHandler = CreateSelectionHandler(); SelectionHandler.DeselectAll = deselectAll; SelectionHandler.SelectedItems.BindTo(SelectedItems); AddRangeInternal(new[] { DragBox = CreateDragBox(selectBlueprintsFromDragRectangle), SelectionHandler, SelectionBlueprints = CreateSelectionBlueprintContainer(), SelectionHandler.CreateProxy(), DragBox.CreateProxy().With(p => p.Depth = float.MinValue) }); } protected virtual Container> CreateSelectionBlueprintContainer() => new Container> { RelativeSizeAxes = Axes.Both }; /// /// Creates a which outlines items and handles movement of selections. /// protected abstract SelectionHandler CreateSelectionHandler(); /// /// Creates a for a specific item. /// /// The item to create the overlay for. [CanBeNull] protected virtual SelectionBlueprint CreateBlueprintFor(T item) => null; protected virtual DragBox CreateDragBox(Action performSelect) => new DragBox(performSelect); /// /// Whether this component is in a state where items outside a drag selection should be deselected. If false, selection will only be added to. /// protected virtual bool AllowDeselectionDuringDrag => true; protected override bool OnMouseDown(MouseDownEvent e) { bool selectionPerformed = performMouseDownActions(e); bool movementPossible = prepareSelectionMovement(); // check if selection has occurred if (selectionPerformed) { // only unmodified right click should show context menu bool shouldShowContextMenu = e.Button == MouseButton.Right && !e.ShiftPressed && !e.AltPressed && !e.SuperPressed; // stop propagation if not showing context menu return !shouldShowContextMenu; } // even if a selection didn't occur, a drag event may still move the selection. return e.Button == MouseButton.Left && movementPossible; } protected SelectionBlueprint ClickedBlueprint { get; private set; } protected override bool OnClick(ClickEvent e) { if (e.Button == MouseButton.Right) return false; // store for double-click handling ClickedBlueprint = SelectionHandler.SelectedBlueprints.FirstOrDefault(b => b.IsHovered); // Deselection should only occur if no selected blueprints are hovered // A special case for when a blueprint was selected via this click is added since OnClick() may occur outside the item and should not trigger deselection if (endClickSelection(e) || ClickedBlueprint != null) return true; deselectAll(); return true; } protected override bool OnDoubleClick(DoubleClickEvent e) { if (e.Button == MouseButton.Right) return false; // ensure the blueprint which was hovered for the first click is still the hovered blueprint. if (ClickedBlueprint == null || SelectionHandler.SelectedBlueprints.FirstOrDefault(b => b.IsHovered) != ClickedBlueprint) return false; return true; } protected override void OnMouseUp(MouseUpEvent e) { // Special case for when a drag happened instead of a click Schedule(() => { endClickSelection(e); clickSelectionBegan = false; isDraggingBlueprint = false; }); finishSelectionMovement(); } protected override bool OnDragStart(DragStartEvent e) { if (e.Button == MouseButton.Right) return false; if (movementBlueprints != null) { isDraggingBlueprint = true; changeHandler?.BeginChange(); return true; } if (DragBox.HandleDrag(e)) { DragBox.Show(); return true; } return false; } protected override void OnDrag(DragEvent e) { if (e.Button == MouseButton.Right) return; if (DragBox.State == Visibility.Visible) DragBox.HandleDrag(e); moveCurrentSelection(e); } protected override void OnDragEnd(DragEndEvent e) { if (e.Button == MouseButton.Right) return; if (isDraggingBlueprint) { DragOperationCompleted(); changeHandler?.EndChange(); } if (DragBox.State == Visibility.Visible) DragBox.Hide(); } /// /// Called whenever a drag operation completes, before any change transaction is committed. /// protected virtual void DragOperationCompleted() { } protected override bool OnKeyDown(KeyDownEvent e) { switch (e.Key) { case Key.Escape: if (!SelectionHandler.SelectedBlueprints.Any()) return false; deselectAll(); return true; } return false; } public bool OnPressed(KeyBindingPressEvent e) { if (e.Repeat) return false; switch (e.Action) { case PlatformAction.SelectAll: SelectAll(); return true; } return false; } public void OnReleased(KeyBindingReleaseEvent e) { } #region Blueprint Addition/Removal protected virtual void AddBlueprintFor(T item) { if (blueprintMap.ContainsKey(item)) return; var blueprint = CreateBlueprintFor(item); if (blueprint == null) return; blueprintMap[item] = blueprint; blueprint.Selected += OnBlueprintSelected; blueprint.Deselected += OnBlueprintDeselected; SelectionBlueprints.Add(blueprint); if (SelectionHandler.SelectedItems.Contains(item)) blueprint.Select(); OnBlueprintAdded(blueprint.Item); } protected void RemoveBlueprintFor(T item) { if (!blueprintMap.Remove(item, out var blueprint)) return; blueprint.Deselect(); blueprint.Selected -= OnBlueprintSelected; blueprint.Deselected -= OnBlueprintDeselected; SelectionBlueprints.Remove(blueprint); if (movementBlueprints?.Contains(blueprint) == true) finishSelectionMovement(); OnBlueprintRemoved(blueprint.Item); } /// /// Called after an item's blueprint has been added. /// /// The item for which the blueprint has been added. protected virtual void OnBlueprintAdded(T item) { } /// /// Called after an item's blueprint has been removed. /// /// The item for which the blueprint has been removed. protected virtual void OnBlueprintRemoved(T item) { } /// /// Retrieves an item's blueprint. /// /// The item to retrieve the blueprint of. /// The blueprint. protected SelectionBlueprint GetBlueprintFor(T item) => blueprintMap[item]; #endregion #region Selection /// /// Whether a blueprint was selected by a previous click event. /// private bool clickSelectionBegan; /// /// Attempts to select any hovered blueprints. /// /// The input event that triggered this selection. /// Whether a selection was performed. private bool performMouseDownActions(MouseButtonEvent e) { // Iterate from the top of the input stack (blueprints closest to the front of the screen first). // Priority is given to already-selected blueprints. foreach (SelectionBlueprint blueprint in SelectionBlueprints.AliveChildren.Reverse().OrderByDescending(b => b.IsSelected)) { if (!blueprint.IsHovered) continue; return clickSelectionBegan = SelectionHandler.MouseDownSelectionRequested(blueprint, e); } return false; } /// /// Finishes the current blueprint selection. /// /// The mouse event which triggered end of selection. /// Whether a click selection was active. private bool endClickSelection(MouseButtonEvent e) { if (!clickSelectionBegan && !isDraggingBlueprint) { // if a selection didn't occur, we may want to trigger a deselection. if (e.ControlPressed && e.Button == MouseButton.Left) { // Iterate from the top of the input stack (blueprints closest to the front of the screen first). // Priority is given to already-selected blueprints. foreach (SelectionBlueprint blueprint in SelectionBlueprints.AliveChildren.Reverse().OrderByDescending(b => b.IsSelected)) { if (!blueprint.IsHovered) continue; return clickSelectionBegan = SelectionHandler.MouseUpSelectionRequested(blueprint, e); } } return false; } return true; } /// /// Select all masks in a given rectangle selection area. /// /// The rectangle to perform a selection on in screen-space coordinates. private void selectBlueprintsFromDragRectangle(RectangleF rect) { foreach (var blueprint in SelectionBlueprints) { // only run when utmost necessary to avoid unnecessary rect computations. bool isValidForSelection() => blueprint.IsAlive && blueprint.IsPresent && rect.Contains(blueprint.ScreenSpaceSelectionPoint); switch (blueprint.State) { case SelectionState.NotSelected: if (isValidForSelection()) blueprint.Select(); break; case SelectionState.Selected: if (AllowDeselectionDuringDrag && !isValidForSelection()) blueprint.Deselect(); break; } } } /// /// Selects all s. /// protected virtual void SelectAll() { // Scheduled to allow the change in lifetime to take place. Schedule(() => SelectionBlueprints.ToList().ForEach(m => m.Select())); } /// /// Deselects all selected s. /// private void deselectAll() => SelectionHandler.SelectedBlueprints.ToList().ForEach(m => m.Deselect()); protected virtual void OnBlueprintSelected(SelectionBlueprint blueprint) { SelectionHandler.HandleSelected(blueprint); SelectionBlueprints.ChangeChildDepth(blueprint, 1); } protected virtual void OnBlueprintDeselected(SelectionBlueprint blueprint) { SelectionBlueprints.ChangeChildDepth(blueprint, 0); SelectionHandler.HandleDeselected(blueprint); } #endregion #region Selection Movement private Vector2[] movementBlueprintOriginalPositions; private SelectionBlueprint[] movementBlueprints; private bool isDraggingBlueprint; /// /// Attempts to begin the movement of any selected blueprints. /// /// Whether a movement is possible. private bool prepareSelectionMovement() { if (!SelectionHandler.SelectedBlueprints.Any()) return false; // Any selected blueprint that is hovered can begin the movement of the group, however only the first item (according to SortForMovement) is used for movement. // A special case is added for when a click selection occurred before the drag if (!clickSelectionBegan && !SelectionHandler.SelectedBlueprints.Any(b => b.IsHovered)) return false; // Movement is tracked from the blueprint of the earliest item, since it only makes sense to distance snap from that item movementBlueprints = SortForMovement(SelectionHandler.SelectedBlueprints).ToArray(); movementBlueprintOriginalPositions = movementBlueprints.Select(m => m.ScreenSpaceSelectionPoint).ToArray(); return true; } /// /// Apply sorting of selected blueprints before performing movement. Generally used to surface the "main" item to the beginning of the collection. /// /// The blueprints to be moved. /// Sorted blueprints. protected virtual IEnumerable> SortForMovement(IReadOnlyList> blueprints) => blueprints; /// /// Moves the current selected blueprints. /// /// The defining the movement event. /// Whether a movement was active. private bool moveCurrentSelection(DragEvent e) { if (movementBlueprints == null) return false; Debug.Assert(movementBlueprintOriginalPositions != null); Vector2 distanceTravelled = e.ScreenSpaceMousePosition - e.ScreenSpaceMouseDownPosition; if (snapProvider != null) { // check for positional snap for every object in selection (for things like object-object snapping) for (int i = 0; i < movementBlueprintOriginalPositions.Length; i++) { Vector2 originalPosition = movementBlueprintOriginalPositions[i]; var testPosition = originalPosition + distanceTravelled; var positionalResult = snapProvider.FindSnappedPositionAndTime(testPosition, SnapType.NearbyObjects); if (positionalResult.ScreenSpacePosition == testPosition) continue; var delta = positionalResult.ScreenSpacePosition - movementBlueprints[i].ScreenSpaceSelectionPoint; // attempt to move the objects, and abort any time based snapping if we can. if (SelectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprints[i], delta))) return true; } } // if no positional snapping could be performed, try unrestricted snapping from the earliest // item in the selection. // The final movement position, relative to movementBlueprintOriginalPosition. Vector2 movePosition = movementBlueprintOriginalPositions.First() + distanceTravelled; // Retrieve a snapped position. var result = snapProvider?.FindSnappedPositionAndTime(movePosition, ~SnapType.NearbyObjects); if (result == null) { return SelectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprints.First(), movePosition - movementBlueprints.First().ScreenSpaceSelectionPoint)); } return ApplySnapResult(movementBlueprints, result); } protected virtual bool ApplySnapResult(SelectionBlueprint[] blueprints, SnapResult result) => SelectionHandler.HandleMovement(new MoveSelectionEvent(blueprints.First(), result.ScreenSpacePosition - blueprints.First().ScreenSpaceSelectionPoint)); /// /// Finishes the current movement of selected blueprints. /// /// Whether a movement was active. private bool finishSelectionMovement() { if (movementBlueprints == null) return false; movementBlueprintOriginalPositions = null; movementBlueprints = null; return true; } #endregion } }