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 ;
2020-11-12 18:52:02 +08:00
using System.Collections.Generic ;
2021-05-11 16:49:00 +08:00
using System.Collections.Specialized ;
2019-10-23 17:58:15 +08:00
using System.Diagnostics ;
2018-10-18 15:36:06 +08:00
using System.Linq ;
2021-06-23 08:40:07 +08:00
using JetBrains.Annotations ;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation ;
2021-05-11 16:49:00 +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 ;
2020-06-23 17:42:56 +08:00
using osu.Game.Graphics.UserInterface ;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Edit ;
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>
2021-04-27 17:33:47 +08:00
/// A container which provides a "blueprint" display of items.
2021-04-27 14:40:35 +08:00
/// Includes selection and manipulation support via a <see cref="Components.SelectionHandler{T}"/>.
2020-01-02 10:46:18 +08:00
/// </summary>
2021-04-27 14:40:35 +08:00
public abstract class BlueprintContainer < T > : CompositeDrawable , IKeyBindingHandler < PlatformAction >
2021-05-11 16:49:00 +08:00
where T : class
2018-04-13 17:19:50 +08:00
{
2020-01-16 10:54:03 +08:00
protected DragBox DragBox { get ; private set ; }
2020-01-15 18:09:49 +08:00
2021-04-27 14:40:35 +08:00
public Container < SelectionBlueprint < T > > SelectionBlueprints { get ; private set ; }
2020-01-15 18:09:49 +08:00
2021-04-27 14:40:35 +08:00
protected SelectionHandler < T > SelectionHandler { get ; private set ; }
2018-11-06 16:51:26 +08:00
2021-04-27 14:40:35 +08:00
private readonly Dictionary < T , SelectionBlueprint < T > > blueprintMap = new Dictionary < T , SelectionBlueprint < T > > ( ) ;
2020-01-21 19:46:39 +08:00
2020-01-15 18:09:49 +08:00
[Resolved(canBeNull: true)]
2020-05-20 16:48:43 +08:00
private IPositionSnapProvider snapProvider { get ; set ; }
2020-01-15 18:09:49 +08:00
2021-04-27 14:40:35 +08:00
[Resolved(CanBeNull = true)]
private IEditorChangeHandler changeHandler { get ; set ; }
2020-11-12 18:52:02 +08:00
2021-05-11 16:49:00 +08:00
protected readonly BindableList < T > SelectedItems = new BindableList < T > ( ) ;
2021-04-27 14:40:35 +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
{
2021-05-11 16:49:00 +08:00
SelectedItems . CollectionChanged + = ( selectedObjects , args ) = >
{
switch ( args . Action )
{
case NotifyCollectionChangedAction . Add :
2021-10-27 12:04:41 +08:00
foreach ( object o in args . NewItems )
2021-05-11 16:49:00 +08:00
SelectionBlueprints . FirstOrDefault ( b = > b . Item = = o ) ? . Select ( ) ;
break ;
case NotifyCollectionChangedAction . Remove :
2021-10-27 12:04:41 +08:00
foreach ( object o in args . OldItems )
2021-05-11 16:49:00 +08:00
SelectionBlueprints . FirstOrDefault ( b = > b . Item = = o ) ? . Deselect ( ) ;
break ;
}
} ;
2020-09-25 13:19:35 +08:00
SelectionHandler = CreateSelectionHandler ( ) ;
SelectionHandler . DeselectAll = deselectAll ;
2022-03-10 19:22:36 +08:00
SelectionHandler . SelectedItems . BindTo ( SelectedItems ) ;
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-09-08 16:22:59 +08:00
DragBox = CreateDragBox ( selectBlueprintsFromDragRectangle ) ,
2020-09-25 13:19:35 +08:00
SelectionHandler ,
2020-02-07 17:04:10 +08:00
SelectionBlueprints = CreateSelectionBlueprintContainer ( ) ,
2020-09-25 13:19:35 +08:00
SelectionHandler . CreateProxy ( ) ,
2020-01-16 10:54:03 +08:00
DragBox . CreateProxy ( ) . With ( p = > p . Depth = float . MinValue )
2020-01-15 18:09:49 +08:00
} ) ;
2019-08-29 15:06:40 +08:00
}
2021-04-27 14:40:35 +08:00
protected virtual Container < SelectionBlueprint < T > > CreateSelectionBlueprintContainer ( ) = > new Container < SelectionBlueprint < T > > { RelativeSizeAxes = Axes . Both } ;
2020-01-24 16:50:36 +08:00
2020-01-16 10:54:03 +08:00
/// <summary>
2021-04-27 17:33:47 +08:00
/// Creates a <see cref="Components.SelectionHandler{T}"/> which outlines items and handles movement of selections.
2020-01-16 10:54:03 +08:00
/// </summary>
2021-04-27 17:06:27 +08:00
protected abstract SelectionHandler < T > CreateSelectionHandler ( ) ;
2020-01-16 10:54:03 +08:00
/// <summary>
2021-04-27 17:33:47 +08:00
/// Creates a <see cref="SelectionBlueprint{T}"/> for a specific item.
2020-01-16 10:54:03 +08:00
/// </summary>
2021-04-27 17:33:47 +08:00
/// <param name="item">The item to create the overlay for.</param>
2021-06-23 08:40:07 +08:00
[CanBeNull]
2021-04-27 17:33:47 +08:00
protected virtual SelectionBlueprint < T > CreateBlueprintFor ( T item ) = > null ;
2020-01-16 10:54:03 +08:00
protected virtual DragBox CreateDragBox ( Action < RectangleF > performSelect ) = > new DragBox ( performSelect ) ;
2021-04-27 14:40:35 +08:00
/// <summary>
2021-04-28 11:02:55 +08:00
/// Whether this component is in a state where items outside a drag selection should be deselected. If false, selection will only be added to.
2021-04-27 14:40:35 +08:00
/// </summary>
2021-04-28 11:02:55 +08:00
protected virtual bool AllowDeselectionDuringDrag = > true ;
2021-04-27 14:40:35 +08:00
2019-10-24 13:58:02 +08:00
protected override bool OnMouseDown ( MouseDownEvent e )
{
2021-04-13 12:03:14 +08:00
bool selectionPerformed = performMouseDownActions ( e ) ;
2021-07-18 22:04:23 +08:00
bool movementPossible = prepareSelectionMovement ( ) ;
2020-01-22 20:43:02 +08:00
2021-10-27 02:52:15 +08:00
// check if selection has occurred
if ( selectionPerformed )
{
2021-10-27 04:24:53 +08:00
// only unmodified right click should show context menu
2021-11-04 15:02:37 +08:00
bool shouldShowContextMenu = e . Button = = MouseButton . Right & & ! e . ShiftPressed & & ! e . AltPressed & & ! e . SuperPressed ;
2021-10-27 04:24:53 +08:00
// stop propagation if not showing context menu
return ! shouldShowContextMenu ;
2021-10-27 02:52:15 +08:00
}
// even if a selection didn't occur, a drag event may still move the selection.
return e . Button = = MouseButton . Left & & movementPossible ;
2019-10-24 13:58:02 +08:00
}
2021-04-27 14:40:35 +08:00
protected SelectionBlueprint < T > ClickedBlueprint { get ; private set ; }
2020-07-17 16:03:57 +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 ;
2020-07-17 16:03:57 +08:00
// store for double-click handling
2021-04-27 14:40:35 +08:00
ClickedBlueprint = SelectionHandler . SelectedBlueprints . FirstOrDefault ( b = > b . IsHovered ) ;
2020-07-17 16:03:57 +08:00
2019-10-24 15:14:29 +08:00
// Deselection should only occur if no selected blueprints are hovered
2021-04-27 17:33:47 +08:00
// 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
2021-04-27 14:40:35 +08:00
if ( endClickSelection ( e ) | | ClickedBlueprint ! = null )
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 ;
2020-07-17 16:03:57 +08:00
// ensure the blueprint which was hovered for the first click is still the hovered blueprint.
2021-04-27 14:40:35 +08:00
if ( ClickedBlueprint = = null | | SelectionHandler . SelectedBlueprints . FirstOrDefault ( b = > b . IsHovered ) ! = ClickedBlueprint )
2019-11-06 17:15:57 +08:00
return false ;
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
2021-04-12 18:05:23 +08:00
Schedule ( ( ) = >
{
endClickSelection ( e ) ;
clickSelectionBegan = false ;
isDraggingBlueprint = false ;
} ) ;
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-11-25 16:25:54 +08:00
if ( movementBlueprints ! = null )
2020-04-09 21:00:56 +08:00
{
isDraggingBlueprint = true ;
changeHandler ? . BeginChange ( ) ;
2020-01-22 16:54:11 +08:00
return true ;
2020-04-09 21:00:56 +08:00
}
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-04-09 21:00:56 +08:00
if ( isDraggingBlueprint )
{
2021-04-27 17:57:51 +08:00
DragOperationCompleted ( ) ;
2020-04-09 21:00:56 +08:00
changeHandler ? . EndChange ( ) ;
}
2020-01-22 16:54:11 +08:00
if ( DragBox . State = = Visibility . Visible )
DragBox . Hide ( ) ;
2019-10-24 14:11:54 +08:00
}
2021-04-27 17:57:51 +08:00
/// <summary>
/// Called whenever a drag operation completes, before any change transaction is committed.
/// </summary>
protected virtual void DragOperationCompleted ( )
2021-04-27 14:40:35 +08:00
{
}
2019-11-11 12:41:10 +08:00
protected override bool OnKeyDown ( KeyDownEvent e )
{
switch ( e . Key )
{
case Key . Escape :
2020-09-25 13:19:35 +08:00
if ( ! SelectionHandler . SelectedBlueprints . Any ( ) )
2019-11-11 12:41:10 +08:00
return false ;
deselectAll ( ) ;
return true ;
}
return false ;
}
2021-09-16 17:26:12 +08:00
public bool OnPressed ( KeyBindingPressEvent < PlatformAction > e )
2019-11-11 12:41:10 +08:00
{
2021-11-18 11:36:15 +08:00
if ( e . Repeat )
return false ;
2021-09-16 17:26:12 +08:00
switch ( e . Action )
2019-11-11 12:41:10 +08:00
{
2021-07-20 13:23:34 +08:00
case PlatformAction . SelectAll :
2021-04-27 14:40:35 +08:00
SelectAll ( ) ;
2019-11-11 12:41:10 +08:00
return true ;
}
return false ;
}
2021-09-16 17:26:12 +08:00
public void OnReleased ( KeyBindingReleaseEvent < PlatformAction > e )
2020-01-22 12:22:34 +08:00
{
}
2019-11-11 12:41:10 +08:00
2019-10-24 15:17:48 +08:00
#region Blueprint Addition / Removal
2021-04-27 14:40:35 +08:00
protected virtual void AddBlueprintFor ( T item )
2018-10-18 15:36:06 +08:00
{
2021-04-27 14:40:35 +08:00
if ( blueprintMap . ContainsKey ( item ) )
2020-11-12 18:52:02 +08:00
return ;
2021-04-27 14:40:35 +08:00
var blueprint = CreateBlueprintFor ( item ) ;
2018-11-06 17:02:55 +08:00
if ( blueprint = = null )
2018-10-18 15:36:06 +08:00
return ;
2021-04-27 14:40:35 +08:00
blueprintMap [ item ] = blueprint ;
2020-11-12 18:52:02 +08:00
2021-04-27 14:40:35 +08:00
blueprint . Selected + = OnBlueprintSelected ;
blueprint . Deselected + = OnBlueprintDeselected ;
2020-11-12 18:52:02 +08:00
SelectionBlueprints . Add ( blueprint ) ;
2021-04-27 17:33:47 +08:00
if ( SelectionHandler . SelectedItems . Contains ( item ) )
blueprint . Select ( ) ;
OnBlueprintAdded ( blueprint . Item ) ;
2020-11-12 18:52:02 +08:00
}
2021-04-27 14:40:35 +08:00
protected void RemoveBlueprintFor ( T item )
2020-11-12 18:52:02 +08:00
{
2021-04-27 14:40:35 +08:00
if ( ! blueprintMap . Remove ( item , out var blueprint ) )
2020-11-12 18:52:02 +08:00
return ;
2018-11-06 16:51:26 +08:00
2020-11-12 18:52:02 +08:00
blueprint . Deselect ( ) ;
2021-04-27 14:40:35 +08:00
blueprint . Selected - = OnBlueprintSelected ;
blueprint . Deselected - = OnBlueprintDeselected ;
2018-11-06 16:51:26 +08:00
2020-02-07 17:04:10 +08:00
SelectionBlueprints . Remove ( blueprint ) ;
2020-06-23 19:36:09 +08:00
2020-11-25 16:25:54 +08:00
if ( movementBlueprints ? . Contains ( blueprint ) = = true )
2020-06-23 19:36:09 +08:00
finishSelectionMovement ( ) ;
2020-11-12 18:52:02 +08:00
2021-04-27 17:33:47 +08:00
OnBlueprintRemoved ( blueprint . Item ) ;
2018-11-06 16:51:26 +08:00
}
2020-11-12 18:52:02 +08:00
/// <summary>
2021-04-27 17:33:47 +08:00
/// Called after an item's blueprint has been added.
2020-11-12 18:52:02 +08:00
/// </summary>
2021-04-27 17:33:47 +08:00
/// <param name="item">The item for which the blueprint has been added.</param>
protected virtual void OnBlueprintAdded ( T item )
2019-08-29 15:06:40 +08:00
{
2020-11-12 18:52:02 +08:00
}
2020-09-12 19:31:50 +08:00
2020-11-12 18:52:02 +08:00
/// <summary>
2021-04-27 17:33:47 +08:00
/// Called after an item's blueprint has been removed.
2020-11-12 18:52:02 +08:00
/// </summary>
2021-04-27 17:33:47 +08:00
/// <param name="item">The item for which the blueprint has been removed.</param>
protected virtual void OnBlueprintRemoved ( T item )
2020-11-12 18:52:02 +08:00
{
2019-08-29 15:06:40 +08:00
}
2021-05-13 20:16:19 +08:00
/// <summary>
/// Retrieves an item's blueprint.
/// </summary>
/// <param name="item">The item to retrieve the blueprint of.</param>
/// <returns>The blueprint.</returns>
protected SelectionBlueprint < T > GetBlueprintFor ( T item ) = > blueprintMap [ item ] ;
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>
2020-11-04 16:53:03 +08:00
/// <returns>Whether a selection was performed.</returns>
2021-04-13 12:03:14 +08:00
private bool performMouseDownActions ( MouseButtonEvent e )
2019-10-24 14:58:22 +08:00
{
2020-11-18 12:37:15 +08:00
// Iterate from the top of the input stack (blueprints closest to the front of the screen first).
2021-03-20 05:20:40 +08:00
// Priority is given to already-selected blueprints.
2021-04-27 14:40:35 +08:00
foreach ( SelectionBlueprint < T > blueprint in SelectionBlueprints . AliveChildren . Reverse ( ) . OrderByDescending ( b = > b . IsSelected ) )
2019-10-24 14:58:22 +08:00
{
2020-11-10 16:16:28 +08:00
if ( ! blueprint . IsHovered ) continue ;
2021-04-13 12:03:14 +08:00
return clickSelectionBegan = SelectionHandler . MouseDownSelectionRequested ( blueprint , e ) ;
2019-10-24 14:58:22 +08:00
}
2020-11-03 19:45:48 +08:00
2020-11-16 04:06:47 +08:00
return false ;
2019-10-24 14:58:22 +08:00
}
/// <summary>
/// Finishes the current blueprint selection.
/// </summary>
2021-04-12 18:05:23 +08:00
/// <param name="e">The mouse event which triggered end of selection.</param>
2019-10-24 15:14:29 +08:00
/// <returns>Whether a click selection was active.</returns>
2021-04-12 18:05:23 +08:00
private bool endClickSelection ( MouseButtonEvent e )
2019-10-24 14:58:22 +08:00
{
2021-04-12 18:05:23 +08:00
if ( ! clickSelectionBegan & & ! isDraggingBlueprint )
{
// if a selection didn't occur, we may want to trigger a deselection.
if ( e . ControlPressed & & e . Button = = MouseButton . Left )
{
// Iterate from the top of the input stack (blueprints closest to the front of the screen first).
// Priority is given to already-selected blueprints.
2021-04-27 14:40:35 +08:00
foreach ( SelectionBlueprint < T > blueprint in SelectionBlueprints . AliveChildren . Reverse ( ) . OrderByDescending ( b = > b . IsSelected ) )
2021-04-12 18:05:23 +08:00
{
if ( ! blueprint . IsHovered ) continue ;
2021-04-13 12:03:14 +08:00
return clickSelectionBegan = SelectionHandler . MouseUpSelectionRequested ( blueprint , e ) ;
2021-04-12 18:05:23 +08:00
}
}
2019-10-24 15:14:29 +08:00
return false ;
2021-04-12 18:05:23 +08:00
}
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>
2020-09-08 16:22:59 +08:00
private void selectBlueprintsFromDragRectangle ( RectangleF rect )
2018-11-06 16:51:26 +08:00
{
2020-02-07 17:04:10 +08:00
foreach ( var blueprint in SelectionBlueprints )
2018-11-06 16:51:26 +08:00
{
2020-06-23 17:42:56 +08:00
// only run when utmost necessary to avoid unnecessary rect computations.
bool isValidForSelection ( ) = > blueprint . IsAlive & & blueprint . IsPresent & & rect . Contains ( blueprint . ScreenSpaceSelectionPoint ) ;
switch ( blueprint . State )
{
case SelectionState . NotSelected :
if ( isValidForSelection ( ) )
blueprint . Select ( ) ;
break ;
case SelectionState . Selected :
2021-04-28 11:02:55 +08:00
if ( AllowDeselectionDuringDrag & & ! isValidForSelection ( ) )
2020-06-23 17:42:56 +08:00
blueprint . Deselect ( ) ;
break ;
}
2018-11-06 16:51:26 +08:00
}
}
2019-11-11 12:41:10 +08:00
/// <summary>
2021-04-27 14:40:35 +08:00
/// Selects all <see cref="SelectionBlueprint{T}"/>s.
2019-11-11 12:41:10 +08:00
/// </summary>
2021-04-27 14:40:35 +08:00
protected virtual void SelectAll ( )
2020-11-13 16:08:20 +08:00
{
// Scheduled to allow the change in lifetime to take place.
Schedule ( ( ) = > SelectionBlueprints . ToList ( ) . ForEach ( m = > m . Select ( ) ) ) ;
}
2019-11-11 12:41:10 +08:00
2018-11-06 16:51:26 +08:00
/// <summary>
2021-04-27 14:40:35 +08:00
/// Deselects all selected <see cref="SelectionBlueprint{T}"/>s.
2018-11-06 16:51:26 +08:00
/// </summary>
2020-09-25 13:19:35 +08:00
private void deselectAll ( ) = > SelectionHandler . SelectedBlueprints . ToList ( ) . ForEach ( m = > m . Deselect ( ) ) ;
2018-11-06 16:51:26 +08:00
2021-04-27 14:40:35 +08:00
protected virtual void OnBlueprintSelected ( SelectionBlueprint < T > blueprint )
2018-11-06 16:51:26 +08:00
{
2020-09-25 13:19:35 +08:00
SelectionHandler . HandleSelected ( blueprint ) ;
2020-02-07 17:04:10 +08:00
SelectionBlueprints . ChangeChildDepth ( blueprint , 1 ) ;
2018-11-06 16:51:26 +08:00
}
2021-04-27 14:40:35 +08:00
protected virtual void OnBlueprintDeselected ( SelectionBlueprint < T > blueprint )
2018-11-06 16:51:26 +08:00
{
2020-02-07 17:04:10 +08:00
SelectionBlueprints . ChangeChildDepth ( blueprint , 0 ) ;
2021-04-16 14:55:33 +08:00
SelectionHandler . HandleDeselected ( blueprint ) ;
2018-11-06 16:51:26 +08:00
}
2019-10-24 14:58:22 +08:00
#endregion
#region Selection Movement
2020-11-24 16:14:39 +08:00
private Vector2 [ ] movementBlueprintOriginalPositions ;
2021-04-27 14:40:35 +08:00
private SelectionBlueprint < T > [ ] movementBlueprints ;
2020-04-09 21:00:56 +08:00
private bool isDraggingBlueprint ;
2019-10-23 17:58:15 +08:00
2019-10-24 14:11:54 +08:00
/// <summary>
/// Attempts to begin the movement of any selected blueprints.
/// </summary>
2021-07-18 22:04:23 +08:00
/// <returns>Whether a movement is possible.</returns>
private bool prepareSelectionMovement ( )
2019-10-23 17:37:57 +08:00
{
2020-09-25 13:19:35 +08:00
if ( ! SelectionHandler . SelectedBlueprints . Any ( ) )
2021-07-18 22:04:23 +08:00
return false ;
2019-10-24 14:11:54 +08:00
2021-04-27 17:33:47 +08:00
// 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.
2019-10-24 15:14:29 +08:00
// A special case is added for when a click selection occurred before the drag
2020-09-25 13:19:35 +08:00
if ( ! clickSelectionBegan & & ! SelectionHandler . SelectedBlueprints . Any ( b = > b . IsHovered ) )
2021-07-18 22:04:23 +08:00
return false ;
2019-10-24 14:11:54 +08:00
2021-04-27 17:33:47 +08:00
// Movement is tracked from the blueprint of the earliest item, since it only makes sense to distance snap from that item
2021-04-27 14:40:35 +08:00
movementBlueprints = SortForMovement ( SelectionHandler . SelectedBlueprints ) . ToArray ( ) ;
2020-11-25 16:25:54 +08:00
movementBlueprintOriginalPositions = movementBlueprints . Select ( m = > m . ScreenSpaceSelectionPoint ) . ToArray ( ) ;
2021-07-18 22:04:23 +08:00
return true ;
2019-10-23 17:37:57 +08:00
}
2018-11-06 16:51:26 +08:00
2021-04-27 17:33:47 +08:00
/// <summary>
/// Apply sorting of selected blueprints before performing movement. Generally used to surface the "main" item to the beginning of the collection.
/// </summary>
/// <param name="blueprints">The blueprints to be moved.</param>
/// <returns>Sorted blueprints.</returns>
2021-04-27 14:40:35 +08:00
protected virtual IEnumerable < SelectionBlueprint < T > > SortForMovement ( IReadOnlyList < SelectionBlueprint < T > > blueprints ) = > blueprints ;
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
{
2020-11-25 16:25:54 +08:00
if ( movementBlueprints = = null )
2019-10-24 14:11:54 +08:00
return false ;
2019-10-23 17:37:57 +08:00
2020-11-24 16:14:39 +08:00
Debug . Assert ( movementBlueprintOriginalPositions ! = null ) ;
2019-10-23 17:58:15 +08:00
2020-11-24 16:14:39 +08:00
Vector2 distanceTravelled = e . ScreenSpaceMousePosition - e . ScreenSpaceMouseDownPosition ;
2021-04-27 16:41:46 +08:00
if ( snapProvider ! = null )
2020-11-24 16:14:39 +08:00
{
2021-04-27 16:41:46 +08:00
// check for positional snap for every object in selection (for things like object-object snapping)
2021-10-27 12:04:41 +08:00
for ( int i = 0 ; i < movementBlueprintOriginalPositions . Length ; i + + )
2021-04-27 16:41:46 +08:00
{
2021-04-29 14:29:25 +08:00
Vector2 originalPosition = movementBlueprintOriginalPositions [ i ] ;
var testPosition = originalPosition + distanceTravelled ;
2020-11-24 16:14:39 +08:00
2021-04-27 16:41:46 +08:00
var positionalResult = snapProvider . SnapScreenSpacePositionToValidPosition ( testPosition ) ;
2020-11-24 16:14:39 +08:00
2021-04-27 16:41:46 +08:00
if ( positionalResult . ScreenSpacePosition = = testPosition ) continue ;
2020-11-24 16:14:39 +08:00
2021-04-29 14:29:25 +08:00
var delta = positionalResult . ScreenSpacePosition - movementBlueprints [ i ] . ScreenSpaceSelectionPoint ;
2021-04-27 16:41:46 +08:00
// attempt to move the objects, and abort any time based snapping if we can.
2021-04-29 14:29:25 +08:00
if ( SelectionHandler . HandleMovement ( new MoveSelectionEvent < T > ( movementBlueprints [ i ] , delta ) ) )
2021-04-27 16:41:46 +08:00
return true ;
}
2020-11-24 16:14:39 +08:00
}
// if no positional snapping could be performed, try unrestricted snapping from the earliest
2021-04-27 17:33:47 +08:00
// item in the selection.
2019-10-25 11:34:49 +08:00
2020-04-27 19:35:24 +08:00
// The final movement position, relative to movementBlueprintOriginalPosition.
2020-11-24 16:14:39 +08:00
Vector2 movePosition = movementBlueprintOriginalPositions . First ( ) + distanceTravelled ;
2020-01-22 20:43:02 +08:00
2020-04-27 19:35:24 +08:00
// Retrieve a snapped position.
2021-04-27 16:41:46 +08:00
var result = snapProvider ? . SnapScreenSpacePositionToValidTime ( movePosition ) ;
if ( result = = null )
{
2021-04-29 14:29:25 +08:00
return SelectionHandler . HandleMovement ( new MoveSelectionEvent < T > ( movementBlueprints . First ( ) , movePosition - movementBlueprints . First ( ) . ScreenSpaceSelectionPoint ) ) ;
2021-04-27 16:41:46 +08:00
}
2019-10-16 19:34:02 +08:00
2021-04-27 14:40:35 +08:00
return ApplySnapResult ( movementBlueprints , result ) ;
}
2019-10-23 17:37:57 +08:00
2021-04-27 16:41:46 +08:00
protected virtual bool ApplySnapResult ( SelectionBlueprint < T > [ ] blueprints , SnapResult result ) = >
2021-04-29 14:29:25 +08:00
SelectionHandler . HandleMovement ( new MoveSelectionEvent < T > ( blueprints . First ( ) , result . ScreenSpacePosition - blueprints . First ( ) . ScreenSpaceSelectionPoint ) ) ;
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
{
2020-11-25 16:25:54 +08:00
if ( movementBlueprints = = null )
2019-10-24 14:11:54 +08:00
return false ;
2020-11-24 16:14:39 +08:00
movementBlueprintOriginalPositions = null ;
2020-11-25 16:25:54 +08:00
movementBlueprints = 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
2018-04-13 17:19:50 +08:00
}
}