2016-12-01 15:05:54 +08:00
|
|
|
|
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
2017-01-11 02:44:40 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2016-12-01 15:05:54 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Toolbar
|
|
|
|
|
{
|
|
|
|
|
class ToolbarUserButton : ToolbarButton, IOnlineComponent
|
|
|
|
|
{
|
|
|
|
|
private Avatar avatar;
|
|
|
|
|
|
|
|
|
|
public ToolbarUserButton()
|
|
|
|
|
{
|
|
|
|
|
DrawableText.Font = @"Exo2.0-MediumItalic";
|
|
|
|
|
|
2016-12-01 17:53:13 +08:00
|
|
|
|
Add(new OpaqueBackground { Depth = 1 });
|
|
|
|
|
|
2016-12-01 15:05:54 +08:00
|
|
|
|
Flow.Add(avatar = new Avatar());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2017-01-31 15:59:38 +08:00
|
|
|
|
private void load(APIAccess api)
|
2016-12-01 15:05:54 +08:00
|
|
|
|
{
|
|
|
|
|
api.Register(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void APIStateChanged(APIAccess api, APIState state)
|
|
|
|
|
{
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
Text = @"Guest";
|
|
|
|
|
avatar.UserId = 1;
|
|
|
|
|
break;
|
|
|
|
|
case APIState.Online:
|
|
|
|
|
Text = api.Username;
|
2016-12-01 15:44:24 +08:00
|
|
|
|
avatar.UserId = api.LocalUser.Value.Id;
|
2016-12-01 15:05:54 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Avatar : Container
|
|
|
|
|
{
|
|
|
|
|
public Drawable Sprite;
|
|
|
|
|
|
|
|
|
|
private int userId;
|
|
|
|
|
private OsuGame game;
|
|
|
|
|
private Texture guestTexture;
|
|
|
|
|
|
|
|
|
|
public Avatar()
|
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(32);
|
|
|
|
|
Anchor = Anchor.CentreLeft;
|
|
|
|
|
Origin = Anchor.CentreLeft;
|
|
|
|
|
|
|
|
|
|
CornerRadius = Size.X / 8;
|
|
|
|
|
|
|
|
|
|
EdgeEffect = new EdgeEffect
|
|
|
|
|
{
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
|
Radius = 4,
|
2017-01-11 02:44:40 +08:00
|
|
|
|
Colour = Color4.Black.Opacity(0.1f),
|
2016-12-01 15:05:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Masking = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuGame game, TextureStore textures)
|
|
|
|
|
{
|
|
|
|
|
this.game = game;
|
|
|
|
|
|
2017-02-06 21:17:29 +08:00
|
|
|
|
guestTexture = textures.Get(@"Online/avatar-guest");
|
2016-12-01 15:05:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int UserId
|
|
|
|
|
{
|
|
|
|
|
get { return userId; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (userId == value)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
userId = value;
|
|
|
|
|
|
|
|
|
|
Sprite newSprite;
|
|
|
|
|
if (userId > 1)
|
|
|
|
|
newSprite = new OnlineSprite($@"https://a.ppy.sh/{userId}");
|
|
|
|
|
else
|
|
|
|
|
newSprite = new Sprite { Texture = guestTexture };
|
|
|
|
|
|
|
|
|
|
newSprite.FillMode = FillMode.Fit;
|
|
|
|
|
|
|
|
|
|
newSprite.Preload(game, s =>
|
|
|
|
|
{
|
|
|
|
|
Sprite?.FadeOut();
|
|
|
|
|
Sprite?.Expire();
|
|
|
|
|
Sprite = s;
|
|
|
|
|
|
|
|
|
|
Add(s);
|
|
|
|
|
|
2016-12-05 20:09:17 +08:00
|
|
|
|
//todo: fix this... clock dependencies are a pain
|
|
|
|
|
if (s.Clock != null)
|
|
|
|
|
s.FadeInFromZero(200);
|
2016-12-01 15:05:54 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OnlineSprite : Sprite
|
|
|
|
|
{
|
|
|
|
|
private readonly string url;
|
|
|
|
|
private readonly int userId;
|
|
|
|
|
|
|
|
|
|
public OnlineSprite(string url)
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(url != null);
|
|
|
|
|
this.url = url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(TextureStore textures)
|
|
|
|
|
{
|
|
|
|
|
Texture = textures.Get(url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|