Skip to content

Commit 9d88c76

Browse files
committed
Add double-click-nub-to-reset function to form slider bars
See ppy#35742 (comment).
1 parent 26da75e commit 9d88c76

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

osu.Game.Tests/Visual/UserInterface/TestSceneFormSliderBar.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
22
// See the LICENCE file in the repository root for full licence text.
33

4+
using System.Linq;
45
using NUnit.Framework;
56
using osu.Framework.Allocation;
67
using osu.Framework.Bindables;
78
using osu.Framework.Extensions.ObjectExtensions;
89
using osu.Framework.Graphics;
910
using osu.Framework.Graphics.Containers;
11+
using osu.Framework.Graphics.Shapes;
12+
using osu.Framework.Testing;
1013
using osu.Game.Graphics.Sprites;
1114
using osu.Game.Graphics.UserInterfaceV2;
1215
using osu.Game.Overlays;
1316
using osuTK;
17+
using osuTK.Input;
1418

1519
namespace osu.Game.Tests.Visual.UserInterface
1620
{
17-
public partial class TestSceneFormSliderBar : OsuTestScene
21+
public partial class TestSceneFormSliderBar : OsuManualInputManagerTestScene
1822
{
1923
[Cached]
2024
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Aquamarine);
@@ -59,5 +63,49 @@ public void TestTransferValueOnCommit()
5963
slider.TransferValueOnCommit = b;
6064
});
6165
}
66+
67+
[Test]
68+
public void TestNubDoubleClickRevertToDefault()
69+
{
70+
FormSliderBar<float> slider = null!;
71+
72+
AddStep("create content", () =>
73+
{
74+
Child = new FillFlowContainer
75+
{
76+
RelativeSizeAxes = Axes.Both,
77+
Anchor = Anchor.Centre,
78+
Origin = Anchor.Centre,
79+
Width = 0.5f,
80+
Direction = FillDirection.Vertical,
81+
Spacing = new Vector2(10),
82+
Children = new Drawable[]
83+
{
84+
slider = new FormSliderBar<float>
85+
{
86+
Caption = "Slider",
87+
Current = new BindableFloat
88+
{
89+
MinValue = 0,
90+
MaxValue = 10,
91+
Precision = 0.1f,
92+
Default = 5f,
93+
}
94+
},
95+
}
96+
};
97+
});
98+
AddStep("set slider to 1", () => slider.Current.Value = 1);
99+
100+
AddStep("move mouse to nub", () => InputManager.MoveMouseTo(slider.ChildrenOfType<Circle>().Single()));
101+
102+
AddStep("double click nub", () =>
103+
{
104+
InputManager.Click(MouseButton.Left);
105+
InputManager.Click(MouseButton.Left);
106+
});
107+
108+
AddAssert("slider is default", () => slider.Current.IsDefault);
109+
}
62110
}
63111
}

osu.Game/Graphics/UserInterfaceV2/FormSliderBar.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ private partial class InnerSlider : OsuSliderBar<T>
324324

325325
private Box leftBox = null!;
326326
private Box rightBox = null!;
327-
private Circle nub = null!;
328-
private const float nub_width = 10;
327+
private InnerSliderNub nub = null!;
328+
public const float NUB_WIDTH = 10;
329329

330330
[Resolved]
331331
private OverlayColourProvider colourProvider { get; set; } = null!;
@@ -335,7 +335,7 @@ private void load()
335335
{
336336
Height = 40;
337337
RelativeSizeAxes = Axes.X;
338-
RangePadding = nub_width / 2;
338+
RangePadding = NUB_WIDTH / 2;
339339

340340
Children = new Drawable[]
341341
{
@@ -364,12 +364,13 @@ private void load()
364364
{
365365
RelativeSizeAxes = Axes.Both,
366366
Padding = new MarginPadding { Horizontal = RangePadding, },
367-
Child = nub = new Circle
367+
Child = nub = new InnerSliderNub
368368
{
369-
Width = nub_width,
370-
RelativeSizeAxes = Axes.Y,
371-
RelativePositionAxes = Axes.X,
372-
Origin = Anchor.TopCentre,
369+
ResetToDefault = () =>
370+
{
371+
if (!Current.Disabled)
372+
Current.SetDefault();
373+
}
373374
}
374375
},
375376
new HoverClickSounds()
@@ -452,5 +453,27 @@ protected override bool Commit()
452453
return result;
453454
}
454455
}
456+
457+
private partial class InnerSliderNub : Circle
458+
{
459+
public Action? ResetToDefault { get; set; }
460+
461+
[BackgroundDependencyLoader]
462+
private void load()
463+
{
464+
Width = InnerSlider.NUB_WIDTH;
465+
RelativeSizeAxes = Axes.Y;
466+
RelativePositionAxes = Axes.X;
467+
Origin = Anchor.TopCentre;
468+
}
469+
470+
protected override bool OnClick(ClickEvent e) => true; // must be handled for double click handler to ever fire
471+
472+
protected override bool OnDoubleClick(DoubleClickEvent e)
473+
{
474+
ResetToDefault?.Invoke();
475+
return true;
476+
}
477+
}
455478
}
456479
}

0 commit comments

Comments
 (0)