2019-01-24 08:43:03 +00: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-11-16 08:12:24 +00:00
2019-11-20 12:19:49 +00:00
using System ;
2018-11-16 08:12:24 +00:00
using System.Linq ;
using osu.Framework.Allocation ;
2024-06-07 07:01:41 +00:00
using osu.Framework.Graphics ;
2020-05-27 14:15:16 +00:00
using osu.Game.Rulesets.Edit ;
2018-11-16 08:12:24 +00:00
using osu.Game.Rulesets.Mania.Objects ;
2021-04-27 06:40:35 +00:00
using osu.Game.Rulesets.Objects ;
2018-11-16 08:12:24 +00:00
using osu.Game.Screens.Edit.Compose.Components ;
namespace osu.Game.Rulesets.Mania.Edit
{
2021-04-27 06:40:35 +00:00
public partial class ManiaSelectionHandler : EditorSelectionHandler
2018-11-16 08:12:24 +00:00
{
[Resolved]
2020-05-27 14:15:16 +00:00
private HitObjectComposer composer { get ; set ; } = null ! ;
2018-11-16 08:12:24 +00:00
2024-06-07 07:01:41 +00:00
protected override void OnSelectionChanged ( )
{
base . OnSelectionChanged ( ) ;
var selectedObjects = SelectedItems . OfType < ManiaHitObject > ( ) . ToArray ( ) ;
SelectionBox . CanFlipX = canFlipX ( selectedObjects ) ;
SelectionBox . CanFlipY = canFlipY ( selectedObjects ) ;
}
2021-04-27 06:40:35 +00:00
public override bool HandleMovement ( MoveSelectionEvent < HitObject > moveEvent )
2018-11-16 08:12:24 +00:00
{
2021-05-13 12:41:06 +00:00
var hitObjectBlueprint = ( HitObjectSelectionBlueprint ) moveEvent . Blueprint ;
int lastColumn = ( ( ManiaHitObject ) hitObjectBlueprint . Item ) . Column ;
2019-10-08 09:57:03 +00:00
2019-10-08 10:08:23 +00:00
performColumnMovement ( lastColumn , moveEvent ) ;
2018-11-26 07:08:56 +00:00
2019-11-06 08:27:41 +00:00
return true ;
2018-11-26 07:08:56 +00:00
}
2024-06-07 07:01:41 +00:00
public override bool HandleFlip ( Direction direction , bool flipOverOrigin )
{
var selectedObjects = SelectedItems . OfType < ManiaHitObject > ( ) . ToArray ( ) ;
var maniaPlayfield = ( ( ManiaHitObjectComposer ) composer ) . Playfield ;
if ( selectedObjects . Length = = 0 )
return false ;
switch ( direction )
{
case Direction . Horizontal :
if ( ! canFlipX ( selectedObjects ) )
return false ;
int firstColumn = flipOverOrigin ? 0 : selectedObjects . Min ( ho = > ho . Column ) ;
int lastColumn = flipOverOrigin ? ( int ) EditorBeatmap . BeatmapInfo . Difficulty . CircleSize - 1 : selectedObjects . Max ( ho = > ho . Column ) ;
EditorBeatmap . PerformOnSelection ( hitObject = >
{
var maniaObject = ( ManiaHitObject ) hitObject ;
maniaPlayfield . Remove ( maniaObject ) ;
maniaObject . Column = firstColumn + ( lastColumn - maniaObject . Column ) ;
maniaPlayfield . Add ( maniaObject ) ;
} ) ;
return true ;
case Direction . Vertical :
if ( ! canFlipY ( selectedObjects ) )
return false ;
double selectionStartTime = selectedObjects . Min ( ho = > ho . StartTime ) ;
double selectionEndTime = selectedObjects . Max ( ho = > ho . GetEndTime ( ) ) ;
EditorBeatmap . PerformOnSelection ( hitObject = >
{
hitObject . StartTime = selectionStartTime + ( selectionEndTime - hitObject . GetEndTime ( ) ) ;
} ) ;
return true ;
default :
throw new ArgumentOutOfRangeException ( nameof ( direction ) , direction , "Cannot flip over the supplied direction." ) ;
}
}
private static bool canFlipX ( ManiaHitObject [ ] selectedObjects )
= > selectedObjects . Select ( ho = > ho . Column ) . Distinct ( ) . Count ( ) > 1 ;
private static bool canFlipY ( ManiaHitObject [ ] selectedObjects )
= > selectedObjects . Length > 1 & & selectedObjects . Min ( ho = > ho . StartTime ) < selectedObjects . Max ( ho = > ho . GetEndTime ( ) ) ;
2021-04-27 06:40:35 +00:00
private void performColumnMovement ( int lastColumn , MoveSelectionEvent < HitObject > moveEvent )
2018-11-16 08:12:24 +00:00
{
2020-05-27 14:15:16 +00:00
var maniaPlayfield = ( ( ManiaHitObjectComposer ) composer ) . Playfield ;
2021-04-29 06:29:25 +00:00
var currentColumn = maniaPlayfield . GetColumnByPosition ( moveEvent . Blueprint . ScreenSpaceSelectionPoint + moveEvent . ScreenSpaceDelta ) ;
2019-10-08 09:57:03 +00:00
if ( currentColumn = = null )
2018-11-16 08:12:24 +00:00
return ;
2019-10-08 09:57:03 +00:00
int columnDelta = currentColumn . Index - lastColumn ;
2018-11-16 08:12:24 +00:00
if ( columnDelta = = 0 )
return ;
int minColumn = int . MaxValue ;
int maxColumn = int . MinValue ;
Ensure selection is preserved when moving selection between columns
Closes https://github.com/ppy/osu/issues/29793.
I believe that the sequence of events that makes this happens is as
follows:
- User selects a range of objects. Some of those objects are off-screen,
and thus would be presumed to be not alive - except the blueprint
container forces them to remain alive, because they're part of the
selection.
- User moves the selection to another column, which is implemented by
temporarily removing the objects from the playfield, changing their
column, and re-adding them.
This sort of pattern is supposed to kick off the
`HitObjectUsageTransferred` flow in `HitObjectUsageEventBuffer` - and
it does... for objects that are *currently visible on screen* and thus
would be alive regardless of `SetKeepAlive()`. However, this does not
hold for objects that are off-screen - nothing ensures they are kept
alive again after re-adding, and thus they inadvertently become dead.
- Thus, this doesn't kick off the `BlueprintContainer` flows associated
with transferring objects to another column, and instead fires the
removal flows, which ensure that the off-screen objects that were
being moved are instead deselected.
I tried a few other options but found no better resolution than this -
calling `SetKeepAlive()` directly would require making it public, which
seems like a bad idea. There's really no good way to generically handle
this either, because it is the ruleset that decides that its way of
implementing this operation will be a removal and re-add of objects,
so...
2024-09-17 11:07:57 +00:00
var selectedObjects = EditorBeatmap . SelectedHitObjects . OfType < ManiaHitObject > ( ) . ToArray ( ) ;
2021-02-26 05:15:12 +00:00
// find min/max in an initial pass before actually performing the movement.
Ensure selection is preserved when moving selection between columns
Closes https://github.com/ppy/osu/issues/29793.
I believe that the sequence of events that makes this happens is as
follows:
- User selects a range of objects. Some of those objects are off-screen,
and thus would be presumed to be not alive - except the blueprint
container forces them to remain alive, because they're part of the
selection.
- User moves the selection to another column, which is implemented by
temporarily removing the objects from the playfield, changing their
column, and re-adding them.
This sort of pattern is supposed to kick off the
`HitObjectUsageTransferred` flow in `HitObjectUsageEventBuffer` - and
it does... for objects that are *currently visible on screen* and thus
would be alive regardless of `SetKeepAlive()`. However, this does not
hold for objects that are off-screen - nothing ensures they are kept
alive again after re-adding, and thus they inadvertently become dead.
- Thus, this doesn't kick off the `BlueprintContainer` flows associated
with transferring objects to another column, and instead fires the
removal flows, which ensure that the off-screen objects that were
being moved are instead deselected.
I tried a few other options but found no better resolution than this -
calling `SetKeepAlive()` directly would require making it public, which
seems like a bad idea. There's really no good way to generically handle
this either, because it is the ruleset that decides that its way of
implementing this operation will be a removal and re-add of objects,
so...
2024-09-17 11:07:57 +00:00
foreach ( var obj in selectedObjects )
2018-11-16 08:12:24 +00:00
{
if ( obj . Column < minColumn )
minColumn = obj . Column ;
if ( obj . Column > maxColumn )
maxColumn = obj . Column ;
}
2020-05-27 14:15:16 +00:00
columnDelta = Math . Clamp ( columnDelta , - minColumn , maniaPlayfield . TotalColumns - 1 - maxColumn ) ;
2018-11-16 08:12:24 +00:00
2021-02-26 05:15:12 +00:00
EditorBeatmap . PerformOnSelection ( h = >
{
2021-05-13 11:13:50 +00:00
maniaPlayfield . Remove ( h ) ;
( ( ManiaHitObject ) h ) . Column + = columnDelta ;
maniaPlayfield . Add ( h ) ;
2021-02-26 05:15:12 +00:00
} ) ;
Ensure selection is preserved when moving selection between columns
Closes https://github.com/ppy/osu/issues/29793.
I believe that the sequence of events that makes this happens is as
follows:
- User selects a range of objects. Some of those objects are off-screen,
and thus would be presumed to be not alive - except the blueprint
container forces them to remain alive, because they're part of the
selection.
- User moves the selection to another column, which is implemented by
temporarily removing the objects from the playfield, changing their
column, and re-adding them.
This sort of pattern is supposed to kick off the
`HitObjectUsageTransferred` flow in `HitObjectUsageEventBuffer` - and
it does... for objects that are *currently visible on screen* and thus
would be alive regardless of `SetKeepAlive()`. However, this does not
hold for objects that are off-screen - nothing ensures they are kept
alive again after re-adding, and thus they inadvertently become dead.
- Thus, this doesn't kick off the `BlueprintContainer` flows associated
with transferring objects to another column, and instead fires the
removal flows, which ensure that the off-screen objects that were
being moved are instead deselected.
I tried a few other options but found no better resolution than this -
calling `SetKeepAlive()` directly would require making it public, which
seems like a bad idea. There's really no good way to generically handle
this either, because it is the ruleset that decides that its way of
implementing this operation will be a removal and re-add of objects,
so...
2024-09-17 11:07:57 +00:00
// `HitObjectUsageEventBuffer`'s usage transferal flows and the playfield's `SetKeepAlive()` functionality do not combine well with this operation's usage pattern,
// leading to selections being sometimes partially dropped if some of the objects being moved are off screen
// (check blame for detailed explanation).
// thus, ensure that selection is preserved manually.
EditorBeatmap . SelectedHitObjects . Clear ( ) ;
EditorBeatmap . SelectedHitObjects . AddRange ( selectedObjects ) ;
2018-11-16 08:12:24 +00:00
}
}
}