2019-10-10 20:55:48 +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 osu.Framework.Allocation;
|
|
|
|
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.Objects;
|
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Compose.Components
|
|
|
|
{
|
|
|
|
public abstract class BeatSnapGrid : CompositeDrawable
|
|
|
|
{
|
2019-10-11 14:41:01 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The velocity of the beatmap at the point of placement in pixels per millisecond.
|
|
|
|
/// </summary>
|
2019-10-10 20:55:48 +08:00
|
|
|
protected double Velocity { get; private set; }
|
|
|
|
|
2019-10-11 14:41:01 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The spacing between each tick of the beat snapping grid.
|
|
|
|
/// </summary>
|
2019-10-10 20:55:48 +08:00
|
|
|
protected float DistanceSpacing { get; private set; }
|
|
|
|
|
2019-10-11 14:30:31 +08:00
|
|
|
protected readonly Vector2 StartPosition;
|
2019-10-10 20:55:48 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private IEditorBeatmap beatmap { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private BindableBeatDivisor beatDivisor { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
|
|
|
private readonly Cached gridCache = new Cached();
|
|
|
|
private readonly HitObject hitObject;
|
|
|
|
|
|
|
|
private double startTime;
|
|
|
|
private double beatLength;
|
|
|
|
|
|
|
|
protected BeatSnapGrid(HitObject hitObject, Vector2 startPosition)
|
|
|
|
{
|
|
|
|
this.hitObject = hitObject;
|
2019-10-11 14:30:31 +08:00
|
|
|
this.StartPosition = startPosition;
|
2019-10-10 20:55:48 +08:00
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
startTime = (hitObject as IHasEndTime)?.EndTime ?? hitObject.StartTime;
|
|
|
|
beatLength = beatmap.ControlPointInfo.TimingPointAt(startTime).BeatLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
beatDivisor.BindValueChanged(_ => updateSpacing(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSpacing()
|
|
|
|
{
|
|
|
|
Velocity = GetVelocity(startTime, beatmap.ControlPointInfo, beatmap.BeatmapInfo.BaseDifficulty);
|
|
|
|
DistanceSpacing = (float)(beatLength / beatDivisor.Value * Velocity);
|
|
|
|
gridCache.Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
|
|
|
|
{
|
|
|
|
if ((invalidation & Invalidation.RequiredParentSizeToFit) > 0)
|
|
|
|
gridCache.Invalidate();
|
|
|
|
|
|
|
|
return base.Invalidate(invalidation, source, shallPropagate);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
if (!gridCache.IsValid)
|
|
|
|
{
|
|
|
|
ClearInternal();
|
2019-10-11 14:30:31 +08:00
|
|
|
CreateGrid(StartPosition);
|
2019-10-10 20:55:48 +08:00
|
|
|
gridCache.Validate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-11 14:41:01 +08:00
|
|
|
/// Creates the content which visualises the grid ticks.
|
2019-10-10 20:55:48 +08:00
|
|
|
/// </summary>
|
|
|
|
protected abstract void CreateGrid(Vector2 startPosition);
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-11 14:41:01 +08:00
|
|
|
/// Retrieves the velocity of gameplay at a point in time in pixels per millisecond.
|
2019-10-10 20:55:48 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="time">The time to retrieve the velocity at.</param>
|
2019-10-11 14:41:01 +08:00
|
|
|
/// <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>
|
2019-10-10 20:55:48 +08:00
|
|
|
protected abstract float GetVelocity(double time, ControlPointInfo controlPointInfo, BeatmapDifficulty difficulty);
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-11 14:41:01 +08:00
|
|
|
/// Snaps a screen-space position to this grid.
|
2019-10-10 20:55:48 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="screenSpacePosition">The original screen-space position.</param>
|
2019-10-11 14:41:01 +08:00
|
|
|
/// <returns>The snapped screen-space position.</returns>
|
2019-10-10 20:55:48 +08:00
|
|
|
public abstract Vector2 GetSnapPosition(Vector2 screenSpacePosition);
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-11 14:41:01 +08:00
|
|
|
/// Retrieves the time at a snapped screen-space position.
|
2019-10-10 20:55:48 +08:00
|
|
|
/// </summary>
|
2019-10-11 14:41:01 +08:00
|
|
|
/// <param name="screenSpacePosition">The snapped screen-space position.</param>
|
2019-10-10 20:55:48 +08:00
|
|
|
/// <returns>The time at the snapped position.</returns>
|
2019-10-11 14:41:01 +08:00
|
|
|
public double GetSnapTime(Vector2 screenSpacePosition) => startTime + (ToLocalSpace(screenSpacePosition) - CentrePosition).Length / Velocity;
|
2019-10-10 20:55:48 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Retrieves the applicable colour for a beat index.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The 0-based beat index.</param>
|
|
|
|
/// <returns>The applicable colour.</returns>
|
|
|
|
protected ColourInfo GetColourForBeatIndex(int index)
|
|
|
|
{
|
2019-10-11 13:01:36 +08:00
|
|
|
int beat = (index + 1) % beatDivisor.Value;
|
2019-10-10 20:55:48 +08:00
|
|
|
ColourInfo colour = colours.Gray5;
|
|
|
|
|
2019-10-11 13:01:36 +08:00
|
|
|
for (int i = 0; i < BindableBeatDivisor.VALID_DIVISORS.Length; i++)
|
2019-10-10 20:55:48 +08:00
|
|
|
{
|
2019-10-11 13:01:36 +08:00
|
|
|
int divisor = BindableBeatDivisor.VALID_DIVISORS[i];
|
2019-10-10 20:55:48 +08:00
|
|
|
|
2019-10-11 13:01:36 +08:00
|
|
|
if ((beat * divisor) % beatDivisor.Value == 0)
|
|
|
|
{
|
|
|
|
colour = BindableBeatDivisor.GetColourFor(divisor, colours);
|
|
|
|
break;
|
2019-10-10 20:55:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int repeatIndex = index / beatDivisor.Value;
|
|
|
|
return colour.MultiplyAlpha(0.5f / (repeatIndex + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|