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/Masks/SliderMasks/SliderPlacementMask.cs

180 lines
5.6 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
2018-10-26 13:18:48 +08:00
using System;
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-10-04 17:24:25 +08:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
2018-10-26 13:18:48 +08:00
using osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks.Components;
using OpenTK;
2018-10-04 17:24:25 +08:00
using OpenTK.Input;
2018-10-26 13:18:48 +08:00
namespace osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks
{
public class SliderPlacementMask : PlacementMask
{
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;
public SliderPlacementMask()
: 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 ControlPointVisualiser(HitObject),
};
2018-10-04 17:24:25 +08:00
setState(PlacementState.Initial);
}
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;
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;
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-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);
}
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 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
}
}
}
}