Merge pull request #23581 from peppy/beat-divisor-visual-improvements

Update beat divisor control to show ticks in more visually correct locations
This commit is contained in:
Bartłomiej Dach 2023-05-24 23:18:25 +02:00 committed by GitHub
commit 59768c5ffd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 20 deletions

View File

@ -42,7 +42,8 @@ namespace osu.Game.Tests.Visual.Editing
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(90, 90) Size = new Vector2(90, 90),
Scale = new Vector2(3),
} }
}; };
}); });
@ -64,17 +65,24 @@ namespace osu.Game.Tests.Visual.Editing
InputManager.MoveMouseTo(tickMarkerHead.ScreenSpaceDrawQuad.Centre); InputManager.MoveMouseTo(tickMarkerHead.ScreenSpaceDrawQuad.Centre);
InputManager.PressButton(MouseButton.Left); InputManager.PressButton(MouseButton.Left);
}); });
AddStep("move to 8 and release", () => AddStep("move to 1", () => InputManager.MoveMouseTo(getPositionForDivisor(1)));
AddStep("move to 16 and release", () =>
{ {
InputManager.MoveMouseTo(tickSliderBar.ScreenSpaceDrawQuad.Centre); InputManager.MoveMouseTo(getPositionForDivisor(16));
InputManager.ReleaseButton(MouseButton.Left); InputManager.ReleaseButton(MouseButton.Left);
}); });
AddAssert("divisor is 8", () => bindableBeatDivisor.Value == 8); AddAssert("divisor is 16", () => bindableBeatDivisor.Value == 16);
AddStep("hold marker", () => InputManager.PressButton(MouseButton.Left)); AddStep("hold marker", () => InputManager.PressButton(MouseButton.Left));
AddStep("move to 16", () => InputManager.MoveMouseTo(getPositionForDivisor(16))); AddStep("move to ~6 and release", () =>
AddStep("move to ~10 and release", () => {
InputManager.MoveMouseTo(getPositionForDivisor(6));
InputManager.ReleaseButton(MouseButton.Left);
});
AddAssert("divisor clamped to 8", () => bindableBeatDivisor.Value == 8);
AddStep("move to ~10 and click", () =>
{ {
InputManager.MoveMouseTo(getPositionForDivisor(10)); InputManager.MoveMouseTo(getPositionForDivisor(10));
InputManager.PressButton(MouseButton.Left);
InputManager.ReleaseButton(MouseButton.Left); InputManager.ReleaseButton(MouseButton.Left);
}); });
AddAssert("divisor clamped to 8", () => bindableBeatDivisor.Value == 8); AddAssert("divisor clamped to 8", () => bindableBeatDivisor.Value == 8);
@ -82,12 +90,11 @@ namespace osu.Game.Tests.Visual.Editing
private Vector2 getPositionForDivisor(int divisor) private Vector2 getPositionForDivisor(int divisor)
{ {
float relativePosition = (float)Math.Clamp(divisor, 0, 16) / 16; float localX = (1 - 1 / (float)divisor) * tickSliderBar.UsableWidth + tickSliderBar.RangePadding;
var sliderDrawQuad = tickSliderBar.ScreenSpaceDrawQuad; return tickSliderBar.ToScreenSpace(new Vector2(
return new Vector2( localX,
sliderDrawQuad.TopLeft.X + sliderDrawQuad.Width * relativePosition, tickSliderBar.DrawHeight / 2
sliderDrawQuad.Centre.Y ));
);
} }
[Test] [Test]

View File

