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

Implement OverlayUpdateStreamControl component

This commit is contained in:
Andrei Zavatski 2020-03-03 17:01:58 +03:00
parent 8dcdd6db6f
commit 937d9da43b
6 changed files with 111 additions and 67 deletions

View File

@ -17,8 +17,8 @@ namespace osu.Game.Tests.Visual.Online
public override IReadOnlyList<Type> RequiredTypes => new[] public override IReadOnlyList<Type> RequiredTypes => new[]
{ {
typeof(ChangelogUpdateStreamBadgeArea), typeof(ChangelogUpdateStreamControl),
typeof(ChangelogUpdateStreamBadge), typeof(ChangelogUpdateStreamItem),
typeof(ChangelogHeader), typeof(ChangelogHeader),
typeof(ChangelogContent), typeof(ChangelogContent),
typeof(ChangelogListing), typeof(ChangelogListing),

View File

@ -20,7 +20,7 @@ namespace osu.Game.Overlays.Changelog
public Action ListingSelected; public Action ListingSelected;
public ChangelogUpdateStreamBadgeArea Streams; public ChangelogUpdateStreamControl Streams;
private const string listing_string = "listing"; private const string listing_string = "listing";
@ -95,7 +95,7 @@ namespace osu.Game.Overlays.Changelog
Horizontal = 65, Horizontal = 65,
Vertical = 20 Vertical = 20
}, },
Child = Streams = new ChangelogUpdateStreamBadgeArea() Child = Streams = new ChangelogUpdateStreamControl()
} }
} }
}; };

View File

@ -0,0 +1,12 @@
// 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.Game.Online.API.Requests.Responses;
namespace osu.Game.Overlays.Changelog
{
public class ChangelogUpdateStreamControl : OverlayUpdateStreamControl<APIUpdateStream>
{
protected override OverlayUpdateStreamItem<APIUpdateStream> CreateStreamItem(APIUpdateStream value) => new ChangelogUpdateStreamItem(value);
}
}

View File

@ -0,0 +1,25 @@
// 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 Humanizer;
using osu.Game.Online.API.Requests.Responses;
using osuTK.Graphics;
namespace osu.Game.Overlays.Changelog
{
public class ChangelogUpdateStreamItem : OverlayUpdateStreamItem<APIUpdateStream>
{
public ChangelogUpdateStreamItem(APIUpdateStream stream)
: base(stream)
{
}
protected override string GetMainText() => Value.DisplayName;
protected override string GetAdditionalText() => Value.LatestBuild.DisplayVersion;
protected override string GetInfoText() => Value.LatestBuild.Users > 0 ? $"{"user".ToQuantity(Value.LatestBuild.Users, "N0")} online" : null;
protected override Color4 GetBarColour() => Value.Colour;
}
}

View File

@ -3,42 +3,32 @@
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Game.Online.API.Requests.Responses;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using JetBrains.Annotations;
namespace osu.Game.Overlays.Changelog namespace osu.Game.Overlays
{ {
public class ChangelogUpdateStreamBadgeArea : TabControl<APIUpdateStream> public abstract class OverlayUpdateStreamControl<T> : TabControl<T>
{ {
public ChangelogUpdateStreamBadgeArea() protected OverlayUpdateStreamControl()
{ {
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y; AutoSizeAxes = Axes.Y;
} }
public void Populate(List<APIUpdateStream> streams) public void Populate(List<T> streams) => streams.ForEach(AddItem);
protected override Dropdown<T> CreateDropdown() => null;
protected override TabItem<T> CreateTabItem(T value) => CreateStreamItem(value).With(item =>
{ {
foreach (var updateStream in streams) item.SelectedItem.BindTo(Current);
AddItem(updateStream); });
}
protected override bool OnHover(HoverEvent e) [NotNull]
{ protected abstract OverlayUpdateStreamItem<T> CreateStreamItem(T value);
foreach (var streamBadge in TabContainer.Children.OfType<ChangelogUpdateStreamBadge>())
streamBadge.UserHoveringArea = true;
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
foreach (var streamBadge in TabContainer.Children.OfType<ChangelogUpdateStreamBadge>())
streamBadge.UserHoveringArea = false;
base.OnHoverLost(e);
}
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{ {
@ -47,9 +37,20 @@ namespace osu.Game.Overlays.Changelog
AllowMultiline = true, AllowMultiline = true,
}; };
protected override Dropdown<APIUpdateStream> CreateDropdown() => null; protected override bool OnHover(HoverEvent e)
{
foreach (var streamBadge in TabContainer.Children.OfType<OverlayUpdateStreamItem<T>>())
streamBadge.UserHoveringArea = true;
protected override TabItem<APIUpdateStream> CreateTabItem(APIUpdateStream value) => return base.OnHover(e);
new ChangelogUpdateStreamBadge(value) { SelectedTab = { BindTarget = Current } }; }
protected override void OnHoverLost(HoverLostEvent e)
{
foreach (var streamBadge in TabContainer.Children.OfType<OverlayUpdateStreamItem<T>>())
streamBadge.UserHoveringArea = false;
base.OnHoverLost(e);
}
} }
} }

