1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 01:02:55 +08:00

Add flow for changing set of valid divisors between presets

This commit is contained in:
Bartłomiej Dach 2022-02-13 15:50:40 +01:00
parent 36137e0619
commit d0c01afc2e
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
7 changed files with 127 additions and 49 deletions

View File

@ -20,8 +20,8 @@ namespace osu.Game.Tests.Visual.Editing
private BeatDivisorControl beatDivisorControl; private BeatDivisorControl beatDivisorControl;
private BindableBeatDivisor bindableBeatDivisor; private BindableBeatDivisor bindableBeatDivisor;
private SliderBar<int> tickSliderBar; private SliderBar<int> tickSliderBar => beatDivisorControl.ChildrenOfType<SliderBar<int>>().Single();
private EquilateralTriangle tickMarkerHead; private EquilateralTriangle tickMarkerHead => tickSliderBar.ChildrenOfType<EquilateralTriangle>().Single();
[SetUp] [SetUp]
public void SetUp() => Schedule(() => public void SetUp() => Schedule(() =>
@ -32,9 +32,6 @@ namespace osu.Game.Tests.Visual.Editing
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(90, 90) Size = new Vector2(90, 90)
}; };
tickSliderBar = beatDivisorControl.ChildrenOfType<SliderBar<int>>().Single();
tickMarkerHead = tickSliderBar.ChildrenOfType<EquilateralTriangle>().Single();
}); });
[Test] [Test]

View File

