1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:47:26 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarUserButton.cs

140 lines
3.9 KiB
C#
Raw Normal View History

// 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 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;
namespace osu.Game.Overlays.Toolbar
{
class ToolbarUserButton : ToolbarButton, IOnlineComponent
{
private Avatar avatar;
public ToolbarUserButton()
{
AutoSizeAxes = Axes.X;
DrawableText.Font = @"Exo2.0-MediumItalic";
Add(new OpaqueBackground { Depth = 1 });
Flow.Add(avatar = new Avatar());
}
[BackgroundDependencyLoader]
2017-01-31 15:59:38 +08:00
private void load(APIAccess api)
{
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;
avatar.UserId = api.LocalUser.Value.Id;
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),
};
Masking = true;
}
[BackgroundDependencyLoader]
private void load(OsuGame game, TextureStore textures)
{
this.game = game;
guestTexture = textures.Get(@"Online/avatar-guest");
}
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);
});
}
}
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);
}
}
}
}
}