1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-05 10:45:36 +08:00

Turn slider path extensions into IRevertibleChange

This commit is contained in:
OliBomby 2024-10-18 22:48:41 +02:00
parent b710742d2f
commit 9f62626bfa
10 changed files with 257 additions and 15 deletions

View File

@ -10,6 +10,7 @@ using osu.Game.Rulesets.Catch.UI;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.UI;
using osu.Game.Rulesets.UI.Scrolling;
using osu.Game.Screens.Edit.Changes;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
using Direction = osu.Framework.Graphics.Direction;
@ -95,9 +96,10 @@ namespace osu.Game.Rulesets.Catch.Edit
if (h is JuiceStream juiceStream)
{
juiceStream.Path.Reverse(out Vector2 positionalOffset);
juiceStream.OriginalX += positionalOffset.X;
juiceStream.LegacyConvertedY += positionalOffset.Y;
var reverse = new ReverseSliderPathChange(juiceStream.Path);
reverse.Apply();
juiceStream.OriginalX += reverse.PositionalOffset.X;
juiceStream.LegacyConvertedY += reverse.PositionalOffset.Y;
EditorBeatmap.Update(juiceStream);
}
}

View File

@ -380,7 +380,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders.Components
EnsureValidPathTypes();
if (hitObject.Path.Distance < originalDistance)
hitObject.SnapTo(distanceSnapProvider, changeHandler);
new SnapToChange<T>(hitObject, distanceSnapProvider).Submit(changeHandler);
else
new ExpectedDistanceChange(hitObject.Path, originalDistance).Submit(changeHandler);
@ -454,7 +454,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders.Components
}
// Snap the path to the current beat divisor before checking length validity.
hitObject.SnapTo(distanceSnapProvider, changeHandler);
new SnapToChange<T>(hitObject, distanceSnapProvider).Submit(changeHandler);
if (!hitObject.Path.HasValidLength)
{
@ -464,7 +464,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders.Components
new PositionChange(hitObject, oldPosition).Submit(changeHandler);
new StartTimeChange(hitObject, oldStartTime).Submit(changeHandler);
// Snap the path length again to undo the invalid length.
hitObject.SnapTo(distanceSnapProvider, changeHandler);
new SnapToChange<T>(hitObject, distanceSnapProvider).Submit(changeHandler);
return;
}

View File

@ -303,7 +303,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
if (Precision.AlmostBigger(segmentEnds[segmentIndex], 1, 1E-3))
{
sliderPath.ControlPoints.SubmitRemoveRange(i + 1, sliderPath.ControlPoints.Count - i - 1, changeHandler);
new RemoveRangePathControlPointChange(sliderPath.ControlPoints, i + 1, sliderPath.ControlPoints.Count - i - 1).Submit(changeHandler);
new PathControlPointTypeChange(sliderPath.ControlPoints[^1], null).Submit(changeHandler);
break;
}
@ -443,7 +443,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
ControlPointVisualiser?.EnsureValidPathTypes();
HitObject.SnapTo(distanceSnapProvider, changeHandler);
new SnapToChange<Slider>(HitObject, distanceSnapProvider).Submit(changeHandler);
return pathControlPoint;
}
@ -467,7 +467,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
ControlPointVisualiser?.EnsureValidPathTypes();
// Snap the slider to the current beat divisor before checking length validity.
HitObject.SnapTo(distanceSnapProvider, changeHandler);
new SnapToChange<Slider>(HitObject, distanceSnapProvider).Submit(changeHandler);
// If there are 0 or 1 remaining control points, or the slider has an invalid length, it is in a degenerate form and should be deleted
if (controlPoints.Count <= 1 || !HitObject.Path.HasValidLength)
@ -512,7 +512,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
// Extract the split portion and remove from the original slider.
var splitControlPoints = controlPoints.Take(index + 1).ToList();
HitObject.Path.ControlPoints.SubmitRemoveRange(0, index, changeHandler);
new RemoveRangePathControlPointChange(HitObject.Path.ControlPoints, 0, index).Submit(changeHandler);
var newSlider = new Slider
{

View File

@ -115,8 +115,9 @@ namespace osu.Game.Rulesets.Osu.Edit
if (h is Slider slider)
{
slider.Path.Reverse(out Vector2 offset, changeHandler);
new PositionChange(slider, slider.Position + offset).Submit(changeHandler);
var reverse = new ReverseSliderPathChange(slider.Path);
reverse.Submit(changeHandler);
new PositionChange(slider, slider.Position + reverse.PositionalOffset).Submit(changeHandler);
}
}
@ -273,7 +274,7 @@ namespace osu.Game.Rulesets.Osu.Edit
new RemovePathControlPointChange(mergedHitObject.Path.ControlPoints, mergedHitObject.Path.ControlPoints.Count - 1).Submit(changeHandler);
}
mergedHitObject.Path.ControlPoints.SubmitAddRange(hasPath.Path.ControlPoints.Select(o => new PathControlPoint(o.Position + offset, o.Type)), changeHandler);
new AddRangePathControlPointChange(mergedHitObject.Path.ControlPoints, hasPath.Path.ControlPoints.Select(o => new PathControlPoint(o.Position + offset, o.Type))).Submit(changeHandler);
lastCircle = false;
}
else

