1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-08 21:37:26 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/CircularDistanceSnapGrid.cs

152 lines
5.9 KiB
C#
Raw Normal View History

2019-10-11 16:13:28 +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 osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
2019-10-11 16:13:28 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
2019-10-11 16:13:28 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
2019-10-11 16:13:28 +08:00
using osuTK;
using osuTK.Graphics;
2019-10-11 16:13:28 +08:00
namespace osu.Game.Screens.Edit.Compose.Components
{
public abstract class CircularDistanceSnapGrid : DistanceSnapGrid
2019-10-11 16:13:28 +08:00
{
protected CircularDistanceSnapGrid(HitObject referenceObject, Vector2 startPosition, double startTime, double? endTime = null)
: base(referenceObject, startPosition, startTime, endTime)
2019-10-11 16:13:28 +08:00
{
}
protected override void CreateContent()
2019-10-11 16:13:28 +08:00
{
2019-10-23 16:56:09 +08:00
const float crosshair_thickness = 1;
const float crosshair_max_size = 10;
2019-10-23 16:56:09 +08:00
AddRangeInternal(new[]
{
2019-10-23 16:56:09 +08:00
new Box
{
Origin = Anchor.Centre,
Position = StartPosition,
2019-10-23 16:56:09 +08:00
Width = crosshair_thickness,
EdgeSmoothness = new Vector2(1),
Height = Math.Min(crosshair_max_size, DistanceBetweenTicks * 2),
2019-10-23 16:56:09 +08:00
},
new Box
{
Origin = Anchor.Centre,
Position = StartPosition,
2019-10-23 16:56:09 +08:00
EdgeSmoothness = new Vector2(1),
Width = Math.Min(crosshair_max_size, DistanceBetweenTicks * 2),
2019-10-23 16:56:09 +08:00
Height = crosshair_thickness,
}
});
float dx = Math.Max(StartPosition.X, DrawWidth - StartPosition.X);
float dy = Math.Max(StartPosition.Y, DrawHeight - StartPosition.Y);
2019-10-16 18:32:45 +08:00
float maxDistance = new Vector2(dx, dy).Length;
int requiredCircles = Math.Min(MaxIntervals, (int)(maxDistance / DistanceBetweenTicks));
2019-10-11 16:13:28 +08:00
for (int i = 0; i < requiredCircles; i++)
{
float diameter = (i + 1) * DistanceBetweenTicks * 2;
2019-10-11 16:13:28 +08:00
AddInternal(new Ring(ReferenceObject, GetColourForIndexFromPlacement(i))
2019-10-11 16:13:28 +08:00
{
Position = StartPosition,
Origin = Anchor.Centre,
Size = new Vector2(diameter),
InnerRadius = 4 * 1f / diameter,
2019-10-11 16:13:28 +08:00
});
}
}
public override (Vector2 position, double time) GetSnappedPosition(Vector2 position)
2019-10-11 16:13:28 +08:00
{
if (MaxIntervals == 0)
return (StartPosition, StartTime);
// This grid implementation factors in the user's distance spacing specification,
// which is usually not considered by an `IDistanceSnapProvider`.
float distanceSpacingMultiplier = (float)DistanceSpacingMultiplier.Value;
Vector2 travelVector = (position - StartPosition);
2019-10-11 16:13:28 +08:00
// We need a non-zero travel vector in order to find a valid direction.
if (travelVector == Vector2.Zero)
travelVector = new Vector2(0, -1);
float travelLength = travelVector.Length;
// FindSnappedDistance will always round down, but we want to potentially round upwards.
travelLength += DistanceBetweenTicks / 2;
// We never want to snap towards zero.
if (travelLength < DistanceBetweenTicks)
travelLength = DistanceBetweenTicks;
// When interacting with the resolved snap provider, the distance spacing multiplier should first be removed
// to allow for snapping at a non-multiplied ratio.
float snappedDistance = SnapProvider.FindSnappedDistance(ReferenceObject, travelLength / distanceSpacingMultiplier);
double snappedTime = StartTime + SnapProvider.DistanceToDuration(ReferenceObject, snappedDistance);
2019-10-11 16:13:28 +08:00
if (snappedTime > LatestEndTime)
{
double tickLength = Beatmap.GetBeatLengthAtTime(StartTime);
snappedDistance = SnapProvider.DurationToDistance(ReferenceObject, MaxIntervals * tickLength);
snappedTime = StartTime + SnapProvider.DistanceToDuration(ReferenceObject, snappedDistance);
}
// The multiplier can then be reapplied to the final position.
Vector2 snappedPosition = StartPosition + travelVector.Normalized() * snappedDistance * distanceSpacingMultiplier;
return (snappedPosition, snappedTime);
2019-10-11 16:13:28 +08:00
}
2022-05-06 23:50:28 +08:00
private class Ring : CircularProgress
{
[Resolved]
private IDistanceSnapProvider snapProvider { get; set; }
[Resolved(canBeNull: true)]
private EditorClock editorClock { get; set; }
private readonly HitObject referenceObject;
private readonly Color4 baseColour;
public Ring(HitObject referenceObject, Color4 baseColour)
{
this.referenceObject = referenceObject;
Colour = this.baseColour = baseColour;
Current.Value = 1;
}
protected override void Update()
{
base.Update();
if (editorClock == null)
return;
float distanceSpacingMultiplier = (float)snapProvider.DistanceSpacingMultiplier.Value;
double timeFromReferencePoint = editorClock.CurrentTime - referenceObject.GetEndTime();
float distanceForCurrentTime = snapProvider.DurationToDistance(referenceObject, timeFromReferencePoint)
* distanceSpacingMultiplier;
float timeBasedAlpha = 1 - Math.Clamp(Math.Abs(distanceForCurrentTime - Size.X / 2) / 30, 0, 1);
Colour = baseColour.Opacity(Math.Max(baseColour.A, timeBasedAlpha));
}
}
2019-10-11 16:13:28 +08:00
}
}