1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 11:47:24 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Edit/Masks/SliderPlacementMask.cs

211 lines
6.3 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
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;
2018-10-04 17:24:25 +08:00
using osu.Framework.Graphics.Lines;
using osu.Framework.Input.Events;
2018-10-04 17:24:25 +08:00
using osu.Game.Graphics;
using osu.Game.Rulesets.Edit;
2018-10-04 17:24:25 +08:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Osu.Objects;
using OpenTK;
2018-10-04 17:24:25 +08:00
using OpenTK.Input;
namespace osu.Game.Rulesets.Osu.Edit.Masks
{
public partial class SliderPlacementMask : PlacementMask
{
public new Slider HitObject => (Slider)base.HitObject;
private readonly CirclePlacementMask headMask;
private readonly CirclePlacementMask tailMask;
2018-10-04 17:24:25 +08:00
private readonly Path path;
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;
public SliderPlacementMask()
: base(new Slider())
{
RelativeSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
path = new SmoothPath { PathWidth = 3 },
headMask = new CirclePlacementMask(),
tailMask = new CirclePlacementMask(),
};
2018-10-04 17:24:25 +08:00
segments.Add(new Segment(Vector2.Zero));
setState(PlacementState.Initial);
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
path.Colour = colours.YellowDark;
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
switch (state)
{
2018-10-04 17:24:25 +08:00
case PlacementState.Initial:
headMask.Position = e.MousePosition;
return true;
2018-10-04 17:24:25 +08:00
case PlacementState.Body:
tailMask.Position = e.MousePosition;
cursor = tailMask.Position - headMask.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;
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()
{
setState(PlacementState.Body);
}
private void endCurve()
{
HitObject.Position = headMask.Position;
HitObject.ControlPoints = segments.SelectMany(s => s.ControlPoints).Concat(cursor.Yield()).ToList();
HitObject.CurveType = HitObject.ControlPoints.Count > 2 ? CurveType.Bezier : CurveType.Linear;
HitObject.Distance = segments.Sum(s => s.Distance);
Finish();
}
protected override void Update()
{
base.Update();
segments.ForEach(s => s.Calculate());
switch (state)
{
case PlacementState.Body:
path.Position = headMask.Position;
path.ClearVertices();
for (int i = 0; i < segments.Count; i++)
{
var segmentPath = segments[i].Calculate(i == segments.Count - 1 ? (Vector2?)cursor : null);
segmentPath.ForEach(v => path.AddVertex(v));
}
path.OriginPosition = path.PositionInBoundingBox(Vector2.Zero);
break;
}
}
private void setState(PlacementState newState)
{
switch (newState)
{
2018-10-04 17:24:25 +08:00
case PlacementState.Initial:
tailMask.Alpha = 0;
break;
2018-10-04 17:24:25 +08:00
case PlacementState.Body:
tailMask.Alpha = 1;
break;
}
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 float Distance { get; private set; }
public readonly List<Vector2> ControlPoints = new List<Vector2>();
public IApproximator Approximator = new LinearApproximator();
public Segment(Vector2 offset)
{
ControlPoints.Add(offset);
}
public List<Vector2> Calculate(Vector2? cursor = null)
{
var allControlPoints = ControlPoints.ToList();
if (cursor.HasValue)
allControlPoints.Add(cursor.Value);
IApproximator approximator;
switch (Approximator)
{
case null:
approximator = new LinearApproximator();
break;
case LinearApproximator _ when allControlPoints.Count > 2:
case CircularArcApproximator _ when allControlPoints.Count > 3:
approximator = new BezierApproximator();
break;
default:
approximator = Approximator;
break;
}
Distance = 0;
var points = approximator.Approximate(allControlPoints);
for (int i = 0; i < points.Count - 1; i++)
Distance += Vector2.Distance(points[i], points[i + 1]);
return points;
}
}
}
}