Apply some renames and refactoring of loading logic

Reduced publicly facing properties where possible. Also fixes a potentially bad state issue when the beatmapset was changed while a load was in progress.
This commit is contained in:
Dean Herbert 2017-10-13 14:06:34 +09:00
parent f3ca6cc387
commit abf5418080
2 changed files with 46 additions and 37 deletions

View File

@ -30,8 +30,8 @@ namespace osu.Game.Overlays.BeatmapSet
public BeatmapSetInfo BeatmapSet public BeatmapSetInfo BeatmapSet
{ {
get { return playButton.SetInfo; } get { return playButton.BeatmapSet; }
set { playButton.SetInfo = value; } set { playButton.BeatmapSet = value; }
} }
public PreviewButton() public PreviewButton()

View File

@ -20,16 +20,17 @@ namespace osu.Game.Overlays.Direct
public readonly Bindable<bool> Playing = new Bindable<bool>(); public readonly Bindable<bool> Playing = new Bindable<bool>();
public Track Preview { get; private set; } public Track Preview { get; private set; }
private BeatmapSetInfo setInfo; private BeatmapSetInfo beatmapSet;
public BeatmapSetInfo SetInfo public BeatmapSetInfo BeatmapSet
{ {
get { return setInfo; } get { return beatmapSet; }
set set
{ {
if (value == setInfo) return; if (value == beatmapSet) return;
setInfo = value; beatmapSet = value;
Playing.Value = false; Playing.Value = false;
trackLoader = null;
Preview = null; Preview = null;
} }
} }
@ -41,13 +42,10 @@ namespace osu.Game.Overlays.Direct
private const float transition_duration = 500; private const float transition_duration = 500;
private bool loading; private bool loading
public bool Loading
{ {
get { return loading; }
set set
{ {
loading = value;
if (value) if (value)
{ {
loadingAnimation.Show(); loadingAnimation.Show();
@ -63,7 +61,7 @@ namespace osu.Game.Overlays.Direct
public PlayButton(BeatmapSetInfo setInfo = null) public PlayButton(BeatmapSetInfo setInfo = null)
{ {
SetInfo = setInfo; BeatmapSet = setInfo;
AddRange(new Drawable[] AddRange(new Drawable[]
{ {
audioWrapper = new Container(), audioWrapper = new Container(),
@ -78,10 +76,12 @@ namespace osu.Game.Overlays.Direct
loadingAnimation = new LoadingAnimation(), loadingAnimation = new LoadingAnimation(),
}); });
Playing.ValueChanged += newValue => icon.Icon = newValue ? FontAwesome.fa_pause : FontAwesome.fa_play; Playing.ValueChanged += playing =>
Playing.ValueChanged += newValue => icon.FadeColour(newValue || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint); {
icon.Icon = playing ? FontAwesome.fa_pause : FontAwesome.fa_play;
Playing.ValueChanged += updatePreviewTrack; icon.FadeColour(playing || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint);
updatePreviewTrack(playing);
};
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -104,7 +104,7 @@ namespace osu.Game.Overlays.Direct
protected override void OnHoverLost(InputState state) protected override void OnHoverLost(InputState state)
{ {
if(!Playing.Value) if (!Playing.Value)
icon.FadeColour(Color4.White, 120, Easing.InOutQuint); icon.FadeColour(Color4.White, 120, Easing.InOutQuint);
base.OnHoverLost(state); base.OnHoverLost(state);
} }
@ -113,49 +113,58 @@ namespace osu.Game.Overlays.Direct
{ {
base.Update(); base.Update();
if(Preview?.HasCompleted ?? false) if (Preview?.HasCompleted ?? false)
{ {
Playing.Value = false; Playing.Value = false;
Preview = null; Preview = null;
} }
} }
private void updatePreviewTrack(bool newValue) private void updatePreviewTrack(bool playing)
{ {
if (newValue) if (playing)
{ {
if (Preview == null) if (Preview == null)
{ {
Loading = true; beginAudioLoad();
audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper("https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3") return;
{
OnLoadComplete = d =>
{
Loading = false;
Preview = (d as AudioLoadWrapper)?.Preview;
Playing.TriggerChange();
},
});
} }
else
{
Preview.Seek(0); Preview.Seek(0);
Preview.Start(); Preview.Start();
} }
}
else else
{ {
Preview?.Stop(); Preview?.Stop();
} }
} }
private class AudioLoadWrapper : Drawable private TrackLoader trackLoader;
private void beginAudioLoad()
{
if (trackLoader != null) return;
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();
},
}));
}
private class TrackLoader : Drawable
{ {
private readonly string preview; private readonly string preview;
public Track Preview; public Track Preview;
public AudioLoadWrapper(string preview) public TrackLoader(string preview)
{ {
this.preview = preview; this.preview = preview;
} }