2018-10-04 12:44:49 +08:00
|
|
|
// 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;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Framework.Graphics;
|
2018-10-05 16:05:39 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-10-04 17:24:25 +08:00
|
|
|
using osu.Framework.Graphics.Lines;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2018-10-04 17:24:25 +08:00
|
|
|
using osu.Game.Graphics;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Game.Rulesets.Edit;
|
2018-10-04 17:24:25 +08:00
|
|
|
using osu.Game.Rulesets.Objects;
|
2018-10-04 12:44:49 +08:00
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
2018-10-25 18:34:35 +08:00
|
|
|
using osu.Game.Rulesets.Osu.Edit.Masks.Slider.Components;
|
2018-10-04 12:44:49 +08:00
|
|
|
using OpenTK;
|
2018-10-04 17:24:25 +08:00
|
|
|
using OpenTK.Input;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2018-10-25 18:34:35 +08:00
|
|
|
namespace osu.Game.Rulesets.Osu.Edit.Masks.Slider
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
2018-10-25 18:34:35 +08:00
|
|
|
public class SliderPlacementMask : PlacementMask
|
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
|
|
|
|
2018-10-24 12:42:51 +08:00
|
|
|
private Path path;
|
|
|
|
private Container<SliderControlPoint> controlPointContainer;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2018-10-04 17:24:25 +08:00
|
|
|
private readonly List<Segment> segments = new List<Segment>();
|
|
|
|
private Vector2 cursor;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2018-10-04 17:24:25 +08:00
|
|
|
private PlacementState state;
|
2018-10-04 12:44:49 +08:00
|
|
|
|
|
|
|
public SliderPlacementMask()
|
2018-10-25 18:34:35 +08:00
|
|
|
: base(new Objects.Slider())
|
2018-10-04 12:44:49 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2018-10-24 12:42:51 +08:00
|
|
|
segments.Add(new Segment(Vector2.Zero));
|
|
|
|
}
|
2018-10-04 12:44:49 +08:00
|
|
|
|
2018-10-24 12:42:51 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
2018-10-04 12:44:49 +08:00
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
2018-10-05 14:59:37 +08:00
|
|
|
path = new SmoothPath { PathWidth = 3 },
|
2018-10-25 18:34:35 +08:00
|
|
|
new SliderCirclePiece(HitObject, SliderPosition.Start),
|
|
|
|
new SliderCirclePiece(HitObject, SliderPosition.End),
|
2018-10-05 16:05:39 +08:00
|
|
|
controlPointContainer = new Container<SliderControlPoint> { RelativeSizeAxes = Axes.Both }
|
2018-10-04 12:44:49 +08:00
|
|
|
};
|
|
|
|
|
2018-10-24 12:42:51 +08:00
|
|
|
path.Colour = colours.YellowDark;
|
2018-10-04 17:24:25 +08:00
|
|
|
|
|
|
|
setState(PlacementState.Initial);
|
|
|
|
}
|
|
|
|
|
2018-10-04 12:44:49 +08:00
|
|
|
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;
|
2018-10-04 12:44:49 +08:00
|
|
|
return true;
|
2018-10-04 17:24:25 +08:00
|
|
|
case PlacementState.Body:
|
2018-10-24 12:42:51 +08:00
|
|
|
cursor = e.MousePosition - HitObject.Position;
|
2018-10-05 16:05:39 +08:00
|
|
|
controlPointContainer.Last().NextPoint = e.MousePosition;
|
2018-10-04 12:44:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
2018-10-04 17:24:25 +08:00
|
|
|
case PlacementState.Initial:
|
|
|
|
beginCurve();
|
2018-10-04 12:44:49 +08:00
|
|
|
break;
|
2018-10-04 17:24:25 +08:00
|
|
|
case PlacementState.Body:
|
|
|
|
switch (e.Button)
|
|
|
|
{
|
|
|
|
case MouseButton.Left:
|
|
|
|
segments.Last().ControlPoints.Add(cursor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-04 12:44:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:05:39 +08:00
|
|
|
controlPointContainer.Add(new SliderControlPoint { Position = e.MousePosition });
|
|
|
|
|
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()));
|
2018-10-05 16:05:39 +08:00
|
|
|
controlPointContainer.Last().SegmentSeparator = true;
|
2018-10-04 17:24:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void beginCurve()
|
|
|
|
{
|
2018-10-17 17:20:39 +08:00
|
|
|
BeginPlacement();
|
|
|
|
|
2018-10-10 14:37:42 +08:00
|
|
|
HitObject.StartTime = EditorClock.CurrentTime;
|
2018-10-04 17:24:25 +08:00
|
|
|
setState(PlacementState.Body);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void endCurve()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2018-10-17 17:20:39 +08:00
|
|
|
EndPlacement();
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
segments.ForEach(s => s.Calculate());
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case PlacementState.Body:
|
2018-10-24 12:42:51 +08:00
|
|
|
path.Position = HitObject.Position;
|
2018-10-04 17:24:25 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setState(PlacementState newState)
|
|
|
|
{
|
|
|
|
state = newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum PlacementState
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private class Segment
|
|
|
|
{
|
|
|
|
public float Distance { get; private set; }
|
|
|
|
|
|
|
|
public readonly List<Vector2> ControlPoints = new List<Vector2>();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-10-05 16:05:39 +08:00
|
|
|
switch (allControlPoints.Count)
|
2018-10-04 17:24:25 +08:00
|
|
|
{
|
2018-10-05 16:05:39 +08:00
|
|
|
case 1:
|
|
|
|
case 2:
|
2018-10-04 17:24:25 +08:00
|
|
|
approximator = new LinearApproximator();
|
|
|
|
break;
|
|
|
|
default:
|
2018-10-05 16:05:39 +08:00
|
|
|
approximator = new BezierApproximator();
|
2018-10-04 17:24:25 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|