1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 17:17:26 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Edit/Blueprints/Sliders/SliderPlacementBlueprint.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

352 lines
12 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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
2018-10-04 17:24:25 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Input;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
using osu.Game.Rulesets.Edit;
2018-11-12 12:55:14 +08:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
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;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Screens.Edit;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Input;
2018-11-07 15:08:56 +08:00
namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
{
2018-11-06 17:04:03 +08:00
public partial class SliderPlacementBlueprint : PlacementBlueprint
{
public new Slider HitObject => (Slider)base.HitObject;
private SliderBodyPiece bodyPiece;
private HitCirclePiece headCirclePiece;
private HitCirclePiece tailCirclePiece;
private PathControlPointVisualiser<Slider> controlPointVisualiser;
private InputManager inputManager;
private SliderPlacementState state;
private PathControlPoint segmentStart;
private PathControlPoint cursor;
private int currentSegmentLength;
2019-10-24 18:04:00 +08:00
[Resolved(CanBeNull = true)]
private IPositionSnapProvider positionSnapProvider { get; set; }
[Resolved(CanBeNull = true)]
private IDistanceSnapProvider distanceSnapProvider { get; set; }
[Resolved(CanBeNull = true)]
private ISliderDrawingSettingsProvider drawingSettingsProvider { get; set; }
private readonly IncrementalBSplineBuilder bSplineBuilder = new IncrementalBSplineBuilder();
protected override bool IsValidForPlacement => HitObject.Path.HasValidLength;
2018-11-06 17:04:03 +08:00
public SliderPlacementBlueprint()
: base(new Slider())
{
RelativeSizeAxes = Axes.Both;
HitObject.Path.ControlPoints.Add(segmentStart = new PathControlPoint(Vector2.Zero, PathType.LINEAR));
currentSegmentLength = 1;
2018-10-24 12:42:51 +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
{
InternalChildren = new Drawable[]
{
bodyPiece = new SliderBodyPiece(),
headCirclePiece = new HitCirclePiece(),
tailCirclePiece = new HitCirclePiece(),
controlPointVisualiser = new PathControlPointVisualiser<Slider>(HitObject, false)
};
setState(SliderPlacementState.Initial);
2018-10-04 17:24:25 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
if (drawingSettingsProvider != null)
{
drawingSettingsProvider.Tolerance.BindValueChanged(e =>
{
if (bSplineBuilder.Tolerance != e.NewValue)
bSplineBuilder.Tolerance = e.NewValue;
updateSliderPathFromBSplineBuilder();
}, true);
drawingSettingsProvider.CornerThreshold.BindValueChanged(e =>
{
if (bSplineBuilder.CornerThreshold != e.NewValue)
bSplineBuilder.CornerThreshold = e.NewValue;
updateSliderPathFromBSplineBuilder();
}, true);
}
}
[Resolved]
private EditorBeatmap editorBeatmap { get; set; }
public override void UpdateTimeAndPosition(SnapResult result)
{
base.UpdateTimeAndPosition(result);
switch (state)
{
case SliderPlacementState.Initial:
BeginPlacement();
double? nearestSliderVelocity = (editorBeatmap
2023-11-15 14:09:33 +08:00
.HitObjects
.LastOrDefault(h => h is Slider && h.GetEndTime() < HitObject.StartTime) as Slider)?.SliderVelocityMultiplier;
HitObject.SliderVelocityMultiplier = nearestSliderVelocity ?? 1;
HitObject.Position = ToLocalSpace(result.ScreenSpacePosition);
// Replacing the DifficultyControlPoint above doesn't trigger any kind of invalidation.
// Without re-applying defaults, velocity won't be updated.
ApplyDefaultsToHitObject();
break;
2019-04-01 11:16:05 +08:00
case SliderPlacementState.ControlPoints:
updateCursor();
break;
}
}
2020-04-23 11:17:11 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
{
2020-04-23 11:17:11 +08:00
if (e.Button != MouseButton.Left)
return base.OnMouseDown(e);
switch (state)
{
case SliderPlacementState.Initial:
2018-10-04 17:24:25 +08:00
beginCurve();
break;
2019-04-01 11:16:05 +08:00
case SliderPlacementState.ControlPoints:
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
{
// Transform the last point into a new segment.
Debug.Assert(lastPoint != null);
segmentStart = lastPoint;
segmentStart.Type = PathType.LINEAR;
currentSegmentLength = 1;
}
2018-10-04 17:24:25 +08:00
2020-04-23 11:17:11 +08:00
break;
}
2018-10-04 17:24:25 +08:00
return true;
}
protected override bool OnDragStart(DragStartEvent e)
{
if (e.Button == MouseButton.Left)
{
switch (state)
{
case SliderPlacementState.Initial:
return true;
case SliderPlacementState.ControlPoints:
if (HitObject.Path.ControlPoints.Count < 3)
{
var lastCp = HitObject.Path.ControlPoints.LastOrDefault();
2023-11-13 11:47:12 +08:00
if (lastCp != cursor && HitObject.Path.ControlPoints.Count == 2)
return false;
bSplineBuilder.Clear();
bSplineBuilder.AddLinearPoint(ToLocalSpace(e.ScreenSpaceMousePosition) - HitObject.Position);
setState(SliderPlacementState.Drawing);
return true;
}
2023-11-11 22:02:06 +08:00
return false;
}
}
2023-11-11 22:02:06 +08:00
return base.OnDragStart(e);
}
protected override void OnDrag(DragEvent e)
{
base.OnDrag(e);
bSplineBuilder.AddLinearPoint(ToLocalSpace(e.ScreenSpaceMousePosition) - HitObject.Position);
updateSliderPathFromBSplineBuilder();
}
private void updateSliderPathFromBSplineBuilder()
{
Scheduler.AddOnce(static self =>
{
var cps = self.bSplineBuilder.ControlPoints;
var sliderCps = self.HitObject.Path.ControlPoints;
sliderCps.RemoveRange(1, sliderCps.Count - 1);
// Add the control points from the BSpline builder while converting control points that repeat
// three or more times to a single PathControlPoint with linear type.
for (int i = 1; i < cps.Count; i++)
{
bool isSharp = i < cps.Count - 2 && cps[i] == cps[i + 1] && cps[i] == cps[i + 2];
sliderCps.Add(new PathControlPoint(cps[i], isSharp ? PathType.BSpline(3) : null));
if (isSharp)
i += 2;
}
}, this);
}
protected override void OnDragEnd(DragEndEvent e)
{
base.OnDragEnd(e);
if (state == SliderPlacementState.Drawing)
endCurve();
}
protected override void OnMouseUp(MouseUpEvent e)
2018-10-04 17:24:25 +08:00
{
if (state == SliderPlacementState.ControlPoints && e.Button == MouseButton.Right)
2018-10-04 17:24:25 +08:00
endCurve();
base.OnMouseUp(e);
2018-10-04 17:24:25 +08:00
}
private void beginCurve()
{
BeginPlacement(commitStart: true);
setState(SliderPlacementState.ControlPoints);
2018-10-04 17:24:25 +08:00
}
private void endCurve()
{
2018-11-01 18:23:37 +08:00
updateSlider();
EndPlacement(true);
2018-10-04 17:24:25 +08:00
}
protected override void Update()
{
base.Update();
2018-11-01 18:23:37 +08:00
updateSlider();
// Maintain the path type in case it got defaulted to bezier at some point during the drag.
updatePathType();
2018-11-01 18:23:37 +08:00
}
2018-10-04 17:24:25 +08:00
private void updatePathType()
{
if (state == SliderPlacementState.Drawing)
{
segmentStart.Type = PathType.BSpline(3);
return;
}
switch (currentSegmentLength)
{
case 1:
case 2:
segmentStart.Type = PathType.LINEAR;
break;
2019-12-09 15:44:19 +08:00
case 3:
2023-11-13 15:24:09 +08:00
segmentStart.Type = PathType.PERFECT_CURVE;
break;
2019-12-09 15:44:19 +08:00
default:
segmentStart.Type = PathType.BEZIER;
break;
}
}
private void updateCursor()
{
if (canPlaceNewControlPoint(out _))
{
// The cursor does not overlap a previous control point, so it can be added if not already existing.
if (cursor == null)
{
HitObject.Path.ControlPoints.Add(cursor = new PathControlPoint { Position = Vector2.Zero });
// The path type should be adjusted in the progression of updatePathType() (LINEAR -> PC -> BEZIER).
currentSegmentLength++;
updatePathType();
}
// Update the cursor position.
var result = positionSnapProvider?.FindSnappedPositionAndTime(inputManager.CurrentState.Mouse.Position, state == SliderPlacementState.ControlPoints ? SnapType.GlobalGrids : SnapType.All);
cursor.Position = ToLocalSpace(result?.ScreenSpacePosition ?? inputManager.CurrentState.Mouse.Position) - HitObject.Position;
}
else if (cursor != null)
{
// The cursor overlaps a previous control point, so it's removed.
HitObject.Path.ControlPoints.Remove(cursor);
cursor = null;
// The path type should be adjusted in the reverse progression of updatePathType() (BEZIER -> PC -> LINEAR).
currentSegmentLength--;
updatePathType();
}
}
/// <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;
}
2018-11-01 18:23:37 +08:00
private void updateSlider()
{
HitObject.Path.ExpectedDistance.Value = distanceSnapProvider?.FindSnappedDistance(HitObject, (float)HitObject.Path.CalculatedDistance) ?? (float)HitObject.Path.CalculatedDistance;
bodyPiece.UpdateFrom(HitObject);
headCirclePiece.UpdateFrom(HitObject.HeadCircle);
tailCirclePiece.UpdateFrom(HitObject.TailCircle);
}
private void setState(SliderPlacementState newState)
{
state = newState;
}
private enum SliderPlacementState
{
2018-10-04 17:24:25 +08:00
Initial,
ControlPoints,
Drawing,
DrawingFinalization
2018-10-04 17:24:25 +08:00
}
}
}