View File

@ -175,7 +175,7 @@ namespace osu.Game.Rulesets.Osu.Edit
// Snap the slider's length to the current beat divisor
// to calculate the final resulting duration / bounding box before the final checks.
slider.SnapTo(snapProvider, changeHandler);
new SnapToChange<Slider>(slider, snapProvider).Submit(changeHandler);
new PositionChange(slider, GeometryUtils.GetScaledPosition(scale, origin, originalInfo.Position, axisRotation)).Submit(changeHandler);
@ -194,7 +194,7 @@ namespace osu.Game.Rulesets.Osu.Edit
new PositionChange(slider, originalInfo.Position).Submit(changeHandler);
// Snap the slider's length again to undo the potentially-invalid length applied by the previous snap.
slider.SnapTo(snapProvider, changeHandler);
new SnapToChange<Slider>(slider, snapProvider).Submit(changeHandler);
}
private (bool X, bool Y) isQuadInBounds(Quad quad)

View File

@ -0,0 +1,30 @@
// 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.
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Game.Rulesets.Objects;
namespace osu.Game.Screens.Edit.Changes
{
/// <summary>
/// Adds a range of <see cref="PathControlPoint"/>s to the provided <see cref="BindableList{T}"/>.
/// </summary>
public class AddRangePathControlPointChange : CompositeChange
{
private readonly BindableList<PathControlPoint> controlPoints;
private readonly IEnumerable<PathControlPoint> points;
public AddRangePathControlPointChange(BindableList<PathControlPoint> controlPoints, IEnumerable<PathControlPoint> points)
{
this.controlPoints = controlPoints;
this.points = points;
}
protected override void SubmitChanges()
{
foreach (var point in points)
Submit(new InsertPathControlPointChange(controlPoints, controlPoints.Count, point));
}
}
}

View File

@ -0,0 +1,46 @@
// 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.
using System.Collections.Generic;
namespace osu.Game.Screens.Edit.Changes
{
public abstract class CompositeChange : IRevertibleChange
{
private List<IRevertibleChange>? changes;
public void Apply()
{
if (changes == null)
{
changes = new List<IRevertibleChange>();
SubmitChanges();
return;
}
foreach (var change in changes)
change.Apply();
}
public void Revert()
{
if (changes == null)
throw new System.InvalidOperationException("Cannot revert before applying.");
for (int i = changes.Count - 1; i >= 0; i--)
changes[i].Revert();
}
protected void Submit(IRevertibleChange change)
{
change.Apply();
changes!.Add(change);
}
/// <summary>
/// Applies the tracks the changes of this <see cref="CompositeChange"/>.
/// </summary>
/// <remarks>Use <see cref="Submit"/> to apply the <see cref="IRevertibleChange"/> created in this method.</remarks>
protected abstract void SubmitChanges();
}
}

View File

@ -0,0 +1,31 @@
// 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.
using osu.Framework.Bindables;
using osu.Game.Rulesets.Objects;
namespace osu.Game.Screens.Edit.Changes
{
/// <summary>
/// Removes a range of <see cref="PathControlPoint"/>s from the provided <see cref="BindableList{T}"/>.
/// </summary>
public class RemoveRangePathControlPointChange : CompositeChange
{
private readonly BindableList<PathControlPoint> controlPoints;
private readonly int startIndex;
private readonly int count;
public RemoveRangePathControlPointChange(BindableList<PathControlPoint> controlPoints, int startIndex, int count)
{
this.controlPoints = controlPoints;
this.startIndex = startIndex;
this.count = count;
}
protected override void SubmitChanges()
{
for (int i = 0; i < count; i++)
Submit(new RemovePathControlPointChange(controlPoints, startIndex));
}
}
}

