2019-01-24 16:43:03 +08: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-10-04 12:44:49 +08:00
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2023-12-07 07:26:13 +08:00
|
|
|
using System;
|
2023-11-21 10:33:05 +08:00
|
|
|
using System.Collections.Generic;
|
2020-04-13 12:57:40 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2020-04-13 14:31:46 +08:00
|
|
|
using JetBrains.Annotations;
|
2018-10-04 17:24:25 +08:00
|
|
|
using osu.Framework.Allocation;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Framework.Graphics;
|
2019-10-23 15:03:16 +08:00
|
|
|
using osu.Framework.Input;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2023-11-11 17:45:22 +08:00
|
|
|
using osu.Framework.Utils;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Game.Rulesets.Edit;
|
2018-11-12 12:55:14 +08:00
|
|
|
using osu.Game.Rulesets.Objects;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
2019-09-27 17:45:22 +08:00
|
|
|
using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles.Components;
|
2018-11-07 15:08:56 +08:00
|
|
|
using osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders.Components;
|
2022-07-19 21:54:11 +08:00
|
|
|
using osu.Game.Rulesets.Osu.Objects;
|
2021-09-10 17:03:27 +08:00
|
|
|
using osu.Game.Screens.Edit;
|
2018-11-20 15:51:59 +08:00
|
|
|
using osuTK;
|
|
|
|
using osuTK.Input;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2018-11-07 15:08:56 +08:00
|
|
|
namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class SliderPlacementBlueprint : PlacementBlueprint
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
2022-07-19 21:54:11 +08:00
|
|
|
public new Slider HitObject => (Slider)base.HitObject;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2019-09-27 17:45:22 +08:00
|
|
|
private SliderBodyPiece bodyPiece;
|
|
|
|
private HitCirclePiece headCirclePiece;
|
|
|
|
private HitCirclePiece tailCirclePiece;
|
2022-11-03 19:25:23 +08:00
|
|
|
private PathControlPointVisualiser<Slider> controlPointVisualiser;
|
2019-09-27 17:45:22 +08:00
|
|
|
|
2019-10-23 15:03:16 +08:00
|
|
|
private InputManager inputManager;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2021-04-16 13:09:35 +08:00
|
|
|
private SliderPlacementState state;
|
2019-12-06 18:39:25 +08:00
|
|
|
private PathControlPoint segmentStart;
|
|
|
|
private PathControlPoint cursor;
|
2019-12-09 17:10:33 +08:00
|
|
|
private int currentSegmentLength;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2019-10-24 18:04:00 +08:00
|
|
|
[Resolved(CanBeNull = true)]
|
2023-12-13 14:15:22 +08:00
|
|
|
[CanBeNull]
|
2023-10-19 15:56:40 +08:00
|
|
|
private IPositionSnapProvider positionSnapProvider { get; set; }
|
|
|
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
2023-12-13 14:15:22 +08:00
|
|
|
[CanBeNull]
|
2023-10-19 15:56:40 +08:00
|
|
|
private IDistanceSnapProvider distanceSnapProvider { get; set; }
|
2019-10-24 17:09:20 +08:00
|
|
|
|
2023-11-11 17:45:22 +08:00
|
|
|
[Resolved(CanBeNull = true)]
|
2023-12-13 14:15:22 +08:00
|
|
|
[CanBeNull]
|
2023-11-21 08:59:49 +08:00
|
|
|
private FreehandSliderToolboxGroup freehandToolboxGroup { get; set; }
|
2023-11-11 17:45:22 +08:00
|
|
|
|
2023-12-06 23:35:59 +08:00
|
|
|
private readonly IncrementalBSplineBuilder bSplineBuilder = new IncrementalBSplineBuilder { Degree = 4 };
|
2023-11-11 17:45:22 +08:00
|
|
|
|
2023-05-12 15:00:40 +08:00
|
|
|
protected override bool IsValidForPlacement => HitObject.Path.HasValidLength;
|
|
|
|
|
2018-11-06 17:04:03 +08:00
|
|
|
public SliderPlacementBlueprint()
|
2022-07-19 21:54:11 +08:00
|
|
|
: base(new Slider())
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-12-06 18:39:25 +08:00
|
|
|
|
2023-11-08 18:43:54 +08:00
|
|
|
HitObject.Path.ControlPoints.Add(segmentStart = new PathControlPoint(Vector2.Zero, PathType.LINEAR));
|
2019-12-09 17:10:33 +08:00
|
|
|
currentSegmentLength = 1;
|
2018-10-24 12:42:51 +08:00
|
|
|
}
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2018-10-24 12:42:51 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2022-01-15 08:06:39 +08:00
|
|
|
private void load()
|
2018-10-24 12:42:51 +08:00
|
|
|
{
|
2018-10-04 12:44:49 +08:00
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
2019-09-27 17:45:22 +08:00
|
|
|
bodyPiece = new SliderBodyPiece(),
|
|
|
|
headCirclePiece = new HitCirclePiece(),
|
|
|
|
tailCirclePiece = new HitCirclePiece(),
|
2022-11-03 19:25:23 +08:00
|
|
|
controlPointVisualiser = new PathControlPointVisualiser<Slider>(HitObject, false)
|
2018-10-04 12:44:49 +08:00
|
|
|
};
|
|
|
|
|
2023-11-21 13:51:09 +08:00
|
|
|
state = SliderPlacementState.Initial;
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
2019-10-23 15:03:16 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
inputManager = GetContainingInputManager();
|
2023-11-11 17:45:22 +08:00
|
|
|
|
2023-11-21 08:59:49 +08:00
|
|
|
if (freehandToolboxGroup != null)
|
2023-11-11 17:45:22 +08:00
|
|
|
{
|
2023-11-21 08:59:49 +08:00
|
|
|
freehandToolboxGroup.Tolerance.BindValueChanged(e =>
|
2023-11-20 11:41:22 +08:00
|
|
|
{
|
2023-11-21 10:33:05 +08:00
|
|
|
bSplineBuilder.Tolerance = e.NewValue;
|
|
|
|
Scheduler.AddOnce(updateSliderPathFromBSplineBuilder);
|
2023-11-21 10:15:00 +08:00
|
|
|
}, true);
|
2023-11-20 11:41:22 +08:00
|
|
|
|
2023-11-21 08:59:49 +08:00
|
|
|
freehandToolboxGroup.CornerThreshold.BindValueChanged(e =>
|
2023-11-20 11:41:22 +08:00
|
|
|
{
|
2023-11-21 10:33:05 +08:00
|
|
|
bSplineBuilder.CornerThreshold = e.NewValue;
|
|
|
|
Scheduler.AddOnce(updateSliderPathFromBSplineBuilder);
|
2023-11-20 11:41:22 +08:00
|
|
|
}, true);
|
2023-12-07 07:43:34 +08:00
|
|
|
|
|
|
|
freehandToolboxGroup.CircleThreshold.BindValueChanged(e =>
|
|
|
|
{
|
|
|
|
Scheduler.AddOnce(updateSliderPathFromBSplineBuilder);
|
|
|
|
}, true);
|
2023-11-20 11:41:22 +08:00
|
|
|
}
|
2019-10-23 15:03:16 +08:00
|
|
|
}
|
|
|
|
|
2021-09-10 17:03:27 +08:00
|
|
|
[Resolved]
|
|
|
|
private EditorBeatmap editorBeatmap { get; set; }
|
|
|
|
|
2020-11-26 18:16:18 +08:00
|
|
|
public override void UpdateTimeAndPosition(SnapResult result)
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
2020-11-26 18:16:18 +08:00
|
|
|
base.UpdateTimeAndPosition(result);
|
2020-05-21 13:38:40 +08:00
|
|
|
|
2018-10-04 12:44:49 +08:00
|
|
|
switch (state)
|
|
|
|
{
|
2021-04-16 13:09:35 +08:00
|
|
|
case SliderPlacementState.Initial:
|
2020-02-07 17:02:48 +08:00
|
|
|
BeginPlacement();
|
2021-09-10 17:03:27 +08:00
|
|
|
|
2023-11-14 15:52:45 +08:00
|
|
|
double? nearestSliderVelocity = (editorBeatmap
|
2023-11-15 14:09:33 +08:00
|
|
|
.HitObjects
|
|
|
|
.LastOrDefault(h => h is Slider && h.GetEndTime() < HitObject.StartTime) as Slider)?.SliderVelocityMultiplier;
|
2021-09-10 17:03:27 +08:00
|
|
|
|
2023-09-06 17:59:15 +08:00
|
|
|
HitObject.SliderVelocityMultiplier = nearestSliderVelocity ?? 1;
|
2020-05-20 18:05:03 +08:00
|
|
|
HitObject.Position = ToLocalSpace(result.ScreenSpacePosition);
|
2022-09-05 15:17:26 +08:00
|
|
|
|
|
|
|
// Replacing the DifficultyControlPoint above doesn't trigger any kind of invalidation.
|
|
|
|
// Without re-applying defaults, velocity won't be updated.
|
|
|
|
ApplyDefaultsToHitObject();
|
2019-10-03 15:14:42 +08:00
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
2023-11-11 17:45:22 +08:00
|
|
|
case SliderPlacementState.ControlPoints:
|
2020-04-13 14:31:46 +08:00
|
|
|
updateCursor();
|
2019-10-03 15:14:42 +08:00
|
|
|
break;
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:17:11 +08:00
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
2020-04-23 11:17:11 +08:00
|
|
|
if (e.Button != MouseButton.Left)
|
|
|
|
return base.OnMouseDown(e);
|
|
|
|
|
2018-10-04 12:44:49 +08:00
|
|
|
switch (state)
|
|
|
|
{
|
2021-04-16 13:09:35 +08:00
|
|
|
case SliderPlacementState.Initial:
|
2018-10-04 17:24:25 +08:00
|
|
|
beginCurve();
|
2018-10-04 12:44:49 +08:00
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
2023-11-11 17:45:22 +08:00
|
|
|
case SliderPlacementState.ControlPoints:
|
2020-04-13 14:31:46 +08:00
|
|
|
if (canPlaceNewControlPoint(out var lastPoint))
|
|
|
|
{
|
|
|
|
// Place a new point by detatching the current cursor.
|
|
|
|
updateCursor();
|
|
|
|
cursor = null;
|
|
|
|
}
|
|
|
|
else
|
2018-10-04 17:24:25 +08:00
|
|
|
{
|
2020-04-13 14:31:46 +08:00
|
|
|
// Transform the last point into a new segment.
|
2020-04-13 12:57:40 +08:00
|
|
|
Debug.Assert(lastPoint != null);
|
2019-12-06 18:39:25 +08:00
|
|
|
|
2020-04-13 12:57:40 +08:00
|
|
|
segmentStart = lastPoint;
|
2023-11-08 18:43:54 +08:00
|
|
|
segmentStart.Type = PathType.LINEAR;
|
2020-04-13 12:57:40 +08:00
|
|
|
|
|
|
|
currentSegmentLength = 1;
|
|
|
|
}
|
2018-10-04 17:24:25 +08:00
|
|
|
|
2020-04-23 11:17:11 +08:00
|
|
|
break;
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
|
2018-10-04 17:24:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-11 17:45:22 +08:00
|
|
|
protected override bool OnDragStart(DragStartEvent e)
|
|
|
|
{
|
2023-11-21 13:51:09 +08:00
|
|
|
if (e.Button != MouseButton.Left)
|
|
|
|
return base.OnDragStart(e);
|
|
|
|
|
|
|
|
if (state != SliderPlacementState.ControlPoints)
|
|
|
|
return base.OnDragStart(e);
|
|
|
|
|
|
|
|
// Only enter drawing mode if no additional control points have been placed.
|
2023-11-21 14:16:40 +08:00
|
|
|
int controlPointCount = HitObject.Path.ControlPoints.Count;
|
|
|
|
if (controlPointCount > 2 || (controlPointCount == 2 && HitObject.Path.ControlPoints.Last() != cursor))
|
2023-11-21 13:51:09 +08:00
|
|
|
return base.OnDragStart(e);
|
2023-11-11 22:02:06 +08:00
|
|
|
|
2023-11-22 17:14:44 +08:00
|
|
|
bSplineBuilder.AddLinearPoint(Vector2.Zero);
|
2023-11-21 13:51:09 +08:00
|
|
|
bSplineBuilder.AddLinearPoint(ToLocalSpace(e.ScreenSpaceMouseDownPosition) - HitObject.Position);
|
|
|
|
state = SliderPlacementState.Drawing;
|
|
|
|
return true;
|
2023-11-11 17:45:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDrag(DragEvent e)
|
|
|
|
{
|
|
|
|
base.OnDrag(e);
|
|
|
|
|
2023-11-21 13:51:09 +08:00
|
|
|
if (state == SliderPlacementState.Drawing)
|
|
|
|
{
|
|
|
|
bSplineBuilder.AddLinearPoint(ToLocalSpace(e.ScreenSpaceMousePosition) - HitObject.Position);
|
|
|
|
Scheduler.AddOnce(updateSliderPathFromBSplineBuilder);
|
|
|
|
}
|
2023-11-11 17:45:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDragEnd(DragEndEvent e)
|
|
|
|
{
|
|
|
|
base.OnDragEnd(e);
|
|
|
|
|
|
|
|
if (state == SliderPlacementState.Drawing)
|
2023-12-04 04:43:37 +08:00
|
|
|
{
|
|
|
|
bSplineBuilder.Finish();
|
|
|
|
updateSliderPathFromBSplineBuilder();
|
2023-12-04 05:03:51 +08:00
|
|
|
|
|
|
|
// Change the state so it will snap the expected distance in endCurve.
|
|
|
|
state = SliderPlacementState.Finishing;
|
2023-11-11 17:45:22 +08:00
|
|
|
endCurve();
|
2023-12-04 04:43:37 +08:00
|
|
|
}
|
2023-11-11 17:45:22 +08:00
|
|
|
}
|
|
|
|
|
2020-01-20 17:17:21 +08:00
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2018-10-04 17:24:25 +08:00
|
|
|
{
|
2023-11-11 17:45:22 +08:00
|
|
|
if (state == SliderPlacementState.ControlPoints && e.Button == MouseButton.Right)
|
2018-10-04 17:24:25 +08:00
|
|
|
endCurve();
|
2020-01-20 17:17:21 +08:00
|
|
|
base.OnMouseUp(e);
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
2023-11-21 13:52:21 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
updateSlider();
|
|
|
|
|
|
|
|
// Maintain the path type in case it got defaulted to bezier at some point during the drag.
|
|
|
|
updatePathType();
|
|
|
|
}
|
|
|
|
|
2018-10-04 17:24:25 +08:00
|
|
|
private void beginCurve()
|
|
|
|
{
|
2020-02-13 08:03:48 +08:00
|
|
|
BeginPlacement(commitStart: true);
|
2023-11-21 13:51:09 +08:00
|
|
|
state = SliderPlacementState.ControlPoints;
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void endCurve()
|
|
|
|
{
|
2018-11-01 18:23:37 +08:00
|
|
|
updateSlider();
|
2023-05-12 15:00:40 +08:00
|
|
|
EndPlacement(true);
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 18:39:25 +08:00
|
|
|
private void updatePathType()
|
|
|
|
{
|
2023-11-11 17:45:22 +08:00
|
|
|
if (state == SliderPlacementState.Drawing)
|
|
|
|
{
|
2023-12-06 23:35:59 +08:00
|
|
|
segmentStart.Type = PathType.BSpline(4);
|
2023-11-11 17:45:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-06 18:39:25 +08:00
|
|
|
switch (currentSegmentLength)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 2:
|
2023-11-08 18:43:54 +08:00
|
|
|
segmentStart.Type = PathType.LINEAR;
|
2019-12-06 18:39:25 +08:00
|
|
|
break;
|
2019-12-09 15:44:19 +08:00
|
|
|
|
2019-12-06 18:39:25 +08:00
|
|
|
case 3:
|
2023-11-13 15:24:09 +08:00
|
|
|
segmentStart.Type = PathType.PERFECT_CURVE;
|
2019-12-06 18:39:25 +08:00
|
|
|
break;
|
2019-12-09 15:44:19 +08:00
|
|
|
|
2019-12-06 18:39:25 +08:00
|
|
|
default:
|
2023-11-08 18:43:54 +08:00
|
|
|
segmentStart.Type = PathType.BEZIER;
|
2019-12-06 18:39:25 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-01-14 05:51:33 +08:00
|
|
|
|
|
|
|
controlPointVisualiser.EnsureValidPathTypes();
|
2019-12-06 18:39:25 +08:00
|
|
|
}
|
|
|
|
|
2020-04-13 14:31:46 +08:00
|
|
|
private void updateCursor()
|
2019-12-06 18:39:25 +08:00
|
|
|
{
|
2020-04-13 14:31:46 +08:00
|
|
|
if (canPlaceNewControlPoint(out _))
|
2019-12-06 18:39:25 +08:00
|
|
|
{
|
2020-04-13 14:31:46 +08:00
|
|
|
// The cursor does not overlap a previous control point, so it can be added if not already existing.
|
|
|
|
if (cursor == null)
|
|
|
|
{
|
2021-08-26 00:42:57 +08:00
|
|
|
HitObject.Path.ControlPoints.Add(cursor = new PathControlPoint { Position = Vector2.Zero });
|
2020-04-13 14:31:46 +08:00
|
|
|
|
2023-11-08 18:43:54 +08:00
|
|
|
// The path type should be adjusted in the progression of updatePathType() (LINEAR -> PC -> BEZIER).
|
2020-04-13 14:31:46 +08:00
|
|
|
currentSegmentLength++;
|
|
|
|
updatePathType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cursor position.
|
2023-11-11 17:45:22 +08:00
|
|
|
var result = positionSnapProvider?.FindSnappedPositionAndTime(inputManager.CurrentState.Mouse.Position, state == SliderPlacementState.ControlPoints ? SnapType.GlobalGrids : SnapType.All);
|
2022-09-20 03:33:38 +08:00
|
|
|
cursor.Position = ToLocalSpace(result?.ScreenSpacePosition ?? inputManager.CurrentState.Mouse.Position) - HitObject.Position;
|
2020-04-13 14:31:46 +08:00
|
|
|
}
|
|
|
|
else if (cursor != null)
|
|
|
|
{
|
|
|
|
// The cursor overlaps a previous control point, so it's removed.
|
|
|
|
HitObject.Path.ControlPoints.Remove(cursor);
|
|
|
|
cursor = null;
|
2019-12-06 18:39:25 +08:00
|
|
|
|
2023-11-08 18:43:54 +08:00
|
|
|
// The path type should be adjusted in the reverse progression of updatePathType() (BEZIER -> PC -> LINEAR).
|
2020-04-13 14:31:46 +08:00
|
|
|
currentSegmentLength--;
|
2019-12-06 18:39:25 +08:00
|
|
|
updatePathType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 14:31:46 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether a new control point can be placed at the current mouse position.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="lastPoint">The last-placed control point. May be null, but is not null if <c>false</c> is returned.</param>
|
|
|
|
/// <returns>Whether a new control point can be placed at the current position.</returns>
|
|
|
|
private bool canPlaceNewControlPoint([CanBeNull] out PathControlPoint lastPoint)
|
|
|
|
{
|
|
|
|
// We cannot rely on the ordering of drawable pieces, so find the respective drawable piece by searching for the last non-cursor control point.
|
|
|
|
var last = HitObject.Path.ControlPoints.LastOrDefault(p => p != cursor);
|
|
|
|
var lastPiece = controlPointVisualiser.Pieces.Single(p => p.ControlPoint == last);
|
|
|
|
|
|
|
|
lastPoint = last;
|
2021-04-27 12:23:14 +08:00
|
|
|
return lastPiece.IsHovered != true;
|
2020-04-13 14:31:46 +08:00
|
|
|
}
|
|
|
|
|
2018-11-01 18:23:37 +08:00
|
|
|
private void updateSlider()
|
|
|
|
{
|
2023-12-04 05:03:51 +08:00
|
|
|
if (state == SliderPlacementState.Drawing)
|
|
|
|
HitObject.Path.ExpectedDistance.Value = (float)HitObject.Path.CalculatedDistance;
|
|
|
|
else
|
|
|
|
HitObject.Path.ExpectedDistance.Value = distanceSnapProvider?.FindSnappedDistance(HitObject, (float)HitObject.Path.CalculatedDistance) ?? (float)HitObject.Path.CalculatedDistance;
|
2019-09-27 17:45:22 +08:00
|
|
|
|
|
|
|
bodyPiece.UpdateFrom(HitObject);
|
|
|
|
headCirclePiece.UpdateFrom(HitObject.HeadCircle);
|
|
|
|
tailCirclePiece.UpdateFrom(HitObject.TailCircle);
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
|
2023-11-21 13:52:21 +08:00
|
|
|
private void updateSliderPathFromBSplineBuilder()
|
|
|
|
{
|
2023-12-04 03:14:00 +08:00
|
|
|
IReadOnlyList<List<Vector2>> builderPoints = bSplineBuilder.ControlPoints;
|
2023-11-21 13:52:21 +08:00
|
|
|
|
2023-12-04 03:53:25 +08:00
|
|
|
if (builderPoints.Count == 0 || builderPoints[0].Count == 0)
|
2023-11-21 13:52:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
HitObject.Path.ControlPoints.Clear();
|
|
|
|
|
2023-12-04 04:10:01 +08:00
|
|
|
// Iterate through generated segments and adding non-inheriting path types where appropriate.
|
2023-11-21 13:52:21 +08:00
|
|
|
for (int i = 0; i < builderPoints.Count; i++)
|
|
|
|
{
|
2023-12-04 03:14:00 +08:00
|
|
|
bool isLastSegment = i == builderPoints.Count - 1;
|
|
|
|
var segment = builderPoints[i];
|
2023-11-21 13:52:21 +08:00
|
|
|
|
2023-12-04 03:53:25 +08:00
|
|
|
if (segment.Count == 0)
|
|
|
|
continue;
|
|
|
|
|
2023-12-07 07:26:13 +08:00
|
|
|
// Replace this segment with a circular arc if it is a reasonable substitute.
|
|
|
|
var circleArcSegment = tryCircleArc(segment);
|
|
|
|
|
|
|
|
if (circleArcSegment is not null)
|
|
|
|
{
|
|
|
|
HitObject.Path.ControlPoints.Add(new PathControlPoint(circleArcSegment[0], PathType.PERFECT_CURVE));
|
|
|
|
HitObject.Path.ControlPoints.Add(new PathControlPoint(circleArcSegment[1]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HitObject.Path.ControlPoints.Add(new PathControlPoint(segment[0], PathType.BSpline(4)));
|
|
|
|
for (int j = 1; j < segment.Count - 1; j++)
|
|
|
|
HitObject.Path.ControlPoints.Add(new PathControlPoint(segment[j]));
|
|
|
|
}
|
2023-11-21 13:52:21 +08:00
|
|
|
|
2023-12-04 03:14:00 +08:00
|
|
|
if (isLastSegment)
|
2023-12-04 05:06:07 +08:00
|
|
|
HitObject.Path.ControlPoints.Add(new PathControlPoint(segment[^1]));
|
2023-11-21 13:52:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 07:26:13 +08:00
|
|
|
private Vector2[] tryCircleArc(List<Vector2> segment)
|
|
|
|
{
|
2023-12-13 14:15:22 +08:00
|
|
|
if (segment.Count < 3 || freehandToolboxGroup?.CircleThreshold.Value == 0) return null;
|
2023-12-07 07:26:13 +08:00
|
|
|
|
|
|
|
// Assume the segment creates a reasonable circular arc and then check if it reasonable
|
|
|
|
var points = PathApproximator.BSplineToPiecewiseLinear(segment.ToArray(), bSplineBuilder.Degree);
|
|
|
|
var circleArcControlPoints = new[] { points[0], points[points.Count / 2], points[^1] };
|
|
|
|
var circleArc = new CircularArcProperties(circleArcControlPoints);
|
|
|
|
|
|
|
|
if (!circleArc.IsValid) return null;
|
|
|
|
|
|
|
|
double length = circleArc.ThetaRange * circleArc.Radius;
|
|
|
|
|
|
|
|
if (length > 1000) return null;
|
|
|
|
|
|
|
|
double loss = 0;
|
|
|
|
Vector2? lastPoint = null;
|
|
|
|
Vector2? lastVec = null;
|
2023-12-07 07:57:29 +08:00
|
|
|
Vector2? lastVec2 = null;
|
2023-12-07 07:26:13 +08:00
|
|
|
int? lastDir = null;
|
2023-12-07 07:57:29 +08:00
|
|
|
int? lastDir2 = null;
|
2023-12-07 07:26:13 +08:00
|
|
|
double totalWinding = 0;
|
|
|
|
|
|
|
|
// Loop through the points and check if they are not too far away from the circular arc.
|
|
|
|
// Also make sure it curves monotonically in one direction and at most one loop is done.
|
|
|
|
foreach (var point in points)
|
|
|
|
{
|
2023-12-07 07:57:29 +08:00
|
|
|
var vec = point - circleArc.Centre;
|
|
|
|
loss += Math.Pow((vec.Length - circleArc.Radius) / length, 2);
|
|
|
|
|
|
|
|
if (lastVec.HasValue)
|
|
|
|
{
|
|
|
|
double det = lastVec.Value.X * vec.Y - lastVec.Value.Y * vec.X;
|
|
|
|
int dir = Math.Sign(det);
|
|
|
|
|
|
|
|
if (dir == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (lastDir.HasValue && dir != lastDir)
|
|
|
|
return null; // Circle center is not inside the polygon
|
|
|
|
|
|
|
|
lastDir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastVec = vec;
|
2023-12-07 07:26:13 +08:00
|
|
|
|
|
|
|
if (lastPoint.HasValue)
|
|
|
|
{
|
2023-12-07 07:57:29 +08:00
|
|
|
var vec2 = point - lastPoint.Value;
|
2023-12-07 07:26:13 +08:00
|
|
|
|
2023-12-07 07:57:29 +08:00
|
|
|
if (lastVec2.HasValue)
|
2023-12-07 07:26:13 +08:00
|
|
|
{
|
2023-12-07 07:57:29 +08:00
|
|
|
double dot = Vector2.Dot(vec2, lastVec2.Value);
|
|
|
|
double det = lastVec2.Value.X * vec2.Y - lastVec2.Value.Y * vec2.X;
|
2023-12-07 07:26:13 +08:00
|
|
|
double angle = Math.Atan2(det, dot);
|
2023-12-07 07:57:29 +08:00
|
|
|
int dir2 = Math.Sign(angle);
|
2023-12-07 07:26:13 +08:00
|
|
|
|
2023-12-07 07:57:29 +08:00
|
|
|
if (dir2 == 0)
|
2023-12-07 07:26:13 +08:00
|
|
|
continue;
|
|
|
|
|
2023-12-07 07:57:29 +08:00
|
|
|
if (lastDir2.HasValue && dir2 != lastDir2)
|
2023-12-07 07:26:13 +08:00
|
|
|
return null; // Curvature changed, like in an S-shape
|
|
|
|
|
|
|
|
totalWinding += Math.Abs(angle);
|
2023-12-07 07:57:29 +08:00
|
|
|
lastDir2 = dir2;
|
2023-12-07 07:26:13 +08:00
|
|
|
}
|
|
|
|
|
2023-12-07 07:57:29 +08:00
|
|
|
lastVec2 = vec2;
|
2023-12-07 07:26:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lastPoint = point;
|
|
|
|
}
|
|
|
|
|
|
|
|
loss /= points.Count;
|
|
|
|
|
2023-12-13 14:15:22 +08:00
|
|
|
return loss > freehandToolboxGroup?.CircleThreshold.Value || totalWinding > MathHelper.TwoPi ? null : circleArcControlPoints;
|
2023-12-07 07:26:13 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 13:09:35 +08:00
|
|
|
private enum SliderPlacementState
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
2018-10-04 17:24:25 +08:00
|
|
|
Initial,
|
2023-11-11 17:45:22 +08:00
|
|
|
ControlPoints,
|
2023-12-04 05:03:51 +08:00
|
|
|
Drawing,
|
|
|
|
Finishing
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
}
|