From 4e094b2127d614fc3d1943351bad9de1a239b83c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sun, 19 Sep 2021 18:45:22 +0200 Subject: [PATCH] Implement grid size toggling matching stable --- .../Editor/TestSceneOsuEditorGrids.cs | 28 +++++++++-- .../Edit/OsuHitObjectComposer.cs | 3 +- .../Edit/OsuRectangularPositionSnapGrid.cs | 50 +++++++++++++++++++ .../TestSceneRectangularPositionSnapGrid.cs | 5 +- .../Components/RectangularPositionSnapGrid.cs | 18 +++++-- 5 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 osu.Game.Rulesets.Osu/Edit/OsuRectangularPositionSnapGrid.cs diff --git a/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneOsuEditorGrids.cs b/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneOsuEditorGrids.cs index 007b27b2e7..81e44a57db 100644 --- a/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneOsuEditorGrids.cs +++ b/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneOsuEditorGrids.cs @@ -5,8 +5,8 @@ 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 @@ public void TestGridExclusivity() AddStep("enable rectangular grid", () => InputManager.Key(Key.Y)); AddUntilStep("distance snap grid hidden", () => !this.ChildrenOfType().Any()); - AddUntilStep("rectangular grid visible", () => this.ChildrenOfType().Any()); + AddUntilStep("rectangular grid visible", () => this.ChildrenOfType().Any()); AddStep("enable distance snap grid", () => InputManager.Key(Key.T)); AddUntilStep("distance snap grid visible", () => this.ChildrenOfType().Any()); - AddUntilStep("rectangular grid hidden", () => !this.ChildrenOfType().Any()); + AddUntilStep("rectangular grid hidden", () => !this.ChildrenOfType().Any()); } + + [Test] + public void TestGridSizeToggling() + { + AddStep("enable rectangular grid", () => InputManager.Key(Key.Y)); + AddUntilStep("rectangular grid visible", () => this.ChildrenOfType().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().Single().Spacing == new Vector2(size)); } } diff --git a/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs b/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs index 491ad655fa..a2ee646341 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs @@ -17,7 +17,6 @@ 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 @@ private void updateRectangularPositionSnapGrid() 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 }); diff --git a/osu.Game.Rulesets.Osu/Edit/OsuRectangularPositionSnapGrid.cs b/osu.Game.Rulesets.Osu/Edit/OsuRectangularPositionSnapGrid.cs new file mode 100644 index 0000000000..2faaab75e4 --- /dev/null +++ b/osu.Game.Rulesets.Osu/Edit/OsuRectangularPositionSnapGrid.cs @@ -0,0 +1,50 @@ +// Copyright (c) ppy Pty Ltd . 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]); + } + } +} diff --git a/osu.Game.Tests/Visual/Editing/TestSceneRectangularPositionSnapGrid.cs b/osu.Game.Tests/Visual/Editing/TestSceneRectangularPositionSnapGrid.cs index ec267bf752..85a98eca47 100644 --- a/osu.Game.Tests/Visual/Editing/TestSceneRectangularPositionSnapGrid.cs +++ b/osu.Game.Tests/Visual/Editing/TestSceneRectangularPositionSnapGrid.cs @@ -49,9 +49,10 @@ public void TestRectangularGrid(Vector2 position, Vector2 spacing) { 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 diff --git a/osu.Game/Screens/Edit/Compose/Components/RectangularPositionSnapGrid.cs b/osu.Game/Screens/Edit/Compose/Components/RectangularPositionSnapGrid.cs index f243c027e3..95b4b2fe53 100644 --- a/osu.Game/Screens/Edit/Compose/Components/RectangularPositionSnapGrid.cs +++ b/osu.Game/Screens/Edit/Compose/Components/RectangularPositionSnapGrid.cs @@ -17,17 +17,29 @@ public class RectangularPositionSnapGrid : CompositeDrawable /// public Vector2 StartPosition { get; } + private Vector2 spacing = Vector2.One; + /// /// The spacing between grid lines of this . /// - 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); }