Skip to content

Commit

Permalink
refactor to alternative extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
OliBomby committed Oct 17, 2024
1 parent cb5be12 commit b710742
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ private void ensureValidPathType(IReadOnlyList<PathControlPoint> segment)
return;

if (segment.Count > 3)
changeHandler.SafeSubmit(new PathControlPointTypeChange(first, PathType.BEZIER));
new PathControlPointTypeChange(first, PathType.BEZIER).Submit(changeHandler);

if (segment.Count != 3)
return;

ReadOnlySpan<Vector2> points = segment.Select(p => p.Position).ToArray();
RectangleF boundingBox = PathApproximator.CircularArcBoundingBox(points);
if (boundingBox.Width >= 640 || boundingBox.Height >= 480)
changeHandler.SafeSubmit(new PathControlPointTypeChange(first, PathType.BEZIER));
new PathControlPointTypeChange(first, PathType.BEZIER).Submit(changeHandler);
}

/// <summary>
Expand Down Expand Up @@ -370,19 +370,19 @@ private void updatePathTypeOfSelectedPieces(PathType? type)
int thirdPointIndex = indexInSegment + 2;

if (pointsInSegment.Count > thirdPointIndex + 1)
changeHandler.SafeSubmit(new PathControlPointTypeChange(pointsInSegment[thirdPointIndex], pointsInSegment[0].Type));
new PathControlPointTypeChange(pointsInSegment[thirdPointIndex], pointsInSegment[0].Type).Submit(changeHandler);
}

changeHandler.SafeSubmit(new ExpectedDistanceChange(hitObject.Path, null));
changeHandler.SafeSubmit(new PathControlPointTypeChange(p.ControlPoint, type));
new ExpectedDistanceChange(hitObject.Path, null).Submit(changeHandler);
new PathControlPointTypeChange(p.ControlPoint, type).Submit(changeHandler);
}

EnsureValidPathTypes();

if (hitObject.Path.Distance < originalDistance)
hitObject.SnapTo(distanceSnapProvider, changeHandler);
else
changeHandler.SafeSubmit(new ExpectedDistanceChange(hitObject.Path, originalDistance));
new ExpectedDistanceChange(hitObject.Path, originalDistance).Submit(changeHandler);

changeHandler?.EndChange();
}
Expand Down Expand Up @@ -425,8 +425,8 @@ public void DragInProgress(DragEvent e)

Vector2 movementDelta = Parent!.ToLocalSpace(result?.ScreenSpacePosition ?? newHeadPosition) - hitObject.Position;

changeHandler.SafeSubmit(new PositionChange(hitObject, hitObject.Position + movementDelta));
changeHandler.SafeSubmit(new StartTimeChange(hitObject, result?.Time ?? hitObject.StartTime));
new PositionChange(hitObject, hitObject.Position + movementDelta).Submit(changeHandler);
new StartTimeChange(hitObject, result?.Time ?? hitObject.StartTime).Submit(changeHandler);

for (int i = 1; i < hitObject.Path.ControlPoints.Count; i++)
{
Expand All @@ -436,7 +436,7 @@ public void DragInProgress(DragEvent e)
// All other selected control points (if any) will move together with the head point
// (and so they will not move at all, relative to each other).
if (!selectedControlPoints.Contains(controlPoint))
changeHandler.SafeSubmit(new PathControlPointPositionChange(controlPoint, controlPoint.Position - movementDelta));
new PathControlPointPositionChange(controlPoint, controlPoint.Position - movementDelta).Submit(changeHandler);
}
}
else
Expand All @@ -449,7 +449,7 @@ public void DragInProgress(DragEvent e)
{
PathControlPoint controlPoint = controlPoints[i];
if (selectedControlPoints.Contains(controlPoint))
changeHandler.SafeSubmit(new PathControlPointPositionChange(controlPoint, dragStartPositions[i] + movementDelta));
new PathControlPointPositionChange(controlPoint, dragStartPositions[i] + movementDelta).Submit(changeHandler);
}
}

