1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 21:02:55 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/SelectionHandler.cs

472 lines
16 KiB
C#
Raw Normal View History

2020-09-30 14:35:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
2018-10-18 15:36:06 +08:00
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2021-04-28 12:43:16 +08:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Primitives;
2021-04-28 12:43:16 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
2021-04-28 12:43:16 +08:00
using osu.Game.Graphics.UserInterface;
2022-01-05 15:46:34 +08:00
using osu.Game.Input.Bindings;
using osu.Game.Resources.Localisation.Web;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Edit;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Input;
2018-04-13 17:19:50 +08:00
2018-11-06 17:28:22 +08:00
namespace osu.Game.Screens.Edit.Compose.Components
2018-04-13 17:19:50 +08:00
{
/// <summary>
/// A component which outlines items and handles movement of selections.
2018-04-13 17:19:50 +08:00
/// </summary>
2022-01-05 15:46:34 +08:00
public abstract class SelectionHandler<T> : CompositeDrawable, IKeyBindingHandler<PlatformAction>, IKeyBindingHandler<GlobalAction>, IHasContextMenu
2018-04-13 17:19:50 +08:00
{
/// <summary>
/// The currently selected blueprints.
/// Should be used when operations are dealing directly with the visible blueprints.
/// For more general selection operations, use <see cref="SelectedItems"/> instead.
/// </summary>
public IReadOnlyList<SelectionBlueprint<T>> SelectedBlueprints => selectedBlueprints;
2018-04-13 17:19:50 +08:00
/// <summary>
/// The currently selected items.
/// </summary>
public readonly BindableList<T> SelectedItems = new BindableList<T>();
private readonly List<SelectionBlueprint<T>> selectedBlueprints;
protected SelectionBox SelectionBox { get; private set; }
[Resolved(CanBeNull = true)]
protected IEditorChangeHandler ChangeHandler { get; private set; }
protected SelectionHandler()
2018-04-13 17:19:50 +08:00
{
selectedBlueprints = new List<SelectionBlueprint<T>>();
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Both;
AlwaysPresent = true;
}
[BackgroundDependencyLoader]
2022-01-15 08:06:39 +08:00
private void load()
2018-04-13 17:19:50 +08:00
{
InternalChild = SelectionBox = CreateSelectionBox();
SelectedItems.CollectionChanged += (sender, args) =>
{
Scheduler.AddOnce(updateVisibility);
};
2018-04-13 17:19:50 +08:00
}
public SelectionBox CreateSelectionBox()
=> new SelectionBox
{
OperationStarted = OnOperationBegan,
OperationEnded = OnOperationEnded,
OnRotation = HandleRotation,
OnScale = HandleScale,
OnFlip = HandleFlip,
OnReverse = HandleReverse,
};
/// <summary>
/// Fired when a drag operation ends from the selection box.
/// </summary>
protected virtual void OnOperationBegan()
{
2020-11-14 22:52:12 +08:00
ChangeHandler?.BeginChange();
}
/// <summary>
/// Fired when a drag operation begins from the selection box.
/// </summary>
protected virtual void OnOperationEnded()
{
2020-11-14 22:52:12 +08:00
ChangeHandler?.EndChange();
}
2020-09-29 18:43:50 +08:00
2018-04-13 17:19:50 +08:00
#region User Input Handling
2018-11-19 15:58:11 +08:00
/// <summary>
/// Handles the selected items being moved.
2018-11-19 15:58:11 +08:00
/// </summary>
2020-05-26 16:00:55 +08:00
/// <remarks>
/// Just returning true is enough to allow default movement to take place.
2020-05-26 16:00:55 +08:00
/// Custom implementation is only required if other attributes are to be considered, like changing columns.
/// </remarks>
/// <param name="moveEvent">The move event.</param>
2020-05-26 16:00:55 +08:00
/// <returns>
/// Whether any items could be moved.
2020-05-26 16:00:55 +08:00
/// </returns>
public virtual bool HandleMovement(MoveSelectionEvent<T> moveEvent) => false;
/// <summary>
/// Handles the selected items being rotated.
/// </summary>
/// <param name="angle">The delta angle to apply to the selection.</param>
/// <returns>Whether any items could be rotated.</returns>
public virtual bool HandleRotation(float angle) => false;
/// <summary>
/// Handles the selected items being scaled.
/// </summary>
/// <param name="scale">The delta scale to apply, in local coordinates.</param>
/// <param name="anchor">The point of reference where the scale is originating from.</param>
/// <returns>Whether any items could be scaled.</returns>
public virtual bool HandleScale(Vector2 scale, Anchor anchor) => false;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Handles the selected items being flipped.
/// </summary>
2022-01-05 15:46:34 +08:00
/// <param name="direction">The direction to flip.</param>
/// <param name="flipOverOrigin">Whether the flip operation should be global to the playfield's origin or local to the selected pattern.</param>
/// <returns>Whether any items could be flipped.</returns>
2022-01-05 15:46:34 +08:00
public virtual bool HandleFlip(Direction direction, bool flipOverOrigin) => false;
/// <summary>
/// Handles the selected items being reversed pattern-wise.
/// </summary>
/// <returns>Whether any items could be reversed.</returns>
public virtual bool HandleReverse() => false;
2022-01-05 15:46:34 +08:00
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat)
return false;
switch (e.Action)
{
case GlobalAction.EditorFlipHorizontally:
return HandleFlip(Direction.Horizontal, true);
2022-01-05 15:46:34 +08:00
case GlobalAction.EditorFlipVertically:
return HandleFlip(Direction.Vertical, true);
2022-01-05 15:46:34 +08:00
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
2018-10-18 15:36:06 +08:00
{
2021-09-16 17:26:12 +08:00
switch (e.Action)
2018-10-18 15:36:06 +08:00
{
2021-07-20 13:23:34 +08:00
case PlatformAction.Delete:
DeleteSelected();
2018-10-18 15:36:06 +08:00
return true;
}
2018-10-31 11:07:06 +08:00
return false;
2018-10-18 15:36:06 +08:00
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<PlatformAction> e)
{
}
2018-04-13 17:19:50 +08:00
#endregion
#region Selection Handling
/// <summary>
2018-11-06 17:06:34 +08:00
/// Bind an action to deselect all selected blueprints.
2018-04-13 17:19:50 +08:00
/// </summary>
internal Action DeselectAll { private get; set; }
2018-04-13 17:19:50 +08:00
/// <summary>
2018-11-06 17:06:34 +08:00
/// Handle a blueprint becoming selected.
2018-04-13 17:19:50 +08:00
/// </summary>
2018-11-06 17:06:34 +08:00
/// <param name="blueprint">The blueprint.</param>
internal virtual void HandleSelected(SelectionBlueprint<T> blueprint)
{
// there are potentially multiple SelectionHandlers active, but we only want to add items to the selected list once.
if (!SelectedItems.Contains(blueprint.Item))
SelectedItems.Add(blueprint.Item);
2020-09-11 21:03:19 +08:00
selectedBlueprints.Add(blueprint);
}
2018-04-13 17:19:50 +08:00
/// <summary>
2018-11-06 17:06:34 +08:00
/// Handle a blueprint becoming deselected.
2018-04-13 17:19:50 +08:00
/// </summary>
2018-11-06 17:06:34 +08:00
/// <param name="blueprint">The blueprint.</param>
internal virtual void HandleDeselected(SelectionBlueprint<T> blueprint)
2018-04-13 17:19:50 +08:00
{
SelectedItems.Remove(blueprint.Item);
2020-09-14 14:47:04 +08:00
selectedBlueprints.Remove(blueprint);
2018-04-13 17:19:50 +08:00
}
/// <summary>
2018-11-06 17:06:34 +08:00
/// Handle a blueprint requesting selection.
2018-04-13 17:19:50 +08:00
/// </summary>
2018-11-06 17:06:34 +08:00
/// <param name="blueprint">The blueprint.</param>
/// <param name="e">The mouse event responsible for selection.</param>
2020-11-04 16:53:03 +08:00
/// <returns>Whether a selection was performed.</returns>
2021-09-27 01:07:38 +08:00
internal virtual bool MouseDownSelectionRequested(SelectionBlueprint<T> blueprint, MouseButtonEvent e)
{
if (e.ShiftPressed && e.Button == MouseButton.Right)
{
handleQuickDeletion(blueprint);
return true;
}
// while holding control, we only want to add to selection, not replace an existing selection.
if (e.ControlPressed && e.Button == MouseButton.Left && !blueprint.IsSelected)
{
2020-10-27 05:16:28 +08:00
blueprint.ToggleSelection();
return true;
}
return ensureSelected(blueprint);
}
/// <summary>
/// Handle a blueprint requesting selection.
/// </summary>
/// <param name="blueprint">The blueprint.</param>
/// <param name="e">The mouse event responsible for deselection.</param>
/// <returns>Whether a deselection was performed.</returns>
internal bool MouseUpSelectionRequested(SelectionBlueprint<T> blueprint, MouseButtonEvent e)
{
if (blueprint.IsSelected)
{
blueprint.ToggleSelection();
return true;
}
return false;
2018-04-13 17:19:50 +08:00
}
private void handleQuickDeletion(SelectionBlueprint<T> blueprint)
{
if (blueprint.HandleQuickDeletion())
return;
if (!blueprint.IsSelected)
DeleteItems(new[] { blueprint.Item });
else
DeleteSelected();
}
/// <summary>
/// Given a selection target and a function of truth, retrieve the correct ternary state for display.
/// </summary>
protected static TernaryState GetStateFromSelection<TObject>(IEnumerable<TObject> selection, Func<TObject, bool> func)
{
if (selection.Any(func))
return selection.All(func) ? TernaryState.True : TernaryState.Indeterminate;
return TernaryState.False;
}
/// <summary>
/// Called whenever the deletion of items has been requested.
/// </summary>
/// <param name="items">The items to be deleted.</param>
protected abstract void DeleteItems(IEnumerable<T> items);
/// <summary>
/// Ensure the blueprint is in a selected state.
/// </summary>
/// <param name="blueprint">The blueprint to select.</param>
/// <returns>Whether selection state was changed.</returns>
private bool ensureSelected(SelectionBlueprint<T> blueprint)
{
2020-10-27 05:16:28 +08:00
if (blueprint.IsSelected)
return false;
2020-10-27 05:16:28 +08:00
DeselectAll?.Invoke();
blueprint.Select();
return true;
}
protected void DeleteSelected()
{
DeleteItems(selectedBlueprints.Select(b => b.Item));
}
2018-04-13 17:19:50 +08:00
#endregion
#region Outline Display
2018-04-13 17:19:50 +08:00
/// <summary>
/// Updates whether this <see cref="SelectionHandler{T}"/> is visible.
2018-04-13 17:19:50 +08:00
/// </summary>
private void updateVisibility()
2018-04-13 17:19:50 +08:00
{
int count = SelectedItems.Count;
SelectionBox.Text = count > 0 ? count.ToString() : string.Empty;
SelectionBox.FadeTo(count > 0 ? 1 : 0);
OnSelectionChanged();
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Triggered whenever the set of selected items changes.
/// Should update the selection box's state to match supported operations.
/// </summary>
protected virtual void OnSelectionChanged()
{
}
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
2018-11-06 17:06:34 +08:00
if (selectedBlueprints.Count == 0)
2018-04-13 17:19:50 +08:00
return;
// Move the rectangle to cover the items
2021-05-02 11:21:14 +08:00
RectangleF selectionRect = ToLocalSpace(selectedBlueprints[0].SelectionQuad).AABBFloat;
2018-04-13 17:19:50 +08:00
2021-05-02 11:21:14 +08:00
for (int i = 1; i < selectedBlueprints.Count; i++)
selectionRect = RectangleF.Union(selectionRect, ToLocalSpace(selectedBlueprints[i].SelectionQuad).AABBFloat);
2018-04-13 17:19:50 +08:00
selectionRect = selectionRect.Inflate(5f);
2018-04-13 17:19:50 +08:00
SelectionBox.Position = selectionRect.Location;
SelectionBox.Size = selectionRect.Size;
2018-04-13 17:19:50 +08:00
}
#endregion
2021-04-28 12:43:16 +08:00
#region Context Menu
public MenuItem[] ContextMenuItems
{
get
{
if (!SelectedBlueprints.Any(b => b.IsHovered))
return Array.Empty<MenuItem>();
var items = new List<MenuItem>();
items.AddRange(GetContextMenuItemsForSelection(SelectedBlueprints));
if (SelectedBlueprints.Count == 1)
items.AddRange(SelectedBlueprints[0].ContextMenuItems);
items.Add(new OsuMenuItem(CommonStrings.ButtonsDelete, MenuItemType.Destructive, DeleteSelected));
2021-04-28 12:43:16 +08:00
return items.ToArray();
}
}
/// <summary>
/// Provide context menu items relevant to current selection. Calling base is not required.
/// </summary>
/// <param name="selection">The current selection.</param>
/// <returns>The relevant menu items.</returns>
protected virtual IEnumerable<MenuItem> GetContextMenuItemsForSelection(IEnumerable<SelectionBlueprint<T>> selection)
=> Enumerable.Empty<MenuItem>();
#endregion
#region Helper Methods
/// <summary>
/// Rotate a point around an arbitrary origin.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="origin">The centre origin to rotate around.</param>
/// <param name="angle">The angle to rotate (in degrees).</param>
protected static Vector2 RotatePointAroundOrigin(Vector2 point, Vector2 origin, float angle)
{
angle = -angle;
point.X -= origin.X;
point.Y -= origin.Y;
Vector2 ret;
ret.X = point.X * MathF.Cos(MathUtils.DegreesToRadians(angle)) + point.Y * MathF.Sin(MathUtils.DegreesToRadians(angle));
ret.Y = point.X * -MathF.Sin(MathUtils.DegreesToRadians(angle)) + point.Y * MathF.Cos(MathUtils.DegreesToRadians(angle));
ret.X += origin.X;
ret.Y += origin.Y;
return ret;
}
/// <summary>
/// Given a flip direction, a surrounding quad for all selected objects, and a position,
/// will return the flipped position in screen space coordinates.
/// </summary>
protected static Vector2 GetFlippedPosition(Direction direction, Quad quad, Vector2 position)
{
var centre = quad.Centre;
switch (direction)
{
case Direction.Horizontal:
position.X = centre.X - (position.X - centre.X);
break;
case Direction.Vertical:
position.Y = centre.Y - (position.Y - centre.Y);
break;
}
return position;
}
/// <summary>
/// Given a scale vector, a surrounding quad for all selected objects, and a position,
/// will return the scaled position in screen space coordinates.
/// </summary>
protected static Vector2 GetScaledPosition(Anchor reference, Vector2 scale, Quad selectionQuad, Vector2 position)
{
// adjust the direction of scale depending on which side the user is dragging.
float xOffset = ((reference & Anchor.x0) > 0) ? -scale.X : 0;
float yOffset = ((reference & Anchor.y0) > 0) ? -scale.Y : 0;
// guard against no-ops and NaN.
if (scale.X != 0 && selectionQuad.Width > 0)
position.X = selectionQuad.TopLeft.X + xOffset + (position.X - selectionQuad.TopLeft.X) / selectionQuad.Width * (selectionQuad.Width + scale.X);
if (scale.Y != 0 && selectionQuad.Height > 0)
position.Y = selectionQuad.TopLeft.Y + yOffset + (position.Y - selectionQuad.TopLeft.Y) / selectionQuad.Height * (selectionQuad.Height + scale.Y);
return position;
}
/// <summary>
/// Returns a quad surrounding the provided points.
/// </summary>
/// <param name="points">The points to calculate a quad for.</param>
protected static Quad GetSurroundingQuad(IEnumerable<Vector2> points)
{
if (!points.Any())
return new Quad();
Vector2 minPosition = new Vector2(float.MaxValue, float.MaxValue);
Vector2 maxPosition = new Vector2(float.MinValue, float.MinValue);
// Go through all hitobjects to make sure they would remain in the bounds of the editor after movement, before any movement is attempted
foreach (var p in points)
{
minPosition = Vector2.ComponentMin(minPosition, p);
maxPosition = Vector2.ComponentMax(maxPosition, p);
}
Vector2 size = maxPosition - minPosition;
return new Quad(minPosition.X, minPosition.Y, size.X, size.Y);
}
#endregion
2018-04-13 17:19:50 +08:00
}
}