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

166 lines
5.2 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.
using System.Collections.Generic;
using System.Linq;
2018-10-04 17:24:25 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
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;
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(new PathControlPoint { Position = { Value = Vector2.Zero } });
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:
HitObject.Position = ToLocalSpace(screenSpacePosition);
break;
2019-04-01 11:16:05 +08:00
2018-10-04 17:24:25 +08:00
case PlacementState.Body:
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
2019-12-06 12:08:28 +08:00
HitObject.Path.ControlPoints.Last().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:
2019-12-06 12:08:28 +08:00
HitObject.Path.ControlPoints.Add(new PathControlPoint { Position = { Value = HitObject.Path.ControlPoints.Last().Position.Value } });
2018-10-04 17:24:25 +08:00
break;
}
break;
}
2018-10-04 17:24:25 +08:00
return true;
}
protected override bool OnMouseUp(MouseUpEvent e)
{
if (state == PlacementState.Body && e.Button == MouseButton.Right)
endCurve();
return base.OnMouseUp(e);
}
protected override bool OnDoubleClick(DoubleClickEvent e)
{
2019-12-06 12:08:28 +08:00
// At the point of a double click, there's guaranteed to be at least two points - one from the click, and one from the cursor
HitObject.Path.ControlPoints[HitObject.Path.ControlPoints.Count - 2].Type.Value = PathType.Bezier;
2018-10-04 17:24:25 +08:00
return true;
}
private void beginCurve()
{
2018-10-17 17:20:39 +08:00
BeginPlacement();
2018-10-04 17:24:25 +08:00
setState(PlacementState.Body);
}
private void endCurve()
{
2018-11-01 18:23:37 +08:00
updateSlider();
2018-10-17 17:20:39 +08:00
EndPlacement();
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
2018-11-01 18:23:37 +08:00
private void updateSlider()
{
HitObject.Path.ExpectedDistance.Value = null;
HitObject.Path.ExpectedDistance.Value = composer?.GetSnappedDistanceFromDistance(HitObject.StartTime, (float)HitObject.Path.Distance) ?? (float)HitObject.Path.Distance;
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
}
private class Segment
{
public readonly List<Vector2> ControlPoints = new List<Vector2>();
public Segment(Vector2 offset)
{
ControlPoints.Add(offset);
}
}
}
}