Expand All @@ -459,18 +459,18 @@ public void DragInProgress(DragEvent e)
if (!hitObject.Path.HasValidLength)
{
for (int i = 0; i < hitObject.Path.ControlPoints.Count; i++)
changeHandler.SafeSubmit(new PathControlPointPositionChange(hitObject.Path.ControlPoints[i], oldControlPoints[i]));
new PathControlPointPositionChange(hitObject.Path.ControlPoints[i], oldControlPoints[i]).Submit(changeHandler);

changeHandler.SafeSubmit(new PositionChange(hitObject, oldPosition));
changeHandler.SafeSubmit(new StartTimeChange(hitObject, oldStartTime));
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);
return;
}

// Maintain the path types in case they got defaulted to bezier at some point during the drag.
for (int i = 0; i < hitObject.Path.ControlPoints.Count; i++)
changeHandler.SafeSubmit(new PathControlPointTypeChange(hitObject.Path.ControlPoints[i], dragPathTypes[i]));
new PathControlPointTypeChange(hitObject.Path.ControlPoints[i], dragPathTypes[i]).Submit(changeHandler);

EnsureValidPathTypes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ private void adjustLength(double proposedDistance, bool adjustVelocity)
if (Precision.AlmostEquals(proposedDistance, HitObject.Path.Distance) && Precision.AlmostEquals(proposedVelocity, HitObject.SliderVelocityMultiplier))
return;

changeHandler.SafeSubmit(new SliderVelocityMultiplierChange(HitObject, proposedVelocity));
changeHandler.SafeSubmit(new ExpectedDistanceChange(HitObject.Path, proposedDistance));
changeHandler.SafeSubmit(new QueueUpdateHitObject(editorBeatmap, HitObject));
new SliderVelocityMultiplierChange(HitObject, proposedVelocity).Submit(changeHandler);
new ExpectedDistanceChange(HitObject.Path, proposedDistance).Submit(changeHandler);
new QueueUpdateHitObject(editorBeatmap, HitObject).Submit(changeHandler);
}

/// <summary>
Expand All @@ -304,7 +304,7 @@ private void trimExcessControlPoints(SliderPath sliderPath)
if (Precision.AlmostBigger(segmentEnds[segmentIndex], 1, 1E-3))
{
sliderPath.ControlPoints.SubmitRemoveRange(i + 1, sliderPath.ControlPoints.Count - i - 1, changeHandler);
changeHandler.SafeSubmit(new PathControlPointTypeChange(sliderPath.ControlPoints[^1], null));
new PathControlPointTypeChange(sliderPath.ControlPoints[^1], null).Submit(changeHandler);
break;
}

Expand Down Expand Up @@ -439,7 +439,7 @@ private PathControlPoint addControlPoint(Vector2 position)
var pathControlPoint = new PathControlPoint { Position = position };

// Move the control points from the insertion index onwards to make room for the insertion
changeHandler.SafeSubmit(new InsertPathControlPointChange(HitObject.Path.ControlPoints, insertionIndex, pathControlPoint));
new InsertPathControlPointChange(HitObject.Path.ControlPoints, insertionIndex, pathControlPoint).Submit(changeHandler);

ControlPointVisualiser?.EnsureValidPathTypes();

Expand All @@ -459,9 +459,9 @@ private void removeControlPoints(List<PathControlPoint> toRemove)
// The first control point in the slider must have a type, so take it from the previous "first" one
// Todo: Should be handled within SliderPath itself
if (c == controlPoints[0] && controlPoints.Count > 1 && controlPoints[1].Type == null)
changeHandler.SafeSubmit(new PathControlPointTypeChange(controlPoints[1], controlPoints[0].Type));
new PathControlPointTypeChange(controlPoints[1], controlPoints[0].Type).Submit(changeHandler);

changeHandler.SafeSubmit(new RemovePathControlPointChange(HitObject.Path.ControlPoints, c));
new RemovePathControlPointChange(HitObject.Path.ControlPoints, c).Submit(changeHandler);
}

