mirror of
https://github.com/ppy/osu.git
synced 2025-03-14 13:27:19 +08:00
Add rotation to snap grid visual
This commit is contained in:
parent
e10733834b
commit
f3b88c318b
@ -33,28 +33,30 @@ namespace osu.Game.Tests.Visual.Editing
|
||||
},
|
||||
content = new Container
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Padding = new MarginPadding(10),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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) },
|
||||
new object[] { new Vector2(0, 0), new Vector2(10, 10), 0f },
|
||||
new object[] { new Vector2(240, 180), new Vector2(10, 15), 30f },
|
||||
new object[] { new Vector2(160, 120), new Vector2(30, 20), -30f },
|
||||
new object[] { new Vector2(480, 360), new Vector2(100, 100), 0f },
|
||||
};
|
||||
|
||||
[TestCaseSource(nameof(test_cases))]
|
||||
public void TestRectangularGrid(Vector2 position, Vector2 spacing)
|
||||
public void TestRectangularGrid(Vector2 position, Vector2 spacing, float rotation)
|
||||
{
|
||||
RectangularPositionSnapGrid grid = null;
|
||||
|
||||
AddStep("create grid", () => Child = grid = new RectangularPositionSnapGrid(position)
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Spacing = spacing
|
||||
Spacing = spacing,
|
||||
GridLineRotation = rotation
|
||||
});
|
||||
|
||||
AddStep("add snapping cursor", () => Add(new SnappingCursorContainer
|
||||
|
@ -15,10 +15,20 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
||||
{
|
||||
public partial class RectangularPositionSnapGrid : CompositeDrawable
|
||||
{
|
||||
private Vector2 startPosition;
|
||||
|
||||
/// <summary>
|
||||
/// The position of the origin of this <see cref="RectangularPositionSnapGrid"/> in local coordinates.
|
||||
/// </summary>
|
||||
public Vector2 StartPosition { get; }
|
||||
public Vector2 StartPosition
|
||||
{
|
||||
get => startPosition;
|
||||
set
|
||||
{
|
||||
startPosition = value;
|
||||
gridCache.Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 spacing = Vector2.One;
|
||||
|
||||
@ -38,11 +48,27 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
||||
}
|
||||
}
|
||||
|
||||
private float gridLineRotation;
|
||||
|
||||
/// <summary>
|
||||
/// The rotation in degrees of the grid lines of this <see cref="RectangularPositionSnapGrid"/>.
|
||||
/// </summary>
|
||||
public float GridLineRotation
|
||||
{
|
||||
get => gridLineRotation;
|
||||
set
|
||||
{
|
||||
gridLineRotation = value;
|
||||
gridCache.Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly LayoutValue gridCache = new LayoutValue(Invalidation.RequiredParentSizeToFit);
|
||||
|
||||
public RectangularPositionSnapGrid(Vector2 startPosition)
|
||||
{
|
||||
StartPosition = startPosition;
|
||||
Masking = true;
|
||||
|
||||
AddLayout(gridCache);
|
||||
}
|
||||
@ -65,47 +91,43 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
||||
private void createContent()
|
||||
{
|
||||
var drawSize = DrawSize;
|
||||
var rot = Quaternion.FromAxisAngle(Vector3.UnitZ, MathHelper.DegreesToRadians(GridLineRotation));
|
||||
|
||||
generateGridLines(Direction.Horizontal, StartPosition.Y, 0, -Spacing.Y);
|
||||
generateGridLines(Direction.Horizontal, StartPosition.Y, drawSize.Y, Spacing.Y);
|
||||
generateGridLines(Vector2.Transform(new Vector2(0, -Spacing.Y), rot), GridLineRotation + 90, drawSize);
|
||||
generateGridLines(Vector2.Transform(new Vector2(0, Spacing.Y), rot), GridLineRotation + 90, drawSize);
|
||||
|
||||
generateGridLines(Direction.Vertical, StartPosition.X, 0, -Spacing.X);
|
||||
generateGridLines(Direction.Vertical, StartPosition.X, drawSize.X, Spacing.X);
|
||||
generateGridLines(Vector2.Transform(new Vector2(-Spacing.X, 0), rot), GridLineRotation, drawSize);
|
||||
generateGridLines(Vector2.Transform(new Vector2(Spacing.X, 0), rot), GridLineRotation, drawSize);
|
||||
|
||||
generateOutline(drawSize);
|
||||
}
|
||||
|
||||
private void generateGridLines(Direction direction, float startPosition, float endPosition, float step)
|
||||
private void generateGridLines(Vector2 step, float rotation, Vector2 drawSize)
|
||||
{
|
||||
int index = 0;
|
||||
float currentPosition = startPosition;
|
||||
var currentPosition = startPosition;
|
||||
|
||||
// Make lines the same width independent of display resolution.
|
||||
float lineWidth = DrawWidth / ScreenSpaceDrawQuad.Width;
|
||||
float lineLength = drawSize.Length * 2;
|
||||
|
||||
List<Box> generatedLines = new List<Box>();
|
||||
|
||||
while (Precision.AlmostBigger((endPosition - currentPosition) * Math.Sign(step), 0))
|
||||
while (lineDefinitelyIntersectsBox(currentPosition, step.PerpendicularLeft, drawSize) ||
|
||||
isMovingTowardsBox(currentPosition, step, drawSize))
|
||||
{
|
||||
var gridLine = new Box
|
||||
{
|
||||
Colour = Colour4.White,
|
||||
Alpha = 0.1f,
|
||||
Origin = Anchor.Centre,
|
||||
RelativeSizeAxes = Axes.None,
|
||||
Width = lineWidth,
|
||||
Height = lineLength,
|
||||
Position = currentPosition,
|
||||
Rotation = rotation,
|
||||
};
|
||||
|
||||
if (direction == Direction.Horizontal)
|
||||
{
|
||||
gridLine.Origin = Anchor.CentreLeft;
|
||||
gridLine.RelativeSizeAxes = Axes.X;
|
||||
gridLine.Height = lineWidth;
|
||||
gridLine.Y = currentPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
gridLine.Origin = Anchor.TopCentre;
|
||||
gridLine.RelativeSizeAxes = Axes.Y;
|
||||
gridLine.Width = lineWidth;
|
||||
gridLine.X = currentPosition;
|
||||
}
|
||||
|
||||
generatedLines.Add(gridLine);
|
||||
|
||||
index += 1;
|
||||
@ -116,11 +138,81 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
||||
return;
|
||||
|
||||
generatedLines.First().Alpha = 0.3f;
|
||||
generatedLines.Last().Alpha = 0.3f;
|
||||
|
||||
AddRangeInternal(generatedLines);
|
||||
}
|
||||
|
||||
private bool isMovingTowardsBox(Vector2 currentPosition, Vector2 step, Vector2 box)
|
||||
{
|
||||
return (currentPosition + step).LengthSquared < currentPosition.LengthSquared ||
|
||||
(currentPosition + step - box).LengthSquared < (currentPosition - box).LengthSquared;
|
||||
}
|
||||
|
||||
private bool lineDefinitelyIntersectsBox(Vector2 lineStart, Vector2 lineDir, Vector2 box)
|
||||
{
|
||||
var p2 = lineStart + lineDir;
|
||||
|
||||
double d1 = det(Vector2.Zero);
|
||||
double d2 = det(new Vector2(box.X, 0));
|
||||
double d3 = det(new Vector2(0, box.Y));
|
||||
double d4 = det(box);
|
||||
|
||||
return definitelyDifferentSign(d1, d2) || definitelyDifferentSign(d3, d4) ||
|
||||
definitelyDifferentSign(d1, d3) || definitelyDifferentSign(d2, d4);
|
||||
|
||||
double det(Vector2 p) => (p.X - lineStart.X) * (p2.Y - lineStart.Y) - (p.Y - lineStart.Y) * (p2.X - lineStart.X);
|
||||
|
||||
bool definitelyDifferentSign(double a, double b) => !Precision.AlmostEquals(a, 0) &&
|
||||
!Precision.AlmostEquals(b, 0) &&
|
||||
Math.Sign(a) != Math.Sign(b);
|
||||
}
|
||||
|
||||
private void generateOutline(Vector2 drawSize)
|
||||
{
|
||||
// Make lines the same width independent of display resolution.
|
||||
float lineWidth = DrawWidth / ScreenSpaceDrawQuad.Width;
|
||||
|
||||
AddRangeInternal(new[]
|
||||
{
|
||||
new Box
|
||||
{
|
||||
Colour = Colour4.White,
|
||||
Alpha = 0.3f,
|
||||
Origin = Anchor.CentreLeft,
|
||||
RelativeSizeAxes = Axes.X,
|
||||
Height = lineWidth,
|
||||
Y = 0,
|
||||
},
|
||||
new Box
|
||||
{
|
||||
Colour = Colour4.White,
|
||||
Alpha = 0.3f,
|
||||
Origin = Anchor.CentreLeft,
|
||||
RelativeSizeAxes = Axes.X,
|
||||
Height = lineWidth,
|
||||
Y = drawSize.Y,
|
||||
},
|
||||
new Box
|
||||
{
|
||||
Colour = Colour4.White,
|
||||
Alpha = 0.3f,
|
||||
Origin = Anchor.TopCentre,
|
||||
RelativeSizeAxes = Axes.Y,
|
||||
Width = lineWidth,
|
||||
X = 0,
|
||||
},
|
||||
new Box
|
||||
{
|
||||
Colour = Colour4.White,
|
||||
Alpha = 0.3f,
|
||||
Origin = Anchor.TopCentre,
|
||||
RelativeSizeAxes = Axes.Y,
|
||||
Width = lineWidth,
|
||||
X = drawSize.X,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public Vector2 GetSnappedPosition(Vector2 original)
|
||||
{
|
||||
Vector2 relativeToStart = original - StartPosition;
|
||||
|
Loading…
x
Reference in New Issue
Block a user