1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 06:47:24 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuTabControl.cs

280 lines
9.1 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Linq;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
{
public class OsuTabControl<T> : TabControl<T>
{
2020-01-26 17:57:19 +08:00
private Color4 accentColour;
2020-01-22 14:36:16 +08:00
public virtual Color4 AccentColour
2020-01-22 14:36:16 +08:00
{
2020-01-26 17:57:19 +08:00
get => accentColour;
2020-01-24 15:31:47 +08:00
set
{
2020-01-26 17:57:19 +08:00
accentColour = value;
2020-01-24 15:31:47 +08:00
if (Dropdown is IHasAccentColour dropdown)
dropdown.AccentColour = value;
foreach (var i in TabContainer.Children.OfType<IHasAccentColour>())
i.AccentColour = value;
2020-01-24 15:31:47 +08:00
}
2020-01-22 14:36:16 +08:00
}
2018-04-13 17:19:50 +08:00
private readonly Box strip;
protected override Dropdown<T> CreateDropdown() => new OsuTabDropdown();
protected override TabItem<T> CreateTabItem(T value) => new OsuTabItem(value);
2020-01-02 13:19:31 +08:00
protected virtual float StripWidth => TabContainer.Children.Sum(c => c.IsPresent ? c.DrawWidth + TabContainer.Spacing.X : 0) - TabContainer.Spacing.X;
2019-08-12 23:14:37 +08:00
/// <summary>
2019-11-17 20:48:23 +08:00
/// Whether entries should be automatically populated if <typeparamref name="T"/> is an <see cref="Enum"/> type.
2019-08-12 23:14:37 +08:00
/// </summary>
protected virtual bool AddEnumEntriesAutomatically => true;
2018-04-13 17:19:50 +08:00
private static bool isEnumType => typeof(T).IsEnum;
public OsuTabControl()
{
TabContainer.Spacing = new Vector2(10f, 0f);
AddInternal(strip = new Box
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
2020-01-03 14:35:18 +08:00
Height = 1,
2018-04-13 17:19:50 +08:00
Colour = Color4.White.Opacity(0),
});
2019-08-12 23:14:37 +08:00
if (isEnumType && AddEnumEntriesAutomatically)
2019-11-11 19:53:22 +08:00
{
2018-04-13 17:19:50 +08:00
foreach (var val in (T[])Enum.GetValues(typeof(T)))
AddItem(val);
2019-11-11 19:53:22 +08:00
}
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2020-01-26 21:49:39 +08:00
if (accentColour == default)
2018-04-13 17:19:50 +08:00
AccentColour = colours.Blue;
}
public Color4 StripColour
{
get => strip.Colour;
set => strip.Colour = value;
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
// dont bother calculating if the strip is invisible
if (strip.Colour.MaxAlpha > 0)
2020-01-02 13:19:31 +08:00
strip.Width = Interpolation.ValueAt(Math.Clamp(Clock.ElapsedFrameTime, 0, 1000), strip.Width, StripWidth, 0, 500, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
public class OsuTabItem : TabItem<T>, IHasAccentColour
{
protected readonly SpriteText Text;
protected readonly Box Bar;
private Color4 accentColour;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public Color4 AccentColour
{
get => accentColour;
2018-04-13 17:19:50 +08:00
set
{
accentColour = value;
2019-02-21 17:56:34 +08:00
if (!Active.Value)
2018-04-13 17:19:50 +08:00
Text.Colour = value;
}
}
private const float transition_length = 500;
private void fadeActive()
{
Bar.FadeIn(transition_length, Easing.OutQuint);
Text.FadeColour(Color4.White, transition_length, Easing.OutQuint);
}
private void fadeInactive()
{
Bar.FadeOut(transition_length, Easing.OutQuint);
Text.FadeColour(AccentColour, transition_length, Easing.OutQuint);
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
2019-02-21 17:56:34 +08:00
if (!Active.Value)
2018-04-13 17:19:50 +08:00
fadeActive();
return true;
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
2019-02-21 17:56:34 +08:00
if (!Active.Value)
2018-04-13 17:19:50 +08:00
fadeInactive();
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2018-10-16 10:40:51 +08:00
if (accentColour == default)
2018-04-13 17:19:50 +08:00
AccentColour = colours.Blue;
}
2019-02-28 12:31:40 +08:00
public OsuTabItem(T value)
: base(value)
2018-04-13 17:19:50 +08:00
{
AutoSizeAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
Children = new Drawable[]
{
Text = new OsuSpriteText
{
Margin = new MarginPadding { Top = 5, Bottom = 5 },
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
2018-05-28 12:02:06 +08:00
Text = (value as IHasDescription)?.Description ?? (value as Enum)?.GetDescription() ?? value.ToString(),
Font = OsuFont.GetFont(size: 14)
2018-04-13 17:19:50 +08:00
},
Bar = new Box
{
RelativeSizeAxes = Axes.X,
Height = 1,
Alpha = 0,
Colour = Color4.White,
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
},
new HoverClickSounds()
};
Active.BindValueChanged(active => Text.Font = Text.Font.With(Typeface.Exo, weight: active.NewValue ? FontWeight.Bold : FontWeight.Medium), true);
2018-04-13 17:19:50 +08:00
}
protected override void OnActivated() => fadeActive();
protected override void OnDeactivated() => fadeInactive();
}
// todo: this needs to go
private class OsuTabDropdown : OsuDropdown<T>
{
public OsuTabDropdown()
{
RelativeSizeAxes = Axes.X;
}
protected override DropdownMenu CreateMenu() => new OsuTabDropdownMenu();
protected override DropdownHeader CreateHeader() => new OsuTabDropdownHeader
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight
};
private class OsuTabDropdownMenu : OsuDropdownMenu
{
public OsuTabDropdownMenu()
{
Anchor = Anchor.TopRight;
Origin = Anchor.TopRight;
BackgroundColour = Color4.Black.Opacity(0.7f);
MaxHeight = 400;
}
protected override DrawableDropdownMenuItem CreateDrawableDropdownMenuItem(MenuItem item) => new DrawableOsuTabDropdownMenuItem(item) { AccentColour = AccentColour };
2018-04-13 17:19:50 +08:00
private class DrawableOsuTabDropdownMenuItem : DrawableOsuDropdownMenuItem
{
public DrawableOsuTabDropdownMenuItem(MenuItem item)
: base(item)
{
ForegroundColourHover = Color4.Black;
}
}
}
protected class OsuTabDropdownHeader : OsuDropdownHeader
{
public override Color4 AccentColour
{
get => base.AccentColour;
2018-04-13 17:19:50 +08:00
set
{
base.AccentColour = value;
Foreground.Colour = value;
}
}
public OsuTabDropdownHeader()
{
RelativeSizeAxes = Axes.None;
AutoSizeAxes = Axes.X;
BackgroundColour = Color4.Black.Opacity(0.5f);
Background.Height = 0.5f;
Background.CornerRadius = 5;
Background.Masking = true;
Foreground.RelativeSizeAxes = Axes.None;
Foreground.AutoSizeAxes = Axes.X;
Foreground.RelativeSizeAxes = Axes.Y;
Foreground.Margin = new MarginPadding(5);
Foreground.Children = new Drawable[]
{
new SpriteIcon
{
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.EllipsisH,
2018-04-13 17:19:50 +08:00
Size = new Vector2(14),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
}
};
Padding = new MarginPadding { Left = 5, Right = 5 };
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
Foreground.Colour = BackgroundColour;
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
Foreground.Colour = BackgroundColourHover;
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
}
}
}
}