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

53 lines
2.2 KiB
C#
Raw Normal View History

2023-06-23 00:37:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2021-09-19 22:41:30 +08:00
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables;
2023-12-29 01:56:49 +08:00
using osu.Game.Utils;
2021-09-19 22:41:30 +08:00
using osuTK;
namespace osu.Game.Screens.Edit.Compose.Components
{
2023-12-29 04:00:47 +08:00
public partial class RectangularPositionSnapGrid : LinedPositionSnapGrid
2021-09-19 22:41:30 +08:00
{
/// <summary>
/// The spacing between grid lines of this <see cref="RectangularPositionSnapGrid"/>.
/// </summary>
public Bindable<Vector2> Spacing { get; } = new Bindable<Vector2>(Vector2.One);
2023-12-29 00:49:14 +08:00
/// <summary>
/// The rotation in degrees of the grid lines of this <see cref="RectangularPositionSnapGrid"/>.
/// </summary>
public BindableFloat GridLineRotation { get; } = new BindableFloat();
public RectangularPositionSnapGrid()
2023-12-29 00:49:14 +08:00
{
Spacing.BindValueChanged(_ => GridCache.Invalidate());
GridLineRotation.BindValueChanged(_ => GridCache.Invalidate());
2023-12-29 00:49:14 +08:00
}
2023-12-29 04:00:47 +08:00
protected override void CreateContent()
2021-09-19 22:41:30 +08:00
{
var drawSize = DrawSize;
var rot = Quaternion.FromAxisAngle(Vector3.UnitZ, MathHelper.DegreesToRadians(GridLineRotation.Value));
2023-12-29 00:49:14 +08:00
GenerateGridLines(Vector2.Transform(new Vector2(0, -Spacing.Value.Y), rot), drawSize);
GenerateGridLines(Vector2.Transform(new Vector2(0, Spacing.Value.Y), rot), drawSize);
2021-09-19 22:41:30 +08:00
GenerateGridLines(Vector2.Transform(new Vector2(-Spacing.Value.X, 0), rot), drawSize);
GenerateGridLines(Vector2.Transform(new Vector2(Spacing.Value.X, 0), rot), drawSize);
2023-12-29 04:00:47 +08:00
GenerateOutline(drawSize);
2023-12-29 00:49:14 +08:00
}
2023-12-29 04:00:47 +08:00
public override Vector2 GetSnappedPosition(Vector2 original)
2021-09-19 22:41:30 +08:00
{
Vector2 relativeToStart = GeometryUtils.RotateVector(original - StartPosition.Value, GridLineRotation.Value);
Vector2 offset = Vector2.Divide(relativeToStart, Spacing.Value);
2021-09-19 22:41:30 +08:00
Vector2 roundedOffset = new Vector2(MathF.Round(offset.X), MathF.Round(offset.Y));
return StartPosition.Value + GeometryUtils.RotateVector(Vector2.Multiply(roundedOffset, Spacing.Value), -GridLineRotation.Value);
2021-09-19 22:41:30 +08:00
}
}
}