1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-30 02:47:25 +08:00
osu-lazer/osu.Game/Overlays/OverlayUpdateStreamItem.cs
2020-03-03 18:08:51 +03:00

147 lines
4.6 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;
using osu.Framework.Input.Events;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
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.Graphics;
namespace osu.Game.Overlays
{
public abstract class OverlayUpdateStreamItem<T> : TabItem<T>
{
private const float transition_duration = 100;
private const float tab_width = 100;
public readonly Bindable<T> SelectedItem = new Bindable<T>();
private bool userHoveringArea;
public bool UserHoveringArea
{
set
{
if (value == userHoveringArea)
return;
userHoveringArea = value;
updateState();
}
}
private FillFlowContainer<SpriteText> text;
private ExpandingBar expandingBar;
protected OverlayUpdateStreamItem(T value)
: base(value)
{
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
Size = new Vector2(GetWidth(), 60);
Padding = new MarginPadding(5);
AddRange(new Drawable[]
{
text = new FillFlowContainer<SpriteText>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Margin = new MarginPadding { Top = 6 },
Children = new[]
{
new OsuSpriteText
{
Text = GetMainText(),
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Black),
},
new OsuSpriteText
{
Text = GetAdditionalText(),
Font = OsuFont.GetFont(size: 16, weight: FontWeight.Regular),
},
new OsuSpriteText
{
Text = GetInfoText(),
Font = OsuFont.GetFont(size: 10),
Colour = colourProvider.Foreground1
},
}
},
expandingBar = new ExpandingBar
{
Anchor = Anchor.TopCentre,
Colour = GetBarColour(),
ExpandedSize = 4,
CollapsedSize = 2,
Expanded = true
},
new HoverClickSounds()
});
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 OnDeactivated() => updateState();
protected override bool OnHover(HoverEvent e)
{
updateState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
updateState();
base.OnHoverLost(e);
}
private void updateState()
{
// highlighted regardless if we are hovered
bool textHighlighted = IsHovered;
bool barExpanded = IsHovered;
if (SelectedItem.Value == null)
{
// at listing, all badges are highlighted when user is not hovering any badge.
textHighlighted |= !userHoveringArea;
barExpanded |= !userHoveringArea;
}
else
{
// bar is always expanded when active
barExpanded |= Active.Value;
// text is highlighted only when hovered or active (but not if in selection mode)
textHighlighted |= Active.Value && !userHoveringArea;
}
expandingBar.Expanded = barExpanded;
text.FadeTo(textHighlighted ? 1 : 0.5f, transition_duration, Easing.OutQuint);
}
}
}