|
| 1 | +using Source; |
| 2 | +using Source.Common.GUI; |
| 3 | +using Source.GUI.Controls; |
| 4 | + |
| 5 | +using Steamworks; |
| 6 | + |
| 7 | +namespace Game.Client.GarrysMod; |
| 8 | + |
| 9 | +public class AvatarImage : Panel |
| 10 | +{ |
| 11 | + static readonly List<TextureID>[] FreeTextures = [[], [], []]; |
| 12 | + |
| 13 | + TextureID TextureID; |
| 14 | + ulong SteamID; |
| 15 | + int Size; |
| 16 | + readonly IImage? DefaultImage; |
| 17 | + |
| 18 | + readonly Callback<AvatarImageLoaded_t> AvatarImageLoaded; |
| 19 | + readonly Callback<PersonaStateChange_t> PersonaStateChange; |
| 20 | + |
| 21 | + public AvatarImage(Panel? parent, ReadOnlySpan<char> name) : base(parent, name) { |
| 22 | + SteamID = 0; |
| 23 | + Size = 0; |
| 24 | + AvatarImageLoaded = Callback<AvatarImageLoaded_t>.Create(OnAvatarImageLoaded); |
| 25 | + PersonaStateChange = Callback<PersonaStateChange_t>.Create(OnPersonaStateChange); |
| 26 | + TextureID = TextureID.INVALID; |
| 27 | + DefaultImage = SchemeManager.GetImage("avatar_default", true); |
| 28 | + } |
| 29 | + |
| 30 | + public override void Dispose() { |
| 31 | + ClearTexture(); |
| 32 | + PersonaStateChange.Dispose(); |
| 33 | + AvatarImageLoaded.Dispose(); |
| 34 | + base.Dispose(); |
| 35 | + } |
| 36 | + |
| 37 | + void OnAvatarImageLoaded(AvatarImageLoaded_t info) { |
| 38 | + if (info.m_steamID.m_SteamID == SteamID) |
| 39 | + LoadAvatar(); |
| 40 | + } |
| 41 | + |
| 42 | + void OnPersonaStateChange(PersonaStateChange_t info) { |
| 43 | + if (info.m_ulSteamID == SteamID && (info.m_nChangeFlags & EPersonaChange.k_EPersonaChangeAvatar) != 0) |
| 44 | + LoadAvatar(); |
| 45 | + } |
| 46 | + |
| 47 | + void ClearTexture() { |
| 48 | + SteamID = 0; |
| 49 | + if (TextureID == TextureID.INVALID) { |
| 50 | + TextureID = TextureID.INVALID; |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + FreeTextures[Size].Add(TextureID); |
| 55 | + TextureID = TextureID.INVALID; |
| 56 | + } |
| 57 | + |
| 58 | + void LoadAvatar() { |
| 59 | + CSteamID steamID = new(SteamID); |
| 60 | + if (SteamFriends.RequestUserInformation(steamID, false)) |
| 61 | + return; |
| 62 | + |
| 63 | + int avatar; |
| 64 | + if (Size == 0) |
| 65 | + avatar = SteamFriends.GetSmallFriendAvatar(steamID); |
| 66 | + else if (Size == 1) |
| 67 | + avatar = SteamFriends.GetMediumFriendAvatar(steamID); |
| 68 | + else if (Size == 2) |
| 69 | + avatar = SteamFriends.GetLargeFriendAvatar(steamID); |
| 70 | + else |
| 71 | + return; |
| 72 | + |
| 73 | + if ((uint)(avatar + 1) <= 1) |
| 74 | + return; |
| 75 | + |
| 76 | + if (!SteamUtils.GetImageSize(avatar, out uint wide, out uint tall)) |
| 77 | + return; |
| 78 | + |
| 79 | + int size = (int)(wide * tall * 4); |
| 80 | + byte[] rgba = new byte[size]; |
| 81 | + if (!SteamUtils.GetImageRGBA(avatar, rgba, size)) |
| 82 | + return; |
| 83 | + |
| 84 | + if (TextureID == TextureID.INVALID) { |
| 85 | + List<TextureID> free = FreeTextures[Size]; |
| 86 | + if (free.Count < 1) |
| 87 | + TextureID = Surface.CreateNewTextureID(true); |
| 88 | + else { |
| 89 | + TextureID = free[^1]; |
| 90 | + free.RemoveAt(free.Count - 1); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + Surface.DrawSetTextureRGBA(TextureID, rgba, (int)wide, (int)tall, 0, true); |
| 95 | + } |
| 96 | + |
| 97 | + public void SetSteamID(ulong steamID, int size) { |
| 98 | + if (size < 0) |
| 99 | + size = 0; |
| 100 | + else if (2 < size) |
| 101 | + size = 2; |
| 102 | + |
| 103 | + if (SteamID != steamID || size != Size) { |
| 104 | + if (size != Size) |
| 105 | + ClearTexture(); |
| 106 | + SteamID = steamID; |
| 107 | + Size = size; |
| 108 | + LoadAvatar(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void SetPlayer(C_BasePlayer? player, int size) { |
| 113 | + if (player == null || !player.GetSteamID(out CSteamID id)) { |
| 114 | + ClearTexture(); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + ulong steamID = id.m_SteamID; |
| 119 | + int clamped = 0; |
| 120 | + if (-1 < size) { |
| 121 | + clamped = size; |
| 122 | + if (2 < size) |
| 123 | + clamped = 2; |
| 124 | + } |
| 125 | + |
| 126 | + if (SteamID == steamID && clamped == Size) |
| 127 | + return; |
| 128 | + |
| 129 | + if (clamped != Size) |
| 130 | + ClearTexture(); |
| 131 | + |
| 132 | + SteamID = steamID; |
| 133 | + Size = clamped; |
| 134 | + LoadAvatar(); |
| 135 | + } |
| 136 | + |
| 137 | + public override void Paint() { |
| 138 | + if (TextureID == TextureID.INVALID) { |
| 139 | + DefaultImage!.SetSize(GetWide(), GetTall()); |
| 140 | + DefaultImage.Paint(); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + Surface.DrawSetTexture(TextureID); |
| 145 | + Surface.DrawSetColor(new Color(255, 255, 255, 255)); |
| 146 | + Surface.DrawTexturedRect(0, 0, GetWide(), GetTall()); |
| 147 | + } |
| 148 | +} |
0 commit comments