2017-02-07 04:59:30 +00:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-12-01 07:05:54 +00:00
|
|
|
|
|
|
|
|
|
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-10 18:44:40 +00:00
|
|
|
|
using osu.Game.Graphics;
|
2016-12-01 07:05:54 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Toolbar
|
|
|
|
|
{
|
|
|
|
|
class ToolbarUserButton : ToolbarButton, IOnlineComponent
|
|
|
|
|
{
|
|
|
|
|
private Avatar avatar;
|
|
|
|
|
|
|
|
|
|
public ToolbarUserButton()
|
|
|
|
|
{
|
2017-02-08 11:14:17 +00:00
|
|
|
|
AutoSizeAxes = Axes.X;
|
|
|
|
|
|
2016-12-01 07:05:54 +00:00
|
|
|
|
DrawableText.Font = @"Exo2.0-MediumItalic";
|
|
|
|
|
|
2016-12-01 09:53:13 +00:00
|
|
|
|
Add(new OpaqueBackground { Depth = 1 });
|
|
|
|
|
|
2016-12-01 07:05:54 +00:00
|
|
|
|
Flow.Add(avatar = new Avatar());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2017-01-31 07:59:38 +00:00
|
|
|
|
private void load(APIAccess api)
|
2016-12-01 07:05:54 +00: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 07:44:24 +00:00
|
|
|
|
avatar.UserId = api.LocalUser.Value.Id;
|
2016-12-01 07:05:54 +00: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-10 18:44:40 +00:00
|
|
|
|
Colour = Color4.Black.Opacity(0.1f),
|
2016-12-01 07:05:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Masking = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuGame game, TextureStore textures)
|
|
|
|
|
{
|
|
|
|
|
this.game = game;
|
|
|
|
|
|
2017-02-06 13:17:29 +00:00
|
|
|
|
guestTexture = textures.Get(@"Online/avatar-guest");
|
2016-12-01 07:05:54 +00: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 12:09:17 +00:00
|
|
|
|
//todo: fix this... clock dependencies are a pain
|
|
|
|
|
if (s.Clock != null)
|
|
|
|
|
s.FadeInFromZero(200);
|
2016-12-01 07:05:54 +00: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|