@ -154,12 +154,15 @@ namespace osu.Game.Screens.Edit
/// </summary> /// </summary>
/// <param name="index">The 0-based beat index.</param> /// <param name="index">The 0-based beat index.</param>
/// <param name="beatDivisor">The beat divisor.</param> /// <param name="beatDivisor">The beat divisor.</param>
/// <param name="validDivisors">The list of valid divisors which can be chosen from. Assumes ordered from low to high. Defaults to <see cref="PREDEFINED_DIVISORS"/> if omitted.</param>
/// <returns>The applicable divisor.</returns> /// <returns>The applicable divisor.</returns>
public static int GetDivisorForBeatIndex(int index, int beatDivisor) public static int GetDivisorForBeatIndex(int index, int beatDivisor, int[] validDivisors = null)
{ {
validDivisors ??= PREDEFINED_DIVISORS;
int beat = index % beatDivisor; int beat = index % beatDivisor;
foreach (int divisor in PREDEFINED_DIVISORS) foreach (int divisor in validDivisors)
{ {
if ((beat * divisor) % beatDivisor == 0) if ((beat * divisor) % beatDivisor == 0)
return divisor; return divisor;

View File

@ -383,7 +383,8 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
CurrentNumber.BindTo(this.beatDivisor = beatDivisor); CurrentNumber.BindTo(this.beatDivisor = beatDivisor);
Padding = new MarginPadding { Horizontal = 5 }; RangePadding = 5;
Padding = new MarginPadding { Horizontal = RangePadding };
} }
protected override void LoadComplete() protected override void LoadComplete()
@ -398,15 +399,20 @@ namespace osu.Game.Screens.Edit.Compose.Components
ClearInternal(); ClearInternal();
CurrentNumber.ValueChanged -= moveMarker; CurrentNumber.ValueChanged -= moveMarker;
foreach (int divisor in beatDivisor.ValidDivisors.Value.Presets) int largestDivisor = beatDivisor.ValidDivisors.Value.Presets.Last();
for (int tickIndex = 0; tickIndex <= largestDivisor; tickIndex++)
{ {
AddInternal(new Tick(divisor) int divisor = BindableBeatDivisor.GetDivisorForBeatIndex(tickIndex, largestDivisor, (int[])beatDivisor.ValidDivisors.Value.Presets);
bool isSolidTick = divisor * (largestDivisor - tickIndex) == largestDivisor;
AddInternal(new Tick(divisor, isSolidTick)
{ {
Anchor = Anchor.CentreLeft, Anchor = Anchor.CentreLeft,
Origin = Anchor.Centre, Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both, RelativePositionAxes = Axes.Both,
Colour = BindableBeatDivisor.GetColourFor(divisor, colours), Colour = BindableBeatDivisor.GetColourFor(divisor, colours),
X = getMappedPosition(divisor), X = tickIndex / (float)largestDivisor,
}); });
} }
@ -418,6 +424,11 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void moveMarker(ValueChangedEvent<int> divisor) private void moveMarker(ValueChangedEvent<int> divisor)
{ {
marker.MoveToX(getMappedPosition(divisor.NewValue), 100, Easing.OutQuint); marker.MoveToX(getMappedPosition(divisor.NewValue), 100, Easing.OutQuint);
foreach (Tick tick in InternalChildren.OfType<Tick>().Where(t => !t.AlwaysDisplayed))
{
tick.FadeTo(divisor.NewValue % tick.Divisor == 0 ? 0.2f : 0f, 100, Easing.OutQuint);
}
} }
protected override void UpdateValue(float value) protected override void UpdateValue(float value)
@ -483,13 +494,22 @@ namespace osu.Game.Screens.Edit.Compose.Components
OnUserChange(Current.Value); OnUserChange(Current.Value);
} }
private float getMappedPosition(float divisor) => MathF.Pow((divisor - 1) / (beatDivisor.ValidDivisors.Value.Presets.Last() - 1), 0.90f); private float getMappedPosition(float divisor) => 1 - 1 / divisor;
private partial class Tick : Circle private partial class Tick : Circle
{ {
public Tick(int divisor) public readonly bool AlwaysDisplayed;
public readonly int Divisor;
public Tick(int divisor, bool alwaysDisplayed)
{ {
AlwaysDisplayed = alwaysDisplayed;
Divisor = divisor;
Size = new Vector2(6f, 12) * BindableBeatDivisor.GetSize(divisor); Size = new Vector2(6f, 12) * BindableBeatDivisor.GetSize(divisor);
Alpha = alwaysDisplayed ? 1 : 0;
InternalChild = new Box { RelativeSizeAxes = Axes.Both }; InternalChild = new Box { RelativeSizeAxes = Axes.Both };
} }
} }