1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 05:27:40 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/BlueprintContainer.cs

440 lines
15 KiB
C#
Raw Normal View History

// 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;
using System.Diagnostics;
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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Timing;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Edit;
2019-08-29 15:06:40 +08:00
using osu.Game.Rulesets.Objects;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Objects.Drawables;
2019-10-16 19:34:16 +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 container which provides a "blueprint" display of hitobjects.
/// Includes selection and manipulation support via a <see cref="SelectionHandler"/>.
/// </summary>
public abstract class BlueprintContainer : CompositeDrawable, IKeyBindingHandler<PlatformAction>
2018-04-13 17:19:50 +08:00
{
public event Action<IEnumerable<HitObject>> SelectionChanged;
2020-01-16 10:54:03 +08:00
protected DragBox DragBox { get; private set; }
private SelectionBlueprintContainer selectionBlueprints;
2018-11-19 15:58:11 +08:00
private SelectionHandler selectionHandler;
[Resolved]
private IAdjustableClock adjustableClock { get; set; }
2019-08-29 15:06:40 +08:00
[Resolved]
private EditorBeatmap beatmap { get; set; }
2019-08-29 15:06:40 +08:00
[Resolved(canBeNull: true)]
private IDistanceSnapProvider snapProvider { get; set; }
protected BlueprintContainer()
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
selectionHandler = CreateSelectionHandler();
2018-11-19 15:58:11 +08:00
selectionHandler.DeselectAll = deselectAll;
2018-04-13 17:19:50 +08:00
AddRangeInternal(new[]
2018-04-13 17:19:50 +08:00
{
DragBox = CreateDragBox(select),
2018-11-19 15:58:11 +08:00
selectionHandler,
selectionBlueprints = new SelectionBlueprintContainer { RelativeSizeAxes = Axes.Both },
2020-01-16 10:54:03 +08:00
DragBox.CreateProxy().With(p => p.Depth = float.MinValue)
});
foreach (var obj in beatmap.HitObjects)
AddBlueprintFor(obj);
2019-08-29 15:06:40 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
beatmap.HitObjectAdded += AddBlueprintFor;
2019-08-29 15:06:40 +08:00
beatmap.HitObjectRemoved += removeBlueprintFor;
2018-04-13 17:19:50 +08:00
}
2020-01-16 10:54:03 +08:00
/// <summary>
/// Creates a <see cref="SelectionHandler"/> which outlines <see cref="DrawableHitObject"/>s and handles movement of selections.
/// </summary>
protected virtual SelectionHandler CreateSelectionHandler() => new SelectionHandler();
/// <summary>
/// Creates a <see cref="SelectionBlueprint"/> for a specific <see cref="DrawableHitObject"/>.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to create the overlay for.</param>
protected virtual SelectionBlueprint CreateBlueprintFor(HitObject hitObject) => null;
protected virtual DragBox CreateDragBox(Action<RectangleF> 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;
2019-10-24 15:14:29 +08:00
// 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.DrawableObject.HitObject.StartTime);
return true;
}
protected override bool OnMouseUp(MouseUpEvent e)
{
2019-10-24 15:14:29 +08:00
// 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())
{
if (!DragBox.UpdateDrag(e))
return false;
DragBox.FadeIn(250, Easing.OutQuint);
}
return true;
}
protected override bool OnDrag(DragEvent e)
{
if (e.Button == MouseButton.Right)
return false;
if (!moveCurrentSelection(e))
{
if (!DragBox.UpdateDrag(e))
return false;
}
return true;
}
protected override bool OnDragEnd(DragEndEvent e)
{
if (e.Button == MouseButton.Right)
return false;
if (!finishSelectionMovement())
{
DragBox.FadeOut(250, Easing.OutQuint);
selectionHandler.UpdateVisibility();
}
return true;
}
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
2019-08-29 15:06:40 +08:00
private void removeBlueprintFor(HitObject hitObject)
2018-10-18 15:36:06 +08:00
{
var blueprint = selectionBlueprints.SingleOrDefault(m => m.DrawableObject.HitObject == hitObject);
if (blueprint == null)
2018-10-18 15:36:06 +08:00
return;
blueprint.Deselect();
blueprint.Selected -= onBlueprintSelected;
blueprint.Deselected -= onBlueprintDeselected;
selectionBlueprints.Remove(blueprint);
}
protected virtual void AddBlueprintFor(HitObject hitObject)
2019-08-29 15:06:40 +08:00
{
var blueprint = CreateBlueprintFor(hitObject);
2019-08-29 15:06:40 +08:00
if (blueprint == null)
return;
blueprint.Selected += onBlueprintSelected;
blueprint.Deselected += onBlueprintDeselected;
selectionBlueprints.Add(blueprint);
}
#endregion
2019-08-29 15:06:40 +08:00
#region Selection
/// <summary>
/// Whether a blueprint was selected by a previous click event.
/// </summary>
private bool clickSelectionBegan;
/// <summary>
/// Attempts to select any hovered blueprints.
/// </summary>
/// <param name="e">The input event that triggered this selection.</param>
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))
2019-11-05 17:25:38 +08:00
return;
2019-11-05 17:25:38 +08:00
foreach (SelectionBlueprint blueprint in selectionBlueprints.AliveBlueprints)
{
2019-11-05 17:25:38 +08:00
if (blueprint.IsHovered)
{
2019-11-05 17:25:38 +08:00
selectionHandler.HandleSelectionRequested(blueprint, e.CurrentState);
clickSelectionBegan = true;
break;
}
}
}
/// <summary>
/// Finishes the current blueprint selection.
/// </summary>
2019-10-24 15:14:29 +08:00
/// <returns>Whether a click selection was active.</returns>
private bool endClickSelection()
{
2019-10-24 15:14:29 +08:00
if (!clickSelectionBegan)
return false;
clickSelectionBegan = false;
2019-10-24 15:14:29 +08:00
return true;
}
/// <summary>
/// Select all masks in a given rectangle selection area.
/// </summary>
/// <param name="rect">The rectangle to perform a selection on in screen-space coordinates.</param>
private void select(RectangleF rect)
{
foreach (var blueprint in selectionBlueprints)
{
if (blueprint.IsAlive && blueprint.IsPresent && rect.Contains(blueprint.SelectionPoint))
blueprint.Select();
else
blueprint.Deselect();
}
}
/// <summary>
/// Selects all <see cref="SelectionBlueprint"/>s.
/// </summary>
private void selectAll()
{
selectionBlueprints.ToList().ForEach(m => m.Select());
selectionHandler.UpdateVisibility();
}
/// <summary>
2018-11-06 16:56:04 +08:00
/// Deselects all selected <see cref="SelectionBlueprint"/>s.
/// </summary>
private void deselectAll() => selectionHandler.SelectedBlueprints.ToList().ForEach(m => m.Deselect());
2018-11-06 16:56:04 +08:00
private void onBlueprintSelected(SelectionBlueprint blueprint)
{
2018-11-19 15:58:11 +08:00
selectionHandler.HandleSelected(blueprint);
2018-11-06 16:56:04 +08:00
selectionBlueprints.ChangeChildDepth(blueprint, 1);
SelectionChanged?.Invoke(selectionHandler.SelectedHitObjects);
}
2018-11-06 16:56:04 +08:00
private void onBlueprintDeselected(SelectionBlueprint blueprint)
{
2018-11-19 15:58:11 +08:00
selectionHandler.HandleDeselected(blueprint);
2018-11-06 16:56:04 +08:00
selectionBlueprints.ChangeChildDepth(blueprint, 0);
SelectionChanged?.Invoke(selectionHandler.SelectedHitObjects);
}
#endregion
#region Selection Movement
private Vector2? screenSpaceMovementStartPosition;
private SelectionBlueprint movementBlueprint;
/// <summary>
/// Attempts to begin the movement of any selected blueprints.
/// </summary>
/// <returns>Whether movement began.</returns>
private bool beginSelectionMovement()
{
Debug.Assert(movementBlueprint == null);
2019-10-24 15:14:29 +08:00
// 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.DrawableObject.HitObject.StartTime).First();
screenSpaceMovementStartPosition = movementBlueprint.DrawableObject.ToScreenSpace(movementBlueprint.DrawableObject.OriginPosition);
return true;
}
/// <summary>
/// Moves the current selected blueprints.
/// </summary>
/// <param name="e">The <see cref="DragEvent"/> defining the movement event.</param>
/// <returns>Whether a movement was active.</returns>
private bool moveCurrentSelection(DragEvent e)
{
if (movementBlueprint == null)
return false;
Debug.Assert(screenSpaceMovementStartPosition != null);
Vector2 startPosition = screenSpaceMovementStartPosition.Value;
HitObject draggedObject = movementBlueprint.DrawableObject.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);
2019-10-16 19:34:02 +08:00
// Move the hitobjects
if (!selectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprint, startPosition, ToScreenSpace(snappedPosition))))
return true;
2019-10-16 19:34:16 +08:00
// Apply the start time at the newly snapped-to position
double offset = snappedTime - draggedObject.StartTime;
2019-10-16 19:34:16 +08:00
foreach (HitObject obj in selectionHandler.SelectedHitObjects)
obj.StartTime += offset;
return true;
}
/// <summary>
/// Finishes the current movement of selected blueprints.
/// </summary>
/// <returns>Whether a movement was active.</returns>
private bool finishSelectionMovement()
{
if (movementBlueprint == null)
return false;
screenSpaceMovementStartPosition = null;
movementBlueprint = null;
2019-10-16 19:34:16 +08:00
return true;
}
#endregion
2019-08-29 15:06:40 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (beatmap != null)
{
beatmap.HitObjectAdded -= AddBlueprintFor;
2019-08-29 15:06:40 +08:00
beatmap.HitObjectRemoved -= removeBlueprintFor;
}
}
2018-11-06 16:56:04 +08:00
private class SelectionBlueprintContainer : Container<SelectionBlueprint>
{
public IEnumerable<SelectionBlueprint> AliveBlueprints => AliveInternalChildren.Cast<SelectionBlueprint>();
protected override int Compare(Drawable x, Drawable y)
{
2018-11-06 16:56:04 +08:00
if (!(x is SelectionBlueprint xBlueprint) || !(y is SelectionBlueprint yBlueprint))
return base.Compare(x, y);
2019-02-28 12:31:40 +08:00
2018-11-06 16:56:04 +08:00
return Compare(xBlueprint, yBlueprint);
}
2018-11-06 16:56:04 +08:00
public int Compare(SelectionBlueprint x, SelectionBlueprint y)
{
2018-11-06 16:56:04 +08:00
// dpeth is used to denote selected status (we always want selected blueprints to handle input first).
int d = x.Depth.CompareTo(y.Depth);
if (d != 0)
return d;
// Put earlier hitobjects towards the end of the list, so they handle input first
int i = y.DrawableObject.HitObject.StartTime.CompareTo(x.DrawableObject.HitObject.StartTime);
return i == 0 ? CompareReverseChildID(x, y) : i;
}
2018-10-18 15:36:06 +08:00
}
2018-04-13 17:19:50 +08:00
}
}