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;
|
2023-12-30 07:43:41 +08:00
|
|
|
|
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>
|
2023-12-30 07:43:41 +08:00
|
|
|
|
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>
|
2023-12-30 07:43:41 +08:00
|
|
|
|
public BindableFloat GridLineRotation { get; } = new BindableFloat();
|
|
|
|
|
|
|
|
|
|
public RectangularPositionSnapGrid()
|
2023-12-29 00:49:14 +08:00
|
|
|
|
{
|
2023-12-30 07:43:41 +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;
|
2023-12-30 07:43:41 +08:00
|
|
|
|
var rot = Quaternion.FromAxisAngle(Vector3.UnitZ, MathHelper.DegreesToRadians(GridLineRotation.Value));
|
2023-12-29 00:49:14 +08:00
|
|
|
|
|
2023-12-30 07:43:41 +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
|
|
|
|
|
2023-12-30 07:43:41 +08:00
|
|
|
|
GenerateGridLines(Vector2.Transform(new Vector2(-Spacing.Value.X, 0), rot), drawSize);
|
|
|
|
|
GenerateGridLines(Vector2.Transform(new Vector2(Spacing.Value.X, 0), rot), drawSize);
|
2022-05-04 12:41:55 +08:00
|
|
|
|
|
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
|
|
|
|
{
|
2023-12-30 07:43:41 +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));
|
|
|
|
|
|
2023-12-30 07:43:41 +08:00
|
|
|
|
return StartPosition.Value + GeometryUtils.RotateVector(Vector2.Multiply(roundedOffset, Spacing.Value), -GridLineRotation.Value);
|
2021-09-19 22:41:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|