From 423838a649fe8400829a2cbee6de186135799936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sun, 13 Feb 2022 16:27:12 +0100 Subject: [PATCH] Add flow for specifying entirely custom snaps --- .../Editing/TestSceneBeatDivisorControl.cs | 13 +- osu.Game/Screens/Edit/BindableBeatDivisor.cs | 5 +- .../Compose/Components/BeatDivisorControl.cs | 117 ++++++++++++++++-- .../Compose/Components/BeatDivisorType.cs | 3 - 4 files changed, 118 insertions(+), 20 deletions(-) diff --git a/osu.Game.Tests/Visual/Editing/TestSceneBeatDivisorControl.cs b/osu.Game.Tests/Visual/Editing/TestSceneBeatDivisorControl.cs index 131bfde86e..e37019e5d3 100644 --- a/osu.Game.Tests/Visual/Editing/TestSceneBeatDivisorControl.cs +++ b/osu.Game.Tests/Visual/Editing/TestSceneBeatDivisorControl.cs @@ -5,6 +5,7 @@ using System; using System.Linq; using NUnit.Framework; using osu.Framework.Graphics; +using osu.Framework.Graphics.Cursor; using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.UserInterface; using osu.Framework.Testing; @@ -26,11 +27,15 @@ namespace osu.Game.Tests.Visual.Editing [SetUp] public void SetUp() => Schedule(() => { - Child = beatDivisorControl = new BeatDivisorControl(bindableBeatDivisor = new BindableBeatDivisor(16)) + Child = new PopoverContainer { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - Size = new Vector2(90, 90) + RelativeSizeAxes = Axes.Both, + Child = beatDivisorControl = new BeatDivisorControl(bindableBeatDivisor = new BindableBeatDivisor(16)) + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(90, 90) + } }; }); diff --git a/osu.Game/Screens/Edit/BindableBeatDivisor.cs b/osu.Game/Screens/Edit/BindableBeatDivisor.cs index 9077898ec8..af958e3448 100644 --- a/osu.Game/Screens/Edit/BindableBeatDivisor.cs +++ b/osu.Game/Screens/Edit/BindableBeatDivisor.cs @@ -52,10 +52,11 @@ namespace osu.Game.Screens.Edit public override void BindTo(Bindable them) { - base.BindTo(them); - + // bind to valid divisors first (if applicable) to ensure correct transfer of the actual divisor. if (them is BindableBeatDivisor otherBeatDivisor) ValidDivisors.BindTo(otherBeatDivisor.ValidDivisors); + + base.BindTo(them); } protected override Bindable CreateInstance() => new BindableBeatDivisor(); diff --git a/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs b/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs index a9b1213738..04e9ac842c 100644 --- a/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs +++ b/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs @@ -7,17 +7,21 @@ using System.Linq; using Humanizer; using osu.Framework.Allocation; using osu.Framework.Bindables; +using osu.Framework.Extensions; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Cursor; using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.UserInterface; using osu.Framework.Input.Events; using osu.Game.Graphics; +using osu.Game.Graphics.Containers; using osu.Game.Graphics.Sprites; using osu.Game.Graphics.UserInterface; +using osu.Game.Graphics.UserInterfaceV2; using osuTK; using osuTK.Graphics; using osuTK.Input; @@ -99,7 +103,7 @@ namespace osu.Game.Screens.Edit.Compose.Components Icon = FontAwesome.Solid.ChevronLeft, Action = beatDivisor.Previous }, - new DivisorText { BeatDivisor = { BindTarget = beatDivisor } }, + new DivisorDisplay { BeatDivisor = { BindTarget = beatDivisor } }, new ChevronButton { Icon = FontAwesome.Solid.ChevronRight, @@ -189,10 +193,10 @@ namespace osu.Game.Screens.Edit.Compose.Components { Debug.Assert(Math.Abs(direction) == 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; + if (nextDivisorType > (int)BeatDivisorType.Triplets) + nextDivisorType = (int)BeatDivisorType.Common; + else if (nextDivisorType < (int)BeatDivisorType.Common) + nextDivisorType = (int)BeatDivisorType.Triplets; switch ((BeatDivisorType)nextDivisorType) { @@ -205,31 +209,122 @@ namespace osu.Game.Screens.Edit.Compose.Components break; case BeatDivisorType.Custom: - beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.Custom(18); // todo + beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.Custom(beatDivisor.ValidDivisors.Value.Presets.Max()); break; } } - private class DivisorText : SpriteText + private class DivisorDisplay : OsuAnimatedButton, IHasPopover { - public Bindable BeatDivisor { get; } = new Bindable(); + public BindableBeatDivisor BeatDivisor { get; } = new BindableBeatDivisor(); - public DivisorText() + private readonly OsuSpriteText divisorText; + + public DivisorDisplay() { Anchor = Anchor.Centre; Origin = Anchor.Centre; + + AutoSizeAxes = Axes.Both; + + Add(divisorText = new OsuSpriteText + { + Font = OsuFont.Default.With(size: 20), + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Margin = new MarginPadding + { + Horizontal = 5 + } + }); + + Action = this.ShowPopover; } [BackgroundDependencyLoader] private void load(OsuColour colours) { - Colour = colours.BlueLighter; + divisorText.Colour = colours.BlueLighter; } protected override void LoadComplete() { base.LoadComplete(); - BeatDivisor.BindValueChanged(val => Text = $"1/{val.NewValue}", true); + updateState(); + } + + private void updateState() + { + BeatDivisor.BindValueChanged(val => divisorText.Text = $"1/{val.NewValue}", true); + } + + public Popover GetPopover() => new CustomDivisorPopover + { + BeatDivisor = { BindTarget = BeatDivisor } + }; + } + + private class CustomDivisorPopover : OsuPopover + { + public BindableBeatDivisor BeatDivisor { get; } = new BindableBeatDivisor(); + + private readonly OsuNumberBox divisorTextBox; + + public CustomDivisorPopover() + { + Child = new FillFlowContainer + { + Width = 150, + AutoSizeAxes = Axes.Y, + Spacing = new Vector2(10), + Children = new Drawable[] + { + divisorTextBox = new OsuNumberBox + { + RelativeSizeAxes = Axes.X, + PlaceholderText = "Beat divisor" + }, + new OsuTextFlowContainer + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + Text = "All other applicable smaller divisors will be automatically added to the list of presets." + } + } + }; + } + + protected override void LoadComplete() + { + base.LoadComplete(); + BeatDivisor.BindValueChanged(_ => updateState(), true); + divisorTextBox.OnCommit += (_, __) => setPresets(); + } + + private void setPresets() + { + if (!int.TryParse(divisorTextBox.Text, out int divisor) || divisor < 1 || divisor > 64) + { + updateState(); + return; + } + + if (!BeatDivisor.ValidDivisors.Value.Presets.Contains(divisor)) + { + if (BeatDivisorPresetCollection.COMMON.Presets.Contains(divisor)) + BeatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.COMMON; + else if (BeatDivisorPresetCollection.TRIPLETS.Presets.Contains(divisor)) + BeatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.TRIPLETS; + else + BeatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.Custom(divisor); + } + + BeatDivisor.Value = divisor; + } + + private void updateState() + { + divisorTextBox.Text = BeatDivisor.Value.ToString(); } } diff --git a/osu.Game/Screens/Edit/Compose/Components/BeatDivisorType.cs b/osu.Game/Screens/Edit/Compose/Components/BeatDivisorType.cs index 15a8c504c5..4a25144881 100644 --- a/osu.Game/Screens/Edit/Compose/Components/BeatDivisorType.cs +++ b/osu.Game/Screens/Edit/Compose/Components/BeatDivisorType.cs @@ -19,8 +19,5 @@ namespace osu.Game.Screens.Edit.Compose.Components /// Fully arbitrary/custom beat divisors. /// Custom, - - First = Common, - Last = Custom } }