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

@ -185,7 +185,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void updatePlacementPosition(Vector2 screenSpacePosition)
{
Vector2 snappedGridPosition = composer.GetSnappedPosition(ToLocalSpace(screenSpacePosition));
Vector2 snappedGridPosition = composer.GetSnappedPosition(ToLocalSpace(screenSpacePosition), 0).position;
Vector2 snappedScreenSpacePosition = ToScreenSpace(snappedGridPosition);
currentPlacement.UpdatePosition(snappedScreenSpacePosition);
@ -232,15 +232,15 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void onDragRequested(SelectionBlueprint blueprint, DragEvent dragEvent)
{
HitObject draggedObject = blueprint.DrawableObject.HitObject;
Vector2 movePosition = blueprint.ScreenSpaceMovementStartPosition + dragEvent.ScreenSpaceMousePosition - dragEvent.ScreenSpaceMouseDownPosition;
Vector2 snappedPosition = composer.GetSnappedPosition(ToLocalSpace(movePosition));
(Vector2 snappedPosition, double snappedTime) = composer.GetSnappedPosition(ToLocalSpace(movePosition), draggedObject.StartTime);
// Move the hitobjects
selectionHandler.HandleMovement(new MoveSelectionEvent(blueprint, blueprint.ScreenSpaceMovementStartPosition, ToScreenSpace(snappedPosition)));
// Apply the start time at the newly snapped-to position
double offset = composer.GetSnappedTime(draggedObject.StartTime, snappedPosition) - draggedObject.StartTime;
double offset = snappedTime - draggedObject.StartTime;
foreach (HitObject obj in selectionHandler.SelectedHitObjects)
obj.StartTime += offset;
}

View File

@ -2,9 +2,11 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
using osuTK;
@ -12,6 +14,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
{
public abstract class CircularDistanceSnapGrid : DistanceSnapGrid
{
[Resolved]
private HitObjectComposer composer { get; set; }
protected CircularDistanceSnapGrid(HitObject hitObject, Vector2 centrePosition)
: base(hitObject, centrePosition)
{
@ -63,7 +68,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
}
}
public override Vector2 GetSnappedPosition(Vector2 position)
public override (Vector2 position, double time) GetSnappedPosition(Vector2 position)
{
Vector2 direction = position - CentrePosition;
@ -76,7 +81,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
int radialCount = Math.Max(1, (int)Math.Round(distance / radius));
Vector2 normalisedDirection = direction * new Vector2(1f / distance);
return CentrePosition + normalisedDirection * radialCount * radius;
Vector2 snappedPosition = CentrePosition + normalisedDirection * radialCount * radius;
return (snappedPosition, StartTime + composer.GetSnappedDurationFromDistance(StartTime, (snappedPosition - CentrePosition).Length));
}
}
}

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.