View File

@ -1,44 +1,54 @@
// 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 Humanizer;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Online.API.Requests.Responses;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Sprites; using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Allocation;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics;
using osuTK; using osuTK;
using osuTK.Graphics;
namespace osu.Game.Overlays.Changelog namespace osu.Game.Overlays
{ {
public class ChangelogUpdateStreamBadge : TabItem<APIUpdateStream> public abstract class OverlayUpdateStreamItem<T> : TabItem<T>
{ {
private const float badge_width = 100;
private const float transition_duration = 100; private const float transition_duration = 100;
private const float tab_width = 100;
public readonly Bindable<APIUpdateStream> SelectedTab = new Bindable<APIUpdateStream>(); public readonly Bindable<T> SelectedItem = new Bindable<T>();
private readonly APIUpdateStream stream; private bool userHoveringArea;
public bool UserHoveringArea
{
set
{
if (value == userHoveringArea)
return;
userHoveringArea = value;
updateState();
}
}
private FillFlowContainer<SpriteText> text; private FillFlowContainer<SpriteText> text;
private ExpandingBar expandingBar; private ExpandingBar expandingBar;
public ChangelogUpdateStreamBadge(APIUpdateStream stream) public OverlayUpdateStreamItem(T value)
: base(stream) : base(value)
{ {
this.stream = stream;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider) private void load(OverlayColourProvider colourProvider)
{ {
Size = new Vector2(stream.IsFeatured ? badge_width * 2 : badge_width, 60); Size = new Vector2(GetWidth(), 60);
Padding = new MarginPadding(5); Padding = new MarginPadding(5);
AddRange(new Drawable[] AddRange(new Drawable[]
@ -52,17 +62,17 @@ namespace osu.Game.Overlays.Changelog
{ {
new OsuSpriteText new OsuSpriteText
{ {
Text = stream.DisplayName, Text = GetMainText(),
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Black), Font = OsuFont.GetFont(size: 12, weight: FontWeight.Black),
}, },
new OsuSpriteText new OsuSpriteText
{ {
Text = stream.LatestBuild.DisplayVersion, Text = GetAdditionalText(),
Font = OsuFont.GetFont(size: 16, weight: FontWeight.Regular), Font = OsuFont.GetFont(size: 16, weight: FontWeight.Regular),
}, },
new OsuSpriteText new OsuSpriteText
{ {
Text = stream.LatestBuild.Users > 0 ? $"{"user".ToQuantity(stream.LatestBuild.Users, "N0")} online" : null, Text = GetInfoText(),
Font = OsuFont.GetFont(size: 10), Font = OsuFont.GetFont(size: 10),
Colour = colourProvider.Foreground1 Colour = colourProvider.Foreground1
}, },
@ -71,7 +81,7 @@ namespace osu.Game.Overlays.Changelog
expandingBar = new ExpandingBar expandingBar = new ExpandingBar
{ {
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
Colour = stream.Colour, Colour = GetBarColour(),
ExpandedSize = 4, ExpandedSize = 4,
CollapsedSize = 2, CollapsedSize = 2,
Expanded = true Expanded = true
@ -79,9 +89,19 @@ namespace osu.Game.Overlays.Changelog
new HoverClickSounds() new HoverClickSounds()
}); });
SelectedTab.BindValueChanged(_ => updateState(), true); SelectedItem.BindValueChanged(_ => updateState(), true);
} }
protected abstract string GetMainText();
protected abstract string GetAdditionalText();
protected virtual string GetInfoText() => string.Empty;
protected abstract Color4 GetBarColour();
protected virtual float GetWidth() => tab_width;
protected override void OnActivated() => updateState(); protected override void OnActivated() => updateState();
protected override void OnDeactivated() => updateState(); protected override void OnDeactivated() => updateState();
@ -104,7 +124,7 @@ namespace osu.Game.Overlays.Changelog
bool textHighlighted = IsHovered; bool textHighlighted = IsHovered;
bool barExpanded = IsHovered; bool barExpanded = IsHovered;
if (SelectedTab.Value == null) if (SelectedItem.Value == null)
{ {
// at listing, all badges are highlighted when user is not hovering any badge. // at listing, all badges are highlighted when user is not hovering any badge.
textHighlighted |= !userHoveringArea; textHighlighted |= !userHoveringArea;
@ -122,19 +142,5 @@ namespace osu.Game.Overlays.Changelog
expandingBar.Expanded = barExpanded; expandingBar.Expanded = barExpanded;
text.FadeTo(textHighlighted ? 1 : 0.5f, transition_duration, Easing.OutQuint); text.FadeTo(textHighlighted ? 1 : 0.5f, transition_duration, Easing.OutQuint);
} }
private bool userHoveringArea;
public bool UserHoveringArea
{
set
{
if (value == userHoveringArea)
return;
userHoveringArea = value;
updateState();
}
}
} }
} }