1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 12:07:23 +08:00
osu-lazer/osu.Game/Screens/Play/ReplayDownloadButton.cs

175 lines
6.0 KiB
C#
Raw Normal View History

// 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.Allocation;
2019-06-29 13:25:30 +08:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics;
using osu.Game.Graphics;
2019-06-29 13:25:30 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Online;
using osu.Game.Scoring;
2019-06-29 13:25:30 +08:00
using osuTK;
using osu.Framework.Graphics.Sprites;
using osu.Game.Online.API.Requests.Responses;
using osu.Framework.Graphics.Effects;
using osuTK.Graphics;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics.Containers;
namespace osu.Game.Screens.Play
{
2019-06-29 13:25:30 +08:00
public class ReplayDownloadButton : DownloadTrackingComposite<ScoreInfo, ScoreManager>, IHasTooltip
{
[Resolved]
private ScoreManager scores { get; set; }
2019-06-29 13:25:30 +08:00
private OsuClickableContainer button;
private SpriteIcon downloadIcon;
private SpriteIcon playIcon;
private ShakeContainer shakeContainer;
private CircularContainer circle;
2019-06-29 13:25:30 +08:00
public string TooltipText
{
2019-06-29 13:25:30 +08:00
get
{
2019-06-30 13:26:20 +08:00
switch (replayAvailability)
{
case ReplayAvailability.Local:
return @"Watch replay";
2019-06-29 13:25:30 +08:00
case ReplayAvailability.Online:
return @"Download replay";
2019-06-29 13:25:30 +08:00
default:
return @"Replay unavailable";
}
2019-06-29 13:25:30 +08:00
}
}
2019-06-30 13:26:20 +08:00
private ReplayAvailability replayAvailability
{
get
{
if (scores.IsAvailableLocally(Model.Value))
return ReplayAvailability.Local;
if (Model.Value is APILegacyScoreInfo apiScore && apiScore.Replay)
return ReplayAvailability.Online;
return ReplayAvailability.NotAvailable;
}
}
2019-06-29 13:25:30 +08:00
public ReplayDownloadButton(ScoreInfo score)
: base(score)
{
AutoSizeAxes = Axes.Both;
}
2019-06-29 13:25:30 +08:00
[BackgroundDependencyLoader(true)]
2019-06-30 13:26:20 +08:00
private void load(OsuGame game, OsuColour colours)
{
2019-06-29 13:25:30 +08:00
InternalChild = shakeContainer = new ShakeContainer
{
AutoSizeAxes = Axes.Both,
Child = circle = new CircularContainer
2019-06-29 13:25:30 +08:00
{
Masking = true,
2019-06-30 13:26:20 +08:00
Size = new Vector2(40),
EdgeEffect = new EdgeEffectParameters
2019-06-29 13:25:30 +08:00
{
Colour = Color4.Black.Opacity(0.4f),
Type = EdgeEffectType.Shadow,
Radius = 5,
},
Child = button = new OsuClickableContainer
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2019-06-29 13:25:30 +08:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.GrayF,
},
playIcon = new SpriteIcon
{
Icon = FontAwesome.Solid.Play,
Size = Vector2.Zero,
Colour = colours.Gray3,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
downloadIcon = new SpriteIcon
{
Icon = FontAwesome.Solid.FileDownload,
Size = Vector2.Zero,
Colour = colours.Gray3,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
2019-06-29 13:25:30 +08:00
},
}
},
2019-06-29 13:25:30 +08:00
};
2019-06-29 13:25:30 +08:00
button.Action = () =>
{
2019-06-29 13:25:30 +08:00
switch (State.Value)
{
case DownloadState.LocallyAvailable:
game?.PresentScore(Model.Value);
2019-06-29 13:25:30 +08:00
break;
case DownloadState.NotDownloaded:
scores.Download(Model.Value);
break;
case DownloadState.Downloading:
shakeContainer.Shake();
break;
}
};
2019-06-29 13:25:30 +08:00
State.BindValueChanged(state =>
{
switch (state.NewValue)
{
case DownloadState.Downloading:
2019-06-30 13:26:20 +08:00
playIcon.ResizeTo(Vector2.Zero, 400, Easing.OutQuint);
downloadIcon.ResizeTo(13, 400, Easing.OutQuint);
circle.FadeEdgeEffectTo(colours.Yellow, 400, Easing.OutQuint);
2019-06-29 13:25:30 +08:00
break;
case DownloadState.LocallyAvailable:
2019-06-30 13:26:20 +08:00
playIcon.ResizeTo(13, 400, Easing.OutQuint);
downloadIcon.ResizeTo(Vector2.Zero, 400, Easing.OutQuint);
circle.FadeEdgeEffectTo(Color4.Black.Opacity(0.4f), 400, Easing.OutQuint);
2019-06-29 13:25:30 +08:00
break;
case DownloadState.NotDownloaded:
2019-06-30 13:26:20 +08:00
playIcon.ResizeTo(Vector2.Zero, 400, Easing.OutQuint);
downloadIcon.ResizeTo(13, 400, Easing.OutQuint);
circle.FadeEdgeEffectTo(Color4.Black.Opacity(0.4f), 400, Easing.OutQuint);
2019-06-29 13:25:30 +08:00
break;
}
}, true);
2019-06-30 13:26:20 +08:00
if (replayAvailability == ReplayAvailability.NotAvailable)
{
button.Enabled.Value = false;
button.Alpha = 0.6f;
}
}
private enum ReplayAvailability
{
Local,
Online,
NotAvailable,
}
}
}