ControlPointVisualiser?.EnsureValidPathTypes();
Expand All @@ -480,9 +480,9 @@ private void removeControlPoints(List<PathControlPoint> toRemove)
// So the slider needs to be offset by this amount instead, and all control points offset backwards such that the path is re-positioned at (0, 0)
Vector2 first = controlPoints[0].Position;
foreach (var c in controlPoints)
changeHandler.SafeSubmit(new PathControlPointPositionChange(c, c.Position - first));
new PathControlPointPositionChange(c, c.Position - first).Submit(changeHandler);

changeHandler.SafeSubmit(new PositionChange(HitObject, HitObject.Position + first));
new PositionChange(HitObject, HitObject.Position + first).Submit(changeHandler);
}

private void splitControlPoints(List<PathControlPoint> controlPointsToSplitAt)
Expand Down Expand Up @@ -526,28 +526,28 @@ private void splitControlPoints(List<PathControlPoint> controlPointsToSplitAt)
};

// Increase the start time of the slider before adding the new slider so the new slider is immediately inserted at the correct index and internal state remains valid.
changeHandler.SafeSubmit(new StartTimeChange(HitObject, HitObject.StartTime + split_gap));
new StartTimeChange(HitObject, HitObject.StartTime + split_gap).Submit(changeHandler);

changeHandler.SafeSubmit(new AddHitObjectChange(editorBeatmap, newSlider));
new AddHitObjectChange(editorBeatmap, newSlider).Submit(changeHandler);

changeHandler.SafeSubmit(new NewComboChange(HitObject, false));
changeHandler.SafeSubmit(new ExpectedDistanceChange(HitObject.Path, HitObject.Path.ExpectedDistance.Value - newSlider.Path.CalculatedDistance));
changeHandler.SafeSubmit(new StartTimeChange(HitObject, HitObject.StartTime + newSlider.SpanDuration));
new NewComboChange(HitObject, false).Submit(changeHandler);
new ExpectedDistanceChange(HitObject.Path, HitObject.Path.ExpectedDistance.Value - newSlider.Path.CalculatedDistance).Submit(changeHandler);
new StartTimeChange(HitObject, HitObject.StartTime + newSlider.SpanDuration).Submit(changeHandler);

// In case the remainder of the slider has no length left over, give it length anyways so we don't get a 0 length slider.
if (HitObject.Path.ExpectedDistance.Value <= Precision.DOUBLE_EPSILON)
{
changeHandler.SafeSubmit(new ExpectedDistanceChange(HitObject.Path, null));
new ExpectedDistanceChange(HitObject.Path, null).Submit(changeHandler);
}
}

// Once all required pieces have been split off, the original slider has the final split.
// As a final step, we must reset its control points to have an origin of (0,0).
Vector2 first = controlPoints[0].Position;
foreach (var c in controlPoints)
changeHandler.SafeSubmit(new PathControlPointPositionChange(c, c.Position - first));
new PathControlPointPositionChange(c, c.Position - first).Submit(changeHandler);

changeHandler.SafeSubmit(new PositionChange(HitObject, HitObject.Position + first));
new PositionChange(HitObject, HitObject.Position + first).Submit(changeHandler);
}

private void convertToStream()
Expand Down Expand Up @@ -575,19 +575,19 @@ private void convertToStream()

Vector2 position = HitObject.Position + HitObject.Path.PositionAt(pathPosition);

changeHandler.SafeSubmit(new AddHitObjectChange(editorBeatmap, new HitCircle
new AddHitObjectChange(editorBeatmap, new HitCircle
{
StartTime = time,
Position = position,
NewCombo = i == 0 && HitObject.NewCombo,
Samples = HitObject.HeadCircle.Samples.Select(s => s.With()).ToList()
}));
}).Submit(changeHandler);

i += 1;
time = HitObject.StartTime + i * streamSpacing;
}

changeHandler.SafeSubmit(new RemoveHitObjectChange(editorBeatmap, HitObject));
new RemoveHitObjectChange(editorBeatmap, HitObject).Submit(changeHandler);

changeHandler?.EndChange();
}
Expand Down
Loading

0 comments on commit b710742

Please sign in to comment.