2023-06-23 00:37:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2021-09-19 23:58:32 +08:00
// See the LICENCE file in the repository root for full licence text.
using System.Linq ;
using NUnit.Framework ;
using osu.Framework.Testing ;
2021-09-21 02:32:34 +08:00
using osu.Framework.Utils ;
2024-05-02 19:59:40 +08:00
using osu.Game.Rulesets.Edit ;
2021-09-19 23:58:32 +08:00
using osu.Game.Rulesets.Osu.Edit ;
2021-09-21 02:32:34 +08:00
using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles ;
2021-09-19 23:58:32 +08:00
using osu.Game.Tests.Visual ;
2021-09-20 00:45:22 +08:00
using osuTK ;
2021-09-19 23:58:32 +08:00
using osuTK.Input ;
namespace osu.Game.Rulesets.Osu.Tests.Editor
{
2022-11-24 13:32:20 +08:00
public partial class TestSceneOsuEditorGrids : EditorTestScene
2021-09-19 23:58:32 +08:00
{
protected override Ruleset CreateEditorRuleset ( ) = > new OsuRuleset ( ) ;
[Test]
2022-10-25 13:32:08 +08:00
public void TestGridToggles ( )
2021-09-19 23:58:32 +08:00
{
AddStep ( "enable distance snap grid" , ( ) = > InputManager . Key ( Key . T ) ) ;
AddStep ( "select second object" , ( ) = > EditorBeatmap . SelectedHitObjects . Add ( EditorBeatmap . HitObjects . ElementAt ( 1 ) ) ) ;
2022-10-25 13:32:08 +08:00
2021-09-19 23:58:32 +08:00
AddUntilStep ( "distance snap grid visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
2021-09-21 02:32:34 +08:00
rectangularGridActive ( false ) ;
2021-09-19 23:58:32 +08:00
AddStep ( "enable rectangular grid" , ( ) = > InputManager . Key ( Key . Y ) ) ;
2022-10-25 13:32:08 +08:00
AddStep ( "select second object" , ( ) = > EditorBeatmap . SelectedHitObjects . Add ( EditorBeatmap . HitObjects . ElementAt ( 1 ) ) ) ;
AddUntilStep ( "distance snap grid still visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
2021-09-21 02:32:34 +08:00
rectangularGridActive ( true ) ;
2021-09-19 23:58:32 +08:00
2022-10-25 13:32:08 +08:00
AddStep ( "disable distance snap grid" , ( ) = > InputManager . Key ( Key . T ) ) ;
AddUntilStep ( "distance snap grid hidden" , ( ) = > ! this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
2021-09-21 02:32:34 +08:00
AddStep ( "select second object" , ( ) = > EditorBeatmap . SelectedHitObjects . Add ( EditorBeatmap . HitObjects . ElementAt ( 1 ) ) ) ;
2022-10-25 13:32:08 +08:00
rectangularGridActive ( true ) ;
AddStep ( "disable rectangular grid" , ( ) = > InputManager . Key ( Key . Y ) ) ;
AddUntilStep ( "distance snap grid still hidden" , ( ) = > ! this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
2021-09-21 02:32:34 +08:00
rectangularGridActive ( false ) ;
}
2022-10-25 13:36:35 +08:00
[Test]
public void TestDistanceSnapMomentaryToggle ( )
{
AddStep ( "select second object" , ( ) = > EditorBeatmap . SelectedHitObjects . Add ( EditorBeatmap . HitObjects . ElementAt ( 1 ) ) ) ;
AddUntilStep ( "distance snap grid hidden" , ( ) = > ! this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
AddStep ( "hold alt" , ( ) = > InputManager . PressKey ( Key . AltLeft ) ) ;
AddUntilStep ( "distance snap grid visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
AddStep ( "release alt" , ( ) = > InputManager . ReleaseKey ( Key . AltLeft ) ) ;
AddUntilStep ( "distance snap grid hidden" , ( ) = > ! this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
2024-05-02 20:15:46 +08:00
AddStep ( "enable distance snap grid" , ( ) = > InputManager . Key ( Key . T ) ) ;
AddUntilStep ( "distance snap grid visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
AddStep ( "hold alt" , ( ) = > InputManager . PressKey ( Key . AltLeft ) ) ;
AddUntilStep ( "distance snap grid hidden" , ( ) = > ! this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
AddStep ( "release alt" , ( ) = > InputManager . ReleaseKey ( Key . AltLeft ) ) ;
AddUntilStep ( "distance snap grid visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
2022-10-25 13:36:35 +08:00
}
2024-05-02 19:59:40 +08:00
[Test]
2024-05-02 20:15:46 +08:00
public void TestDistanceSnapAdjustDoesNotHideTheGridIfStartingEnabled ( )
2024-05-02 19:59:40 +08:00
{
double distanceSnap = double . PositiveInfinity ;
AddStep ( "enable distance snap grid" , ( ) = > InputManager . Key ( Key . T ) ) ;
AddStep ( "select second object" , ( ) = > EditorBeatmap . SelectedHitObjects . Add ( EditorBeatmap . HitObjects . ElementAt ( 1 ) ) ) ;
AddUntilStep ( "distance snap grid visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
AddStep ( "store distance snap" , ( ) = > distanceSnap = this . ChildrenOfType < IDistanceSnapProvider > ( ) . First ( ) . DistanceSpacingMultiplier . Value ) ;
AddStep ( "increase distance" , ( ) = >
{
InputManager . PressKey ( Key . AltLeft ) ;
InputManager . PressKey ( Key . ControlLeft ) ;
InputManager . ScrollVerticalBy ( 1 ) ;
InputManager . ReleaseKey ( Key . ControlLeft ) ;
InputManager . ReleaseKey ( Key . AltLeft ) ;
} ) ;
AddUntilStep ( "distance snap increased" , ( ) = > this . ChildrenOfType < IDistanceSnapProvider > ( ) . First ( ) . DistanceSpacingMultiplier . Value , ( ) = > Is . GreaterThan ( distanceSnap ) ) ;
AddUntilStep ( "distance snap grid still visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
}
2024-05-02 20:15:46 +08:00
[Test]
public void TestDistanceSnapAdjustShowsGridMomentarilyIfStartingDisabled ( )
{
double distanceSnap = double . PositiveInfinity ;
AddStep ( "select second object" , ( ) = > EditorBeatmap . SelectedHitObjects . Add ( EditorBeatmap . HitObjects . ElementAt ( 1 ) ) ) ;
AddUntilStep ( "distance snap grid hidden" , ( ) = > ! this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
AddStep ( "store distance snap" , ( ) = > distanceSnap = this . ChildrenOfType < IDistanceSnapProvider > ( ) . First ( ) . DistanceSpacingMultiplier . Value ) ;
AddStep ( "start increasing distance" , ( ) = >
{
InputManager . PressKey ( Key . AltLeft ) ;
InputManager . PressKey ( Key . ControlLeft ) ;
} ) ;
AddUntilStep ( "distance snap grid visible" , ( ) = > this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
AddStep ( "finish increasing distance" , ( ) = >
{
InputManager . ScrollVerticalBy ( 1 ) ;
InputManager . ReleaseKey ( Key . ControlLeft ) ;
InputManager . ReleaseKey ( Key . AltLeft ) ;
} ) ;
AddUntilStep ( "distance snap increased" , ( ) = > this . ChildrenOfType < IDistanceSnapProvider > ( ) . First ( ) . DistanceSpacingMultiplier . Value , ( ) = > Is . GreaterThan ( distanceSnap ) ) ;
AddUntilStep ( "distance snap hidden in the end" , ( ) = > ! this . ChildrenOfType < OsuDistanceSnapGrid > ( ) . Any ( ) ) ;
}
2022-10-25 13:36:35 +08:00
[Test]
public void TestGridSnapMomentaryToggle ( )
{
rectangularGridActive ( false ) ;
AddStep ( "hold shift" , ( ) = > InputManager . PressKey ( Key . ShiftLeft ) ) ;
rectangularGridActive ( true ) ;
AddStep ( "release shift" , ( ) = > InputManager . ReleaseKey ( Key . ShiftLeft ) ) ;
rectangularGridActive ( false ) ;
}
2021-09-21 02:32:34 +08:00
private void rectangularGridActive ( bool active )
{
AddStep ( "choose placement tool" , ( ) = > InputManager . Key ( Key . Number2 ) ) ;
AddStep ( "move cursor to (1, 1)" , ( ) = >
{
var composer = Editor . ChildrenOfType < OsuRectangularPositionSnapGrid > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( composer . ToScreenSpace ( new Vector2 ( 1 , 1 ) ) ) ;
} ) ;
if ( active )
AddAssert ( "placement blueprint at (0, 0)" , ( ) = > Precision . AlmostEquals ( Editor . ChildrenOfType < HitCirclePlacementBlueprint > ( ) . Single ( ) . HitObject . Position , new Vector2 ( 0 , 0 ) ) ) ;
else
AddAssert ( "placement blueprint at (1, 1)" , ( ) = > Precision . AlmostEquals ( Editor . ChildrenOfType < HitCirclePlacementBlueprint > ( ) . Single ( ) . HitObject . Position , new Vector2 ( 1 , 1 ) ) ) ;
2021-09-19 23:58:32 +08:00
}
2021-09-20 00:45:22 +08:00
[Test]
public void TestGridSizeToggling ( )
{
AddStep ( "enable rectangular grid" , ( ) = > InputManager . Key ( Key . Y ) ) ;
AddUntilStep ( "rectangular grid visible" , ( ) = > this . ChildrenOfType < OsuRectangularPositionSnapGrid > ( ) . Any ( ) ) ;
2021-09-21 22:45:03 +08:00
gridSizeIs ( 4 ) ;
2021-09-20 00:45:22 +08:00
nextGridSizeIs ( 8 ) ;
nextGridSizeIs ( 16 ) ;
nextGridSizeIs ( 32 ) ;
2021-09-21 22:45:03 +08:00
nextGridSizeIs ( 4 ) ;
2021-09-20 00:45:22 +08:00
}
private void nextGridSizeIs ( int size )
{
AddStep ( "toggle to next grid size" , ( ) = > InputManager . Key ( Key . G ) ) ;
gridSizeIs ( size ) ;
}
private void gridSizeIs ( int size )
2021-09-21 02:39:39 +08:00
= > AddAssert ( $"grid size is {size}" , ( ) = > this . ChildrenOfType < OsuRectangularPositionSnapGrid > ( ) . Single ( ) . Spacing = = new Vector2 ( size )
& & EditorBeatmap . BeatmapInfo . GridSize = = size ) ;
2021-09-19 23:58:32 +08:00
}
}