osu/osu.Game/Overlays/Changelog/Streams/StreamBadge.cs

141 lines
4.6 KiB
C#
Raw Normal View History

2018-07-17 23:35:06 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK.Graphics;
2018-07-18 17:32:15 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2018-07-17 23:35:06 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using System;
namespace osu.Game.Overlays.Changelog.Streams
{
2018-07-18 01:26:08 +00:00
public class StreamBadge : ClickableContainer
2018-07-17 23:35:06 +00:00
{
2018-07-18 17:32:15 +00:00
private const float badge_height = 56.5f;
private const float badge_width = 100;
2018-07-17 23:35:06 +00:00
private const float transition_duration = 100;
public Action OnActivation;
2018-07-18 17:32:15 +00:00
private bool isActivated;
2018-07-17 23:35:06 +00:00
2018-07-18 17:32:15 +00:00
private readonly Header.LineBadge lineBadge;
private SampleChannel sampleHover;
public readonly string Name;
public readonly string DisplayVersion;
public readonly bool IsFeatured;
public readonly float Users;
2018-07-17 23:35:06 +00:00
public StreamBadge(ColourInfo colour, string streamName, string streamBuild, float onlineUsers = 0, bool isFeatured = false)
{
Name = streamName;
DisplayVersion = streamBuild;
IsFeatured = isFeatured;
Users = onlineUsers;
2018-07-18 17:32:15 +00:00
Height = badge_height;
Width = isFeatured ? badge_width * 2 : badge_width;
2018-07-17 23:35:06 +00:00
Margin = new MarginPadding(5);
2018-07-18 17:32:15 +00:00
isActivated = true;
2018-07-17 23:35:06 +00:00
Children = new Drawable[]
{
new FillFlowContainer<SpriteText>
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new[]
{
new SpriteText
{
Text = streamName,
Font = @"Exo2.0-Bold",
TextSize = 16,
Margin = new MarginPadding
{
2018-07-18 01:26:08 +00:00
Top = 7,
2018-07-17 23:35:06 +00:00
}
},
new SpriteText
{
Text = streamBuild,
Font = @"Exo2.0-Light",
TextSize = 21,
},
new SpriteText
{
Text = onlineUsers > 0 ?
string.Join(" ", onlineUsers.ToString("N0"), "users online"):
null,
TextSize = 12,
Font = @"Exo2.0-Regular",
Colour = new Color4(203, 164, 218, 255),
},
}
},
lineBadge = new Header.LineBadge(false, 2, 4)
{
Anchor = Anchor.TopCentre,
2018-07-18 01:26:08 +00:00
Origin = Anchor.TopCentre,
2018-07-17 23:35:06 +00:00
Width = 1,
Colour = colour,
RelativeSizeAxes = Axes.X,
2018-07-18 17:32:15 +00:00
TransitionDuration = 600,
2018-07-17 23:35:06 +00:00
},
};
}
public void Activate(bool withoutHeaderUpdate = false)
{
2018-07-18 17:32:15 +00:00
isActivated = true;
2018-07-17 23:35:06 +00:00
this.FadeIn(transition_duration);
lineBadge.IsCollapsed = false;
if (!withoutHeaderUpdate) OnActivation?.Invoke();
}
public void Deactivate()
{
2018-07-18 17:32:15 +00:00
isActivated = false;
if (!IsHovered)
{
this.FadeTo(0.5f, transition_duration);
lineBadge.IsCollapsed = true;
}
2018-07-17 23:35:06 +00:00
}
protected override bool OnClick(InputState state)
{
Activate();
return base.OnClick(state);
}
protected override bool OnHover(InputState state)
{
2018-07-18 17:32:15 +00:00
if (!isActivated) sampleHover?.Play();
2018-07-17 23:35:06 +00:00
this.FadeIn(transition_duration);
lineBadge.IsCollapsed = false;
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
2018-07-18 17:32:15 +00:00
if (!isActivated)
2018-07-17 23:35:06 +00:00
{
this.FadeTo(0.5f, transition_duration);
lineBadge.IsCollapsed = true;
}
base.OnHoverLost(state);
}
2018-07-18 17:32:15 +00:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleHover = audio.Sample.Get(@"UI/generic-hover-soft");
}
2018-07-17 23:35:06 +00:00
}
}