2019-01-24 16:43:03 +08:00
|
|
|
|
// 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
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2017-03-15 11:19:41 +08:00
|
|
|
|
using System;
|
2017-03-16 10:39:09 +08:00
|
|
|
|
using System.Linq;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2017-03-16 08:52:31 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2024-05-22 16:29:39 +08:00
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2017-03-23 07:44:52 +08:00
|
|
|
|
using osu.Framework.Extensions;
|
2017-03-16 20:17:14 +08:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
|
|
|
|
using osu.Framework.Graphics;
|
2017-06-20 13:54:23 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2017-03-23 07:44:52 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2017-03-16 20:17:14 +08:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2021-07-17 19:40:32 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2020-01-09 12:43:44 +08:00
|
|
|
|
using osu.Framework.Utils;
|
2017-03-23 07:44:52 +08:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-03-16 08:11:50 +08:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
2017-03-05 12:07:47 +08:00
|
|
|
|
{
|
2017-03-16 08:11:50 +08:00
|
|
|
|
public partial class OsuTabControl<T> : TabControl<T>
|
2017-03-05 12:07:47 +08:00
|
|
|
|
{
|
2020-01-26 17:57:19 +08:00
|
|
|
|
private Color4 accentColour;
|
2020-01-22 14:36:16 +08:00
|
|
|
|
|
2020-07-09 12:47:11 +08:00
|
|
|
|
public const float HORIZONTAL_SPACING = 10;
|
|
|
|
|
|
2020-01-26 22:21:22 +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
|
|
|
|
|
2020-01-26 22:21:22 +08:00
|
|
|
|
if (Dropdown is IHasAccentColour dropdown)
|
|
|
|
|
dropdown.AccentColour = value;
|
2024-01-23 04:32:11 +08:00
|
|
|
|
foreach (var i in TabContainer.OfType<IHasAccentColour>())
|
2020-01-26 22:21:22 +08:00
|
|
|
|
i.AccentColour = value;
|
2020-01-24 15:31:47 +08:00
|
|
|
|
}
|
2020-01-22 14:36:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-30 02:29:45 +08:00
|
|
|
|
private readonly Box strip;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-02-18 04:56:35 +08:00
|
|
|
|
protected override Dropdown<T> CreateDropdown() => new OsuTabDropdown<T>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-04-03 17:35:33 +08:00
|
|
|
|
protected override TabItem<T> CreateTabItem(T value) => new OsuTabItem(value);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2024-01-23 04:32:11 +08:00
|
|
|
|
protected virtual float StripWidth => TabContainer.Sum(c => c.IsPresent ? c.DrawWidth + TabContainer.Spacing.X : 0) - TabContainer.Spacing.X;
|
2020-01-02 13:19:31 +08:00
|
|
|
|
|
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;
|
2019-08-12 17:33:01 +08:00
|
|
|
|
|
2017-06-09 19:04:29 +08:00
|
|
|
|
private static bool isEnumType => typeof(T).IsEnum;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-03-16 15:35:44 +08:00
|
|
|
|
public OsuTabControl()
|
2017-03-08 17:19:00 +08:00
|
|
|
|
{
|
2020-07-09 12:47:11 +08:00
|
|
|
|
TabContainer.Spacing = new Vector2(HORIZONTAL_SPACING, 0f);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-07-31 16:26:54 +08:00
|
|
|
|
AddInternal(strip = new Box
|
2018-03-30 02:29:45 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
2020-01-03 14:35:18 +08:00
|
|
|
|
Height = 1,
|
2018-03-30 02:29:45 +08:00
|
|
|
|
Colour = Color4.White.Opacity(0),
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-08-12 23:14:37 +08:00
|
|
|
|
if (isEnumType && AddEnumEntriesAutomatically)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2017-05-11 22:10:19 +08:00
|
|
|
|
foreach (var val in (T[])Enum.GetValues(typeof(T)))
|
|
|
|
|
AddItem(val);
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
2017-03-08 17:19:00 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-03-16 08:52:31 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
2020-01-26 21:49:39 +08:00
|
|
|
|
if (accentColour == default)
|
2017-03-16 08:52:31 +08:00
|
|
|
|
AccentColour = colours.Blue;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-03-30 02:29:45 +08:00
|
|
|
|
public Color4 StripColour
|
|
|
|
|
{
|
|
|
|
|
get => strip.Colour;
|
|
|
|
|
set => strip.Colour = value;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-03-30 02:29:45 +08:00
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-03-30 02:29:45 +08:00
|
|
|
|
// 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-03-30 02:29:45 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
|
public partial class OsuTabItem : TabItem<T>, IHasAccentColour
|
2017-03-23 07:44:52 +08:00
|
|
|
|
{
|
2017-05-17 16:58:34 +08:00
|
|
|
|
protected readonly SpriteText Text;
|
2017-09-27 21:01:53 +08:00
|
|
|
|
protected readonly Box Bar;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
|
private Color4 accentColour;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2017-03-23 07:44:52 +08:00
|
|
|
|
public Color4 AccentColour
|
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => accentColour;
|
2017-03-23 07:44:52 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
accentColour = value;
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (!Active.Value)
|
2017-05-17 16:58:34 +08:00
|
|
|
|
Text.Colour = value;
|
2017-03-23 07:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2024-03-07 13:20:20 +08:00
|
|
|
|
protected const float TRANSITION_LENGTH = 500;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2024-03-07 13:20:20 +08:00
|
|
|
|
protected virtual void FadeHovered()
|
2017-03-23 07:44:52 +08:00
|
|
|
|
{
|
2024-03-07 13:20:20 +08:00
|
|
|
|
Bar.FadeIn(TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
|
Text.FadeColour(Color4.White, TRANSITION_LENGTH, Easing.OutQuint);
|
2017-03-23 07:44:52 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2024-03-07 13:20:20 +08:00
|
|
|
|
protected virtual void FadeUnhovered()
|
2017-03-23 07:44:52 +08:00
|
|
|
|
{
|
2024-03-07 13:20:20 +08:00
|
|
|
|
Bar.FadeTo(IsHovered ? 1 : 0, TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
|
Text.FadeColour(IsHovered ? Color4.White : AccentColour, TRANSITION_LENGTH, Easing.OutQuint);
|
2017-03-23 07:44:52 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2017-03-23 07:44:52 +08:00
|
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (!Active.Value)
|
2020-03-26 21:19:36 +08:00
|
|
|
|
FadeHovered();
|
2017-03-23 07:44:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2017-03-23 07:44:52 +08:00
|
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (!Active.Value)
|
2020-03-26 21:19:36 +08:00
|
|
|
|
FadeUnhovered();
|
2017-03-23 07:44:52 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-02-28 12:31:40 +08:00
|
|
|
|
public OsuTabItem(T value)
|
|
|
|
|
: base(value)
|
2017-03-23 07:44:52 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.X;
|
|
|
|
|
RelativeSizeAxes = Axes.Y;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-07-17 21:16:57 +08:00
|
|
|
|
LocalisableString text;
|
2021-07-17 21:37:58 +08:00
|
|
|
|
|
2021-07-17 21:16:57 +08:00
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case IHasDescription hasDescription:
|
|
|
|
|
text = hasDescription.GetDescription();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Enum e:
|
|
|
|
|
text = e.GetLocalisableDescription();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LocalisableString l:
|
|
|
|
|
text = l;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
text = value.ToString();
|
|
|
|
|
break;
|
2021-07-17 21:37:58 +08:00
|
|
|
|
}
|
2021-07-17 21:16:57 +08:00
|
|
|
|
|
2017-03-23 07:44:52 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-05-17 16:58:34 +08:00
|
|
|
|
Text = new OsuSpriteText
|
2017-04-03 17:35:33 +08:00
|
|
|
|
{
|
|
|
|
|
Margin = new MarginPadding { Top = 5, Bottom = 5 },
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
2021-07-17 21:16:57 +08:00
|
|
|
|
Text = text,
|
2019-02-12 12:04:46 +08:00
|
|
|
|
Font = OsuFont.GetFont(size: 14)
|
2017-04-03 17:35:33 +08:00
|
|
|
|
},
|
2017-09-27 21:01:53 +08:00
|
|
|
|
Bar = new Box
|
2017-04-03 17:35:33 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
Height = 1,
|
|
|
|
|
Alpha = 0,
|
|
|
|
|
Colour = Color4.White,
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
2017-11-26 01:41:18 +08:00
|
|
|
|
},
|
2024-05-22 16:29:39 +08:00
|
|
|
|
new HoverSounds(HoverSampleSet.TabSelect)
|
2017-03-23 07:44:52 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2024-05-22 16:29:39 +08:00
|
|
|
|
private Sample selectSample;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours, AudioManager audio)
|
|
|
|
|
{
|
|
|
|
|
if (accentColour == default)
|
|
|
|
|
AccentColour = colours.Blue;
|
|
|
|
|
|
|
|
|
|
selectSample = audio.Samples.Get(@"UI/tabselect-select");
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 21:19:36 +08:00
|
|
|
|
protected override void OnActivated()
|
|
|
|
|
{
|
|
|
|
|
Text.Font = Text.Font.With(weight: FontWeight.Bold);
|
|
|
|
|
FadeHovered();
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-03-26 21:19:36 +08:00
|
|
|
|
protected override void OnDeactivated()
|
|
|
|
|
{
|
|
|
|
|
Text.Font = Text.Font.With(weight: FontWeight.Medium);
|
|
|
|
|
FadeUnhovered();
|
|
|
|
|
}
|
2024-05-22 16:29:39 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnActivatedByUser() => selectSample.Play();
|
2017-03-23 07:44:52 +08:00
|
|
|
|
}
|
2017-03-05 12:07:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|