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

102 lines
3.4 KiB
C#
Raw Normal View History

2023-12-30 01: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.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
2023-12-30 01:24:05 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Utils;
using osuTK;
namespace osu.Game.Screens.Edit.Compose.Components
{
public partial class CircularPositionSnapGrid : PositionSnapGrid
{
/// <summary>
/// The spacing between grid lines of this <see cref="CircularPositionSnapGrid"/>.
/// </summary>
public BindableFloat Spacing { get; } = new BindableFloat(1f)
2023-12-30 01:24:05 +08:00
{
MinValue = 0f,
};
2023-12-30 01:24:05 +08:00
public CircularPositionSnapGrid()
{
Spacing.BindValueChanged(_ => GridCache.Invalidate());
2023-12-30 01:24:05 +08:00
}
protected override void CreateContent()
{
var drawSize = DrawSize;
// Calculate the maximum distance from the origin to the edge of the grid.
float maxDist = MathF.Max(
MathF.Max(StartPosition.Value.Length, (StartPosition.Value - drawSize).Length),
MathF.Max((StartPosition.Value - new Vector2(drawSize.X, 0)).Length, (StartPosition.Value - new Vector2(0, drawSize.Y)).Length)
2023-12-30 01:24:05 +08:00
);
generateCircles((int)(maxDist / Spacing.Value) + 1);
2023-12-30 01:24:05 +08:00
GenerateOutline(drawSize);
}
private void generateCircles(int count)
{
// Make lines the same width independent of display resolution.
float lineWidth = 2 * DrawWidth / ScreenSpaceDrawQuad.Width;
List<CircularContainer> generatedCircles = new List<CircularContainer>();
for (int i = 0; i < count; i++)
{
// Add a minimum diameter so the center circle is clearly visible.
float diameter = MathF.Max(lineWidth * 1.5f, i * Spacing.Value * 2);
2023-12-30 01:24:05 +08:00
var gridCircle = new CircularContainer
{
BorderColour = Colour4.White,
BorderThickness = lineWidth,
Alpha = 0.2f,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.None,
Width = diameter,
Height = diameter,
Position = StartPosition.Value,
2023-12-30 01:24:05 +08:00
Masking = true,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
AlwaysPresent = true,
Alpha = 0f,
}
};
generatedCircles.Add(gridCircle);
}
if (generatedCircles.Count == 0)
return;
generatedCircles.First().Alpha = 0.8f;
AddRangeInternal(generatedCircles);
}
public override Vector2 GetSnappedPosition(Vector2 original)
{
Vector2 relativeToStart = original - StartPosition.Value;
2023-12-30 01:24:05 +08:00
if (relativeToStart.LengthSquared < Precision.FLOAT_EPSILON)
return StartPosition.Value;
2023-12-30 01:24:05 +08:00
float length = relativeToStart.Length;
float wantedLength = MathF.Round(length / Spacing.Value) * Spacing.Value;
2023-12-30 01:24:05 +08:00
return StartPosition.Value + Vector2.Multiply(relativeToStart, wantedLength / length);
2023-12-30 01:24:05 +08:00
}
}
}