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
|
|
|
|
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;
|
2021-09-10 17:03:27 +08:00
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
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;
|
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
|
|
|
{
|
2018-11-06 17:04:03 +08:00
|
|
|
public class SliderPlacementBlueprint : PlacementBlueprint
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
2018-10-25 18:34:35 +08:00
|
|
|
public new Objects.Slider HitObject => (Objects.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;
|
2020-04-13 12:57:40 +08:00
|
|
|
private PathControlPointVisualiser 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)]
|
2022-04-28 10:48:45 +08:00
|
|
|
private IDistanceSnapProvider snapProvider { get; set; }
|
2019-10-24 17:09:20 +08:00
|
|
|
|
2018-11-06 17:04:03 +08:00
|
|
|
public SliderPlacementBlueprint()
|
2018-10-25 18:34:35 +08:00
|
|
|
: base(new Objects.Slider())
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-12-06 18:39:25 +08:00
|
|
|
|
2019-12-09 17:10:33 +08:00
|
|
|
HitObject.Path.ControlPoints.Add(segmentStart = new PathControlPoint(Vector2.Zero, PathType.Linear));
|
|
|
|
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(),
|
2020-04-13 12:57:40 +08:00
|
|
|
controlPointVisualiser = new PathControlPointVisualiser(HitObject, false)
|
2018-10-04 12:44:49 +08:00
|
|
|
};
|
|
|
|
|
2021-04-16 13:09:35 +08:00
|
|
|
setState(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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
var nearestDifficultyPoint = editorBeatmap.HitObjects.LastOrDefault(h => h.GetEndTime() < HitObject.StartTime)?.DifficultyControlPoint?.DeepClone() as DifficultyControlPoint;
|
|
|
|
|
|
|
|
HitObject.DifficultyControlPoint = nearestDifficultyPoint ?? new DifficultyControlPoint();
|
2020-05-20 18:05:03 +08:00
|
|
|
HitObject.Position = ToLocalSpace(result.ScreenSpacePosition);
|
2019-10-03 15:14:42 +08:00
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
2021-04-16 13:09:35 +08:00
|
|
|
case SliderPlacementState.Body:
|
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
|
|
|
|
2021-04-16 13:09:35 +08:00
|
|
|
case SliderPlacementState.Body:
|
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;
|
2021-08-26 00:42:57 +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;
|
|
|
|
}
|
|
|
|
|
2020-01-20 17:17:21 +08:00
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2018-10-04 17:24:25 +08:00
|
|
|
{
|
2021-04-16 13:09:35 +08:00
|
|
|
if (state == SliderPlacementState.Body && 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
|
|
|
}
|
|
|
|
|
|
|
|
private void beginCurve()
|
|
|
|
{
|
2020-02-13 08:03:48 +08:00
|
|
|
BeginPlacement(commitStart: true);
|
2021-04-16 13:09:35 +08:00
|
|
|
setState(SliderPlacementState.Body);
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void endCurve()
|
|
|
|
{
|
2018-11-01 18:23:37 +08:00
|
|
|
updateSlider();
|
2021-04-16 14:22:32 +08:00
|
|
|
EndPlacement(HitObject.Path.HasValidLength);
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
2018-11-01 18:23:37 +08:00
|
|
|
updateSlider();
|
2021-03-22 22:59:06 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-12-06 18:39:25 +08:00
|
|
|
private void updatePathType()
|
|
|
|
{
|
|
|
|
switch (currentSegmentLength)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 2:
|
2021-08-26 00:42:57 +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:
|
2021-08-26 00:42:57 +08:00
|
|
|
segmentStart.Type = PathType.PerfectCurve;
|
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:
|
2021-08-26 00:42:57 +08:00
|
|
|
segmentStart.Type = PathType.Bezier;
|
2019-12-06 18:39:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// The path type should be adjusted in the progression of updatePathType() (Linear -> PC -> Bezier).
|
|
|
|
currentSegmentLength++;
|
|
|
|
updatePathType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cursor position.
|
2021-08-26 00:42:57 +08:00
|
|
|
cursor.Position = ToLocalSpace(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
|
|
|
|
2020-04-13 14:31:46 +08:00
|
|
|
// The path type should be adjusted in the reverse progression of updatePathType() (Bezier -> PC -> Linear).
|
|
|
|
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()
|
|
|
|
{
|
2022-05-05 15:25:05 +08:00
|
|
|
HitObject.Path.ExpectedDistance.Value = snapProvider?.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
|
|
|
}
|
|
|
|
|
2021-04-16 13:09:35 +08:00
|
|
|
private void setState(SliderPlacementState newState)
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
|
|
|
state = newState;
|
|
|
|
}
|
|
|
|
|
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,
|
2018-10-04 12:44:49 +08:00
|
|
|
Body,
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
}
|