1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 04:47:24 +08:00
osu-lazer/osu.Game/Overlays/OverlaySortTabControl.cs
2020-07-13 01:22:05 +03:00

179 lines
5.5 KiB
C#

// 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.Framework.Graphics;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osuTK;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Bindables;
using osu.Framework.Allocation;
using osu.Game.Graphics.Sprites;
using osuTK.Graphics;
using osu.Game.Overlays.Comments;
using JetBrains.Annotations;
using System;
using osu.Framework.Extensions;
namespace osu.Game.Overlays
{
public class OverlaySortTabControl<T> : CompositeDrawable, IHasCurrentValue<T>
{
private readonly BindableWithCurrent<T> current = new BindableWithCurrent<T>();
public Bindable<T> Current
{
get => current.Current;
set => current.Current = value;
}
public string Title
{
get => text.Text;
set => text.Text = value;
}
private readonly OsuSpriteText text;
public OverlaySortTabControl()
{
AutoSizeAxes = Axes.Both;
AddInternal(new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10, 0),
Children = new Drawable[]
{
text = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold),
Text = @"Sort by"
},
CreateControl().With(c =>
{
c.Anchor = Anchor.CentreLeft;
c.Origin = Anchor.CentreLeft;
c.Current = current;
})
}
});
}
[NotNull]
protected virtual SortTabControl CreateControl() => new SortTabControl();
protected class SortTabControl : OsuTabControl<T>
{
protected override Dropdown<T> CreateDropdown() => null;
protected override TabItem<T> CreateTabItem(T value) => new SortTabItem(value);
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 0),
};
public SortTabControl()
{
AutoSizeAxes = Axes.Both;
}
}
protected class SortTabItem : TabItem<T>
{
public SortTabItem(T value)
: base(value)
{
AutoSizeAxes = Axes.Both;
Child = CreateTabButton(value);
}
[NotNull]
protected virtual TabButton CreateTabButton(T value) => new TabButton(value)
{
Active = { BindTarget = Active }
};
protected override void OnActivated()
{
}
protected override void OnDeactivated()
{
}
}
protected class TabButton : HeaderButton
{
public readonly BindableBool Active = new BindableBool();
protected override Container<Drawable> Content => content;
protected virtual Color4 ContentColour
{
set => text.Colour = value;
}
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
private readonly SpriteText text;
private readonly FillFlowContainer content;
public TabButton(T value)
{
base.Content.Add(content = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(3, 0),
Children = new Drawable[]
{
text = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold),
Text = (value as Enum)?.GetDescription() ?? value.ToString()
}
}
});
}
protected override void LoadComplete()
{
base.LoadComplete();
Active.BindValueChanged(_ => UpdateState(), true);
}
protected override bool OnHover(HoverEvent e)
{
UpdateState();
return true;
}
protected override void OnHoverLost(HoverLostEvent e) => UpdateState();
protected virtual void UpdateState()
{
if (Active.Value || IsHovered)
ShowBackground();
else
HideBackground();
ContentColour = Active.Value && !IsHovered ? colourProvider.Light1 : Color4.White;
text.Font = text.Font.With(weight: Active.Value ? FontWeight.Bold : FontWeight.SemiBold);
}
}
}
}