From 5cec5c4230c7add26195ebfc45bbd561f0dc2127 Mon Sep 17 00:00:00 2001 From: JacobTech Date: Sat, 13 Apr 2024 00:36:33 -0400 Subject: [PATCH] User Profiles The app now uses a server profile to load avatars and names. --- .../MainScreen/UI/LuskiControls/RoleView.cs | 6 ++- .../MainScreen/UI/LuskiControls/UserView.cs | 8 ++-- .../UI/PublicServers/ChatMessage.cs | 11 +++-- .../MainScreen/UI/PublicServers/PublicChat.cs | 12 ++--- Luski/GUI/MainScreenWindow.cs | 5 +- Luski/Globals.cs | 48 +++++-------------- Luski/Luski.csproj | 2 +- 7 files changed, 34 insertions(+), 58 deletions(-) diff --git a/Luski/GUI/MainScreen/UI/LuskiControls/RoleView.cs b/Luski/GUI/MainScreen/UI/LuskiControls/RoleView.cs index e5505f0..356f63c 100644 --- a/Luski/GUI/MainScreen/UI/LuskiControls/RoleView.cs +++ b/Luski/GUI/MainScreen/UI/LuskiControls/RoleView.cs @@ -28,12 +28,14 @@ public class RoleView : FlowLayout public async Task AddUser(SocketUser user, Role TopRole) { - UserView f = await UserView.Make(user, TopRole, r is null); + ServerProfile DefaultProfile = await user.Server.GetProfile(user.ServerProfile, CancellationToken.None); + UserView f = await UserView.Make(user, DefaultProfile, TopRole, r is null); + uuu.Add(user.Id, f); bool ff = false; for (int j = 0; j < Users.Count; j++) { - if (string.Compare(Users[j].DisplayName, user.DisplayName) > 0) + if (string.Compare(DefaultProfile.DisplayName, DefaultProfile.DisplayName) > 0) { Users.Insert(j, user); Controls.Insert(j+1, f); diff --git a/Luski/GUI/MainScreen/UI/LuskiControls/UserView.cs b/Luski/GUI/MainScreen/UI/LuskiControls/UserView.cs index d5f7795..743dbcf 100644 --- a/Luski/GUI/MainScreen/UI/LuskiControls/UserView.cs +++ b/Luski/GUI/MainScreen/UI/LuskiControls/UserView.cs @@ -9,7 +9,7 @@ public class UserView : UserControl { public SocketUser User { get; set; } - private UserView(IRenderObject user, SocketUser u, Role r, bool offline) + private UserView(IRenderObject user, SocketUser u, ServerProfile p, Role r, bool offline) { this.User = u; base.Size = new(244.ScaleInt(), 44.ScaleInt()); @@ -18,7 +18,7 @@ public class UserView : UserControl user.ForceDistanceUpdate(this); Label uname = new(Globals.DefaultFont) { - Text = u.DisplayName, + Text = p.DisplayName, Color = r.Color.ToColor4() }; if (offline) uname.Color = new(uname.Color.R, uname.Color.G, uname.Color.B, uname.Color.A * 0.6f); @@ -28,9 +28,9 @@ public class UserView : UserControl Controls.Add(user); } - public static async Task Make(SocketUser u, Role r, bool offline) + public static async Task Make(SocketUser u, ServerProfile p, Role r, bool offline) { - UserView m = new(await u.MakeRct(new(32.ScaleInt()), true), u, r, offline); + UserView m = new(await p.MakeRct(u, new(32.ScaleInt())), u, p, r, offline); return m; } } \ No newline at end of file diff --git a/Luski/GUI/MainScreen/UI/PublicServers/ChatMessage.cs b/Luski/GUI/MainScreen/UI/PublicServers/ChatMessage.cs index dff0c4d..2b8be1b 100644 --- a/Luski/GUI/MainScreen/UI/PublicServers/ChatMessage.cs +++ b/Luski/GUI/MainScreen/UI/PublicServers/ChatMessage.cs @@ -31,14 +31,16 @@ public class ChatMessage : UserControl public static async Task MakeChatMessage(PublicChat p, SocketMessage message) { - IUser auth = await message.GetAuthor(CancellationToken.None); - Color c = await auth.GetColor(); + SocketUser auth = (SocketUser)(await message.GetAuthor(CancellationToken.None)); + ServerProfile prof = await message.GetProfile(CancellationToken.None); + Color? c = await prof.GetColor(); + if (c is null) c = await auth.GetColor(); Color4 c4 = new(c.R, c.G, c.B, c.A); - return new ChatMessage(p, message, await message.GetParent(CancellationToken.None), auth, await auth.MakeRct(new(40.ScaleInt()), message.IsProfile), c4); + return new ChatMessage(p, message, await message.GetParent(CancellationToken.None), prof, await prof.MakeRct(auth, new(40.ScaleInt())), c4); } - private ChatMessage(PublicChat p, SocketMessage message, SocketChannel chan, IUser Author, IRenderObject UserIcon, Color4 UserNameColor) + private ChatMessage(PublicChat p, SocketMessage message, SocketChannel chan, ServerProfile Author, IRenderObject UserIcon, Color4 UserNameColor) { pc = p; Label label1; @@ -87,7 +89,6 @@ public class ChatMessage : UserControl Controls.Add(label1 = new Label(Globals.DefaultFont) { Color = UserNameColor, Text = Author.DisplayName }); label1.Location = new( 54.ScaleInt(), - //(int)(UserIcon.Location.Y + (UserIcon.Size.Y / 2) - (label1.Font.CurrentFonts[0].Face.Size.Metrics.NominalHeight / 2) - label1.Size.Y + label1.Font.PixelHeight), UserIcon.Location.Y, 0); Label label2; diff --git a/Luski/GUI/MainScreen/UI/PublicServers/PublicChat.cs b/Luski/GUI/MainScreen/UI/PublicServers/PublicChat.cs index 14d1da8..86976ad 100644 --- a/Luski/GUI/MainScreen/UI/PublicServers/PublicChat.cs +++ b/Luski/GUI/MainScreen/UI/PublicServers/PublicChat.cs @@ -355,7 +355,6 @@ public class PublicChat : UserControl private SocketMessage? lastm; private long? lastUser; - private bool fake = false; public ChatMessage? LastChatMessage; private SocketMessage? lastmIndex; @@ -454,9 +453,8 @@ public class PublicChat : UserControl } lastm = Message; - if (lastUser is null || lastUser != Message.AuthorID || hasbeentenmin || fake != Message.IsProfile) + if (lastUser is null || lastUser != Message.ProfileID || hasbeentenmin) { - if (Window is null || !Globals.ms.InvokeRequired) { MessageFlow.Controls.Add(LastChatMessage = await ChatMessage.MakeChatMessage(this, Message)); @@ -490,8 +488,7 @@ public class PublicChat : UserControl }); } } - fake = Message.IsProfile; - lastUser = Message.AuthorID; + lastUser = Message.ProfileID; } private int index = 0; @@ -504,7 +501,7 @@ public class PublicChat : UserControl Channel!.Epoch.AddMilliseconds(lastmIndex.ID >> 22).ToLocalTime().AddMinutes(10) < Channel!.Epoch.AddMilliseconds(Message.ID >> 22).ToLocalTime(); lastmIndex = Message; - if (lastUserIndex is null || lastUserIndex != Message.AuthorID || hasbeentenmin || fake != Message.IsProfile) + if (lastUserIndex is null || lastUserIndex != Message.ProfileID || hasbeentenmin) { if (Window is null || !Globals.ms.InvokeRequired) { @@ -539,7 +536,6 @@ public class PublicChat : UserControl }); } } - fakeIndex = Message.IsProfile; - lastUserIndex = Message.AuthorID; + lastUserIndex = Message.ProfileID; } } \ No newline at end of file diff --git a/Luski/GUI/MainScreenWindow.cs b/Luski/GUI/MainScreenWindow.cs index 1fa5ac4..71c7c44 100644 --- a/Luski/GUI/MainScreenWindow.cs +++ b/Luski/GUI/MainScreenWindow.cs @@ -259,7 +259,8 @@ public class MainScreenWindow : Window Role[] ra = await Server.User.GetRoles(); Color c = ra[0].Color; Color4 c4 = new(c.R, c.G, c.B, c.A); - IRenderObject u = await Server.User.MakeRct(new(46.ScaleInt()), false); + ServerProfile DefaultProfile = await Server.GetProfile(Server.User.ServerProfile, CancellationToken.None); + IRenderObject u = await DefaultProfile.MakeRct(Server.User, new(46.ScaleInt())); int ii = 4.ScaleInt(); u.Location = new(ii, cs.Size.Y + ii, 0); u.Anchor = ObjectAnchor.Bottom | ObjectAnchor.Left; @@ -269,7 +270,7 @@ public class MainScreenWindow : Window Label ul = new Label(Globals.DefaultFont) { Anchor = u.Anchor, - Text = Server.User.DisplayName, + Text = DefaultProfile.DisplayName, Color = c4 }; diff --git a/Luski/Globals.cs b/Luski/Globals.cs index cc31b91..58d16be 100644 --- a/Luski/Globals.cs +++ b/Luski/Globals.cs @@ -265,22 +265,11 @@ public static class Globals public static Dictionary UserTextureMap = new(); public static Dictionary ProfileTextureMap = new(); - - private static async Task GetIcon(this SocketUser User) - { - if (UserTextureMap.TryGetValue(User.Id, out Texture? t)) return t; - Stream UserStream = await User.GetAvatar(CancellationToken.None); - t = Globals.ms.TextureManager.AddTexture(UserStream); - UserTextureMap.Add(User.Id, t); - UserStream.Dispose(); - t.Unit = TextureUnit.Texture1; - return t; - } - public static async Task MakeRct(this TUser User, Vector2i Size, bool IsProfile) where TUser : IUser + public static async Task MakeRct(this ServerProfile Profile, TUser User, Vector2i Size) where TUser : SocketUser { Texture t = ms.TextureManager.GetTextureResource("Status.png"); - if (User.PictureType == PictureType.none) + if (Profile.PictureType == PictureType.none) { UserControl r = new(t); r.Size = Size; @@ -291,7 +280,7 @@ public static class Globals { Color = c.ToColor4() }; - l.Text = User.DisplayName[0].ToString(); + l.Text = Profile.DisplayName[0].ToString(); var y = l.GetSizeOfChar(0); l.Location = new((r.Size.X - l.Size.X)/2, (int)(r.Size.Y - (l.Font.PixelHeight - y.Y) - (r.Size.Y / 2) - (y.Y/2)), @@ -304,33 +293,20 @@ public static class Globals Rectangle r = new(t); r.Size = Size; r.Shader = Rectangle.DefaultAlphaTextureShader[ms.Context]; - r.Textures.Add(await User.GetIcon(IsProfile)); + r.Textures.Add(await Profile.GetIcon()); return r; } } - private static async Task GetIcon(this IUser User, bool IsProfile) + private static async Task GetIcon(this ServerProfile User) { - if (IsProfile) - { - if (ProfileTextureMap.TryGetValue(User.Id, out Texture? t)) return t; - Stream UserStream = await User.GetAvatar(CancellationToken.None); - t = Globals.ms.TextureManager.AddTexture(UserStream); - ProfileTextureMap.Add(User.Id, t); - UserStream.Dispose(); - t.Unit = TextureUnit.Texture1; - return t; - } - else - { - if (UserTextureMap.TryGetValue(User.Id, out Texture? t)) return t; - Stream UserStream = await User.GetAvatar(CancellationToken.None); - t = Globals.ms.TextureManager.AddTexture(UserStream); - UserTextureMap.Add(User.Id, t); - UserStream.Dispose(); - t.Unit = TextureUnit.Texture1; - return t; - } + if (ProfileTextureMap.TryGetValue(User.ID, out Texture? t)) return t; + Stream UserStream = await User.GetAvatar(CancellationToken.None); + t = Globals.ms.TextureManager.AddTexture(UserStream); + ProfileTextureMap.Add(User.ID, t); + UserStream.Dispose(); + t.Unit = TextureUnit.Texture1; + return t; } public static Settings Settings { get; set; } diff --git a/Luski/Luski.csproj b/Luski/Luski.csproj index 9c2d070..2b332ac 100644 --- a/Luski/Luski.csproj +++ b/Luski/Luski.csproj @@ -22,7 +22,7 @@ - +