2019-01-24 16:43:03 +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
2019-10-16 19:07:11 +08:00
using System ;
2018-11-06 16:51:26 +08:00
using System.Collections.Generic ;
2019-10-23 17:58:15 +08:00
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 ;
2020-01-21 19:46:39 +08:00
using osu.Framework.Bindables ;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
2018-11-06 16:51:26 +08:00
using osu.Framework.Graphics.Primitives ;
2019-10-03 15:14:42 +08:00
using osu.Framework.Input ;
2019-11-11 12:41:10 +08:00
using osu.Framework.Input.Bindings ;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events ;
2019-11-06 17:15:57 +08:00
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 ;
2019-11-07 21:51:49 +08:00
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
{
2020-01-02 10:46:18 +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
{
2019-10-16 19:07:11 +08:00
public event Action < IEnumerable < HitObject > > SelectionChanged ;
2018-11-13 11:52:04 +08:00
2020-01-16 10:54:03 +08:00
protected DragBox DragBox { get ; private set ; }
2020-01-15 18:09:49 +08:00
2020-01-23 15:20:42 +08:00
private Container < SelectionBlueprint > selectionBlueprints ;
2020-01-15 18:09:49 +08:00
2018-11-19 15:58:11 +08:00
private SelectionHandler selectionHandler ;
2018-11-06 16:51:26 +08:00
2019-11-06 17:15:57 +08:00
[Resolved]
private IAdjustableClock adjustableClock { get ; set ; }
2019-08-29 15:06:40 +08:00
[Resolved]
2019-12-27 18:46:33 +08:00
private EditorBeatmap beatmap { get ; set ; }
2019-08-29 15:06:40 +08:00
2020-01-21 19:46:39 +08:00
private readonly BindableList < HitObject > selectedHitObjects = new BindableList < HitObject > ( ) ;
2020-01-15 18:09:49 +08:00
[Resolved(canBeNull: true)]
private IDistanceSnapProvider snapProvider { get ; set ; }
2020-01-02 10:46:18 +08:00
protected BlueprintContainer ( )
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes . Both ;
}
[BackgroundDependencyLoader]
2018-10-17 16:41:17 +08:00
private void load ( )
2018-04-13 17:19:50 +08:00
{
2020-01-02 10:46:18 +08:00
selectionHandler = CreateSelectionHandler ( ) ;
2018-11-19 15:58:11 +08:00
selectionHandler . DeselectAll = deselectAll ;
2018-04-13 17:19:50 +08:00
2020-01-15 18:09:49 +08:00
AddRangeInternal ( new [ ]
2018-04-13 17:19:50 +08:00
{
2020-01-15 18:09:49 +08:00
DragBox = CreateDragBox ( select ) ,
2018-11-19 15:58:11 +08:00
selectionHandler ,
2020-01-20 23:53:59 +08:00
selectionBlueprints = CreateSelectionBlueprintContainer ( ) ,
2020-01-16 10:54:03 +08:00
DragBox . CreateProxy ( ) . With ( p = > p . Depth = float . MinValue )
2020-01-15 18:09:49 +08:00
} ) ;
2018-06-07 15:27:49 +08:00
2020-01-02 18:09:37 +08:00
foreach ( var obj in beatmap . HitObjects )
2020-01-15 18:09:49 +08:00
AddBlueprintFor ( obj ) ;
2020-01-21 19:46:39 +08:00
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 ( ) ;
} ;
2019-08-29 15:06:40 +08:00
}
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2020-01-15 18:09:49 +08:00
beatmap . HitObjectAdded + = AddBlueprintFor ;
2019-08-29 15:06:40 +08:00
beatmap . HitObjectRemoved + = removeBlueprintFor ;
2018-04-13 17:19:50 +08:00
}
2020-01-24 16:50:36 +08:00
protected virtual Container < SelectionBlueprint > CreateSelectionBlueprintContainer ( ) = >
new Container < SelectionBlueprint > { RelativeSizeAxes = Axes . Both } ;
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 ) ;
2019-10-24 13:58:02 +08:00
protected override bool OnMouseDown ( MouseDownEvent e )
{
2019-10-24 14:58:22 +08:00
beginClickSelection ( e ) ;
2020-01-22 20:43:02 +08:00
prepareSelectionMovement ( ) ;
2019-11-08 12:40:47 +08:00
return e . Button = = MouseButton . Left ;
2019-10-24 13:58:02 +08:00
}
2018-11-06 17:02:55 +08:00
protected override bool OnClick ( ClickEvent e )
{
2019-11-07 21:51:49 +08:00
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 ) )
2019-10-24 13:58:02 +08:00
return true ;
2018-11-06 17:02:55 +08:00
deselectAll ( ) ;
return true ;
}
2019-11-06 17:15:57 +08:00
protected override bool OnDoubleClick ( DoubleClickEvent e )
{
2019-11-07 21:51:49 +08:00
if ( e . Button = = MouseButton . Right )
return false ;
2019-11-06 17:15:57 +08:00
SelectionBlueprint clickedBlueprint = selectionHandler . SelectedBlueprints . FirstOrDefault ( b = > b . IsHovered ) ;
if ( clickedBlueprint = = null )
return false ;
2020-01-20 23:53:59 +08:00
adjustableClock ? . Seek ( clickedBlueprint . HitObject . StartTime ) ;
2019-11-06 17:15:57 +08:00
return true ;
}
2020-01-20 17:17:21 +08:00
protected override void OnMouseUp ( MouseUpEvent e )
2019-10-24 14:58:22 +08:00
{
2019-10-24 15:14:29 +08:00
// Special case for when a drag happened instead of a click
Schedule ( ( ) = > endClickSelection ( ) ) ;
2020-01-22 20:43:02 +08:00
finishSelectionMovement ( ) ;
2019-10-24 14:58:22 +08:00
}
2019-10-24 14:11:54 +08:00
protected override bool OnDragStart ( DragStartEvent e )
{
2019-11-07 21:51:49 +08:00
if ( e . Button = = MouseButton . Right )
return false ;
2020-01-22 20:43:02 +08:00
if ( movementBlueprint ! = null )
2020-01-22 16:54:11 +08:00
return true ;
2020-01-15 18:09:49 +08:00
2020-01-22 16:54:11 +08:00
if ( DragBox . HandleDrag ( e ) )
{
DragBox . Show ( ) ;
return true ;
2019-10-24 16:22:14 +08:00
}
2019-10-24 14:11:54 +08:00
2020-01-22 16:54:11 +08:00
return false ;
2019-10-24 14:11:54 +08:00
}
2020-01-20 17:17:21 +08:00
protected override void OnDrag ( DragEvent e )
2019-10-24 14:11:54 +08:00
{
2019-11-07 21:51:49 +08:00
if ( e . Button = = MouseButton . Right )
2020-01-20 17:17:21 +08:00
return ;
2019-11-07 21:51:49 +08:00
2020-01-22 16:54:11 +08:00
if ( DragBox . State = = Visibility . Visible )
2020-01-23 14:37:54 +08:00
DragBox . HandleDrag ( e ) ;
2019-10-24 14:11:54 +08:00
2020-01-23 14:37:54 +08:00
moveCurrentSelection ( e ) ;
2019-10-24 14:11:54 +08:00
}
2020-01-20 17:17:21 +08:00
protected override void OnDragEnd ( DragEndEvent e )
2019-10-24 14:11:54 +08:00
{
2019-11-07 21:51:49 +08:00
if ( e . Button = = MouseButton . Right )
2020-01-20 17:17:21 +08:00
return ;
2019-11-07 21:51:49 +08:00
2020-01-22 16:54:11 +08:00
if ( DragBox . State = = Visibility . Visible )
2019-10-24 14:11:54 +08:00
{
2020-01-22 16:54:11 +08:00
DragBox . Hide ( ) ;
2019-10-24 14:11:54 +08:00
selectionHandler . UpdateVisibility ( ) ;
}
}
2019-11-11 12:41:10 +08:00
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 ( PlatformAction action )
{
switch ( action . ActionType )
{
case PlatformActionType . SelectAll :
selectAll ( ) ;
return true ;
}
return false ;
}
2020-01-22 12:22:34 +08:00
public void OnReleased ( PlatformAction action )
{
}
2019-11-11 12:41:10 +08:00
2019-10-24 15:17:48 +08:00
#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
{
2020-01-20 23:53:59 +08:00
var blueprint = selectionBlueprints . SingleOrDefault ( m = > m . HitObject = = hitObject ) ;
2018-11-06 17:02:55 +08:00
if ( blueprint = = null )
2018-10-18 15:36:06 +08:00
return ;
2018-11-06 17:02:55 +08:00
blueprint . Deselect ( ) ;
2018-11-06 16:51:26 +08:00
2018-11-06 17:02:55 +08:00
blueprint . Selected - = onBlueprintSelected ;
blueprint . Deselected - = onBlueprintDeselected ;
2018-11-06 16:51:26 +08:00
2018-11-06 17:02:55 +08:00
selectionBlueprints . Remove ( blueprint ) ;
2018-11-06 16:51:26 +08:00
}
2020-01-15 18:09:49 +08:00
protected virtual void AddBlueprintFor ( HitObject hitObject )
2019-08-29 15:06:40 +08:00
{
2020-01-02 10:46:18 +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 ) ;
}
2019-10-24 15:17:48 +08:00
#endregion
2019-08-29 15:06:40 +08:00
2019-10-24 14:58:22 +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>
2019-11-13 16:21:48 +08:00
private void beginClickSelection ( MouseButtonEvent e )
2019-10-24 14:58:22 +08:00
{
Debug . Assert ( ! clickSelectionBegan ) ;
2019-11-13 16:21:48 +08:00
// 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 16:28:42 +08:00
2020-01-21 13:20:52 +08:00
foreach ( SelectionBlueprint blueprint in selectionBlueprints . AliveChildren )
2019-10-24 14:58:22 +08:00
{
2019-11-05 17:25:38 +08:00
if ( blueprint . IsHovered )
2019-10-24 14:58:22 +08:00
{
2019-11-05 17:25:38 +08:00
selectionHandler . HandleSelectionRequested ( blueprint , e . CurrentState ) ;
clickSelectionBegan = true ;
2019-10-24 14:58:22 +08:00
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 14:58:22 +08:00
{
2019-10-24 15:14:29 +08:00
if ( ! clickSelectionBegan )
return false ;
2019-10-24 14:58:22 +08:00
clickSelectionBegan = false ;
2019-10-24 15:14:29 +08:00
return true ;
2019-10-24 14:58:22 +08:00
}
2018-11-06 16:51:26 +08:00
/// <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 )
{
2019-10-16 19:07:11 +08:00
foreach ( var blueprint in selectionBlueprints )
2018-11-06 16:51:26 +08:00
{
2019-10-16 19:07:11 +08:00
if ( blueprint . IsAlive & & blueprint . IsPresent & & rect . Contains ( blueprint . SelectionPoint ) )
2018-11-06 17:02:55 +08:00
blueprint . Select ( ) ;
2018-11-06 16:51:26 +08:00
else
2018-11-06 17:02:55 +08:00
blueprint . Deselect ( ) ;
2018-11-06 16:51:26 +08:00
}
}
2019-11-11 12:41:10 +08:00
/// <summary>
/// Selects all <see cref="SelectionBlueprint"/>s.
/// </summary>
private void selectAll ( )
{
selectionBlueprints . ToList ( ) . ForEach ( m = > m . Select ( ) ) ;
selectionHandler . UpdateVisibility ( ) ;
}
2018-11-06 16:51:26 +08:00
/// <summary>
2018-11-06 16:56:04 +08:00
/// Deselects all selected <see cref="SelectionBlueprint"/>s.
2018-11-06 16:51:26 +08:00
/// </summary>
2019-10-16 19:07:11 +08:00
private void deselectAll ( ) = > selectionHandler . SelectedBlueprints . ToList ( ) . ForEach ( m = > m . Deselect ( ) ) ;
2018-11-06 16:51:26 +08:00
2018-11-06 16:56:04 +08:00
private void onBlueprintSelected ( SelectionBlueprint blueprint )
2018-11-06 16:51:26 +08:00
{
2018-11-19 15:58:11 +08:00
selectionHandler . HandleSelected ( blueprint ) ;
2018-11-06 16:56:04 +08:00
selectionBlueprints . ChangeChildDepth ( blueprint , 1 ) ;
2020-01-21 19:46:39 +08:00
beatmap . SelectedHitObjects . Add ( blueprint . HitObject ) ;
2019-10-16 19:07:11 +08:00
SelectionChanged ? . Invoke ( selectionHandler . SelectedHitObjects ) ;
2018-11-06 16:51:26 +08:00
}
2018-11-06 16:56:04 +08:00
private void onBlueprintDeselected ( SelectionBlueprint blueprint )
2018-11-06 16:51:26 +08:00
{
2018-11-19 15:58:11 +08:00
selectionHandler . HandleDeselected ( blueprint ) ;
2018-11-06 16:56:04 +08:00
selectionBlueprints . ChangeChildDepth ( blueprint , 0 ) ;
2020-01-21 19:46:39 +08:00
beatmap . SelectedHitObjects . Remove ( blueprint . HitObject ) ;
2019-10-16 19:07:11 +08:00
SelectionChanged ? . Invoke ( selectionHandler . SelectedHitObjects ) ;
2018-11-06 16:51:26 +08:00
}
2019-10-24 14:58:22 +08:00
#endregion
#region Selection Movement
2020-01-22 20:43:02 +08:00
private Vector2 ? movementBlueprintOriginalPosition ;
2019-10-23 17:58:15 +08:00
private SelectionBlueprint movementBlueprint ;
2019-10-24 14:11:54 +08:00
/// <summary>
/// Attempts to begin the movement of any selected blueprints.
/// </summary>
2020-01-24 16:30:37 +08:00
private void prepareSelectionMovement ( )
2019-10-23 17:37:57 +08:00
{
2020-01-24 16:30:37 +08:00
if ( ! selectionHandler . SelectedBlueprints . Any ( ) )
return ;
2019-10-24 14:11:54 +08:00
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 ) )
2020-01-24 16:30:37 +08:00
return ;
2019-10-24 14:11:54 +08:00
// Movement is tracked from the blueprint of the earliest hitobject, since it only makes sense to distance snap from that hitobject
2020-01-20 23:53:59 +08:00
movementBlueprint = selectionHandler . SelectedBlueprints . OrderBy ( b = > b . HitObject . StartTime ) . First ( ) ;
2020-01-22 20:43:02 +08:00
movementBlueprintOriginalPosition = movementBlueprint . SelectionPoint ; // todo: unsure if correct
2019-10-23 17:37:57 +08:00
}
2018-11-06 16:51:26 +08:00
2019-10-24 14:11:54 +08:00
/// <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 )
2019-10-08 17:57:03 +08:00
{
2019-10-24 14:11:54 +08:00
if ( movementBlueprint = = null )
return false ;
2019-10-23 17:37:57 +08:00
2020-01-22 20:43:02 +08:00
Debug . Assert ( movementBlueprintOriginalPosition ! = null ) ;
2019-10-23 17:58:15 +08:00
2020-01-20 23:53:59 +08:00
HitObject draggedObject = movementBlueprint . HitObject ;
2019-10-25 11:34:49 +08:00
2019-10-24 14:11:54 +08:00
// The final movement position, relative to screenSpaceMovementStartPosition
2020-01-22 20:43:02 +08:00
Vector2 movePosition = movementBlueprintOriginalPosition . Value + e . ScreenSpaceMousePosition - e . ScreenSpaceMouseDownPosition ;
2020-01-15 18:09:49 +08:00
( Vector2 snappedPosition , double snappedTime ) = snapProvider . GetSnappedPosition ( ToLocalSpace ( movePosition ) , draggedObject . StartTime ) ;
2019-10-16 19:34:02 +08:00
// Move the hitobjects
2020-01-22 20:37:57 +08:00
if ( ! selectionHandler . HandleMovement ( new MoveSelectionEvent ( movementBlueprint , ToScreenSpace ( snappedPosition ) ) ) )
2019-11-06 16:27:41 +08:00
return true ;
2019-10-16 19:34:16 +08:00
// Apply the start time at the newly snapped-to position
2019-10-25 11:34:49 +08:00
double offset = snappedTime - draggedObject . StartTime ;
2019-10-16 19:34:16 +08:00
foreach ( HitObject obj in selectionHandler . SelectedHitObjects )
obj . StartTime + = offset ;
2019-10-23 17:37:57 +08:00
return true ;
2019-10-08 17:57:03 +08:00
}
2018-11-06 16:51:26 +08:00
2019-10-24 14:11:54 +08:00
/// <summary>
/// Finishes the current movement of selected blueprints.
/// </summary>
/// <returns>Whether a movement was active.</returns>
private bool finishSelectionMovement ( )
2019-10-08 17:57:03 +08:00
{
2019-10-24 14:11:54 +08:00
if ( movementBlueprint = = null )
return false ;
2020-01-22 20:43:02 +08:00
movementBlueprintOriginalPosition = null ;
2019-10-24 14:11:54 +08:00
movementBlueprint = null ;
2019-10-16 19:34:16 +08:00
2019-10-23 17:58:15 +08:00
return true ;
2019-10-08 17:57:03 +08:00
}
2018-11-06 16:51:26 +08:00
2019-10-24 14:58:22 +08:00
#endregion
2019-08-29 15:06:40 +08:00
protected override void Dispose ( bool isDisposing )
{
base . Dispose ( isDisposing ) ;
if ( beatmap ! = null )
{
2020-01-15 18:09:49 +08:00
beatmap . HitObjectAdded - = AddBlueprintFor ;
2019-08-29 15:06:40 +08:00
beatmap . HitObjectRemoved - = removeBlueprintFor ;
}
}
2018-04-13 17:19:50 +08:00
}
}