1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

improve code

This commit is contained in:
OliBomby 2023-12-28 23:10:06 +01:00
parent 0ce1a48e68
commit f223487e1c
9 changed files with 75 additions and 108 deletions

View File

@ -7,6 +7,7 @@ using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Rulesets.Osu.Edit;
using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles;
using osu.Game.Screens.Edit.Compose.Components;
using osu.Game.Tests.Visual;
using osuTK;
using osuTK.Input;
@ -69,7 +70,7 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor
AddStep("choose placement tool", () => InputManager.Key(Key.Number2));
AddStep("move cursor to (1, 1)", () =>
{
var composer = Editor.ChildrenOfType<OsuRectangularPositionSnapGrid>().Single();
var composer = Editor.ChildrenOfType<RectangularPositionSnapGrid>().Single();
InputManager.MoveMouseTo(composer.ToScreenSpace(new Vector2(1, 1)));
});
@ -83,7 +84,7 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor
public void TestGridSizeToggling()
{
AddStep("enable rectangular grid", () => InputManager.Key(Key.Y));
AddUntilStep("rectangular grid visible", () => this.ChildrenOfType<OsuRectangularPositionSnapGrid>().Any());
AddUntilStep("rectangular grid visible", () => this.ChildrenOfType<RectangularPositionSnapGrid>().Any());
gridSizeIs(4);
nextGridSizeIs(8);
@ -99,7 +100,7 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor
}
private void gridSizeIs(int size)
=> AddAssert($"grid size is {size}", () => this.ChildrenOfType<OsuRectangularPositionSnapGrid>().Single().Spacing == new Vector2(size)
=> AddAssert($"grid size is {size}", () => this.ChildrenOfType<RectangularPositionSnapGrid>().Single().Spacing == new Vector2(size)
&& EditorBeatmap.BeatmapInfo.GridSize == size);
}
}

View File

@ -1,35 +1,40 @@
// 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.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Osu.UI;
using osu.Game.Screens.Edit;
namespace osu.Game.Rulesets.Edit
namespace osu.Game.Rulesets.Osu.Edit
{
public partial class GridToolboxGroup : EditorToolboxGroup
public partial class OsuGridToolboxGroup : EditorToolboxGroup, IKeyBindingHandler<GlobalAction>
{
private static readonly int[] grid_sizes = { 4, 8, 16, 32 };
private int currentGridSizeIndex = grid_sizes.Length - 1;
[Resolved]
private EditorBeatmap editorBeatmap { get; set; } = null!;
public GridToolboxGroup()
: base("grid")
{
}
public BindableFloat StartPositionX { get; } = new BindableFloat(256f)
public BindableFloat StartPositionX { get; } = new BindableFloat(OsuPlayfield.BASE_SIZE.X / 2)
{
MinValue = 0f,
MaxValue = 512f,
MaxValue = OsuPlayfield.BASE_SIZE.X,
Precision = 1f
};
public BindableFloat StartPositionY { get; } = new BindableFloat(192)
public BindableFloat StartPositionY { get; } = new BindableFloat(OsuPlayfield.BASE_SIZE.Y / 2)
{
MinValue = 0f,
MaxValue = 384f,
MaxValue = OsuPlayfield.BASE_SIZE.Y,
Precision = 1f
};
@ -42,8 +47,8 @@ namespace osu.Game.Rulesets.Edit
public BindableFloat GridLinesRotation { get; } = new BindableFloat(0f)
{
MinValue = -180f,
MaxValue = 180f,
MinValue = -45f,
MaxValue = 45f,
Precision = 1f
};
@ -52,6 +57,11 @@ namespace osu.Game.Rulesets.Edit
private ExpandableSlider<float> spacingSlider = null!;
private ExpandableSlider<float> gridLinesRotationSlider = null!;
public OsuGridToolboxGroup()
: base("grid")
{
}
[BackgroundDependencyLoader]
private void load()
{
@ -74,6 +84,11 @@ namespace osu.Game.Rulesets.Edit
Current = GridLinesRotation
}
};
int gridSizeIndex = Array.IndexOf(grid_sizes, editorBeatmap.BeatmapInfo.GridSize);
if (gridSizeIndex >= 0)
currentGridSizeIndex = gridSizeIndex;
updateSpacing();
}
protected override void LoadComplete()
@ -104,5 +119,35 @@ namespace osu.Game.Rulesets.Edit
gridLinesRotationSlider.ExpandedLabelText = $"Rotation: {rotation.NewValue:N0}";
}, true);
}
private void nextGridSize()
{
currentGridSizeIndex = (currentGridSizeIndex + 1) % grid_sizes.Length;
updateSpacing();
}
private void updateSpacing()
{
int gridSize = grid_sizes[currentGridSizeIndex];
editorBeatmap.BeatmapInfo.GridSize = gridSize;
Spacing.Value = gridSize;
}
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.EditorCycleGridDisplayMode:
nextGridSize();
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
}
}

View File

