Refactor flow of snapping through HitObjectComposer

This commit is contained in:
smoogipoo
2019-10-25 12:34:49 +09:00
parent a6458fdeab
commit 607b4d874a
9 changed files with 128 additions and 89 deletions

View File

@ -6,9 +6,8 @@ using osu.Framework.Caching;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osuTK;
@ -20,16 +19,16 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// </summary>
public abstract class DistanceSnapGrid : CompositeDrawable
{
/// <summary>
/// The velocity of the beatmap at the point of placement in pixels per millisecond.
/// </summary>
protected double Velocity { get; private set; }
/// <summary>
/// The spacing between each tick of the beat snapping grid.
/// </summary>
protected float DistanceSpacing { get; private set; }
/// <summary>
/// The snapping time at <see cref="CentrePosition"/>.
/// </summary>
protected double StartTime { get; private set; }
/// <summary>
/// The position which the grid is centred on.
/// The first beat snapping tick is located at <see cref="CentrePosition"/> + <see cref="DistanceSpacing"/> in the desired direction.
@ -45,25 +44,24 @@ namespace osu.Game.Screens.Edit.Compose.Components
[Resolved]
private BindableBeatDivisor beatDivisor { get; set; }
[Resolved]
private HitObjectComposer composer { get; set; }
private readonly Cached gridCache = new Cached();
private readonly HitObject hitObject;
private double startTime;
private double beatLength;
protected DistanceSnapGrid(HitObject hitObject, Vector2 centrePosition)
{
this.hitObject = hitObject;
this.CentrePosition = centrePosition;
CentrePosition = centrePosition;
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load()
{
startTime = (hitObject as IHasEndTime)?.EndTime ?? hitObject.StartTime;
beatLength = beatmap.ControlPointInfo.TimingPointAt(startTime).BeatLength;
StartTime = (hitObject as IHasEndTime)?.EndTime ?? hitObject.StartTime;
}
protected override void LoadComplete()
@ -75,8 +73,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void updateSpacing()
{
Velocity = GetVelocity(startTime, beatmap.ControlPointInfo, beatmap.BeatmapInfo.BaseDifficulty);
DistanceSpacing = (float)(beatLength / beatDivisor.Value * Velocity);
DistanceSpacing = composer.GetBeatSnapDistanceAt(StartTime);
gridCache.Invalidate();
}
@ -105,35 +102,12 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// </summary>
protected abstract void CreateContent(Vector2 centrePosition);
/// <summary>
/// Retrieves the velocity of gameplay at a point in time in pixels per millisecond.
/// </summary>
/// <param name="time">The time to retrieve the velocity at.</param>
/// <param name="controlPointInfo">The beatmap's <see cref="ControlPointInfo"/> at the point in time.</param>
/// <param name="difficulty">The beatmap's <see cref="BeatmapDifficulty"/> at the point in time.</param>
/// <returns>The velocity.</returns>
protected abstract float GetVelocity(double time, ControlPointInfo controlPointInfo, BeatmapDifficulty difficulty);
/// <summary>
/// Snaps a position to this grid.
/// </summary>
/// <param name="position">The original position in coordinate space local to this <see cref="DistanceSnapGrid"/>.</param>
/// <returns>The snapped position in coordinate space local to this <see cref="DistanceSnapGrid"/>.</returns>
public abstract Vector2 GetSnappedPosition(Vector2 position);
/// <summary>
/// Retrieves the time at a snapped position.
/// </summary>
/// <param name="position">The snapped position in coordinate space local to this <see cref="DistanceSnapGrid"/>.</param>
/// <returns>The time at the snapped position.</returns>
public double GetSnappedTime(Vector2 position) => startTime + (position - CentrePosition).Length / Velocity;
/// <summary>
/// Snaps a distance by the snap distance of this grid.
/// </summary>
/// <param name="distance">The distance to snap in coordinate space local to this <see cref="DistanceSnapGrid"/>.</param>
/// <returns>The snapped distance.</returns>
public float GetSnappedDistance(float distance) => (int)(distance / DistanceSpacing) * DistanceSpacing;
/// <returns>A tuple containing the snapped position in coordinate space local to this <see cref="DistanceSnapGrid"/> and the respective time value.</returns>
public abstract (Vector2 position, double time) GetSnappedPosition(Vector2 position);
/// <summary>
/// Retrieves the applicable colour for a beat index.