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

Refactor OverlayHeader hierarchy

This commit is contained in:
Andrei Zavatski 2020-01-21 06:00:12 +03:00
parent 178a72f9b8
commit 30edd80c8c
8 changed files with 67 additions and 97 deletions

View File

@ -22,7 +22,6 @@ namespace osu.Game.Tests.Visual.UserInterface
public override IReadOnlyList<Type> RequiredTypes => new[] public override IReadOnlyList<Type> RequiredTypes => new[]
{ {
typeof(OverlayHeader), typeof(OverlayHeader),
typeof(ControllableOverlayHeader<>),
typeof(TabControlOverlayHeader<>), typeof(TabControlOverlayHeader<>),
typeof(BreadcrumbControlOverlayHeader), typeof(BreadcrumbControlOverlayHeader),
typeof(TestNoControlHeader), typeof(TestNoControlHeader),
@ -139,9 +138,9 @@ namespace osu.Game.Tests.Visual.UserInterface
public TestBreadcrumbControlHeader(OverlayColourScheme colourScheme) public TestBreadcrumbControlHeader(OverlayColourScheme colourScheme)
: base(colourScheme) : base(colourScheme)
{ {
BreadcrumbControl.AddItem("tab1"); TabControl.AddItem("tab1");
BreadcrumbControl.AddItem("tab2"); TabControl.AddItem("tab2");
BreadcrumbControl.Current.Value = "tab2"; TabControl.Current.Value = "tab2";
} }
} }

View File

@ -17,6 +17,7 @@ using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
@ -75,6 +76,7 @@ namespace osu.Game.Graphics.UserInterface
dropdown.AccentColour = value; dropdown.AccentColour = value;
foreach (var i in TabContainer.Children.OfType<IHasAccentColour>()) foreach (var i in TabContainer.Children.OfType<IHasAccentColour>())
i.AccentColour = value; i.AccentColour = value;
InternalChildren.OfType<IHasAccentColour>().ForEach(c => c.AccentColour = value);
} }
} }

View File

