2019-01-24 17:43:03 +09: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 18:19:50 +09:00
2019-11-06 14:55:05 +09:00
using System ;
2018-04-13 18:19:50 +09:00
using System.Collections.Generic ;
2019-10-16 20:20:07 +09:00
using System.Linq ;
2018-04-13 18:19:50 +09:00
using osu.Game.Beatmaps ;
using osu.Game.Rulesets.Edit ;
using osu.Game.Rulesets.Edit.Tools ;
2019-04-08 18:32:05 +09:00
using osu.Game.Rulesets.Mods ;
2019-10-16 20:20:07 +09:00
using osu.Game.Rulesets.Objects ;
2018-10-17 18:01:38 +09:00
using osu.Game.Rulesets.Osu.Objects ;
using osu.Game.Rulesets.UI ;
2018-11-16 17:12:24 +09:00
using osu.Game.Screens.Edit.Compose.Components ;
2018-04-13 18:19:50 +09:00
namespace osu.Game.Rulesets.Osu.Edit
{
2018-10-17 18:01:38 +09:00
public class OsuHitObjectComposer : HitObjectComposer < OsuHitObject >
2018-04-13 18:19:50 +09:00
{
public OsuHitObjectComposer ( Ruleset ruleset )
: base ( ruleset )
{
}
2019-12-12 15:58:11 +09:00
protected override DrawableRuleset < OsuHitObject > CreateDrawableRuleset ( Ruleset ruleset , IBeatmap beatmap , IReadOnlyList < Mod > mods = null )
2019-04-08 18:32:05 +09:00
= > new DrawableOsuEditRuleset ( ruleset , beatmap , mods ) ;
2018-04-13 18:19:50 +09:00
2018-10-04 13:44:49 +09:00
protected override IReadOnlyList < HitObjectCompositionTool > CompositionTools = > new HitObjectCompositionTool [ ]
2018-04-13 18:19:50 +09:00
{
2018-10-03 16:27:26 +09:00
new HitCircleCompositionTool ( ) ,
2018-10-04 13:44:49 +09:00
new SliderCompositionTool ( ) ,
2018-10-29 18:35:46 +09:00
new SpinnerCompositionTool ( )
2018-04-13 18:19:50 +09:00
} ;
2020-01-15 19:09:49 +09:00
protected override ComposeBlueprintContainer CreateBlueprintContainer ( ) = > new OsuBlueprintContainer ( HitObjects ) ;
2019-10-16 20:20:07 +09:00
protected override DistanceSnapGrid CreateDistanceSnapGrid ( IEnumerable < HitObject > selectedHitObjects )
{
2020-02-07 19:08:49 +09:00
if ( BlueprintContainer . CurrentTool is SpinnerCompositionTool )
return null ;
2019-10-16 20:20:07 +09:00
var objects = selectedHitObjects . ToList ( ) ;
if ( objects . Count = = 0 )
2019-11-06 14:55:05 +09:00
return createGrid ( h = > h . StartTime < = EditorClock . CurrentTime ) ;
double minTime = objects . Min ( h = > h . StartTime ) ;
2019-11-06 16:04:20 +09:00
return createGrid ( h = > h . StartTime < minTime , objects . Count + 1 ) ;
2019-11-06 14:55:05 +09:00
}
2019-11-06 16:04:20 +09:00
/// <summary>
/// Creates a grid from the last <see cref="HitObject"/> matching a predicate to a target <see cref="HitObject"/>.
/// </summary>
/// <param name="sourceSelector">A predicate that matches <see cref="HitObject"/>s where the grid can start from.
/// Only the last <see cref="HitObject"/> matching the predicate is used.</param>
/// <param name="targetOffset">An offset from the <see cref="HitObject"/> selected via <paramref name="sourceSelector"/> at which the grid should stop.</param>
/// <returns>The <see cref="OsuDistanceSnapGrid"/> from a selected <see cref="HitObject"/> to a target <see cref="HitObject"/>.</returns>
private OsuDistanceSnapGrid createGrid ( Func < HitObject , bool > sourceSelector , int targetOffset = 1 )
2019-11-06 14:55:05 +09:00
{
2019-11-06 16:04:20 +09:00
if ( targetOffset < 1 ) throw new ArgumentOutOfRangeException ( nameof ( targetOffset ) ) ;
int sourceIndex = - 1 ;
2019-11-06 14:55:05 +09:00
for ( int i = 0 ; i < EditorBeatmap . HitObjects . Count ; i + + )
2019-10-16 20:20:07 +09:00
{
2019-11-06 16:04:20 +09:00
if ( ! sourceSelector ( EditorBeatmap . HitObjects [ i ] ) )
2019-11-06 14:55:05 +09:00
break ;
2019-10-16 20:20:07 +09:00
2019-11-06 16:04:20 +09:00
sourceIndex = i ;
2019-10-16 20:20:07 +09:00
}
2019-11-06 16:04:20 +09:00
if ( sourceIndex = = - 1 )
2019-11-06 14:55:05 +09:00
return null ;
2019-10-16 20:20:07 +09:00
2019-12-27 19:39:30 +09:00
HitObject sourceObject = EditorBeatmap . HitObjects [ sourceIndex ] ;
2019-12-17 16:35:40 +09:00
int targetIndex = sourceIndex + targetOffset ;
2019-12-27 19:39:30 +09:00
HitObject targetObject = null ;
2019-12-17 16:35:40 +09:00
// Keep advancing the target object while its start time falls before the end time of the source object
while ( true )
{
if ( targetIndex > = EditorBeatmap . HitObjects . Count )
break ;
if ( EditorBeatmap . HitObjects [ targetIndex ] . StartTime > = sourceObject . GetEndTime ( ) )
{
targetObject = EditorBeatmap . HitObjects [ targetIndex ] ;
break ;
}
targetIndex + + ;
}
2019-10-16 20:20:07 +09:00
2020-02-07 19:08:49 +09:00
if ( sourceObject is Spinner )
return null ;
2019-12-27 19:39:30 +09:00
return new OsuDistanceSnapGrid ( ( OsuHitObject ) sourceObject , ( OsuHitObject ) targetObject ) ;
2019-10-16 20:20:07 +09:00
}
2018-04-13 18:19:50 +09:00
}
}