1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 12:57:36 +08:00

Add rectangular position snap grid

This commit is contained in:
Bartłomiej Dach 2021-09-19 16:41:30 +02:00
parent 801bee7c47
commit 56e80a0706
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
2 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,103 @@
// 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 NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.Editing
{
public class TestSceneRectangularPositionSnapGrid : OsuManualInputManagerTestScene
{
private Container content;
protected override Container<Drawable> Content => content;
[BackgroundDependencyLoader]
private void load()
{
base.Content.AddRange(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Colour4.Gray
},
content = new Container
{
RelativeSizeAxes = Axes.Both
}
});
}
private static readonly object[][] test_cases =
{
new object[] { new Vector2(0, 0), new Vector2(10, 10) },
new object[] { new Vector2(240, 180), new Vector2(10, 15) },
new object[] { new Vector2(160, 120), new Vector2(30, 20) },
new object[] { new Vector2(480, 360), new Vector2(100, 100) },
};
[TestCaseSource(nameof(test_cases))]
public void TestRectangularGrid(Vector2 position, Vector2 spacing)
{
RectangularPositionSnapGrid grid = null;
AddStep("create grid", () => Child = grid = new RectangularPositionSnapGrid(position, spacing)
{
RelativeSizeAxes = Axes.Both
});
AddStep("add snapping cursor", () => Add(new SnappingCursorContainer
{
RelativeSizeAxes = Axes.Both,
GetSnapPosition = pos => grid.GetSnappedPosition(grid.ToLocalSpace(pos))
}));
}
private class SnappingCursorContainer : CompositeDrawable
{
public Func<Vector2, Vector2> GetSnapPosition;
private readonly Drawable cursor;
public SnappingCursorContainer()
{
RelativeSizeAxes = Axes.Both;
InternalChild = cursor = new Circle
{
Origin = Anchor.Centre,
Size = new Vector2(50),
Colour = Color4.Red
};
}
protected override void LoadComplete()
{
base.LoadComplete();
updatePosition(GetContainingInputManager().CurrentState.Mouse.Position);
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
base.OnMouseMove(e);
updatePosition(e.ScreenSpaceMousePosition);
return true;
}
private void updatePosition(Vector2 screenSpacePosition)
{
cursor.Position = GetSnapPosition.Invoke(screenSpacePosition);
}
}
}
}

View File

@ -0,0 +1,101 @@
// 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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Layout;
using osuTK;
namespace osu.Game.Screens.Edit.Compose.Components
{
public class RectangularPositionSnapGrid : CompositeDrawable
{
/// <summary>
/// The position of the origin of this <see cref="RectangularPositionSnapGrid"/> in local coordinates.
/// </summary>
public Vector2 StartPosition { get; }
/// <summary>
/// The spacing between grid lines of this <see cref="RectangularPositionSnapGrid"/>.
/// </summary>
public Vector2 Spacing { get; }
private readonly LayoutValue gridCache = new LayoutValue(Invalidation.RequiredParentSizeToFit);
public RectangularPositionSnapGrid(Vector2 startPosition, Vector2 spacing)
{
StartPosition = startPosition;
Spacing = spacing;
AddLayout(gridCache);
}
protected override void Update()
{
base.Update();
if (!gridCache.IsValid)
{
ClearInternal();
createContent();
gridCache.Validate();
}
}
private void createContent()
{
var drawSize = DrawSize;
generateGridLines(Direction.Horizontal, StartPosition.Y, 0, -Spacing.Y);
generateGridLines(Direction.Horizontal, StartPosition.Y, drawSize.Y, Spacing.Y);
generateGridLines(Direction.Vertical, StartPosition.X, 0, -Spacing.X);
generateGridLines(Direction.Vertical, StartPosition.X, drawSize.X, Spacing.X);
}
private void generateGridLines(Direction direction, float startPosition, float endPosition, float step)
{
int index = 0;
float currentPosition = startPosition;
while ((endPosition - currentPosition) * Math.Sign(step) > 0)
{
var gridLine = new Box
{
Colour = Colour4.White,
Alpha = index == 0 ? 0.3f : 0.1f,
EdgeSmoothness = new Vector2(0.2f)
};
if (direction == Direction.Horizontal)
{
gridLine.RelativeSizeAxes = Axes.X;
gridLine.Height = 1;
gridLine.Y = currentPosition;
}
else
{
gridLine.RelativeSizeAxes = Axes.Y;
gridLine.Width = 1;
gridLine.X = currentPosition;
}
AddInternal(gridLine);
index += 1;
currentPosition = startPosition + index * step;
}
}
public Vector2 GetSnappedPosition(Vector2 original)
{
Vector2 relativeToStart = original - StartPosition;
Vector2 offset = Vector2.Divide(relativeToStart, Spacing);
Vector2 roundedOffset = new Vector2(MathF.Round(offset.X), MathF.Round(offset.Y));
return StartPosition + Vector2.Multiply(roundedOffset, Spacing);
}
}
}