2021-07-07 19:24:05 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-10-26 10:00:44 +08:00
|
|
|
using System;
|
2021-07-07 19:24:05 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Lines;
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
2021-10-26 13:51:52 +08:00
|
|
|
using osu.Game.Rulesets.Catch.UI;
|
2021-07-07 19:24:05 +08:00
|
|
|
using osu.Game.Rulesets.Edit;
|
|
|
|
using osu.Game.Rulesets.UI;
|
|
|
|
using osu.Game.Rulesets.UI.Scrolling;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Catch.Edit
|
|
|
|
{
|
2021-10-26 10:12:10 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The guide lines used in the osu!catch editor to compose patterns that can be caught with constant speed.
|
|
|
|
/// Currently, only forward placement (an object is snapped based on the previous object, not the opposite) is supported.
|
|
|
|
/// </summary>
|
2021-07-07 19:24:05 +08:00
|
|
|
public partial class CatchDistanceSnapGrid : CompositeDrawable
|
|
|
|
{
|
|
|
|
public double StartTime { get; set; }
|
|
|
|
|
|
|
|
public float StartX { get; set; }
|
|
|
|
|
2021-10-26 19:20:43 +08:00
|
|
|
private const double max_vertical_line_length_in_time = CatchPlayfield.WIDTH / Catcher.BASE_WALK_SPEED;
|
2021-10-26 13:51:52 +08:00
|
|
|
|
2021-07-07 19:24:05 +08:00
|
|
|
private readonly double[] velocities;
|
|
|
|
|
|
|
|
private readonly List<Path> verticalPaths = new List<Path>();
|
|
|
|
|
2021-10-26 09:41:17 +08:00
|
|
|
private readonly List<Vector2[]> verticalLineVertices = new List<Vector2[]>();
|
2021-07-07 19:24:05 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private Playfield playfield { get; set; }
|
|
|
|
|
|
|
|
private ScrollingHitObjectContainer hitObjectContainer => (ScrollingHitObjectContainer)playfield.HitObjectContainer;
|
|
|
|
|
|
|
|
public CatchDistanceSnapGrid(double[] velocities)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
Anchor = Anchor.BottomLeft;
|
|
|
|
|
|
|
|
this.velocities = velocities;
|
|
|
|
|
|
|
|
for (int i = 0; i < velocities.Length; i++)
|
|
|
|
{
|
|
|
|
verticalPaths.Add(new SmoothPath
|
|
|
|
{
|
|
|
|
PathRadius = 2,
|
|
|
|
Alpha = 0.5f,
|
|
|
|
});
|
|
|
|
|
2021-10-26 09:41:17 +08:00
|
|
|
verticalLineVertices.Add(new[] { Vector2.Zero, Vector2.Zero });
|
2021-07-07 19:24:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AddRangeInternal(verticalPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2021-10-26 13:51:52 +08:00
|
|
|
double currentTime = hitObjectContainer.Time.Current;
|
2021-07-07 19:24:05 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < velocities.Length; i++)
|
|
|
|
{
|
|
|
|
double velocity = velocities[i];
|
|
|
|
|
2021-10-26 13:51:52 +08:00
|
|
|
// The line ends at the top of the playfield.
|
|
|
|
double endTime = hitObjectContainer.TimeAtPosition(-hitObjectContainer.DrawHeight, currentTime);
|
2021-07-07 19:24:05 +08:00
|
|
|
|
2021-10-26 13:51:52 +08:00
|
|
|
// Non-vertical lines are cut at the sides of the playfield.
|
|
|
|
// Vertical lines are cut at some reasonable length.
|
|
|
|
if (velocity > 0)
|
|
|
|
endTime = Math.Min(endTime, StartTime + (CatchPlayfield.WIDTH - StartX) / velocity);
|
|
|
|
else if (velocity < 0)
|
|
|
|
endTime = Math.Min(endTime, StartTime + StartX / -velocity);
|
|
|
|
else
|
|
|
|
endTime = Math.Min(endTime, StartTime + max_vertical_line_length_in_time);
|
2021-07-07 19:24:05 +08:00
|
|
|
|
2021-10-26 09:41:17 +08:00
|
|
|
Vector2[] lineVertices = verticalLineVertices[i];
|
2021-10-26 13:51:52 +08:00
|
|
|
lineVertices[0] = calculatePosition(velocity, StartTime);
|
|
|
|
lineVertices[1] = calculatePosition(velocity, endTime);
|
2021-07-07 19:24:05 +08:00
|
|
|
|
2021-10-26 13:51:52 +08:00
|
|
|
var verticalPath = verticalPaths[i];
|
2021-07-07 19:24:05 +08:00
|
|
|
verticalPath.Vertices = verticalLineVertices[i];
|
|
|
|
verticalPath.OriginPosition = verticalPath.PositionInBoundingBox(Vector2.Zero);
|
|
|
|
}
|
2021-10-26 13:51:52 +08:00
|
|
|
|
|
|
|
Vector2 calculatePosition(double velocity, double time)
|
|
|
|
{
|
|
|
|
// Don't draw inverted lines.
|
|
|
|
time = Math.Max(time, StartTime);
|
|
|
|
|
|
|
|
float x = StartX + (float)((time - StartTime) * velocity);
|
|
|
|
float y = hitObjectContainer.PositionAtTime(time, currentTime);
|
|
|
|
return new Vector2(x, y);
|
|
|
|
}
|
2021-07-07 19:24:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[CanBeNull]
|
|
|
|
public SnapResult GetSnappedPosition(Vector2 screenSpacePosition)
|
|
|
|
{
|
|
|
|
double time = hitObjectContainer.TimeAtScreenSpacePosition(screenSpacePosition);
|
|
|
|
|
2021-10-26 10:00:55 +08:00
|
|
|
// If the cursor is below the distance snap grid, snap to the origin.
|
|
|
|
// Not returning `null` to retain the continuous snapping behavior when the cursor is slightly below the origin.
|
|
|
|
// This behavior is not currently visible in the editor because editor chooses the snap start time based on the mouse position.
|
|
|
|
if (time <= StartTime)
|
|
|
|
{
|
|
|
|
float y = hitObjectContainer.PositionAtTime(StartTime);
|
|
|
|
Vector2 originPosition = hitObjectContainer.ToScreenSpace(new Vector2(StartX, y));
|
|
|
|
return new SnapResult(originPosition, StartTime);
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:24:05 +08:00
|
|
|
return enumerateSnappingCandidates(time)
|
|
|
|
.OrderBy(pos => Vector2.DistanceSquared(screenSpacePosition, pos.ScreenSpacePosition))
|
|
|
|
.FirstOrDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<SnapResult> enumerateSnappingCandidates(double time)
|
|
|
|
{
|
|
|
|
float y = hitObjectContainer.PositionAtTime(time);
|
|
|
|
|
|
|
|
foreach (double velocity in velocities)
|
|
|
|
{
|
|
|
|
float x = (float)(StartX + (time - StartTime) * velocity);
|
|
|
|
Vector2 screenSpacePosition = hitObjectContainer.ToScreenSpace(new Vector2(x, y + hitObjectContainer.DrawHeight));
|
|
|
|
yield return new SnapResult(screenSpacePosition, time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool ComputeIsMaskedAway(RectangleF maskingBounds) => false;
|
|
|
|
}
|
|
|
|
}
|