1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 21:07:33 +08:00
osu-lazer/osu.Game/Overlays/Direct/PlayButton.cs

165 lines
4.6 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08: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 osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Game.Audio;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
2018-06-22 13:27:36 +08:00
using OpenTK;
using OpenTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Direct
{
public class PlayButton : Container
{
2018-06-22 11:12:59 +08:00
public readonly BindableBool Playing = new BindableBool();
2018-05-25 05:37:53 +08:00
public PreviewTrack Preview { get; private set; }
2018-04-13 17:19:50 +08:00
private BeatmapSetInfo beatmapSet;
public BeatmapSetInfo BeatmapSet
{
get { return beatmapSet; }
set
{
if (value == beatmapSet) return;
beatmapSet = value;
2018-06-22 11:12:59 +08:00
Preview?.Stop();
Preview?.Expire();
Preview = null;
2018-06-02 02:06:37 +08:00
Playing.Value = false;
2018-06-02 02:06:37 +08:00
}
}
2018-04-13 17:19:50 +08:00
private Color4 hoverColour;
private readonly SpriteIcon icon;
private readonly LoadingAnimation loadingAnimation;
private const float transition_duration = 500;
private bool loading
{
set
{
if (value)
2018-06-22 13:27:36 +08:00
{
icon.FadeTo(0.5f, transition_duration, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
loadingAnimation.Show();
2018-06-22 13:27:36 +08:00
}
2018-04-13 17:19:50 +08:00
else
2018-06-22 13:27:36 +08:00
{
icon.FadeTo(1, transition_duration, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
loadingAnimation.Hide();
2018-06-22 13:27:36 +08:00
}
2018-04-13 17:19:50 +08:00
}
}
public PlayButton(BeatmapSetInfo setInfo = null)
{
BeatmapSet = setInfo;
AddRange(new Drawable[]
{
icon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fit,
RelativeSizeAxes = Axes.Both,
Icon = FontAwesome.fa_play,
},
2018-06-22 13:27:36 +08:00
loadingAnimation = new LoadingAnimation
{
Size = new Vector2(15),
},
2018-04-13 17:19:50 +08:00
});
Playing.ValueChanged += playingStateChanged;
2018-04-13 17:19:50 +08:00
}
private PreviewTrackManager previewTrackManager;
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colour, PreviewTrackManager previewTrackManager)
2018-04-13 17:19:50 +08:00
{
this.previewTrackManager = previewTrackManager;
2018-04-13 17:19:50 +08:00
hoverColour = colour.Yellow;
}
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
2018-06-22 11:12:59 +08:00
Playing.Toggle();
2018-04-13 17:19:50 +08:00
return true;
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
icon.FadeColour(hoverColour, 120, Easing.InOutQuint);
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
if (!Playing.Value)
icon.FadeColour(Color4.White, 120, Easing.InOutQuint);
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
private void playingStateChanged(bool playing)
2018-04-13 17:19:50 +08:00
{
2018-05-31 23:09:19 +08:00
icon.Icon = playing ? FontAwesome.fa_stop : FontAwesome.fa_play;
icon.FadeColour(playing || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint);
2018-06-22 11:12:59 +08:00
if (playing)
2018-04-13 17:19:50 +08:00
{
2018-06-22 11:12:59 +08:00
if (BeatmapSet == null)
{
Playing.Value = false;
return;
}
if (Preview != null)
{
Preview.Start();
return;
}
loading = true;
LoadComponentAsync(Preview = previewTrackManager.Get(beatmapSet), preview =>
{
// beatmapset may have changed.
if (Preview != preview)
return;
AddInternal(preview);
loading = false;
preview.Stopped += () => Playing.Value = false;
// user may have changed their mind.
if (Playing)
preview.Start();
});
}
else
{
Preview?.Stop();
2018-04-13 17:19:50 +08:00
loading = false;
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Playing.Value = false;
}
}
}