View File

@ -0,0 +1,102 @@
// 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.
using System.Collections.Generic;
using System.Linq;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osuTK;
namespace osu.Game.Screens.Edit.Changes
{
/// <summary>
/// Reverse the direction of this path.
/// </summary>
public class ReverseSliderPathChange : CompositeChange
{
/// <summary>
/// The positional offset of the resulting path. It should be added to the start position of the path.
/// </summary>
public Vector2 PositionalOffset { get; private set; }
private readonly SliderPath sliderPath;
/// <summary>
/// Reverse the direction of this path.
/// </summary>
/// <param name="sliderPath">The <see cref="SliderPath"/>.</param>
public ReverseSliderPathChange(SliderPath sliderPath)
{
this.sliderPath = sliderPath;
}
protected override void SubmitChanges()
{
var controlPoints = sliderPath.ControlPoints;
var inheritedLinearPoints = controlPoints.Where(p => sliderPath.PointsInSegment(p)[0].Type == PathType.LINEAR && p.Type == null).ToList();
// Inherited points after a linear point, as well as the first control point if it inherited,
// should be treated as linear points, so their types are temporarily changed to linear.
inheritedLinearPoints.ForEach(p => Submit(new PathControlPointTypeChange(p, PathType.LINEAR)));
double[] segmentEnds = sliderPath.GetSegmentEnds().ToArray();
// Remove segments after the end of the slider.
for (int numSegmentsToRemove = segmentEnds.Count(se => se >= 1) - 1; numSegmentsToRemove > 0 && controlPoints.Count > 0;)
{
if (controlPoints.Last().Type is not null)
{
numSegmentsToRemove--;
segmentEnds = segmentEnds[..^1];
}
Submit(new RemovePathControlPointChange(controlPoints, controlPoints.Count - 1));
}
// Restore original control point types.
inheritedLinearPoints.ForEach(p => Submit(new PathControlPointTypeChange(p, null)));
// Recalculate middle perfect curve control points at the end of the slider path.
if (controlPoints.Count >= 3 && controlPoints[^3].Type == PathType.PERFECT_CURVE && controlPoints[^2].Type == null && segmentEnds.Any())
{
double lastSegmentStart = segmentEnds.Length > 1 ? segmentEnds[^2] : 0;
double lastSegmentEnd = segmentEnds[^1];
var circleArcPath = new List<Vector2>();
sliderPath.GetPathToProgress(circleArcPath, lastSegmentStart / lastSegmentEnd, 1);
Submit(new PathControlPointPositionChange(controlPoints[^2], circleArcPath[circleArcPath.Count / 2]));
}
reverseControlPoints();
}
private void reverseControlPoints()
{
var points = sliderPath.ControlPoints.ToArray();
PositionalOffset = sliderPath.PositionAt(1);
Submit(new RemoveRangePathControlPointChange(sliderPath.ControlPoints, 0, sliderPath.ControlPoints.Count));
PathType? lastType = null;
for (int i = 0; i < points.Length; i++)
{
var p = new PathControlPoint(points[i].Position, points[i].Type);
p.Position -= PositionalOffset;
// propagate types forwards to last null type
if (i == points.Length - 1)
{
p.Type = lastType;
p.Position = Vector2.Zero;
}
else if (p.Type != null)
(p.Type, lastType) = (lastType, p.Type);
Submit(new InsertPathControlPointChange(sliderPath.ControlPoints, 0, p));
}
}
}
}

View File

@ -0,0 +1,30 @@
// 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.
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
namespace osu.Game.Screens.Edit.Changes
{
/// <summary>
/// Snaps the provided <see cref="HitObject"/>'s duration using the <see cref="IDistanceSnapProvider"/>.
/// </summary>
public class SnapToChange<THitObject> : CompositeChange where THitObject : HitObject, IHasPath
{
private readonly THitObject hitObject;
private readonly IDistanceSnapProvider? snapProvider;
public SnapToChange(THitObject hitObject, IDistanceSnapProvider? snapProvider)
{
this.hitObject = hitObject;
this.snapProvider = snapProvider;
}
protected override void SubmitChanges()
{
double newDistance = snapProvider?.FindSnappedDistance(hitObject, (float)hitObject.Path.CalculatedDistance, DistanceSnapTarget.Start) ?? hitObject.Path.CalculatedDistance;
Submit(new ExpectedDistanceChange(hitObject.Path, newDistance));
}
}
}