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
|
|
|
|
|
2018-10-26 13:18:48 +08:00
|
|
|
using System;
|
2018-10-04 12:44:49 +08:00
|
|
|
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;
|
|
|
|
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-26 13:18:48 +08:00
|
|
|
using osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks.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-26 13:18:48 +08:00
|
|
|
namespace osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks
|
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-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-26 13:18:48 +08:00
|
|
|
new SliderBodyPiece(HitObject),
|
2018-10-25 18:34:35 +08:00
|
|
|
new SliderCirclePiece(HitObject, SliderPosition.Start),
|
|
|
|
new SliderCirclePiece(HitObject, SliderPosition.End),
|
2018-10-29 14:56:17 +08:00
|
|
|
new ControlPointVisualiser(HitObject),
|
2018-10-04 12:44:49 +08:00
|
|
|
};
|
|
|
|
|
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-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-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();
|
|
|
|
|
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()
|
|
|
|
{
|
2018-10-26 13:18:48 +08:00
|
|
|
HitObject.ControlPoints = segments.SelectMany(s => s.ControlPoints).Concat(cursor.Yield()).ToArray();
|
|
|
|
HitObject.CurveType = HitObject.ControlPoints.Length > 2 ? CurveType.Bezier : CurveType.Linear;
|
2018-10-04 17:24:25 +08:00
|
|
|
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();
|
|
|
|
|
2018-10-25 18:38:00 +08:00
|
|
|
for (int i = 0; i < segments.Count; i++)
|
|
|
|
segments[i].Calculate(i == segments.Count - 1 ? (Vector2?)cursor : null);
|
2018-10-04 17:24:25 +08:00
|
|
|
|
2018-10-26 13:18:48 +08:00
|
|
|
HitObject.ControlPoints = segments.SelectMany(s => s.ControlPoints).Concat(cursor.Yield()).ToArray();
|
|
|
|
HitObject.CurveType = HitObject.ControlPoints.Length > 2 ? CurveType.Bezier : CurveType.Linear;
|
2018-10-25 18:38:00 +08:00
|
|
|
HitObject.Distance = segments.Sum(s => s.Distance);
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-26 13:18:48 +08:00
|
|
|
public void Calculate(Vector2? cursor = null)
|
2018-10-04 17:24:25 +08:00
|
|
|
{
|
2018-10-26 13:18:48 +08:00
|
|
|
Span<Vector2> allControlPoints = stackalloc Vector2[ControlPoints.Count + (cursor.HasValue ? 1 : 0)];
|
|
|
|
|
|
|
|
for (int i = 0; i < ControlPoints.Count; i++)
|
|
|
|
allControlPoints[i] = ControlPoints[i];
|
2018-10-04 17:24:25 +08:00
|
|
|
if (cursor.HasValue)
|
2018-10-26 13:18:48 +08:00
|
|
|
allControlPoints[allControlPoints.Length - 1] = cursor.Value;
|
2018-10-04 17:24:25 +08:00
|
|
|
|
2018-10-26 13:18:48 +08:00
|
|
|
List<Vector2> result;
|
2018-10-04 17:24:25 +08:00
|
|
|
|
2018-10-26 13:18:48 +08:00
|
|
|
switch (allControlPoints.Length)
|
2018-10-04 17:24:25 +08:00
|
|
|
{
|
2018-10-05 16:05:39 +08:00
|
|
|
case 1:
|
|
|
|
case 2:
|
2018-10-26 13:18:48 +08:00
|
|
|
result = new LinearApproximator(allControlPoints).CreateLinear();
|
2018-10-04 17:24:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
2018-10-26 13:18:48 +08:00
|
|
|
result = new BezierApproximator(allControlPoints).CreateBezier();
|
2018-10-04 17:24:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Distance = 0;
|
2018-10-26 13:18:48 +08:00
|
|
|
for (int i = 0; i < result.Count - 1; i++)
|
|
|
|
Distance += Vector2.Distance(result[i], result[i + 1]);
|
2018-10-04 17:24:25 +08:00
|
|
|
}
|
2018-10-04 12:44:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|