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

157 lines
4.4 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.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;
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;
2018-10-04 17:24:25 +08:00
private readonly List<Segment> segments = new List<Segment>();
private Vector2 cursor;
2018-10-04 17:24:25 +08:00
private PlacementState state;
2018-11-06 17:04:03 +08:00
public SliderPlacementBlueprint()
: base(new Objects.Slider())
{
RelativeSizeAxes = Axes.Both;
2018-10-24 12:42:51 +08:00
segments.Add(new Segment(Vector2.Zero));
}
2018-10-24 12:42:51 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
InternalChildren = new Drawable[]
{
2018-10-26 13:18:48 +08:00
new SliderBodyPiece(HitObject),
new SliderCirclePiece(HitObject, SliderPosition.Start),
new SliderCirclePiece(HitObject, SliderPosition.End),
new PathControlPointVisualiser(HitObject),
};
2018-10-04 17:24:25 +08:00
setState(PlacementState.Initial);
}
2019-01-26 22:56:31 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
// Fixes a 1-frame position discrepancy due to the first mouse move event happening in the next frame
HitObject.Position = Parent?.ToLocalSpace(GetContainingInputManager().CurrentState.Mouse.Position) ?? Vector2.Zero;
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
switch (state)
{
2018-10-04 17:24:25 +08:00
case PlacementState.Initial:
2018-10-24 12:42:51 +08:00
HitObject.Position = e.MousePosition;
return true;
2019-04-01 11:16:05 +08:00
2018-10-04 17:24:25 +08:00
case PlacementState.Body:
2018-10-24 12:42:51 +08:00
cursor = e.MousePosition - HitObject.Position;
return true;
}
return false;
}
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:
segments.Last().ControlPoints.Add(cursor);
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)
{
segments.Add(new Segment(segments[segments.Count - 1].ControlPoints.Last()));
return true;
}
private void beginCurve()
{
2018-10-17 17:20:39 +08:00
BeginPlacement();
HitObject.StartTime = EditorClock.CurrentTime;
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()
{
2018-11-12 12:55:14 +08:00
var newControlPoints = segments.SelectMany(s => s.ControlPoints).Concat(cursor.Yield()).ToArray();
HitObject.Path = new SliderPath(newControlPoints.Length > 2 ? PathType.Bezier : PathType.Linear, newControlPoints);
}
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);
}
}
}
}