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

150 lines
5.0 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.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
2018-07-19 17:07:24 +00:00
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
2018-07-19 17:07:24 +00:00
using osu.Game.Online.API.Requests.Responses;
2018-07-17 23:35:06 +00:00
using System;
2018-07-19 17:07:24 +00:00
namespace osu.Game.Overlays.Changelog
2018-07-17 23:35:06 +00:00
{
2018-07-18 01:26:08 +00:00
public class StreamBadge : ClickableContainer
2018-07-17 23:35:06 +00:00
{
private const float badge_height = 66.5f;
2018-07-18 17:32:15 +00:00
private const float badge_width = 100;
2018-07-17 23:35:06 +00:00
private const float transition_duration = 100;
2018-07-22 17:51:31 +00:00
public delegate void SelectedHandler(StreamBadge source, EventArgs args);
public event SelectedHandler Selected;
2018-07-17 23:35:06 +00:00
2018-07-18 17:32:15 +00:00
private bool isActivated;
2018-07-17 23:35:06 +00:00
private readonly LineBadge lineBadge;
private SampleChannel sampleClick;
2018-07-18 17:32:15 +00:00
private SampleChannel sampleHover;
public readonly APIChangelogBuild LatestBuild;
private readonly FillFlowContainer<SpriteText> text;
2018-07-17 23:35:06 +00:00
public StreamBadge(APIChangelogBuild latestBuild)
2018-07-17 23:35:06 +00:00
{
2018-07-24 02:03:56 +00:00
LatestBuild = latestBuild;
2018-07-18 17:32:15 +00:00
Height = badge_height;
2018-07-24 02:03:56 +00:00
Width = LatestBuild.IsFeatured ? badge_width * 2 : badge_width;
Padding = new MarginPadding(5);
2018-07-18 17:32:15 +00:00
isActivated = true;
2018-07-17 23:35:06 +00:00
Children = new Drawable[]
{
text = new FillFlowContainer<SpriteText>
2018-07-17 23:35:06 +00:00
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new[]
{
new SpriteText
{
2018-07-24 02:03:56 +00:00
Text = LatestBuild.UpdateStream.DisplayName,
2018-07-17 23:35:06 +00:00
Font = @"Exo2.0-Bold",
TextSize = 14, // web: 12,
2018-07-25 15:04:36 +00:00
Margin = new MarginPadding { Top = 6 },
2018-07-17 23:35:06 +00:00
},
new SpriteText
{
2018-07-24 02:03:56 +00:00
Text = LatestBuild.DisplayVersion,
2018-07-17 23:35:06 +00:00
Font = @"Exo2.0-Light",
TextSize = 20, // web: 16,
2018-07-17 23:35:06 +00:00
},
new SpriteText
{
2018-07-24 02:03:56 +00:00
Text = LatestBuild.Users > 0 ?
$"{LatestBuild.Users:N0} users online" :
2018-07-17 23:35:06 +00:00
null,
TextSize = 12, // web: 10,
2018-07-17 23:35:06 +00:00
Font = @"Exo2.0-Regular",
Colour = new Color4(203, 164, 218, 255),
},
}
},
lineBadge = new LineBadge(false)
2018-07-17 23:35:06 +00:00
{
Anchor = Anchor.TopCentre,
2018-07-24 02:03:56 +00:00
Colour = StreamColour.FromStreamName(LatestBuild.UpdateStream.Name),
UncollapsedSize = 4,
CollapsedSize = 2,
2018-07-17 23:35:06 +00:00
},
};
}
/// <param name="withoutFiringUpdates">In case we don't want to
/// fire the <see cref="Selected"/> event.</param>
2018-07-22 20:13:14 +00:00
public void Activate(bool withoutFiringUpdates = true)
2018-07-17 23:35:06 +00:00
{
2018-07-18 17:32:15 +00:00
isActivated = true;
2018-07-17 23:35:06 +00:00
this.FadeIn(transition_duration);
lineBadge.Uncollapse();
2018-07-22 20:31:24 +00:00
if (!withoutFiringUpdates)
Selected?.Invoke(this, EventArgs.Empty);
2018-07-17 23:35:06 +00:00
}
public void Deactivate()
{
2018-07-18 17:32:15 +00:00
isActivated = false;
DisableDim();
2018-07-18 17:32:15 +00:00
if (!IsHovered)
{
this.FadeTo(0.5f, transition_duration);
lineBadge.Collapse(200);
2018-07-18 17:32:15 +00:00
}
2018-07-17 23:35:06 +00:00
}
protected override bool OnClick(ClickEvent e)
2018-07-17 23:35:06 +00:00
{
2018-07-22 20:13:14 +00:00
Activate(false);
sampleClick?.Play();
return base.OnClick(e);
2018-07-17 23:35:06 +00:00
}
protected override bool OnHover(HoverEvent e)
2018-07-17 23:35:06 +00:00
{
sampleHover?.Play();
DisableDim();
2018-07-17 23:35:06 +00:00
this.FadeIn(transition_duration);
lineBadge.Uncollapse();
return base.OnHover(e);
2018-07-17 23:35:06 +00:00
}
protected override void OnHoverLost(HoverLostEvent e)
2018-07-17 23:35:06 +00:00
{
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.Collapse(200);
2018-07-17 23:35:06 +00:00
}
else
EnableDim();
base.OnHoverLost(e);
2018-07-17 23:35:06 +00:00
}
2018-07-18 17:32:15 +00:00
public void EnableDim() => text.FadeTo(0.5f, transition_duration);
public void DisableDim() => text.FadeIn(transition_duration);
2018-07-18 17:32:15 +00:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleClick = audio.Sample.Get(@"UI/generic-select-soft");
2018-07-18 17:32:15 +00:00
sampleHover = audio.Sample.Get(@"UI/generic-hover-soft");
}
2018-07-17 23:35:06 +00:00
}
}