1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 10:12:54 +08:00

Implement grid size toggling matching stable

This commit is contained in:
Bartłomiej Dach 2021-09-19 18:45:22 +02:00
parent c403e628dd
commit 4e094b2127
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
5 changed files with 94 additions and 10 deletions

View File

@ -5,8 +5,8 @@ using System.Linq;
using NUnit.Framework;
using osu.Framework.Testing;
using osu.Game.Rulesets.Osu.Edit;
using osu.Game.Screens.Edit.Compose.Components;
using osu.Game.Tests.Visual;
using osuTK;
using osuTK.Input;
namespace osu.Game.Rulesets.Osu.Tests.Editor
@ -24,11 +24,33 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor
AddStep("enable rectangular grid", () => InputManager.Key(Key.Y));
AddUntilStep("distance snap grid hidden", () => !this.ChildrenOfType<OsuDistanceSnapGrid>().Any());
AddUntilStep("rectangular grid visible", () => this.ChildrenOfType<RectangularPositionSnapGrid>().Any());
AddUntilStep("rectangular grid visible", () => this.ChildrenOfType<OsuRectangularPositionSnapGrid>().Any());
AddStep("enable distance snap grid", () => InputManager.Key(Key.T));
AddUntilStep("distance snap grid visible", () => this.ChildrenOfType<OsuDistanceSnapGrid>().Any());
AddUntilStep("rectangular grid hidden", () => !this.ChildrenOfType<RectangularPositionSnapGrid>().Any());
AddUntilStep("rectangular grid hidden", () => !this.ChildrenOfType<OsuRectangularPositionSnapGrid>().Any());
}
[Test]
public void TestGridSizeToggling()
{
AddStep("enable rectangular grid", () => InputManager.Key(Key.Y));
AddUntilStep("rectangular grid visible", () => this.ChildrenOfType<OsuRectangularPositionSnapGrid>().Any());
gridSizeIs(4);
nextGridSizeIs(8);
nextGridSizeIs(16);
nextGridSizeIs(32);
nextGridSizeIs(4);
}
private void nextGridSizeIs(int size)
{
AddStep("toggle to next grid size", () => InputManager.Key(Key.G));
gridSizeIs(size);
}
private void gridSizeIs(int size)
=> AddAssert($"grid size is {size}", () => this.ChildrenOfType<OsuRectangularPositionSnapGrid>().Single().Spacing == new Vector2(size));
}
}

View File

@ -17,7 +17,6 @@ using osu.Game.Rulesets.Edit.Tools;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.UI;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Edit.Components.TernaryButtons;
using osu.Game.Screens.Edit.Compose.Components;
@ -309,7 +308,7 @@ namespace osu.Game.Rulesets.Osu.Edit
if (gridSnapToggle.Value == TernaryState.True)
{
rectangularPositionSnapGridContainer.Add(rectangularPositionSnapGrid = new RectangularPositionSnapGrid(OsuPlayfield.BASE_SIZE / 2, new Vector2(16))
rectangularPositionSnapGridContainer.Add(rectangularPositionSnapGrid = new OsuRectangularPositionSnapGrid(EditorBeatmap.BeatmapInfo.GridSize)
{
RelativeSizeAxes = Axes.Both
});

View File

@ -0,0 +1,50 @@
// 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.Input.Events;
using osu.Game.Rulesets.Osu.UI;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
using osuTK.Input;
namespace osu.Game.Rulesets.Osu.Edit
{
public class OsuRectangularPositionSnapGrid : RectangularPositionSnapGrid
{
private static readonly int[] grid_sizes = { 4, 8, 16, 32 };
private int currentGridSizeIndex;
public OsuRectangularPositionSnapGrid(int gridSize)
: base(OsuPlayfield.BASE_SIZE / 2)
{
var gridSizeIndex = Array.IndexOf(grid_sizes, gridSize);
if (gridSizeIndex > 0)
currentGridSizeIndex = gridSizeIndex;
updateSpacing();
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Key == Key.G)
{
nextGridSize();
return true;
}
return false;
}
private void nextGridSize()
{
currentGridSizeIndex = (currentGridSizeIndex + 1) % grid_sizes.Length;
updateSpacing();
}
private void updateSpacing()
{
Spacing = new Vector2(grid_sizes[currentGridSizeIndex]);
}
}
}

View File

@ -49,9 +49,10 @@ namespace osu.Game.Tests.Visual.Editing
{
RectangularPositionSnapGrid grid = null;
AddStep("create grid", () => Child = grid = new RectangularPositionSnapGrid(position, spacing)
AddStep("create grid", () => Child = grid = new RectangularPositionSnapGrid(position)
{
RelativeSizeAxes = Axes.Both
RelativeSizeAxes = Axes.Both,
Spacing = spacing
});
AddStep("add snapping cursor", () => Add(new SnappingCursorContainer

View File

@ -17,17 +17,29 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// </summary>
public Vector2 StartPosition { get; }
private Vector2 spacing = Vector2.One;
/// <summary>
/// The spacing between grid lines of this <see cref="RectangularPositionSnapGrid"/>.
/// </summary>
public Vector2 Spacing { get; }
public Vector2 Spacing
{
get => spacing;
set
{
if (spacing.X <= 0 || spacing.Y <= 0)
throw new ArgumentException("Grid spacing must be positive.");
spacing = value;
gridCache.Invalidate();
}
}
private readonly LayoutValue gridCache = new LayoutValue(Invalidation.RequiredParentSizeToFit);
public RectangularPositionSnapGrid(Vector2 startPosition, Vector2 spacing)
public RectangularPositionSnapGrid(Vector2 startPosition)
{
StartPosition = startPosition;
Spacing = spacing;
AddLayout(gridCache);
}