1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-25 03:07:34 +08:00

Centralise download tracking logic so we can show it in multiple scenarios

This commit is contained in:
Dean Herbert 2019-01-17 21:10:34 +09:00
parent e62f1e8851
commit b62630ecd2
4 changed files with 188 additions and 89 deletions

View File

@ -11,6 +11,7 @@ using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.API;
using osu.Game.Overlays.Direct;
using osu.Game.Users;
using osuTK;
using osuTK.Graphics;
@ -28,44 +29,53 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
Width = 120;
BeatmapSetDownloader downloader;
Add(new Container
AddRange(new Drawable[]
{
Depth = -1,
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Horizontal = 10 },
Children = new Drawable[]
new Container
{
downloader = new BeatmapSetDownloader(set, noVideo),
new FillFlowContainer
Depth = -1,
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Horizontal = 10 },
Children = new Drawable[]
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
downloader = new BeatmapSetDownloader(set, noVideo),
new FillFlowContainer
{
new OsuSpriteText
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
Text = "Download",
TextSize = 13,
Font = @"Exo2.0-Bold",
},
new OsuSpriteText
{
Text = set.OnlineInfo.HasVideo && noVideo ? "without Video" : string.Empty,
TextSize = 11,
Font = @"Exo2.0-Bold",
new OsuSpriteText
{
Text = "Download",
TextSize = 13,
Font = @"Exo2.0-Bold",
},
new OsuSpriteText
{
Text = set.OnlineInfo.HasVideo && noVideo ? "without Video" : string.Empty,
TextSize = 11,
Font = @"Exo2.0-Bold",
},
},
},
new SpriteIcon
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Icon = FontAwesome.fa_download,
Size = new Vector2(16),
Margin = new MarginPadding { Right = 5 },
},
},
new SpriteIcon
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Icon = FontAwesome.fa_download,
Size = new Vector2(16),
Margin = new MarginPadding { Right = 5 },
},
},
new DownloadProgressBar(set)
{
Depth = -2,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
},
});

View File