@ -1,7 +1,6 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics; using osu.Game.Graphics;
@ -9,23 +8,15 @@ using osu.Game.Graphics.UserInterface;
namespace osu.Game.Overlays namespace osu.Game.Overlays
{ {
public abstract class BreadcrumbControlOverlayHeader : ControllableOverlayHeader<string> public abstract class BreadcrumbControlOverlayHeader : TabControlOverlayHeader<string>
{ {
protected OverlayHeaderBreadcrumbControl BreadcrumbControl; protected override OsuTabControl<string> CreateTabControl() => new OverlayHeaderBreadcrumbControl();
protected override TabControl<string> CreateTabControl() => BreadcrumbControl = new OverlayHeaderBreadcrumbControl();
protected BreadcrumbControlOverlayHeader(OverlayColourScheme colourScheme) protected BreadcrumbControlOverlayHeader(OverlayColourScheme colourScheme)
: base(colourScheme) : base(colourScheme)
{ {
} }
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BreadcrumbControl.AccentColour = colours.ForOverlayElement(ColourScheme, 1, 0.75f);
}
public class OverlayHeaderBreadcrumbControl : BreadcrumbControl<string> public class OverlayHeaderBreadcrumbControl : BreadcrumbControl<string>
{ {
public OverlayHeaderBreadcrumbControl() public OverlayHeaderBreadcrumbControl()

View File

@ -28,8 +28,8 @@ namespace osu.Game.Overlays.Changelog
public ChangelogHeader() public ChangelogHeader()
: base(OverlayColourScheme.Purple) : base(OverlayColourScheme.Purple)
{ {
BreadcrumbControl.AddItem(listing_string); TabControl.AddItem(listing_string);
BreadcrumbControl.Current.ValueChanged += e => TabControl.Current.ValueChanged += e =>
{ {
if (e.NewValue == listing_string) if (e.NewValue == listing_string)
ListingSelected?.Invoke(); ListingSelected?.Invoke();
@ -49,12 +49,12 @@ namespace osu.Game.Overlays.Changelog
private void showBuild(ValueChangedEvent<APIChangelogBuild> e) private void showBuild(ValueChangedEvent<APIChangelogBuild> e)
{ {
if (e.OldValue != null) if (e.OldValue != null)
BreadcrumbControl.RemoveItem(e.OldValue.ToString()); TabControl.RemoveItem(e.OldValue.ToString());
if (e.NewValue != null) if (e.NewValue != null)
{ {
BreadcrumbControl.AddItem(e.NewValue.ToString()); TabControl.AddItem(e.NewValue.ToString());
BreadcrumbControl.Current.Value = e.NewValue.ToString(); TabControl.Current.Value = e.NewValue.ToString();
Streams.Current.Value = Streams.Items.FirstOrDefault(s => s.Name == e.NewValue.UpdateStream.Name); Streams.Current.Value = Streams.Items.FirstOrDefault(s => s.Name == e.NewValue.UpdateStream.Name);
@ -62,7 +62,7 @@ namespace osu.Game.Overlays.Changelog
} }
else else
{ {
BreadcrumbControl.Current.Value = listing_string; TabControl.Current.Value = listing_string;
Streams.Current.Value = null; Streams.Current.Value = null;
title.Version = null; title.Version = null;
} }

View File

@ -1,49 +0,0 @@
// 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.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics;
using osuTK.Graphics;
namespace osu.Game.Overlays
{
/// <summary>
/// <see cref="OverlayHeader"/> which contains <see cref="TabControl{T}"/>.
/// </summary>
/// <typeparam name="T">The type of item to be represented by tabs in <see cref="TabControl{T}"/>.</typeparam>
public abstract class ControllableOverlayHeader<T> : OverlayHeader
{
private readonly Box controlBackground;
protected ControllableOverlayHeader(OverlayColourScheme colourScheme)
: base(colourScheme)
{
HeaderInfo.Add(new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
controlBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Gray,
},
CreateTabControl().With(control => control.Margin = new MarginPadding { Left = UserProfileOverlay.CONTENT_X_MARGIN })
}
});
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
controlBackground.Colour = colours.ForOverlayElement(ColourScheme, 0.2f, 0.2f);
}
protected abstract TabControl<T> CreateTabControl();
}
}

View File

@ -25,9 +25,9 @@ namespace osu.Game.Overlays.News
public NewsHeader() public NewsHeader()
: base(OverlayColourScheme.Purple) : base(OverlayColourScheme.Purple)
{ {
BreadcrumbControl.AddItem(front_page_string); TabControl.AddItem(front_page_string);
BreadcrumbControl.Current.ValueChanged += e => TabControl.Current.ValueChanged += e =>
{ {
if (e.NewValue == front_page_string) if (e.NewValue == front_page_string)
ShowFrontPage?.Invoke(); ShowFrontPage?.Invoke();
@ -39,18 +39,18 @@ namespace osu.Game.Overlays.News
private void showPost(ValueChangedEvent<string> e) private void showPost(ValueChangedEvent<string> e)
{ {
if (e.OldValue != null) if (e.OldValue != null)
BreadcrumbControl.RemoveItem(e.OldValue); TabControl.RemoveItem(e.OldValue);
if (e.NewValue != null) if (e.NewValue != null)
{ {
BreadcrumbControl.AddItem(e.NewValue); TabControl.AddItem(e.NewValue);
BreadcrumbControl.Current.Value = e.NewValue; TabControl.Current.Value = e.NewValue;
title.IsReadingPost = true; title.IsReadingPost = true;
} }
else else
{ {
BreadcrumbControl.Current.Value = front_page_string; TabControl.Current.Value = front_page_string;
title.IsReadingPost = false; title.IsReadingPost = false;
} }
} }

View File

@ -2,6 +2,7 @@
// 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 osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
@ -15,13 +16,7 @@ namespace osu.Game.Overlays
{ {
public abstract class OverlayTabControl<T> : OsuTabControl<T> public abstract class OverlayTabControl<T> : OsuTabControl<T>
{ {
private readonly Box bar; private readonly Bar bar;
public new Color4 AccentColour
{
get => base.AccentColour;
set => base.AccentColour = bar.Colour = value;
}
protected float BarHeight protected float BarHeight
{ {
@ -33,7 +28,7 @@ namespace osu.Game.Overlays
TabContainer.Masking = false; TabContainer.Masking = false;
TabContainer.Spacing = new Vector2(15, 0); TabContainer.Spacing = new Vector2(15, 0);
AddInternal(bar = new Box AddInternal(bar = new Bar
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Height = 2, Height = 2,
@ -143,5 +138,24 @@ namespace osu.Game.Overlays
Text.FadeColour(AccentColour, 120, Easing.InQuad); Text.FadeColour(AccentColour, 120, Easing.InQuad);
} }
} }
private class Bar : CompositeDrawable, IHasAccentColour
{
public Color4 AccentColour
{
get => background.Colour;
set => background.Colour = value;
}
private readonly Box background;
public Bar()
{
AddInternal(background = new Box
{
RelativeSizeAxes = Axes.Both
});
}
}
} }
} }

View File

@ -5,29 +5,51 @@ using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osuTK; using osuTK;
namespace osu.Game.Overlays namespace osu.Game.Overlays
{ {
public abstract class TabControlOverlayHeader<T> : ControllableOverlayHeader<T> /// <summary>
/// <see cref="OverlayHeader"/> which contains <see cref="OsuTabControl{T}"/>.
/// </summary>
/// <typeparam name="T">The type of item to be represented by tabs in <see cref="OsuTabControl{T}"/>.</typeparam>
public abstract class TabControlOverlayHeader<T> : OverlayHeader
{ {
protected OverlayHeaderTabControl TabControl; protected OsuTabControl<T> TabControl;
protected override TabControl<T> CreateTabControl() => TabControl = new OverlayHeaderTabControl(); private readonly Box controlBackground;
protected TabControlOverlayHeader(OverlayColourScheme colourScheme) protected TabControlOverlayHeader(OverlayColourScheme colourScheme)
: base(colourScheme) : base(colourScheme)
{ {
HeaderInfo.Add(new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
controlBackground = new Box
{
RelativeSizeAxes = Axes.Both,
},
TabControl = CreateTabControl().With(control => control.Margin = new MarginPadding { Left = UserProfileOverlay.CONTENT_X_MARGIN })
}
});
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours)
{ {
TabControl.AccentColour = colours.ForOverlayElement(ColourScheme, 1, 0.75f); TabControl.AccentColour = colours.ForOverlayElement(ColourScheme, 1, 0.75f);
controlBackground.Colour = colours.ForOverlayElement(ColourScheme, 0.2f, 0.2f);
} }
protected virtual OsuTabControl<T> CreateTabControl() => new OverlayHeaderTabControl();
public class OverlayHeaderTabControl : OverlayTabControl<T> public class OverlayHeaderTabControl : OverlayTabControl<T>
{ {
public OverlayHeaderTabControl() public OverlayHeaderTabControl()
@ -38,18 +60,9 @@ namespace osu.Game.Overlays
Anchor = Anchor.BottomLeft; Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomLeft; Origin = Anchor.BottomLeft;
Height = 35; Height = 35;
if (typeof(T).IsEnum)
{
foreach (var val in (T[])Enum.GetValues(typeof(T)))
AddItem(val);
}
} }
protected override TabItem<T> CreateTabItem(T value) => new OverlayHeaderTabItem(value) protected override TabItem<T> CreateTabItem(T value) => new OverlayHeaderTabItem(value);
{
AccentColour = AccentColour,
};
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{ {