1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-28 10:17:19 +08:00

Add ability to scroll back to previous position after scrolling to top via button on overlays

This commit is contained in:
Joseph Madamba 2023-02-26 14:38:51 -08:00
parent d98d330da2
commit dc00905f8d
No known key found for this signature in database
GPG Key ID: 8B746C7BDDF0BD76

View File

@ -5,6 +5,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -30,16 +31,20 @@ namespace osu.Game.Overlays
/// </summary> /// </summary>
private const int button_scroll_position = 200; private const int button_scroll_position = 200;
protected readonly ScrollToTopButton Button; protected ScrollToTopButton Button;
public OverlayScrollContainer() private readonly Bindable<float?> lastScrollTarget = new Bindable<float?>();
[BackgroundDependencyLoader]
private void load()
{ {
AddInternal(Button = new ScrollToTopButton AddInternal(Button = new ScrollToTopButton
{ {
Anchor = Anchor.BottomRight, Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight, Origin = Anchor.BottomRight,
Margin = new MarginPadding(20), Margin = new MarginPadding(20),
Action = scrollToTop Action = scrollBack,
LastScrollTarget = { BindTarget = lastScrollTarget }
}); });
} }
@ -53,13 +58,28 @@ namespace osu.Game.Overlays
return; return;
} }
Button.State = Target > button_scroll_position ? Visibility.Visible : Visibility.Hidden; Button.State = Target > button_scroll_position || lastScrollTarget.Value != null ? Visibility.Visible : Visibility.Hidden;
} }
private void scrollToTop() protected override void OnUserScroll(float value, bool animated = true, double? distanceDecay = default)
{ {
ScrollToStart(); base.OnUserScroll(value, animated, distanceDecay);
Button.State = Visibility.Hidden;
lastScrollTarget.Value = null;
}
private void scrollBack()
{
if (lastScrollTarget.Value == null)
{
lastScrollTarget.Value = Target;
ScrollToStart();
}
else
{
ScrollTo(lastScrollTarget.Value.Value);
lastScrollTarget.Value = null;
}
} }
public partial class ScrollToTopButton : OsuHoverContainer public partial class ScrollToTopButton : OsuHoverContainer
@ -88,6 +108,9 @@ namespace osu.Game.Overlays
private readonly Container content; private readonly Container content;
private readonly Box background; private readonly Box background;
private readonly SpriteIcon spriteIcon;
public Bindable<float?> LastScrollTarget = new Bindable<float?>();
public ScrollToTopButton() public ScrollToTopButton()
: base(HoverSampleSet.ScrollToTop) : base(HoverSampleSet.ScrollToTop)
@ -113,7 +136,7 @@ namespace osu.Game.Overlays
{ {
RelativeSizeAxes = Axes.Both RelativeSizeAxes = Axes.Both
}, },
new SpriteIcon spriteIcon = new SpriteIcon
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
@ -134,6 +157,17 @@ namespace osu.Game.Overlays
flashColour = colourProvider.Light1; flashColour = colourProvider.Light1;
} }
protected override void LoadComplete()
{
base.LoadComplete();
LastScrollTarget.BindValueChanged(target =>
{
spriteIcon.RotateTo(target.NewValue != null ? 180 : 0, fade_duration, Easing.OutQuint);
TooltipText = target.NewValue != null ? CommonStrings.ButtonsBackToPrevious : CommonStrings.ButtonsBackToTop;
}, true);
}
protected override bool OnClick(ClickEvent e) protected override bool OnClick(ClickEvent e)
{ {
background.FlashColour(flashColour, 800, Easing.OutQuint); background.FlashColour(flashColour, 800, Easing.OutQuint);