1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 21:43:01 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Edit/Blueprints/Sliders/SliderPlacementBlueprint.cs

196 lines
5.8 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.
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;
2018-10-04 17:24:25 +08:00
using osu.Game.Graphics;
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;
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 class SliderPlacementBlueprint : PlacementBlueprint
{
public new Objects.Slider HitObject => (Objects.Slider)base.HitObject;
private SliderBodyPiece bodyPiece;
private HitCirclePiece headCirclePiece;
private HitCirclePiece tailCirclePiece;
private InputManager inputManager;
2018-10-04 17:24:25 +08:00
private PlacementState state;
private PathControlPoint segmentStart;
private PathControlPoint cursor;
private int currentSegmentLength;
2019-10-24 18:04:00 +08:00
[Resolved(CanBeNull = true)]
private HitObjectComposer composer { get; set; }
2018-11-06 17:04:03 +08:00
public SliderPlacementBlueprint()
: base(new Objects.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]
private void load(OsuColour colours)
{
InternalChildren = new Drawable[]
{
bodyPiece = new SliderBodyPiece(),
headCirclePiece = new HitCirclePiece(),
tailCirclePiece = new HitCirclePiece(),
2019-12-06 11:31:22 +08:00
new PathControlPointVisualiser(HitObject, false)
};
2018-10-04 17:24:25 +08:00
setState(PlacementState.Initial);
}
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
public override void UpdatePosition(Vector2 screenSpacePosition)
{
switch (state)
{
2018-10-04 17:24:25 +08:00
case PlacementState.Initial:
BeginPlacement();
HitObject.Position = ToLocalSpace(screenSpacePosition);
break;
2019-04-01 11:16:05 +08:00
2018-10-04 17:24:25 +08:00
case PlacementState.Body:
ensureCursor();
2019-10-23 16:58:44 +08:00
// The given screen-space position may have been externally snapped, but the unsnapped position from the input manager
// is used instead since snapping control points doesn't make much sense
cursor.Position.Value = ToLocalSpace(inputManager.CurrentState.Mouse.Position) - HitObject.Position;
break;
}
}
protected override bool OnClick(ClickEvent e)
{
switch (state)
{
2018-10-04 17:24:25 +08:00
case PlacementState.Initial:
beginCurve();
break;
2019-04-01 11:16:05 +08:00
2018-10-04 17:24:25 +08:00
case PlacementState.Body:
switch (e.Button)
{
case MouseButton.Left:
ensureCursor();
// Detatch the cursor
cursor = null;
2018-10-04 17:24:25 +08:00
break;
}
break;
}
2018-10-04 17:24:25 +08:00
return true;
}
protected override void OnMouseUp(MouseUpEvent e)
2018-10-04 17:24:25 +08:00
{
if (state == PlacementState.Body && e.Button == MouseButton.Right)
endCurve();
base.OnMouseUp(e);
2018-10-04 17:24:25 +08:00
}
protected override bool OnDoubleClick(DoubleClickEvent e)
{
// Todo: This should all not occur on double click, but rather if the previous control point is hovered.
2019-12-14 20:54:22 +08:00
segmentStart = HitObject.Path.ControlPoints[^1];
segmentStart.Type.Value = PathType.Linear;
currentSegmentLength = 1;
2018-10-04 17:24:25 +08:00
return true;
}
private void beginCurve()
{
BeginPlacement(commitStart: true);
2018-10-04 17:24:25 +08:00
setState(PlacementState.Body);
}
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();
}
2018-10-04 17:24:25 +08:00
private void updatePathType()
{
switch (currentSegmentLength)
{
case 1:
case 2:
segmentStart.Type.Value = PathType.Linear;
break;
2019-12-09 15:44:19 +08:00
case 3:
segmentStart.Type.Value = PathType.PerfectCurve;
break;
2019-12-09 15:44:19 +08:00
default:
segmentStart.Type.Value = PathType.Bezier;
break;
}
}
private void ensureCursor()
{
if (cursor == null)
{
HitObject.Path.ControlPoints.Add(cursor = new PathControlPoint { Position = { Value = Vector2.Zero } });
currentSegmentLength++;
updatePathType();
}
}
2018-11-01 18:23:37 +08:00
private void updateSlider()
{
HitObject.Path.ExpectedDistance.Value = composer?.GetSnappedDistanceFromDistance(HitObject.StartTime, (float)HitObject.Path.CalculatedDistance) ?? (float)HitObject.Path.CalculatedDistance;
bodyPiece.UpdateFrom(HitObject);
headCirclePiece.UpdateFrom(HitObject.HeadCircle);
tailCirclePiece.UpdateFrom(HitObject.TailCircle);
}
private void setState(PlacementState newState)
{
state = newState;
}
private enum PlacementState
{
2018-10-04 17:24:25 +08:00
Initial,
Body,
2018-10-04 17:24:25 +08:00
}
}
}