From dbd05eff663d1d3907aa938e0e75a367724a6a75 Mon Sep 17 00:00:00 2001 From: JacobTech Date: Sun, 14 Apr 2024 14:15:12 -0400 Subject: [PATCH] Server Icon Improvements Server icons are now red when the client can't connect to the server. Server icons now support servers without icons. --- Luski/GUI/MainScreen/UI/ServerIcon.cs | 35 ++++++++++++++++------- Luski/GUI/MainScreenWindow.cs | 8 ++++++ Luski/Globals.cs | 40 +++++++++++++++++++++++++++ Luski/Luski.csproj | 2 +- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/Luski/GUI/MainScreen/UI/ServerIcon.cs b/Luski/GUI/MainScreen/UI/ServerIcon.cs index 5f9f773..c748036 100644 --- a/Luski/GUI/MainScreen/UI/ServerIcon.cs +++ b/Luski/GUI/MainScreen/UI/ServerIcon.cs @@ -5,6 +5,7 @@ using GraphicsManager.Objects; using GraphicsManager.Objects.Core; using Luski.net; using OpenTK.Graphics.OpenGL4; +using OpenTK.Mathematics; namespace Luski.GUI.MainScreen.UI; @@ -45,15 +46,30 @@ public class ServerIcon : UserControl where TServer : Server } } - public ServerIcon(TServer Server) + public ServerIcon(TServer Server, bool Failed = false) { - Rectangle r = new(Globals.ms.TextureManager.GetTextureResource("rc.png")) + Rectangle r; + if (Server is PublicServer ps) { - Location = new(18.ScaleInt(), 8.ScaleInt(), 0), - Size = new(32.ScaleInt()), - Shader = Rectangle.DefaultAlphaTextureShader[Globals.ms.Context], - IgnoreHover = true - }; + var rpre = ps.MakeRct(new(32.ScaleInt())); + Task.WaitAll(rpre); + r = (Rectangle)rpre.Result; + } + else + { + r = new(Globals.ms.TextureManager.GetTextureResource("rc.png")) + { + Size = new(32.ScaleInt()), + Shader = Rectangle.DefaultAlphaTextureShader[Globals.ms.Context], + IgnoreHover = true + }; + Texture t = Globals.ms.TextureManager.AddTexture(Server.GetAvatar(CancellationToken.None).Result); + t.Unit = TextureUnit.Texture1; + r.Textures.Add(t); + } + + r.Location = new(18.ScaleInt(), 8.ScaleInt(), 0); + r.IgnoreHover = true; Rectangle rr = new(r.Textures[0]) { Location = new(17.ScaleInt(), 7.ScaleInt(), 0), @@ -70,10 +86,9 @@ public class ServerIcon : UserControl where TServer : Server BackgroundColor = new(26, 26, 26, 255), IgnoreHover = true }; + if (Failed) SelectedRect.BackgroundColor = Color4.DarkRed; this.Server = Server; - Texture t = Globals.ms.TextureManager.AddTexture(Server.GetAvatar(CancellationToken.None).Result); - t.Unit = TextureUnit.Texture1; - r.Textures.Add(t); + Controls.Add(SelectedRect); Controls.Add(rr); Controls.Add(r); diff --git a/Luski/GUI/MainScreenWindow.cs b/Luski/GUI/MainScreenWindow.cs index 0bd90e6..f98b4d8 100644 --- a/Luski/GUI/MainScreenWindow.cs +++ b/Luski/GUI/MainScreenWindow.cs @@ -442,6 +442,14 @@ public class MainScreenWindow : Window si.LoadToParent(ser, this); } + foreach (PublicServer pser in Globals.Luski.FailedServers) + { + Console.WriteLine(pser.Name); + ServerIcon si = new ServerIcon(pser, true); + ser.Controls.Add(si); + si.LoadToParent(ser, this); + } + AddServerIcon asi = new(); asi.Clicked += AddButtonClicked; ser.Controls.Add(asi); diff --git a/Luski/Globals.cs b/Luski/Globals.cs index 97cb998..21ac3e6 100644 --- a/Luski/Globals.cs +++ b/Luski/Globals.cs @@ -300,6 +300,46 @@ public static class Globals } } + public static async Task MakeRct(this PublicServer Server, Vector2i Size) + { + Texture t = ms.TextureManager.GetTextureResource("rc.png"); + if (Server.PictureType == PictureType.none) + { + UserControl r = new(t); + r.Size = Size; + r.Shader = Rectangle.DefaultAlphaShader[ms.Context]; + r.BackgroundColor = new(25, 25, 25, 255); + Label l = new(DefaultFont) + { + Color = new(255,255,255,255) + }; + l.Text = Server.Name[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)), + 0); + r.Controls.Add(l); + return r; + } + else + { + Rectangle r = new(t); + r.Size = Size; + r.Shader = Rectangle.DefaultAlphaTextureShader[ms.Context]; + r.Textures.Add(await Server.GetIcon()); + return r; + } + } + + private static async Task GetIcon(this TServer Server) where TServer : Server + { + Stream UserStream = await Server.GetAvatar(CancellationToken.None); + Texture t = Globals.ms.TextureManager.AddTexture(UserStream); + UserStream.Dispose(); + t.Unit = TextureUnit.Texture1; + return t; + } + private static async Task GetIcon(this ServerProfile User) { if (ProfileTextureMap.TryGetValue(User.ID, out Texture? t)) return t; diff --git a/Luski/Luski.csproj b/Luski/Luski.csproj index d8aa17f..0afaad2 100644 --- a/Luski/Luski.csproj +++ b/Luski/Luski.csproj @@ -22,7 +22,7 @@ - +