mirror of
https://github.com/osukey/osukey.git
synced 2025-07-02 08:49:59 +09:00
Implement OverlayScrollContainer component
This commit is contained in:
@ -0,0 +1,90 @@
|
||||
// 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 osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Overlays;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osuTK.Graphics;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Utils;
|
||||
using osuTK.Input;
|
||||
|
||||
namespace osu.Game.Tests.Visual.UserInterface
|
||||
{
|
||||
public class TestSceneOverlayScrollContainer : OsuManualInputManagerTestScene
|
||||
{
|
||||
public override IReadOnlyList<Type> RequiredTypes => new[]
|
||||
{
|
||||
typeof(OverlayScrollContainer)
|
||||
};
|
||||
|
||||
[Cached]
|
||||
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Blue);
|
||||
|
||||
private OverlayScrollContainer scroll;
|
||||
|
||||
private int invocationCount;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp() => Schedule(() =>
|
||||
{
|
||||
Add(scroll = new OverlayScrollContainer
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Child = new Container
|
||||
{
|
||||
Height = 3000,
|
||||
RelativeSizeAxes = Axes.X,
|
||||
Child = new Box
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Colour = Color4.Gray
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
invocationCount = 0;
|
||||
|
||||
scroll.Button.Action += () => invocationCount++;
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void TestButtonVisibility()
|
||||
{
|
||||
AddAssert("button is hidden", () => scroll.Button.State.Value == Visibility.Hidden);
|
||||
|
||||
AddStep("scroll to end", () => scroll.ScrollToEnd(false));
|
||||
AddAssert("button is visible", () => scroll.Button.State.Value == Visibility.Visible);
|
||||
|
||||
AddStep("scroll to start", () => scroll.ScrollToStart(false));
|
||||
AddAssert("button is hidden", () => scroll.Button.State.Value == Visibility.Hidden);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestButtonAction()
|
||||
{
|
||||
AddStep("scroll to end", () => scroll.ScrollToEnd(false));
|
||||
|
||||
AddStep("invoke action", () => scroll.Button.Action.Invoke());
|
||||
|
||||
AddUntilStep("scrolled back to start", () => Precision.AlmostEquals(scroll.Current, 0, 0.1f));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMultipleClicks()
|
||||
{
|
||||
AddStep("scroll to end", () => scroll.ScrollToEnd(false));
|
||||
|
||||
AddAssert("invocation count is 0", () => invocationCount == 0);
|
||||
|
||||
AddStep("hover button", () => InputManager.MoveMouseTo(scroll.Button));
|
||||
AddRepeatStep("click button", () => InputManager.Click(MouseButton.Left), 3);
|
||||
|
||||
AddAssert("invocation count is 1", () => invocationCount == 1);
|
||||
}
|
||||
}
|
||||
}
|
169
osu.Game/Overlays/OverlayScrollContainer.cs
Normal file
169
osu.Game/Overlays/OverlayScrollContainer.cs
Normal file
@ -0,0 +1,169 @@
|
||||
// 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 osu.Framework.Allocation;
|
||||
using osu.Framework.Extensions.Color4Extensions;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Effects;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Framework.Input.Events;
|
||||
using osu.Game.Graphics.Containers;
|
||||
using osuTK;
|
||||
using osuTK.Graphics;
|
||||
|
||||
namespace osu.Game.Overlays
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="OsuScrollContainer"/> which provides <see cref="ScrollToTopButton"/>. Mostly used in <see cref="FullscreenOverlay"/>.
|
||||
/// </summary>
|
||||
public class OverlayScrollContainer : OsuScrollContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Scroll position at which the <see cref="ScrollToTopButton"/> will be shown.
|
||||
/// </summary>
|
||||
private const int button_scroll_position = 200;
|
||||
|
||||
public ScrollToTopButton Button { get; }
|
||||
|
||||
private float currentTarget;
|
||||
|
||||
public OverlayScrollContainer()
|
||||
{
|
||||
AddInternal(Button = new ScrollToTopButton
|
||||
{
|
||||
Anchor = Anchor.BottomRight,
|
||||
Origin = Anchor.BottomRight,
|
||||
Margin = new MarginPadding(20),
|
||||
Action = () =>
|
||||
{
|
||||
ScrollToStart();
|
||||
currentTarget = Target;
|
||||
Button.State.Value = Visibility.Hidden;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected override void UpdateAfterChildren()
|
||||
{
|
||||
base.UpdateAfterChildren();
|
||||
|
||||
if (ScrollContent.DrawHeight + button_scroll_position < DrawHeight)
|
||||
{
|
||||
Button.State.Value = Visibility.Hidden;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Target == currentTarget)
|
||||
return;
|
||||
|
||||
currentTarget = Target;
|
||||
Button.State.Value = Current > button_scroll_position ? Visibility.Visible : Visibility.Hidden;
|
||||
}
|
||||
|
||||
public class ScrollToTopButton : VisibilityContainer
|
||||
{
|
||||
private const int fade_duration = 500;
|
||||
|
||||
public Action Action
|
||||
{
|
||||
get => button.Action;
|
||||
set => button.Action = value;
|
||||
}
|
||||
|
||||
public override bool PropagatePositionalInputSubTree => true;
|
||||
|
||||
protected override bool StartHidden => true;
|
||||
|
||||
private readonly Button button;
|
||||
|
||||
public ScrollToTopButton()
|
||||
{
|
||||
Size = new Vector2(50);
|
||||
Child = button = new Button();
|
||||
}
|
||||
|
||||
protected override bool OnMouseDown(MouseDownEvent e) => true;
|
||||
|
||||
protected override void PopIn() => button.FadeIn(fade_duration, Easing.OutQuint);
|
||||
|
||||
protected override void PopOut() => button.FadeOut(fade_duration, Easing.OutQuint);
|
||||
|
||||
private class Button : OsuHoverContainer
|
||||
{
|
||||
public override bool PropagatePositionalInputSubTree => Alpha == 1;
|
||||
|
||||
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
|
||||
|
||||
private Color4 flashColour;
|
||||
|
||||
private readonly Container content;
|
||||
private readonly Box background;
|
||||
|
||||
public Button()
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
Add(content = new CircularContainer
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Masking = true,
|
||||
EdgeEffect = new EdgeEffectParameters
|
||||
{
|
||||
Type = EdgeEffectType.Shadow,
|
||||
Offset = new Vector2(0f, 1f),
|
||||
Radius = 3f,
|
||||
Colour = Color4.Black.Opacity(0.25f),
|
||||
},
|
||||
Children = new Drawable[]
|
||||
{
|
||||
background = new Box
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both
|
||||
},
|
||||
new SpriteIcon
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Size = new Vector2(15),
|
||||
Icon = FontAwesome.Solid.ChevronUp
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
TooltipText = "Scroll to top";
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OverlayColourProvider colourProvider)
|
||||
{
|
||||
IdleColour = colourProvider.Background6;
|
||||
HoverColour = colourProvider.Background5;
|
||||
flashColour = colourProvider.Light1;
|
||||
}
|
||||
|
||||
protected override bool OnClick(ClickEvent e)
|
||||
{
|
||||
background.FlashColour(flashColour, 800, Easing.OutQuint);
|
||||
return base.OnClick(e);
|
||||
}
|
||||
|
||||
protected override bool OnMouseDown(MouseDownEvent e)
|
||||
{
|
||||
content.ScaleTo(0.75f, 2000, Easing.OutQuint);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override void OnMouseUp(MouseUpEvent e)
|
||||
{
|
||||
content.ScaleTo(1, 1000, Easing.OutElastic);
|
||||
base.OnMouseUp(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user