2021-07-19 21:15:35 +08:00
|
|
|
// 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;
|
|
|
|
using System.Collections.Generic;
|
2021-07-19 21:43:28 +08:00
|
|
|
using System.Diagnostics;
|
2021-07-19 21:15:35 +08:00
|
|
|
using System.Linq;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
|
|
|
using osu.Game.Rulesets.Catch.Objects;
|
|
|
|
using osu.Game.Rulesets.Edit;
|
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
|
|
|
using osu.Game.Rulesets.UI.Scrolling;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Catch.Edit.Blueprints.Components
|
|
|
|
{
|
|
|
|
public abstract class EditablePath : CompositeDrawable
|
|
|
|
{
|
|
|
|
public int PathId => path.InvalidationID;
|
|
|
|
|
|
|
|
public IReadOnlyList<JuiceStreamPathVertex> Vertices => path.Vertices;
|
|
|
|
|
|
|
|
public int VertexCount => path.Vertices.Count;
|
|
|
|
|
2022-05-08 17:32:01 +08:00
|
|
|
protected readonly Func<float, double> PositionToTime;
|
2021-07-19 21:15:35 +08:00
|
|
|
|
|
|
|
protected IReadOnlyList<VertexState> VertexStates => vertexStates;
|
|
|
|
|
|
|
|
private readonly JuiceStreamPath path = new JuiceStreamPath();
|
|
|
|
|
|
|
|
// Invariant: `path.Vertices.Count == vertexStates.Count`
|
|
|
|
private readonly List<VertexState> vertexStates = new List<VertexState>
|
|
|
|
{
|
|
|
|
new VertexState { IsFixed = true }
|
|
|
|
};
|
|
|
|
|
|
|
|
private readonly List<VertexState> previousVertexStates = new List<VertexState>();
|
|
|
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
[CanBeNull]
|
|
|
|
private IBeatSnapProvider beatSnapProvider { get; set; }
|
|
|
|
|
2022-05-08 17:32:01 +08:00
|
|
|
protected EditablePath(Func<float, double> positionToTime)
|
2021-07-19 21:15:35 +08:00
|
|
|
{
|
2022-05-08 17:32:01 +08:00
|
|
|
PositionToTime = positionToTime;
|
2021-07-19 21:15:35 +08:00
|
|
|
|
|
|
|
Anchor = Anchor.BottomLeft;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateFrom(ScrollingHitObjectContainer hitObjectContainer, JuiceStream hitObject)
|
|
|
|
{
|
|
|
|
while (path.Vertices.Count < InternalChildren.Count)
|
|
|
|
RemoveInternal(InternalChildren[^1]);
|
|
|
|
|
|
|
|
while (InternalChildren.Count < path.Vertices.Count)
|
|
|
|
AddInternal(new VertexPiece());
|
|
|
|
|
2022-05-08 17:32:01 +08:00
|
|
|
double timeToYFactor = -hitObjectContainer.LengthAtTime(hitObject.StartTime, hitObject.StartTime + 1);
|
2021-07-19 21:15:35 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < VertexCount; i++)
|
|
|
|
{
|
|
|
|
var piece = (VertexPiece)InternalChildren[i];
|
|
|
|
var vertex = path.Vertices[i];
|
2022-05-08 17:32:01 +08:00
|
|
|
piece.Position = new Vector2(vertex.X, (float)(vertex.Time * timeToYFactor));
|
2021-07-19 21:15:35 +08:00
|
|
|
piece.UpdateFrom(vertexStates[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void InitializeFromHitObject(JuiceStream hitObject)
|
|
|
|
{
|
|
|
|
var sliderPath = hitObject.Path;
|
2022-05-08 17:32:01 +08:00
|
|
|
path.ConvertFromSliderPath(sliderPath, hitObject.Velocity);
|
2021-07-19 21:15:35 +08:00
|
|
|
|
|
|
|
// If the original slider path has non-linear type segments, resample the vertices at nested hit object times to reduce the number of vertices.
|
2021-08-26 00:42:57 +08:00
|
|
|
if (sliderPath.ControlPoints.Any(p => p.Type != null && p.Type != PathType.Linear))
|
2021-07-19 21:15:35 +08:00
|
|
|
{
|
|
|
|
path.ResampleVertices(hitObject.NestedHitObjects
|
|
|
|
.Skip(1).TakeWhile(h => !(h is Fruit)) // Only droplets in the first span are used.
|
|
|
|
.Select(h => (h.StartTime - hitObject.StartTime) * hitObject.Velocity));
|
|
|
|
}
|
|
|
|
|
|
|
|
vertexStates.Clear();
|
|
|
|
vertexStates.AddRange(path.Vertices.Select((_, i) => new VertexState
|
|
|
|
{
|
|
|
|
IsFixed = i == 0
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateHitObjectFromPath(JuiceStream hitObject)
|
|
|
|
{
|
2022-05-08 20:17:57 +08:00
|
|
|
// The SV setting may need to be changed for the current path.
|
|
|
|
var svBindable = hitObject.DifficultyControlPoint.SliderVelocityBindable;
|
|
|
|
double svToVelocityFactor = hitObject.Velocity / svBindable.Value;
|
|
|
|
double requiredVelocity = path.ComputeRequiredVelocity();
|
|
|
|
|
|
|
|
// The value is pre-rounded here because setting it to the bindable will rounded to the nearest value
|
|
|
|
// but it should be always rounded up to satisfy the required minimum velocity condition.
|
|
|
|
//
|
|
|
|
// This is rounded to integers instead of using the precision of the bindable
|
|
|
|
// because it results in a smaller number of non-redundant control points.
|
|
|
|
//
|
|
|
|
// The value is clamped here by the bindable min and max values.
|
|
|
|
// In case the required velocity is too large, the path is not preserved.
|
|
|
|
svBindable.Value = Math.Ceiling(requiredVelocity / svToVelocityFactor);
|
|
|
|
|
|
|
|
// Calculate the velocity using the resulting SV because `hitObject.Velocity` is not recomputed yet.
|
|
|
|
double velocity = svBindable.Value * svToVelocityFactor;
|
|
|
|
|
|
|
|
path.ConvertToSliderPath(hitObject.Path, hitObject.LegacyConvertedY, velocity);
|
2021-07-19 21:15:35 +08:00
|
|
|
|
|
|
|
if (beatSnapProvider == null) return;
|
|
|
|
|
2022-05-08 17:32:01 +08:00
|
|
|
double endTime = hitObject.StartTime + path.Duration;
|
2021-07-19 21:15:35 +08:00
|
|
|
double snappedEndTime = beatSnapProvider.SnapTime(endTime, hitObject.StartTime);
|
2022-05-08 20:17:57 +08:00
|
|
|
hitObject.Path.ExpectedDistance.Value = (snappedEndTime - hitObject.StartTime) * velocity;
|
2021-07-19 21:15:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 ToRelativePosition(Vector2 screenSpacePosition)
|
|
|
|
{
|
|
|
|
return ToLocalSpace(screenSpacePosition) - new Vector2(0, DrawHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool ComputeIsMaskedAway(RectangleF maskingBounds) => false;
|
|
|
|
|
2022-05-08 17:32:01 +08:00
|
|
|
protected int AddVertex(double time, float x)
|
2021-07-19 21:43:28 +08:00
|
|
|
{
|
2022-05-08 17:32:01 +08:00
|
|
|
int index = path.InsertVertex(time);
|
2021-07-19 21:43:28 +08:00
|
|
|
path.SetVertexPosition(index, x);
|
|
|
|
vertexStates.Insert(index, new VertexState());
|
|
|
|
|
|
|
|
correctFixedVertexPositions();
|
|
|
|
|
|
|
|
Debug.Assert(vertexStates.Count == VertexCount);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2021-07-19 21:46:32 +08:00
|
|
|
protected bool RemoveVertex(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= path.Vertices.Count)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (vertexStates[index].IsFixed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
path.RemoveVertices((_, i) => i == index);
|
|
|
|
|
|
|
|
vertexStates.RemoveAt(index);
|
|
|
|
if (vertexStates.Count == 0)
|
|
|
|
vertexStates.Add(new VertexState());
|
|
|
|
|
|
|
|
Debug.Assert(vertexStates.Count == VertexCount);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:32:01 +08:00
|
|
|
protected void MoveSelectedVertices(double timeDelta, float xDelta)
|
2021-07-19 21:15:35 +08:00
|
|
|
{
|
2022-05-08 17:32:01 +08:00
|
|
|
// Because the vertex list may be reordered due to time change, the state list must be reordered as well.
|
2021-07-19 21:15:35 +08:00
|
|
|
previousVertexStates.Clear();
|
|
|
|
previousVertexStates.AddRange(vertexStates);
|
|
|
|
|
|
|
|
// We will recreate the path from scratch. Note that `Clear` leaves the first vertex.
|
|
|
|
int vertexCount = VertexCount;
|
|
|
|
path.Clear();
|
|
|
|
vertexStates.RemoveRange(1, vertexCount - 1);
|
|
|
|
|
|
|
|
for (int i = 1; i < vertexCount; i++)
|
|
|
|
{
|
|
|
|
var state = previousVertexStates[i];
|
2022-05-08 17:32:01 +08:00
|
|
|
double time = state.VertexBeforeChange.Time;
|
2021-07-19 21:15:35 +08:00
|
|
|
if (state.IsSelected)
|
2022-05-08 17:32:01 +08:00
|
|
|
time += timeDelta;
|
2021-07-19 21:15:35 +08:00
|
|
|
|
2022-05-08 17:32:01 +08:00
|
|
|
int newIndex = path.InsertVertex(Math.Max(0, time));
|
2021-07-19 21:15:35 +08:00
|
|
|
vertexStates.Insert(newIndex, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, restore positions of the non-selected vertices.
|
|
|
|
for (int i = 0; i < vertexCount; i++)
|
|
|
|
{
|
|
|
|
if (!vertexStates[i].IsSelected && !vertexStates[i].IsFixed)
|
|
|
|
path.SetVertexPosition(i, vertexStates[i].VertexBeforeChange.X);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, move the selected vertices.
|
|
|
|
for (int i = 0; i < vertexCount; i++)
|
|
|
|
{
|
|
|
|
if (vertexStates[i].IsSelected && !vertexStates[i].IsFixed)
|
|
|
|
path.SetVertexPosition(i, vertexStates[i].VertexBeforeChange.X + xDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, correct the position of fixed vertices.
|
|
|
|
correctFixedVertexPositions();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void correctFixedVertexPositions()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < VertexCount; i++)
|
|
|
|
{
|
|
|
|
if (vertexStates[i].IsFixed)
|
|
|
|
path.SetVertexPosition(i, vertexStates[i].VertexBeforeChange.X);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|