@ -164,7 +164,7 @@ namespace osu.Game.Beatmaps.ControlPoints
int closestDivisor = 0; int closestDivisor = 0;
double closestTime = double.MaxValue; double closestTime = double.MaxValue;
foreach (int divisor in BindableBeatDivisor.VALID_DIVISORS) foreach (int divisor in BindableBeatDivisor.PREDEFINED_DIVISORS)
{ {
double distanceFromSnap = Math.Abs(time - getClosestSnappedTime(timingPoint, time, divisor)); double distanceFromSnap = Math.Abs(time - getClosestSnappedTime(timingPoint, time, divisor));

View File

@ -1,46 +1,63 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq; using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK.Graphics; using osuTK.Graphics;
namespace osu.Game.Screens.Edit namespace osu.Game.Screens.Edit
{ {
public class BindableBeatDivisor : BindableInt public class BindableBeatDivisor : BindableInt
{ {
public static readonly int[] VALID_DIVISORS = { 1, 2, 3, 4, 6, 8, 12, 16 }; public static readonly int[] PREDEFINED_DIVISORS = { 1, 2, 3, 4, 6, 8, 12, 16 };
public Bindable<BeatDivisorPresetCollection> ValidDivisors { get; } = new Bindable<BeatDivisorPresetCollection>(BeatDivisorPresetCollection.COMMON);
public BindableBeatDivisor(int value = 1) public BindableBeatDivisor(int value = 1)
: base(value) : base(value)
{ {
ValidDivisors.BindValueChanged(_ => updateBindableProperties(), true);
BindValueChanged(_ => ensureValidDivisor());
} }
public void Next() => Value = VALID_DIVISORS[Math.Min(VALID_DIVISORS.Length - 1, Array.IndexOf(VALID_DIVISORS, Value) + 1)]; private void updateBindableProperties()
public void Previous() => Value = VALID_DIVISORS[Math.Max(0, Array.IndexOf(VALID_DIVISORS, Value) - 1)];
public override int Value
{ {
get => base.Value; ensureValidDivisor();
set
{
if (!VALID_DIVISORS.Contains(value))
{
// If it doesn't match, value will be 0, but will be clamped to the valid range via DefaultMinValue
value = Array.FindLast(VALID_DIVISORS, d => d < value);
}
base.Value = value; MinValue = ValidDivisors.Value.Presets.Min();
} MaxValue = ValidDivisors.Value.Presets.Max();
}
private void ensureValidDivisor()
{
if (!ValidDivisors.Value.Presets.Contains(Value))
Value = 1;
}
public void Next()
{
var presets = ValidDivisors.Value.Presets;
Value = presets.Cast<int?>().SkipWhile(preset => preset != Value).ElementAtOrDefault(1) ?? presets[0];
}
public void Previous()
{
var presets = ValidDivisors.Value.Presets;
Value = presets.Cast<int?>().TakeWhile(preset => preset != Value).LastOrDefault() ?? presets[^1];
} }
protected override int DefaultMinValue => VALID_DIVISORS.First();
protected override int DefaultMaxValue => VALID_DIVISORS.Last();
protected override int DefaultPrecision => 1; protected override int DefaultPrecision => 1;
public override void BindTo(Bindable<int> them)
{
base.BindTo(them);
if (them is BindableBeatDivisor otherBeatDivisor)
ValidDivisors.BindTo(otherBeatDivisor.ValidDivisors);
}
protected override Bindable<int> CreateInstance() => new BindableBeatDivisor(); protected override Bindable<int> CreateInstance() => new BindableBeatDivisor();
/// <summary> /// <summary>
@ -92,7 +109,7 @@ namespace osu.Game.Screens.Edit
{ {
int beat = index % beatDivisor; int beat = index % beatDivisor;
foreach (int divisor in BindableBeatDivisor.VALID_DIVISORS) foreach (int divisor in PREDEFINED_DIVISORS)
{ {
if ((beat * divisor) % beatDivisor == 0) if ((beat * divisor) % beatDivisor == 0)
return divisor; return divisor;

View File

@ -27,7 +27,6 @@ namespace osu.Game.Screens.Edit.Compose.Components
public class BeatDivisorControl : CompositeDrawable public class BeatDivisorControl : CompositeDrawable
{ {
private readonly BindableBeatDivisor beatDivisor = new BindableBeatDivisor(); private readonly BindableBeatDivisor beatDivisor = new BindableBeatDivisor();
private readonly Bindable<BeatDivisorType> divisorType = new Bindable<BeatDivisorType>();
public BeatDivisorControl(BindableBeatDivisor beatDivisor) public BeatDivisorControl(BindableBeatDivisor beatDivisor)
{ {
@ -66,7 +65,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Colour = Color4.Black Colour = Color4.Black
}, },
new TickSliderBar(beatDivisor, BindableBeatDivisor.VALID_DIVISORS) new TickSliderBar(beatDivisor)
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
} }
@ -156,7 +155,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
Icon = FontAwesome.Solid.ChevronLeft, Icon = FontAwesome.Solid.ChevronLeft,
Action = () => cycleDivisorType(-1) Action = () => cycleDivisorType(-1)
}, },
new DivisorTypeText { BeatDivisorType = { BindTarget = divisorType } }, new DivisorTypeText { BeatDivisor = { BindTarget = beatDivisor } },
new ChevronButton new ChevronButton
{ {
Icon = FontAwesome.Solid.ChevronRight, Icon = FontAwesome.Solid.ChevronRight,
@ -189,7 +188,26 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void cycleDivisorType(int direction) private void cycleDivisorType(int direction)
{ {
Debug.Assert(Math.Abs(direction) == 1); Debug.Assert(Math.Abs(direction) == 1);
divisorType.Value = (BeatDivisorType)(((int)divisorType.Value + direction) % (int)(BeatDivisorType.Last + 1)); int nextDivisorType = (int)beatDivisor.ValidDivisors.Value.Type + direction;
if (nextDivisorType > (int)BeatDivisorType.Last)
nextDivisorType = (int)BeatDivisorType.First;
else if (nextDivisorType < (int)BeatDivisorType.First)
nextDivisorType = (int)BeatDivisorType.Last;
switch ((BeatDivisorType)nextDivisorType)
{
case BeatDivisorType.Common:
beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.COMMON;
break;
case BeatDivisorType.Triplets:
beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.TRIPLETS;
break;
case BeatDivisorType.Custom:
beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.Custom(18); // todo
break;
}
} }
private class DivisorText : SpriteText private class DivisorText : SpriteText
@ -217,7 +235,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
private class DivisorTypeText : OsuSpriteText private class DivisorTypeText : OsuSpriteText
{ {
public Bindable<BeatDivisorType> BeatDivisorType { get; } = new Bindable<BeatDivisorType>(); public BindableBeatDivisor BeatDivisor { get; } = new BindableBeatDivisor();
public DivisorTypeText() public DivisorTypeText()
{ {
@ -230,7 +248,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
BeatDivisorType.BindValueChanged(val => Text = val.NewValue.Humanize(LetterCasing.LowerCase), true); BeatDivisor.ValidDivisors.BindValueChanged(val => Text = val.NewValue.Type.Humanize(LetterCasing.LowerCase), true);
} }
} }
@ -265,20 +283,27 @@ namespace osu.Game.Screens.Edit.Compose.Components
private OsuColour colours { get; set; } private OsuColour colours { get; set; }
private readonly BindableBeatDivisor beatDivisor; private readonly BindableBeatDivisor beatDivisor;
private readonly int[] availableDivisors;
public TickSliderBar(BindableBeatDivisor beatDivisor, params int[] divisors) public TickSliderBar(BindableBeatDivisor beatDivisor)
{ {
CurrentNumber.BindTo(this.beatDivisor = beatDivisor); CurrentNumber.BindTo(this.beatDivisor = beatDivisor);
availableDivisors = divisors;
Padding = new MarginPadding { Horizontal = 5 }; Padding = new MarginPadding { Horizontal = 5 };
} }
[BackgroundDependencyLoader] protected override void LoadComplete()
private void load()
{ {
foreach (int t in availableDivisors) base.LoadComplete();
beatDivisor.ValidDivisors.BindValueChanged(_ => updateDivisors(), true);
}
private void updateDivisors()
{
ClearInternal();
CurrentNumber.ValueChanged -= moveMarker;
foreach (int t in beatDivisor.ValidDivisors.Value.Presets)
{ {
AddInternal(new Tick AddInternal(new Tick
{ {
@ -291,17 +316,14 @@ namespace osu.Game.Screens.Edit.Compose.Components
} }
AddInternal(marker = new Marker()); AddInternal(marker = new Marker());
CurrentNumber.ValueChanged += moveMarker;
CurrentNumber.TriggerChange();
} }
protected override void LoadComplete() private void moveMarker(ValueChangedEvent<int> divisor)
{ {
base.LoadComplete(); marker.MoveToX(getMappedPosition(divisor.NewValue), 100, Easing.OutQuint);
marker.Flash();
CurrentNumber.BindValueChanged(div =>
{
marker.MoveToX(getMappedPosition(div.NewValue), 100, Easing.OutQuint);
marker.Flash();
}, true);
} }
protected override void UpdateValue(float value) protected override void UpdateValue(float value)
@ -362,11 +384,11 @@ namespace osu.Game.Screens.Edit.Compose.Components
// copied from SliderBar so we can do custom spacing logic. // copied from SliderBar so we can do custom spacing logic.
float xPosition = (ToLocalSpace(screenSpaceMousePosition).X - RangePadding) / UsableWidth; float xPosition = (ToLocalSpace(screenSpaceMousePosition).X - RangePadding) / UsableWidth;
CurrentNumber.Value = availableDivisors.OrderBy(d => Math.Abs(getMappedPosition(d) - xPosition)).First(); CurrentNumber.Value = beatDivisor.ValidDivisors.Value.Presets.OrderBy(d => Math.Abs(getMappedPosition(d) - xPosition)).First();
OnUserChange(Current.Value); OnUserChange(Current.Value);
} }
private float getMappedPosition(float divisor) => MathF.Pow((divisor - 1) / (availableDivisors.Last() - 1), 0.90f); private float getMappedPosition(float divisor) => MathF.Pow((divisor - 1) / (beatDivisor.ValidDivisors.Value.Presets.Last() - 1), 0.90f);
private class Tick : CompositeDrawable private class Tick : CompositeDrawable
{ {

View File

@ -0,0 +1,41 @@
// 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 System.Collections.Generic;
using System.Linq;
namespace osu.Game.Screens.Edit.Compose.Components
{
public class BeatDivisorPresetCollection
{
public BeatDivisorType Type { get; }
public IReadOnlyList<int> Presets { get; }
private BeatDivisorPresetCollection(BeatDivisorType type, IEnumerable<int> presets)
{
Type = type;
Presets = presets.ToArray();
}
public static readonly BeatDivisorPresetCollection COMMON = new BeatDivisorPresetCollection(BeatDivisorType.Common, new[] { 1, 2, 4, 8, 16 });
public static readonly BeatDivisorPresetCollection TRIPLETS = new BeatDivisorPresetCollection(BeatDivisorType.Triplets, new[] { 1, 3, 6, 12 });
public static BeatDivisorPresetCollection Custom(int maxDivisor)
{
var presets = new List<int>();
for (int candidate = 1; candidate <= Math.Sqrt(maxDivisor); ++candidate)
{
if (maxDivisor % candidate != 0)
continue;
presets.Add(candidate);
presets.Add(maxDivisor / candidate);
}
return new BeatDivisorPresetCollection(BeatDivisorType.Custom, presets.Distinct().OrderBy(d => d));
}
}
}

View File

@ -20,6 +20,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// </summary> /// </summary>
Custom, Custom,
First = Common,
Last = Custom Last = Custom
} }
} }

View File

@ -31,7 +31,7 @@ namespace osu.Game.Screens.Edit.Compose.Components.Timeline
[Resolved] [Resolved]
private OsuColour colours { get; set; } private OsuColour colours { get; set; }
private static readonly int highest_divisor = BindableBeatDivisor.VALID_DIVISORS.Last(); private static readonly int highest_divisor = BindableBeatDivisor.PREDEFINED_DIVISORS.Last();
public TimelineTickDisplay() public TimelineTickDisplay()
{ {