2017-09-18 04:39:34 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using osu.Framework.Allocation;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Track;
|
2017-09-18 04:39:34 +08:00
|
|
|
|
using osu.Framework.Configuration;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Input;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2017-09-18 04:39:34 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2017-09-30 05:08:30 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2017-09-18 04:39:34 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Direct
|
|
|
|
|
{
|
|
|
|
|
public class PlayButton : Container
|
|
|
|
|
{
|
2017-10-07 03:00:23 +08:00
|
|
|
|
public readonly Bindable<bool> Playing = new Bindable<bool>();
|
|
|
|
|
public Track Preview { get; private set; }
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
private BeatmapSetInfo beatmapSet;
|
|
|
|
|
public BeatmapSetInfo BeatmapSet
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
get { return beatmapSet; }
|
2017-10-07 03:00:23 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
if (value == beatmapSet) return;
|
|
|
|
|
beatmapSet = value;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
|
|
|
|
|
Playing.Value = false;
|
2017-10-13 13:06:34 +08:00
|
|
|
|
trackLoader = null;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
Preview = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-18 04:39:34 +08:00
|
|
|
|
|
|
|
|
|
private Color4 hoverColour;
|
|
|
|
|
private readonly SpriteIcon icon;
|
2017-09-30 05:08:30 +08:00
|
|
|
|
private readonly LoadingAnimation loadingAnimation;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
|
2017-09-30 05:08:30 +08:00
|
|
|
|
private const float transition_duration = 500;
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
private bool loading
|
2017-09-30 05:08:30 +08:00
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
loadingAnimation.Show();
|
|
|
|
|
icon.FadeOut(transition_duration * 5, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
loadingAnimation.Hide();
|
|
|
|
|
icon.FadeIn(transition_duration, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-18 04:39:34 +08:00
|
|
|
|
|
2017-10-07 03:00:23 +08:00
|
|
|
|
public PlayButton(BeatmapSetInfo setInfo = null)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
BeatmapSet = setInfo;
|
2017-09-30 05:08:30 +08:00
|
|
|
|
AddRange(new Drawable[]
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
2017-09-30 05:08:30 +08:00
|
|
|
|
icon = new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
FillMode = FillMode.Fit,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Icon = FontAwesome.fa_play,
|
|
|
|
|
},
|
|
|
|
|
loadingAnimation = new LoadingAnimation(),
|
2017-09-18 04:39:34 +08:00
|
|
|
|
});
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
Playing.ValueChanged += playing =>
|
|
|
|
|
{
|
|
|
|
|
icon.Icon = playing ? FontAwesome.fa_pause : FontAwesome.fa_play;
|
|
|
|
|
icon.FadeColour(playing || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint);
|
|
|
|
|
updatePreviewTrack(playing);
|
|
|
|
|
};
|
2017-09-18 04:39:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2017-09-30 05:08:30 +08:00
|
|
|
|
private void load(OsuColour colour)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
|
|
|
|
hoverColour = colour.Yellow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnClick(InputState state)
|
|
|
|
|
{
|
2017-10-07 03:00:23 +08:00
|
|
|
|
Playing.Value = !Playing.Value;
|
2017-09-18 04:39:34 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
|
{
|
|
|
|
|
icon.FadeColour(hoverColour, 120, Easing.InOutQuint);
|
|
|
|
|
return base.OnHover(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
if (!Playing.Value)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
icon.FadeColour(Color4.White, 120, Easing.InOutQuint);
|
|
|
|
|
base.OnHoverLost(state);
|
|
|
|
|
}
|
2017-10-07 03:00:23 +08:00
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
if (Preview?.HasCompleted ?? false)
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
|
|
|
|
Playing.Value = false;
|
|
|
|
|
Preview = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
private void updatePreviewTrack(bool playing)
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
if (playing)
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
|
|
|
|
if (Preview == null)
|
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
beginAudioLoad();
|
|
|
|
|
return;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
}
|
2017-10-13 13:06:34 +08:00
|
|
|
|
|
|
|
|
|
Preview.Seek(0);
|
|
|
|
|
Preview.Start();
|
2017-10-07 03:00:23 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Preview?.Stop();
|
2017-10-14 09:53:39 +08:00
|
|
|
|
loading = false;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
private TrackLoader trackLoader;
|
|
|
|
|
|
|
|
|
|
private void beginAudioLoad()
|
|
|
|
|
{
|
|
|
|
|
if (trackLoader != null) return;
|
|
|
|
|
|
2017-10-14 02:44:05 +08:00
|
|
|
|
loading = true;
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
Add(new AsyncLoadWrapper(trackLoader = new TrackLoader($"https://b.ppy.sh/preview/{BeatmapSet.OnlineBeatmapSetID}.mp3")
|
|
|
|
|
{
|
|
|
|
|
OnLoadComplete = d =>
|
|
|
|
|
{
|
|
|
|
|
// we may have been replaced by another loader
|
|
|
|
|
if (trackLoader != d) return;
|
|
|
|
|
|
|
|
|
|
Preview = (d as TrackLoader)?.Preview;
|
|
|
|
|
Playing.TriggerChange();
|
2017-10-14 02:44:05 +08:00
|
|
|
|
loading = false;
|
2017-10-13 13:06:34 +08:00
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TrackLoader : Drawable
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly string preview;
|
|
|
|
|
|
|
|
|
|
public Track Preview;
|
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
public TrackLoader(string preview)
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
|
|
|
|
this.preview = preview;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(AudioManager audio)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(preview))
|
|
|
|
|
{
|
|
|
|
|
Preview = audio.Track.Get(preview);
|
|
|
|
|
Preview.Volume.Value = 0.5;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-18 04:39:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|