// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; 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.Framework.Timing; using osu.Game.Rulesets.Edit; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects.Drawables; using osuTK; using osuTK.Input; namespace osu.Game.Screens.Edit.Compose.Components { /// /// A container which provides a "blueprint" display of hitobjects. /// Includes selection and manipulation support via a . /// public abstract class BlueprintContainer : CompositeDrawable, IKeyBindingHandler { public event Action> SelectionChanged; protected DragBox DragBox { get; private set; } private SelectionBlueprintContainer selectionBlueprints; private SelectionHandler selectionHandler; [Resolved] private IAdjustableClock adjustableClock { get; set; } [Resolved] private EditorBeatmap beatmap { get; set; } private readonly BindableList selectedHitObjects = new BindableList(); [Resolved(canBeNull: true)] private IDistanceSnapProvider snapProvider { get; set; } protected BlueprintContainer() { RelativeSizeAxes = Axes.Both; } [BackgroundDependencyLoader] private void load() { selectionHandler = CreateSelectionHandler(); selectionHandler.DeselectAll = deselectAll; AddRangeInternal(new[] { DragBox = CreateDragBox(select), selectionHandler, selectionBlueprints = CreateSelectionBlueprintContainer(), DragBox.CreateProxy().With(p => p.Depth = float.MinValue) }); foreach (var obj in beatmap.HitObjects) AddBlueprintFor(obj); selectedHitObjects.BindTo(beatmap.SelectedHitObjects); selectedHitObjects.ItemsAdded += objects => { foreach (var o in objects) selectionBlueprints.FirstOrDefault(b => b.HitObject == o)?.Select(); }; selectedHitObjects.ItemsRemoved += objects => { foreach (var o in objects) selectionBlueprints.FirstOrDefault(b => b.HitObject == o)?.Deselect(); }; } protected virtual SelectionBlueprintContainer CreateSelectionBlueprintContainer() => new SelectionBlueprintContainer { RelativeSizeAxes = Axes.Both }; protected override void LoadComplete() { base.LoadComplete(); beatmap.HitObjectAdded += AddBlueprintFor; beatmap.HitObjectRemoved += removeBlueprintFor; } /// /// Creates a which outlines s and handles movement of selections. /// protected virtual SelectionHandler CreateSelectionHandler() => new SelectionHandler(); /// /// Creates a for a specific . /// /// The to create the overlay for. protected virtual SelectionBlueprint CreateBlueprintFor(HitObject hitObject) => null; protected virtual DragBox CreateDragBox(Action performSelect) => new DragBox(performSelect); protected override bool OnMouseDown(MouseDownEvent e) { beginClickSelection(e); return e.Button == MouseButton.Left; } protected override bool OnClick(ClickEvent e) { if (e.Button == MouseButton.Right) return false; // 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 hitobject and should not trigger deselection if (endClickSelection() || selectionHandler.SelectedBlueprints.Any(b => b.IsHovered)) return true; deselectAll(); return true; } protected override bool OnDoubleClick(DoubleClickEvent e) { if (e.Button == MouseButton.Right) return false; SelectionBlueprint clickedBlueprint = selectionHandler.SelectedBlueprints.FirstOrDefault(b => b.IsHovered); if (clickedBlueprint == null) return false; adjustableClock?.Seek(clickedBlueprint.HitObject.StartTime); return true; } protected override bool OnMouseUp(MouseUpEvent e) { // Special case for when a drag happened instead of a click Schedule(() => endClickSelection()); return e.Button == MouseButton.Left; } protected override bool OnDragStart(DragStartEvent e) { if (e.Button == MouseButton.Right) return false; if (beginSelectionMovement()) return true; if (DragBox.HandleDrag(e)) { DragBox.Show(); return true; } return false; } protected override bool OnDrag(DragEvent e) { if (e.Button == MouseButton.Right) return false; if (DragBox.State == Visibility.Visible) return DragBox.HandleDrag(e); return moveCurrentSelection(e); } protected override bool OnDragEnd(DragEndEvent e) { if (e.Button == MouseButton.Right) return false; if (DragBox.State == Visibility.Visible) { DragBox.Hide(); selectionHandler.UpdateVisibility(); return true; } return finishSelectionMovement(); } protected override bool OnKeyDown(KeyDownEvent e) { switch (e.Key) { case Key.Escape: if (!selectionHandler.SelectedBlueprints.Any()) return false; deselectAll(); return true; } return false; } protected override bool OnKeyUp(KeyUpEvent e) => false; public bool OnPressed(PlatformAction action) { switch (action.ActionType) { case PlatformActionType.SelectAll: selectAll(); return true; } return false; } public bool OnReleased(PlatformAction action) => false; #region Blueprint Addition/Removal private void removeBlueprintFor(HitObject hitObject) { var blueprint = selectionBlueprints.SingleOrDefault(m => m.HitObject == hitObject); if (blueprint == null) return; blueprint.Deselect(); blueprint.Selected -= onBlueprintSelected; blueprint.Deselected -= onBlueprintDeselected; selectionBlueprints.Remove(blueprint); } protected virtual void AddBlueprintFor(HitObject hitObject) { var blueprint = CreateBlueprintFor(hitObject); if (blueprint == null) return; blueprint.Selected += onBlueprintSelected; blueprint.Deselected += onBlueprintDeselected; selectionBlueprints.Add(blueprint); } #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. private void beginClickSelection(MouseButtonEvent e) { Debug.Assert(!clickSelectionBegan); // Deselections are only allowed for control + left clicks bool allowDeselection = e.ControlPressed && e.Button == MouseButton.Left; // Todo: This is probably incorrectly disallowing multiple selections on stacked objects if (!allowDeselection && selectionHandler.SelectedBlueprints.Any(s => s.IsHovered)) return; foreach (SelectionBlueprint blueprint in selectionBlueprints.AliveChildren) { if (blueprint.IsHovered) { selectionHandler.HandleSelectionRequested(blueprint, e.CurrentState); clickSelectionBegan = true; break; } } } /// /// Finishes the current blueprint selection. /// /// Whether a click selection was active. private bool endClickSelection() { if (!clickSelectionBegan) return false; clickSelectionBegan = 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 select(RectangleF rect) { foreach (var blueprint in selectionBlueprints) { if (blueprint.IsAlive && blueprint.IsPresent && rect.Contains(blueprint.SelectionPoint)) blueprint.Select(); else blueprint.Deselect(); } } /// /// Selects all s. /// private void selectAll() { selectionBlueprints.ToList().ForEach(m => m.Select()); selectionHandler.UpdateVisibility(); } /// /// Deselects all selected s. /// private void deselectAll() => selectionHandler.SelectedBlueprints.ToList().ForEach(m => m.Deselect()); private void onBlueprintSelected(SelectionBlueprint blueprint) { selectionHandler.HandleSelected(blueprint); selectionBlueprints.ChangeChildDepth(blueprint, 1); beatmap.SelectedHitObjects.Add(blueprint.HitObject); SelectionChanged?.Invoke(selectionHandler.SelectedHitObjects); } private void onBlueprintDeselected(SelectionBlueprint blueprint) { selectionHandler.HandleDeselected(blueprint); selectionBlueprints.ChangeChildDepth(blueprint, 0); beatmap.SelectedHitObjects.Remove(blueprint.HitObject); SelectionChanged?.Invoke(selectionHandler.SelectedHitObjects); } #endregion #region Selection Movement private Vector2? screenSpaceMovementStartPosition; private SelectionBlueprint movementBlueprint; /// /// Attempts to begin the movement of any selected blueprints. /// /// Whether movement began. private bool beginSelectionMovement() { Debug.Assert(movementBlueprint == null); // Any selected blueprint that is hovered can begin the movement of the group, however only the earliest hitobject 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 hitobject, since it only makes sense to distance snap from that hitobject movementBlueprint = selectionHandler.SelectedBlueprints.OrderBy(b => b.HitObject.StartTime).First(); screenSpaceMovementStartPosition = movementBlueprint.SelectionPoint; // todo: unsure if correct return true; } /// /// Moves the current selected blueprints. /// /// The defining the movement event. /// Whether a movement was active. private bool moveCurrentSelection(DragEvent e) { if (movementBlueprint == null) return false; Debug.Assert(screenSpaceMovementStartPosition != null); Vector2 startPosition = screenSpaceMovementStartPosition.Value; HitObject draggedObject = movementBlueprint.HitObject; // The final movement position, relative to screenSpaceMovementStartPosition Vector2 movePosition = startPosition + e.ScreenSpaceMousePosition - e.ScreenSpaceMouseDownPosition; (Vector2 snappedPosition, double snappedTime) = snapProvider.GetSnappedPosition(ToLocalSpace(movePosition), draggedObject.StartTime); // Move the hitobjects if (!selectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprint, ToScreenSpace(snappedPosition)))) return true; // Apply the start time at the newly snapped-to position double offset = snappedTime - draggedObject.StartTime; foreach (HitObject obj in selectionHandler.SelectedHitObjects) obj.StartTime += offset; return true; } /// /// Finishes the current movement of selected blueprints. /// /// Whether a movement was active. private bool finishSelectionMovement() { if (movementBlueprint == null) return false; screenSpaceMovementStartPosition = null; movementBlueprint = null; return true; } #endregion protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); if (beatmap != null) { beatmap.HitObjectAdded -= AddBlueprintFor; beatmap.HitObjectRemoved -= removeBlueprintFor; } } protected class SelectionBlueprintContainer : Container { //todo: remove } } }