@ -16,8 +16,6 @@ using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API.Requests;
using osuTK;
using osuTK.Graphics;
@ -31,8 +29,6 @@ namespace osu.Game.Overlays.Direct
private Container content;
private ProgressBar progressBar;
private BeatmapManager beatmaps;
private BeatmapSetOverlay beatmapSetOverlay;
public PreviewTrack Preview => PlayButton.Preview;
@ -65,14 +61,10 @@ namespace osu.Game.Overlays.Direct
Colour = Color4.Black.Opacity(0.3f),
};
private OsuColour colours;
[BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapManager beatmaps, OsuColour colours, BeatmapSetOverlay beatmapSetOverlay)
{
this.beatmaps = beatmaps;
this.beatmapSetOverlay = beatmapSetOverlay;
this.colours = colours;
AddInternal(content = new Container
{
@ -82,33 +74,14 @@ namespace osu.Game.Overlays.Direct
Children = new[]
{
CreateBackground(),
progressBar = new ProgressBar
new DownloadProgressBar(SetInfo)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Height = 0,
Alpha = 0,
BackgroundColour = Color4.Black.Opacity(0.7f),
FillColour = colours.Blue,
Depth = -1,
},
}
});
var downloadRequest = beatmaps.GetExistingDownload(SetInfo);
if (downloadRequest != null)
attachDownload(downloadRequest);
beatmaps.BeatmapDownloadBegan += attachDownload;
beatmaps.ItemAdded += setAdded;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmaps.BeatmapDownloadBegan -= attachDownload;
beatmaps.ItemAdded -= setAdded;
}
protected override void Update()
@ -149,37 +122,6 @@ namespace osu.Game.Overlays.Direct
protected void ShowInformation() => beatmapSetOverlay?.ShowBeatmapSet(SetInfo);
private void attachDownload(DownloadBeatmapSetRequest request)
{
if (request.BeatmapSet.OnlineBeatmapSetID != SetInfo.OnlineBeatmapSetID)
return;
progressBar.FadeIn(400, Easing.OutQuint);
progressBar.ResizeHeightTo(4, 400, Easing.OutQuint);
progressBar.Current.Value = 0;
request.Failure += e =>
{
progressBar.Current.Value = 0;
progressBar.FadeOut(500);
};
request.DownloadProgressed += progress => Schedule(() => progressBar.Current.Value = progress);
request.Success += data =>
{
progressBar.Current.Value = 1;
progressBar.FillColour = colours.Yellow;
};
}
private void setAdded(BeatmapSetInfo s, bool existing, bool silent) => Schedule(() =>
{
if (s.OnlineBeatmapSetID == SetInfo.OnlineBeatmapSetID)
progressBar.FadeOut(500);
});
protected override void LoadComplete()
{
base.LoadComplete();

View File

@ -0,0 +1,70 @@
// 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.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osuTK.Graphics;
namespace osu.Game.Overlays.Direct
{
public class DownloadProgressBar : DownloadTrackingComponent
{
private readonly ProgressBar progressBar;
private OsuColour colours;
public DownloadProgressBar(BeatmapSetInfo setInfo)
: base(setInfo)
{
AddInternal(progressBar = new ProgressBar
{
Height = 0,
Alpha = 0,
});
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
}
[BackgroundDependencyLoader(true)]
private void load(OsuColour colours)
{
this.colours = colours;
progressBar.FillColour = colours.Blue;
progressBar.BackgroundColour = Color4.Black.Opacity(0.7f);
}
protected override void DownloadFailed()
{
progressBar.Current.Value = 0;
progressBar.FadeOut(500);
}
protected override void DownloadComleted()
{
progressBar.Current.Value = 1;
progressBar.FillColour = colours.Yellow;
}
protected override void DownloadStarted()
{
progressBar.FadeIn(400, Easing.OutQuint);
progressBar.ResizeHeightTo(4, 400, Easing.OutQuint);
}
protected override void BeatmapImported()
{
progressBar.FadeOut(500);
}
protected override void ProgressChanged(float progress)
{
progressBar.Current.Value = progress;
}
}
}

View File

@ -0,0 +1,77 @@
// Copyright (c) 2007-2019 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.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Online.API.Requests;
namespace osu.Game.Overlays.Direct
{
public abstract class DownloadTrackingComponent : CompositeDrawable
{
private readonly BeatmapSetInfo setInfo;
private BeatmapManager beatmaps;
protected DownloadTrackingComponent(BeatmapSetInfo beatmapSetInfo)
{
setInfo = beatmapSetInfo;
}
#region Disposal
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmaps.BeatmapDownloadBegan -= attachDownload;
beatmaps.ItemAdded -= setAdded;
}
#endregion
[BackgroundDependencyLoader(true)]
private void load(BeatmapManager beatmaps)
{
this.beatmaps = beatmaps;
var downloadRequest = beatmaps.GetExistingDownload(setInfo);
if (downloadRequest != null)
attachDownload(downloadRequest);
beatmaps.BeatmapDownloadBegan += attachDownload;
beatmaps.ItemAdded += setAdded;
}
private void attachDownload(DownloadBeatmapSetRequest request)
{
if (request.BeatmapSet.OnlineBeatmapSetID != setInfo.OnlineBeatmapSetID)
return;
DownloadStarted();
request.Failure += e => { DownloadFailed(); };
request.DownloadProgressed += progress => Schedule(() => ProgressChanged(progress));
request.Success += data => { DownloadComleted(); };
}
protected abstract void ProgressChanged(float progress);
protected abstract void DownloadFailed();
protected abstract void DownloadComleted();
protected abstract void BeatmapImported();
protected abstract void DownloadStarted();
private void setAdded(BeatmapSetInfo s, bool existing, bool silent)
{
if (s.OnlineBeatmapSetID != setInfo.OnlineBeatmapSetID)
return;
Schedule(BeatmapImported);
}
}
}