1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 01:43:20 +08:00

Move async logic to framework.

This commit is contained in:
Dean Herbert 2017-03-28 14:24:21 +09:00
parent 36af868f44
commit f690e1d0c4
No known key found for this signature in database
GPG Key ID: 46D71BF4958ABB49
3 changed files with 37 additions and 68 deletions

View File

@ -142,17 +142,25 @@ namespace osu.Game.Screens.Select.Leaderboards
Padding = new MarginPadding(edge_margin),
Children = new Drawable[]
{
avatar = new Avatar(Score.User ?? new User { Id = Score.UserID })
avatar = new DelayedLoadContainer
{
TimeBeforeLoad = 500,
Size = new Vector2(HEIGHT - edge_margin * 2, HEIGHT - edge_margin * 2),
CornerRadius = corner_radius,
Masking = true,
EdgeEffect = new EdgeEffect
Children = new Drawable[]
{
Type = EdgeEffectType.Shadow,
Radius = 1,
Colour = Color4.Black.Opacity(0.2f),
},
new Avatar(Score.User ?? new User { Id = Score.UserID })
{
RelativeSizeAxes = Axes.Both,
CornerRadius = corner_radius,
Masking = true,
EdgeEffect = new EdgeEffect
{
Type = EdgeEffectType.Shadow,
Radius = 1,
Colour = Color4.Black.Opacity(0.2f),
},
},
}
},
new Container
{

View File

@ -1,8 +1,6 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -13,75 +11,31 @@ namespace osu.Game.Users
{
public class Avatar : Container
{
private const int time_before_load = 500;
private Drawable sprite;
private readonly User user;
private readonly bool delayedLoad;
/// <summary>
/// An avatar for specified user.
/// </summary>
/// <param name="user">The user. A null value will get a placeholder avatar.</param>
/// <param name="delayedLoad">Whether we should delay the load of the avatar until it has been on-screen for a specified duration.</param>
public Avatar(User user = null, bool delayedLoad = true)
public Avatar(User user = null)
{
this.user = user;
this.delayedLoad = delayedLoad;
}
private Action performLoad;
private Task loadTask;
[BackgroundDependencyLoader(permitNulls: true)]
[BackgroundDependencyLoader]
private void load(TextureStore textures)
{
performLoad = () =>
Texture texture = null;
if (user?.Id > 1) texture = textures.Get($@"https://a.ppy.sh/{user.Id}");
if (texture == null) texture = textures.Get(@"Online/avatar-guest");
Add(new Sprite
{
Texture texture = null;
if (user?.Id > 1) texture = textures.Get($@"https://a.ppy.sh/{user.Id}");
if (texture == null) texture = textures.Get(@"Online/avatar-guest");
sprite = new Sprite
{
Texture = texture,
FillMode = FillMode.Fit,
Anchor = Anchor.Centre,
Origin = Anchor.Centre
};
Schedule(() =>
{
Add(sprite);
sprite.FadeInFromZero(150);
});
};
}
private double timeVisible;
private bool shouldLoad => !delayedLoad || timeVisible > time_before_load;
protected override void Update()
{
base.Update();
if (!shouldLoad)
{
//Special optimisation to not start loading until we are within bounds of our closest ScrollContainer parent.
ScrollContainer scroll = null;
IContainer cursor = this;
while (scroll == null && (cursor = cursor.Parent) != null)
scroll = cursor as ScrollContainer;
if (scroll?.ScreenSpaceDrawQuad.Intersects(ScreenSpaceDrawQuad) ?? true)
timeVisible += Time.Elapsed;
else
timeVisible = 0;
}
if (shouldLoad && loadTask == null)
(loadTask = Task.Factory.StartNew(performLoad, TaskCreationOptions.LongRunning)).ConfigureAwait(false);
Texture = texture,
FillMode = FillMode.Fit,
Anchor = Anchor.Centre,
Origin = Anchor.Centre
});
}
}
}

View File

@ -11,7 +11,7 @@ namespace osu.Game.Users
/// </summary>
public class UpdateableAvatar : Container
{
private Avatar displayedAvatar;
private Container displayedAvatar;
private User user;
@ -40,7 +40,14 @@ namespace osu.Game.Users
{
displayedAvatar?.FadeOut(300);
displayedAvatar?.Expire();
Add(displayedAvatar = new Avatar(user, false) { RelativeSizeAxes = Axes.Both });
Add(displayedAvatar = new AsyncLoadContainer
{
RelativeSizeAxes = Axes.Both,
Children = new[]
{
new Avatar(user) { RelativeSizeAxes = Axes.Both }
}
});
}
}
}