1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 23:07:26 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs

101 lines
4.0 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;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
2019-10-16 19:20:07 +08:00
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Tools;
2019-04-08 17:32:05 +08:00
using osu.Game.Rulesets.Mods;
2019-10-16 19:20:07 +08:00
using osu.Game.Rulesets.Objects;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Objects.Drawables;
2018-11-07 15:08:56 +08:00
using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles;
using osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders;
using osu.Game.Rulesets.Osu.Edit.Blueprints.Spinners;
using osu.Game.Rulesets.Osu.Objects;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Edit.Compose.Components;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Edit
{
public class OsuHitObjectComposer : HitObjectComposer<OsuHitObject>
2018-04-13 17:19:50 +08:00
{
public OsuHitObjectComposer(Ruleset ruleset)
: base(ruleset)
{
}
protected override DrawableRuleset<OsuHitObject> CreateDrawableRuleset(Ruleset ruleset, IWorkingBeatmap beatmap, IReadOnlyList<Mod> mods)
2019-04-08 17:32:05 +08:00
=> new DrawableOsuEditRuleset(ruleset, beatmap, mods);
2018-04-13 17:19:50 +08:00
protected override IReadOnlyList<HitObjectCompositionTool> CompositionTools => new HitObjectCompositionTool[]
2018-04-13 17:19:50 +08:00
{
new HitCircleCompositionTool(),
new SliderCompositionTool(),
2018-10-29 17:35:46 +08:00
new SpinnerCompositionTool()
2018-04-13 17:19:50 +08:00
};
2018-11-19 15:58:11 +08:00
public override SelectionHandler CreateSelectionHandler() => new OsuSelectionHandler();
2018-11-06 17:03:21 +08:00
public override SelectionBlueprint CreateBlueprintFor(DrawableHitObject hitObject)
2018-04-13 17:19:50 +08:00
{
switch (hitObject)
{
case DrawableHitCircle circle:
2018-11-06 16:56:04 +08:00
return new HitCircleSelectionBlueprint(circle);
2019-04-01 11:44:46 +08:00
2018-04-13 17:19:50 +08:00
case DrawableSlider slider:
2018-11-06 16:56:04 +08:00
return new SliderSelectionBlueprint(slider);
2019-04-01 11:44:46 +08:00
2018-10-29 17:23:23 +08:00
case DrawableSpinner spinner:
2018-11-06 16:56:04 +08:00
return new SpinnerSelectionBlueprint(spinner);
2018-04-13 17:19:50 +08:00
}
2018-11-06 17:03:21 +08:00
return base.CreateBlueprintFor(hitObject);
2018-04-13 17:19:50 +08:00
}
2019-10-16 19:20:07 +08:00
protected override DistanceSnapGrid CreateDistanceSnapGrid(IEnumerable<HitObject> selectedHitObjects)
{
var objects = selectedHitObjects.ToList();
if (objects.Count == 0)
return createGrid(h => h.StartTime <= EditorClock.CurrentTime);
double minTime = objects.Min(h => h.StartTime);
2019-11-06 15:04:20 +08:00
return createGrid(h => h.StartTime < minTime, objects.Count + 1);
}
2019-11-06 15:04:20 +08: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 15:04:20 +08:00
if (targetOffset < 1) throw new ArgumentOutOfRangeException(nameof(targetOffset));
int sourceIndex = -1;
for (int i = 0; i < EditorBeatmap.HitObjects.Count; i++)
2019-10-16 19:20:07 +08:00
{
2019-11-06 15:04:20 +08:00
if (!sourceSelector(EditorBeatmap.HitObjects[i]))
break;
2019-10-16 19:20:07 +08:00
2019-11-06 15:04:20 +08:00
sourceIndex = i;
2019-10-16 19:20:07 +08:00
}
2019-11-06 15:04:20 +08:00
if (sourceIndex == -1)
return null;
2019-10-16 19:20:07 +08:00
2019-11-06 15:04:20 +08:00
OsuHitObject sourceObject = EditorBeatmap.HitObjects[sourceIndex];
OsuHitObject targetObject = sourceIndex + targetOffset < EditorBeatmap.HitObjects.Count ? EditorBeatmap.HitObjects[sourceIndex + targetOffset] : null;
2019-10-16 19:20:07 +08:00
2019-11-06 15:04:20 +08:00
return new OsuDistanceSnapGrid(sourceObject, targetObject);
2019-10-16 19:20:07 +08:00
}
2018-04-13 17:19:50 +08:00
}
}