mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 10:12:54 +08:00
Make Avatar accept a user.
Add UpdateableAvatar to handle the toolbar use-case.
This commit is contained in:
parent
039f4a65dc
commit
13272e6995
@ -14,7 +14,7 @@ namespace osu.Game.Overlays.Toolbar
|
||||
{
|
||||
internal class ToolbarUserButton : ToolbarButton, IOnlineComponent
|
||||
{
|
||||
private readonly Avatar avatar;
|
||||
private readonly UpdateableAvatar avatar;
|
||||
|
||||
public ToolbarUserButton()
|
||||
{
|
||||
@ -24,7 +24,7 @@ namespace osu.Game.Overlays.Toolbar
|
||||
|
||||
Add(new OpaqueBackground { Depth = 1 });
|
||||
|
||||
Flow.Add(avatar = new Avatar
|
||||
Flow.Add(avatar = new UpdateableAvatar
|
||||
{
|
||||
Masking = true,
|
||||
Size = new Vector2(32),
|
||||
@ -52,11 +52,11 @@ namespace osu.Game.Overlays.Toolbar
|
||||
{
|
||||
default:
|
||||
Text = @"Guest";
|
||||
avatar.UserId = 1;
|
||||
avatar.User = new User();
|
||||
break;
|
||||
case APIState.Online:
|
||||
Text = api.Username;
|
||||
avatar.UserId = api.LocalUser.Value.Id;
|
||||
avatar.User = api.LocalUser;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ namespace osu.Game.Screens.Select.Leaderboards
|
||||
Padding = new MarginPadding(edge_margin),
|
||||
Children = new Drawable[]
|
||||
{
|
||||
avatar = new Avatar
|
||||
avatar = new Avatar(Score.User ?? new User { Id = Score.UserID })
|
||||
{
|
||||
Size = new Vector2(HEIGHT - edge_margin * 2, HEIGHT - edge_margin * 2),
|
||||
CornerRadius = corner_radius,
|
||||
@ -153,7 +153,6 @@ namespace osu.Game.Screens.Select.Leaderboards
|
||||
Radius = 1,
|
||||
Colour = Color4.Black.Opacity(0.2f),
|
||||
},
|
||||
UserId = Score.User?.Id ?? Score.UserID,
|
||||
},
|
||||
new Container
|
||||
{
|
||||
|
@ -1,7 +1,7 @@
|
||||
// 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.Diagnostics;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
@ -13,68 +13,60 @@ namespace osu.Game.Users
|
||||
{
|
||||
public class Avatar : Container
|
||||
{
|
||||
public Drawable Sprite;
|
||||
private const int time_before_load = 500;
|
||||
|
||||
private long userId;
|
||||
private OsuGameBase game;
|
||||
private Texture guestTexture;
|
||||
private Drawable sprite;
|
||||
private readonly User user;
|
||||
private readonly bool delayedLoad;
|
||||
|
||||
[BackgroundDependencyLoader(permitNulls: true)]
|
||||
private void load(OsuGameBase game, TextureStore textures)
|
||||
/// <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)
|
||||
{
|
||||
this.game = game;
|
||||
guestTexture = textures.Get(@"Online/avatar-guest");
|
||||
}
|
||||
|
||||
public long UserId
|
||||
{
|
||||
get { return userId; }
|
||||
set
|
||||
{
|
||||
if (userId == value)
|
||||
return;
|
||||
|
||||
userId = value;
|
||||
invalidateSprite();
|
||||
}
|
||||
this.user = user;
|
||||
this.delayedLoad = delayedLoad;
|
||||
}
|
||||
|
||||
private Action performLoad;
|
||||
private Task loadTask;
|
||||
|
||||
private void invalidateSprite()
|
||||
[BackgroundDependencyLoader(permitNulls: true)]
|
||||
private void load(TextureStore textures)
|
||||
{
|
||||
Sprite?.FadeOut(100);
|
||||
Sprite?.Expire();
|
||||
Sprite = null;
|
||||
}
|
||||
|
||||
private void updateSprite()
|
||||
{
|
||||
if (loadTask != null || Sprite != null) return;
|
||||
|
||||
var newSprite = userId > 1 ? new OnlineSprite($@"https://a.ppy.sh/{userId}", guestTexture) : new Sprite { Texture = guestTexture };
|
||||
|
||||
newSprite.FillMode = FillMode.Fill;
|
||||
|
||||
loadTask = newSprite.LoadAsync(game, s =>
|
||||
performLoad = () =>
|
||||
{
|
||||
Sprite = s;
|
||||
Add(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.FadeInFromZero(200);
|
||||
loadTask = null;
|
||||
});
|
||||
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 shouldUpdate => Sprite != null || timeVisible > 500;
|
||||
private bool shouldLoad => !delayedLoad || timeVisible > time_before_load;
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
if (!shouldUpdate)
|
||||
if (!shouldLoad)
|
||||
{
|
||||
//Special optimisation to not start loading until we are within bounds of our closest ScrollContainer parent.
|
||||
ScrollContainer scroll = null;
|
||||
@ -88,27 +80,8 @@ namespace osu.Game.Users
|
||||
timeVisible = 0;
|
||||
}
|
||||
|
||||
if (shouldUpdate)
|
||||
updateSprite();
|
||||
}
|
||||
|
||||
public class OnlineSprite : Sprite
|
||||
{
|
||||
private readonly string url;
|
||||
private readonly Texture fallbackTexture;
|
||||
|
||||
public OnlineSprite(string url, Texture fallbackTexture = null)
|
||||
{
|
||||
Debug.Assert(url != null);
|
||||
this.url = url;
|
||||
this.fallbackTexture = fallbackTexture;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(TextureStore textures)
|
||||
{
|
||||
Texture = textures.Get(url) ?? fallbackTexture;
|
||||
}
|
||||
if (shouldLoad && loadTask == null)
|
||||
(loadTask = Task.Factory.StartNew(performLoad, TaskCreationOptions.LongRunning)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
43
osu.Game/Users/UpdateableAvatar.cs
Normal file
43
osu.Game/Users/UpdateableAvatar.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
|
||||
namespace osu.Game.Users
|
||||
{
|
||||
/// <summary>
|
||||
/// An avatar which can update to a new user when needed.
|
||||
/// </summary>
|
||||
public class UpdateableAvatar : Container
|
||||
{
|
||||
private Avatar displayedAvatar;
|
||||
|
||||
private User user;
|
||||
|
||||
public User User
|
||||
{
|
||||
get { return user; }
|
||||
set
|
||||
{
|
||||
if (user?.Id == value?.Id)
|
||||
return;
|
||||
|
||||
user = value;
|
||||
|
||||
if (IsLoaded)
|
||||
updateAvatar();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
base.LoadComplete();
|
||||
updateAvatar();
|
||||
}
|
||||
|
||||
private void updateAvatar()
|
||||
{
|
||||
displayedAvatar?.FadeOut(300);
|
||||
displayedAvatar?.Expire();
|
||||
Add(displayedAvatar = new Avatar(user, false) { RelativeSizeAxes = Axes.Both });
|
||||
}
|
||||
}
|
||||
}
|
@ -266,6 +266,7 @@
|
||||
<Compile Include="Screens\Tournament\Teams\ITeamList.cs" />
|
||||
<Compile Include="Screens\Tournament\ScrollingTeamContainer.cs" />
|
||||
<Compile Include="Screens\Tournament\Teams\StorageBackedTeamList.cs" />
|
||||
<Compile Include="Users\UpdateableAvatar.cs" />
|
||||
<Compile Include="Users\User.cs" />
|
||||
<Compile Include="Graphics\UserInterface\Volume\VolumeControl.cs" />
|
||||
<Compile Include="Database\BeatmapDatabase.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user