@ -66,7 +66,7 @@ namespace osu.Game.Rulesets.Osu.Edit
protected readonly OsuDistanceSnapProvider DistanceSnapProvider = new OsuDistanceSnapProvider();
[Cached]
protected readonly GridToolboxGroup GridToolboxGroup = new GridToolboxGroup();
protected readonly OsuGridToolboxGroup OsuGridToolboxGroup = new OsuGridToolboxGroup();
[Cached]
protected readonly FreehandSliderToolboxGroup FreehandlSliderToolboxGroup = new FreehandSliderToolboxGroup();
@ -86,7 +86,7 @@ namespace osu.Game.Rulesets.Osu.Edit
{
RelativeSizeAxes = Axes.Both
},
rectangularPositionSnapGrid = new OsuRectangularPositionSnapGrid
rectangularPositionSnapGrid = new RectangularPositionSnapGrid
{
RelativeSizeAxes = Axes.Both
}
@ -102,16 +102,16 @@ namespace osu.Game.Rulesets.Osu.Edit
// we may be entering the screen with a selection already active
updateDistanceSnapGrid();
GridToolboxGroup.StartPositionX.ValueChanged += x =>
rectangularPositionSnapGrid.StartPosition = new Vector2(x.NewValue, rectangularPositionSnapGrid.StartPosition.Y);
GridToolboxGroup.StartPositionY.ValueChanged += y =>
rectangularPositionSnapGrid.StartPosition = new Vector2(rectangularPositionSnapGrid.StartPosition.X, y.NewValue);
GridToolboxGroup.Spacing.ValueChanged += s => rectangularPositionSnapGrid.Spacing = new Vector2(s.NewValue);
GridToolboxGroup.GridLinesRotation.ValueChanged += r => rectangularPositionSnapGrid.GridLineRotation = r.NewValue;
OsuGridToolboxGroup.StartPositionX.BindValueChanged(x =>
rectangularPositionSnapGrid.StartPosition = new Vector2(x.NewValue, rectangularPositionSnapGrid.StartPosition.Y), true);
OsuGridToolboxGroup.StartPositionY.BindValueChanged(y =>
rectangularPositionSnapGrid.StartPosition = new Vector2(rectangularPositionSnapGrid.StartPosition.X, y.NewValue), true);
OsuGridToolboxGroup.Spacing.BindValueChanged(s => rectangularPositionSnapGrid.Spacing = new Vector2(s.NewValue), true);
OsuGridToolboxGroup.GridLinesRotation.BindValueChanged(r => rectangularPositionSnapGrid.GridLineRotation = r.NewValue, true);
RightToolbox.AddRange(new EditorToolboxGroup[]
{
GridToolboxGroup,
OsuGridToolboxGroup,
new TransformToolboxGroup { RotationHandler = BlueprintContainer.SelectionHandler.RotationHandler, },
FreehandlSliderToolboxGroup
}

View File

@ -1,69 +0,0 @@
// 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.Allocation;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Osu.UI;
using osu.Game.Screens.Edit;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
namespace osu.Game.Rulesets.Osu.Edit
{
public partial class OsuRectangularPositionSnapGrid : RectangularPositionSnapGrid, IKeyBindingHandler<GlobalAction>
{
private static readonly int[] grid_sizes = { 4, 8, 16, 32 };
private int currentGridSizeIndex = grid_sizes.Length - 1;
[Resolved]
private EditorBeatmap editorBeatmap { get; set; } = null!;
public OsuRectangularPositionSnapGrid()
: base(OsuPlayfield.BASE_SIZE / 2)
{
}
[BackgroundDependencyLoader]
private void load()
{
int gridSizeIndex = Array.IndexOf(grid_sizes, editorBeatmap.BeatmapInfo.GridSize);
if (gridSizeIndex >= 0)
currentGridSizeIndex = gridSizeIndex;
updateSpacing();
}
private void nextGridSize()
{
currentGridSizeIndex = (currentGridSizeIndex + 1) % grid_sizes.Length;
updateSpacing();
}
private void updateSpacing()
{
int gridSize = grid_sizes[currentGridSizeIndex];
editorBeatmap.BeatmapInfo.GridSize = gridSize;
Spacing = new Vector2(gridSize);
}
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.EditorCycleGridDisplayMode:
nextGridSize();
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
}
}

View File

@ -52,9 +52,10 @@ namespace osu.Game.Tests.Visual.Editing
{
RectangularPositionSnapGrid grid = null;
AddStep("create grid", () => Child = grid = new RectangularPositionSnapGrid(position)
AddStep("create grid", () => Child = grid = new RectangularPositionSnapGrid()
{
RelativeSizeAxes = Axes.Both,
StartPosition = position,
Spacing = spacing,
GridLineRotation = rotation
});

View File

@ -51,7 +51,7 @@ namespace osu.Game.Tournament.Screens.Editors
AddInternal(rightClickMessage = new WarningBox("Right click to place and link matches"));
ScrollContent.Add(grid = new RectangularPositionSnapGrid(Vector2.Zero)
ScrollContent.Add(grid = new RectangularPositionSnapGrid
{
Spacing = new Vector2(GRID_SPACING),
Anchor = Anchor.Centre,

View File

@ -32,9 +32,8 @@ namespace osu.Game.Screens.Edit.Compose.Components
protected readonly LayoutValue GridCache = new LayoutValue(Invalidation.RequiredParentSizeToFit);
protected LinedPositionSnapGrid(Vector2 startPosition)
protected LinedPositionSnapGrid()
{
StartPosition = startPosition;
Masking = true;
AddLayout(GridCache);

View File

@ -42,11 +42,6 @@ namespace osu.Game.Screens.Edit.Compose.Components
}
}
public RectangularPositionSnapGrid(Vector2 startPosition)
: base(startPosition)
{
}
protected override void CreateContent()
{
var drawSize = DrawSize;

View File

@ -42,11 +42,6 @@ namespace osu.Game.Screens.Edit.Compose.Components
}
}
public TriangularPositionSnapGrid(Vector2 startPosition)
: base(startPosition)
{
}
private const float sqrt3 = 1.73205080757f;
private const float sqrt3_over2 = 0.86602540378f;
private const float one_over_sqrt3 = 0.57735026919f;