// 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.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.Input; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; using osu.Game.Graphics.UserInterface; using osu.Game.Input.Bindings; 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 partial class BlueprintContainer : CompositeDrawable, IKeyBindingHandler, IKeyBindingHandler where T : class { protected DragBox DragBox { get; private set; } public Container> SelectionBlueprints { get; private set; } public 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 += (_, args) => { switch (args.Action) { case NotifyCollectionChangedAction.Add: Debug.Assert(args.NewItems != null); foreach (object o in args.NewItems) { if (blueprintMap.TryGetValue((T)o, out var blueprint)) blueprint.Select(); } break; case NotifyCollectionChangedAction.Remove: Debug.Assert(args.OldItems != null); foreach (object o in args.OldItems) { if (blueprintMap.TryGetValue((T)o, out var blueprint)) blueprint.Deselect(); } break; } }; SelectionHandler = CreateSelectionHandler(); SelectionHandler.SelectedItems.BindTo(SelectedItems); AddRangeInternal(new[] { DragBox = CreateDragBox(), 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() => new DragBox(); protected override bool OnMouseDown(MouseDownEvent e) { bool selectionPerformed = performMouseDownActions(e); bool movementPossible = prepareSelectionMovement(e); // 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; doubleClickHandled = true; return true; } protected override void OnMouseUp(MouseUpEvent e) { // Special case for when a drag happened instead of a click Schedule(() => { endClickSelection(e); clickSelectionHandled = false; doubleClickHandled = false; isDraggingBlueprint = false; wasDragStarted = false; }); finishSelectionMovement(); } private MouseButtonEvent lastDragEvent; protected override bool OnDragStart(DragStartEvent e) { if (e.Button == MouseButton.Right) return false; lastDragEvent = e; wasDragStarted = true; if (movementBlueprints != null) { isDraggingBlueprint = true; changeHandler?.BeginChange(); return true; } DragBox.HandleDrag(e); DragBox.Show(); return true; } protected override void OnDrag(DragEvent e) { lastDragEvent = e; moveCurrentSelection(e); } protected override void OnDragEnd(DragEndEvent e) { lastDragEvent = null; if (isDraggingBlueprint) { DragOperationCompleted(); changeHandler?.EndChange(); } DragBox.Hide(); } protected override void Update() { base.Update(); if (lastDragEvent != null && DragBox.State == Visibility.Visible) { lastDragEvent.Target = this; DragBox.HandleDrag(lastDragEvent); UpdateSelectionFromDragBox(); } } /// /// 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) { } public bool OnPressed(KeyBindingPressEvent e) { if (e.Repeat) return false; switch (e.Action) { case GlobalAction.Back: if (SelectedItems.Count > 0) { DeselectAll(); return true; } break; } 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, true); 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 clickSelectionHandled; /// /// Whether a blueprint was double-clicked since last mouse down. /// private bool doubleClickHandled; /// /// Whether the selected blueprint(s) were already selected on mouse down. Generally used to perform selection cycling on mouse up in such a case. /// private bool selectedBlueprintAlreadySelectedOnMouseDown; /// /// Sorts the supplied by the order of preference when making a selection. /// Blueprints at the start of the list will be prioritised over later items if the selection requested is ambiguous due to spatial overlap. /// protected virtual IEnumerable> ApplySelectionOrder(IEnumerable> blueprints) => blueprints.Reverse(); /// /// 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.Where(b => b.IsSelected)) { if (runForBlueprint(blueprint)) return true; } foreach (SelectionBlueprint blueprint in ApplySelectionOrder(SelectionBlueprints.AliveChildren)) { if (runForBlueprint(blueprint)) return true; } return false; bool runForBlueprint(SelectionBlueprint blueprint) { if (!blueprint.IsHovered) return false; selectedBlueprintAlreadySelectedOnMouseDown = blueprint.State == SelectionState.Selected; clickSelectionHandled = SelectionHandler.MouseDownSelectionRequested(blueprint, e); return true; } } /// /// 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 already handled a selection, double-click, or drag, we don't want to perform a mouse up / click action. if (clickSelectionHandled || doubleClickHandled || isDraggingBlueprint) return true; if (e.Button != MouseButton.Left) return false; if (e.ControlPressed) { // if a selection didn't occur, we may want to trigger a deselection. // 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.Where(b => b.IsHovered).OrderByDescending(b => b.IsSelected)) return clickSelectionHandled = SelectionHandler.MouseUpSelectionRequested(blueprint, e); return false; } if (!wasDragStarted && selectedBlueprintAlreadySelectedOnMouseDown && SelectedItems.Count == 1) { // If a click occurred and was handled by the currently selected blueprint but didn't result in a drag, // cycle between other blueprints which are also under the cursor. // The depth of blueprints is constantly changing (see above where selected blueprints are brought to the front). // For this logic, we want a stable sort order so we can correctly cycle, thus using the blueprintMap instead. IEnumerable> cyclingSelectionBlueprints = ApplySelectionOrder(blueprintMap.Values); // If there's already a selection, let's start from the blueprint after the selection. cyclingSelectionBlueprints = cyclingSelectionBlueprints.SkipWhile(b => !b.IsSelected).Skip(1); // Add the blueprints from before the selection to the end of the enumerable to allow for cyclic selection. cyclingSelectionBlueprints = cyclingSelectionBlueprints.Concat(ApplySelectionOrder(blueprintMap.Values).TakeWhile(b => !b.IsSelected)); foreach (SelectionBlueprint blueprint in cyclingSelectionBlueprints) { if (!blueprint.IsHovered) continue; // We are performing a mouse up, but selection handlers perform selection on mouse down, so we need to call that instead. return clickSelectionHandled = SelectionHandler.MouseDownSelectionRequested(blueprint, e); } } return false; } /// /// Select all blueprints in a selection area specified by . /// protected virtual void UpdateSelectionFromDragBox() { var quad = DragBox.Box.ScreenSpaceDrawQuad; foreach (var blueprint in SelectionBlueprints) { switch (blueprint.State) { case SelectionState.Selected: // Selection is preserved even after blueprint becomes dead. if (!quad.Contains(blueprint.ScreenSpaceSelectionPoint)) blueprint.Deselect(); break; case SelectionState.NotSelected: if (blueprint.IsSelectable && quad.Contains(blueprint.ScreenSpaceSelectionPoint)) blueprint.Select(); break; } } } /// /// Select all currently-present items. /// protected abstract void SelectAll(); /// /// Deselect all selected items. /// protected void DeselectAll() => SelectedItems.Clear(); protected virtual void OnBlueprintSelected(SelectionBlueprint blueprint) { SelectionHandler.HandleSelected(blueprint); SelectionBlueprints.ChangeChildDepth(blueprint, 1); } protected virtual void OnBlueprintDeselected(SelectionBlueprint blueprint) { if (SelectionBlueprints.Contains(blueprint)) SelectionBlueprints.ChangeChildDepth(blueprint, 0); SelectionHandler.HandleDeselected(blueprint); } #endregion #region Selection Movement private Vector2[][] movementBlueprintsOriginalPositions; private SelectionBlueprint[] movementBlueprints; /// /// Whether a blueprint is currently being dragged. /// private bool isDraggingBlueprint; /// /// Whether a drag operation was started at all. /// private bool wasDragStarted; /// /// Attempts to begin the movement of any selected blueprints. /// /// The defining the beginning of a movement. /// Whether a movement is possible. private bool prepareSelectionMovement(MouseDownEvent e) { if (e.Button == MouseButton.Right) return false; 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 (!clickSelectionHandled && !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(); movementBlueprintsOriginalPositions = movementBlueprints.Select(m => m.ScreenSpaceSnapPoints).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(movementBlueprintsOriginalPositions != null); Vector2 distanceTravelled = e.ScreenSpaceMousePosition - e.ScreenSpaceMouseDownPosition; if (snapProvider != null) { for (int i = 0; i < movementBlueprints.Length; i++) { if (checkSnappingBlueprintToNearbyObjects(movementBlueprints[i], distanceTravelled, movementBlueprintsOriginalPositions[i])) 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 = movementBlueprintsOriginalPositions.First().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); } /// /// Check for positional snap for given blueprint. /// /// The blueprint to check for snapping. /// Distance travelled since start of dragging action. /// The snap positions of blueprint before start of dragging action. /// Whether an object to snap to was found. private bool checkSnappingBlueprintToNearbyObjects(SelectionBlueprint blueprint, Vector2 distanceTravelled, Vector2[] originalPositions) { var currentPositions = blueprint.ScreenSpaceSnapPoints; for (int i = 0; i < originalPositions.Length; i++) { Vector2 originalPosition = originalPositions[i]; var testPosition = originalPosition + distanceTravelled; var positionalResult = snapProvider.FindSnappedPositionAndTime(testPosition, SnapType.NearbyObjects); if (positionalResult.ScreenSpacePosition == testPosition) continue; var delta = positionalResult.ScreenSpacePosition - currentPositions[i]; // attempt to move the objects, and abort any time based snapping if we can. if (SelectionHandler.HandleMovement(new MoveSelectionEvent(blueprint, delta))) return true; } return false; } 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; movementBlueprintsOriginalPositions = null; movementBlueprints = null; return true; } #endregion } }