1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-15 15:23:14 +08:00

Apply some code review

This commit is contained in:
Dean Herbert 2018-06-22 12:12:59 +09:00
parent de10480e95
commit 4b2b1f51f9
2 changed files with 44 additions and 64 deletions

View File

@ -22,15 +22,12 @@ namespace osu.Game.Audio
public event Action Started; public event Action Started;
private Track track; private Track track;
private bool wasPlaying; private bool hasStarted;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
track = GetTrack(); track = GetTrack();
if (track != null)
track.Looping = false;
} }
/// <summary> /// <summary>
@ -58,7 +55,7 @@ namespace osu.Game.Audio
base.Update(); base.Update();
// Todo: Track currently doesn't signal its completion, so we have to handle it manually // Todo: Track currently doesn't signal its completion, so we have to handle it manually
if (track != null && wasPlaying && track.HasCompleted) if (hasStarted && track.HasCompleted)
Stop(); Stop();
} }
@ -69,15 +66,12 @@ namespace osu.Game.Audio
/// </summary> /// </summary>
public void Start() => startDelegate = Schedule(() => public void Start() => startDelegate = Schedule(() =>
{ {
if (!IsLoaded)
return;
if (track == null) if (track == null)
return; return;
if (wasPlaying) if (hasStarted)
return; return;
wasPlaying = true; hasStarted = true;
track.Restart(); track.Restart();
Started?.Invoke(); Started?.Invoke();
@ -90,15 +84,12 @@ namespace osu.Game.Audio
{ {
startDelegate?.Cancel(); startDelegate?.Cancel();
if (!IsLoaded)
return;
if (track == null) if (track == null)
return; return;
if (!wasPlaying) if (!hasStarted)
return; return;
wasPlaying = false; hasStarted = false;
track.Stop(); track.Stop();
Stopped?.Invoke(); Stopped?.Invoke();

View File

@ -16,7 +16,7 @@ namespace osu.Game.Overlays.Direct
{ {
public class PlayButton : Container public class PlayButton : Container
{ {
public readonly Bindable<bool> Playing = new Bindable<bool>(); public readonly BindableBool Playing = new BindableBool();
public PreviewTrack Preview { get; private set; } public PreviewTrack Preview { get; private set; }
private BeatmapSetInfo beatmapSet; private BeatmapSetInfo beatmapSet;
@ -29,12 +29,9 @@ namespace osu.Game.Overlays.Direct
if (value == beatmapSet) return; if (value == beatmapSet) return;
beatmapSet = value; beatmapSet = value;
if (Preview != null) Preview?.Stop();
{ Preview?.Expire();
Preview.Stop(); Preview = null;
RemoveInternal(Preview);
Preview = null;
}
Playing.Value = false; Playing.Value = false;
} }
@ -51,15 +48,9 @@ namespace osu.Game.Overlays.Direct
set set
{ {
if (value) if (value)
{
loadingAnimation.Show(); loadingAnimation.Show();
icon.FadeOut(transition_duration * 5, Easing.OutQuint);
}
else else
{
loadingAnimation.Hide(); loadingAnimation.Hide();
icon.FadeIn(transition_duration, Easing.OutQuint);
}
} }
} }
@ -94,35 +85,7 @@ namespace osu.Game.Overlays.Direct
protected override bool OnClick(InputState state) protected override bool OnClick(InputState state)
{ {
if (!Playing.Value) Playing.Toggle();
{
if (Preview != null)
{
Preview.Start();
return true;
}
loading = true;
var loadingPreview = previewTrackManager.Get(beatmapSet);
loadingPreview.Started += () => Playing.Value = true;
loadingPreview.Stopped += () => Playing.Value = false;
LoadComponentAsync(Preview = loadingPreview, t =>
{
if (Preview != loadingPreview)
return;
AddInternal(t);
Preview.Start();
loading = false;
});
}
else
Preview?.Stop();
return true; return true;
} }
@ -141,17 +104,43 @@ namespace osu.Game.Overlays.Direct
private void playingStateChanged(bool playing) private void playingStateChanged(bool playing)
{ {
if (playing && BeatmapSet == null)
{
Playing.Value = false;
return;
}
icon.Icon = playing ? FontAwesome.fa_stop : FontAwesome.fa_play; icon.Icon = playing ? FontAwesome.fa_stop : FontAwesome.fa_play;
icon.FadeColour(playing || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint); icon.FadeColour(playing || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint);
if (!playing) if (playing)
{ {
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();
loading = false; loading = false;
} }
} }