From 5239fee947435bf4fe99b43d28a7fe0118ca64b8 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 23 Nov 2023 17:15:03 +0900 Subject: [PATCH] Allow use of skin username/flag/avatar components outside of gameplay --- osu.Game/Screens/Play/HUD/PlayerAvatar.cs | 20 ++++++++++++++++++-- osu.Game/Screens/Play/HUD/PlayerFlag.cs | 22 ++++++++++++++++++++-- osu.Game/Skinning/Components/PlayerName.cs | 21 +++++++++++++++++++-- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/osu.Game/Screens/Play/HUD/PlayerAvatar.cs b/osu.Game/Screens/Play/HUD/PlayerAvatar.cs index 1341a10d60..06d0f7bc9a 100644 --- a/osu.Game/Screens/Play/HUD/PlayerAvatar.cs +++ b/osu.Game/Screens/Play/HUD/PlayerAvatar.cs @@ -7,6 +7,8 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Configuration; using osu.Game.Localisation.SkinComponents; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests.Responses; using osu.Game.Overlays.Settings; using osu.Game.Skinning; using osu.Game.Users.Drawables; @@ -29,6 +31,14 @@ namespace osu.Game.Screens.Play.HUD private const float default_size = 80f; + [Resolved] + private GameplayState? gameplayState { get; set; } + + [Resolved] + private IAPIProvider api { get; set; } = null!; + + private IBindable? apiUser; + public PlayerAvatar() { Size = new Vector2(default_size); @@ -41,9 +51,15 @@ namespace osu.Game.Screens.Play.HUD } [BackgroundDependencyLoader] - private void load(GameplayState gameplayState) + private void load() { - avatar.User = gameplayState.Score.ScoreInfo.User; + if (gameplayState != null) + avatar.User = gameplayState.Score.ScoreInfo.User; + else + { + apiUser = api.LocalUser.GetBoundCopy(); + apiUser.BindValueChanged(u => avatar.User = u.NewValue, true); + } } protected override void LoadComplete() diff --git a/osu.Game/Screens/Play/HUD/PlayerFlag.cs b/osu.Game/Screens/Play/HUD/PlayerFlag.cs index 70ad078e34..c7e247d26a 100644 --- a/osu.Game/Screens/Play/HUD/PlayerFlag.cs +++ b/osu.Game/Screens/Play/HUD/PlayerFlag.cs @@ -2,8 +2,11 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests.Responses; using osu.Game.Skinning; using osu.Game.Users.Drawables; using osuTK; @@ -18,9 +21,18 @@ namespace osu.Game.Screens.Play.HUD private const float default_size = 40f; + [Resolved] + private GameplayState? gameplayState { get; set; } + + [Resolved] + private IAPIProvider api { get; set; } = null!; + + private IBindable? apiUser; + public PlayerFlag() { Size = new Vector2(default_size, default_size / 1.4f); + InternalChild = flag = new UpdateableFlag { RelativeSizeAxes = Axes.Both, @@ -28,9 +40,15 @@ namespace osu.Game.Screens.Play.HUD } [BackgroundDependencyLoader] - private void load(GameplayState gameplayState) + private void load() { - flag.CountryCode = gameplayState.Score.ScoreInfo.User.CountryCode; + if (gameplayState != null) + flag.CountryCode = gameplayState.Score.ScoreInfo.User.CountryCode; + else + { + apiUser = api.LocalUser.GetBoundCopy(); + apiUser.BindValueChanged(u => flag.CountryCode = u.NewValue.CountryCode, true); + } } public bool UsesFixedAnchor { get; set; } diff --git a/osu.Game/Skinning/Components/PlayerName.cs b/osu.Game/Skinning/Components/PlayerName.cs index 34ace53d47..21bf615bc6 100644 --- a/osu.Game/Skinning/Components/PlayerName.cs +++ b/osu.Game/Skinning/Components/PlayerName.cs @@ -3,9 +3,12 @@ using JetBrains.Annotations; using osu.Framework.Allocation; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Sprites; using osu.Game.Graphics.Sprites; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests.Responses; using osu.Game.Screens.Play; namespace osu.Game.Skinning.Components @@ -15,6 +18,14 @@ namespace osu.Game.Skinning.Components { private readonly OsuSpriteText text; + [Resolved] + private GameplayState? gameplayState { get; set; } + + [Resolved] + private IAPIProvider api { get; set; } = null!; + + private IBindable? apiUser; + public PlayerName() { AutoSizeAxes = Axes.Both; @@ -30,9 +41,15 @@ namespace osu.Game.Skinning.Components } [BackgroundDependencyLoader] - private void load(GameplayState gameplayState) + private void load() { - text.Text = gameplayState.Score.ScoreInfo.User.Username; + if (gameplayState != null) + text.Text = gameplayState.Score.ScoreInfo.User.Username; + else + { + apiUser = api.LocalUser.GetBoundCopy(); + apiUser.BindValueChanged(u => text.Text = u.NewValue.Username, true); + } } protected override void SetFont(FontUsage font) => text.Font = font.With(size: 40);