ChatMessage control to resolve #5

This commit is contained in:
JacobTech 2023-01-02 11:55:59 -05:00
parent dc96ec3874
commit b82677bb64
3 changed files with 133 additions and 2 deletions

View File

@ -0,0 +1,131 @@
using GraphicsManager.Enums;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects;
using GraphicsManager.Objects.Core;
using Luski.net.Interfaces;
using Luski.net.JsonTypes;
namespace Luski.GUI.MainScreen.UI;
public class ChatMessage : UserControl
{
readonly int padding = 2;
private SocketMessage Msg { get; }
private Label label1, label2;
public ChatMessage(SocketMessage message)
{
Msg = message;
IUser user = message.GetAuthor().Result;
Anchor = ObjectAnchor.Left | ObjectAnchor.Right;
Controls.Add(label1 = new Label() {Location = new(83, 9), Text = user.Username});
Controls.Add(label2 = new Label() {Location = new(83, 40), Text = message.Context});
if (Msg.Files != null && Msg.Files.Length > 0)
{
/*
for (int i = 0; i < Msg.Files.Length; i++)
{
File file = Msg.Files[i];
ContentEmbed button = new(file, Msg.ChannelID);
if (InvokeRequired)
{
Invoke(new Action(() =>
{
Controls.Add(button);
}));
}
else
{
Controls.Add(button);
}
}*/
}
DateTime time = new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(Msg.Id >> 22).ToLocalTime();
string timestr;
if (time.Date == DateTime.Now.ToLocalTime().Date)
{
timestr = $"Today at {time.ToShortTimeString()}";
}
else if (time.Date == DateTime.Now.ToLocalTime().AddDays(-1).Date)
{
timestr = $"Yesterday at {time.ToShortTimeString()}";
}
else
{
timestr = $"{time:M/dd/yyyy h:mm tt}";
}
Controls.Add(new Label() {Location = new(label1.Location.X + label1.Size.X + 4, 17), Text = timestr});
Controls.Add(new Label() {Location = new(129, 17), Text = message.Context});
Controls.Add(new Rectangle(new Texture(user.GetAvatar().Result)) { Location = new(15, 3), Size = new(58, 58) });
}
public void AddMessage(SocketMessage msg)
{
Label newLabel = new()
{
Text = msg.Context,
Tag = msg,
Location = new(label2.Location.X, Size.Y)
};
newLabel.MouseEnter += NewLabel_MouseEnter;
newLabel.MouseLeave += NewLabel_MouseLeave;
Controls.Add(newLabel);
if (msg.Files != null && msg.Files.Length > 0)
{
for (int i = 0; i < msg.Files.Length; i++)
{
//files example
/*
File file = msg.Files[i];
ContentEmbed button = new(file, Msg.ChannelID);
if (InvokeRequired)
{
Invoke(new Action(() =>
{
Controls.Add(button);
}));
}
else
{
Controls.Add(button);
}*/
}
}
}
readonly List<Label> Labels = new();
private Task NewLabel_MouseLeave(IRenderObject sender)
{
if (sender is not Label label) return Task.CompletedTask;
if (label.Tag is not SocketMessage Message) return Task.CompletedTask;
DateTime time = new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(Message.Id >> 22).ToLocalTime();
string b = time.ToString("h:mm tt");
IEnumerable<Label>? l = Labels.Where(s => s.Text == b);
if (l is not null && l.Any())
{
Controls.Remove(l.First());
Labels.Remove(l.First());
}
return Task.CompletedTask;
}
private Task NewLabel_MouseEnter(IRenderObject sender)
{
if (sender is not Label label) return Task.CompletedTask;
if (label.Tag is not SocketMessage Message) return Task.CompletedTask;
DateTime time = new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(Message.Id >> 22).ToLocalTime();
Label m = new()
{
Text = time.ToString("h:mm tt"),
Location = new(15, label.Location.Y + 1),
};
Controls.Add(m);
Labels.Add(m);
return Task.CompletedTask;
}
}

View File

@ -12,7 +12,7 @@ public class Login : UserControl
{
Size = new(481, 838);
Controls.Add(new Rectangle(new Texture(Tools.GetResourceBytes(Assembly.GetExecutingAssembly(), "Luski.Resources.Textures.Luski.png"))) { Location = new(103,8), Size = new(276, 289)});
Controls.Add(new Label() { Location = new(173,305), Text = "Luski", PixelHeight = 18, Color = new(0.9529f, 0.46666f, 0.203921569f, 1f) });
Controls.Add(new Label() { Location = new(173,305), Text = "Luski", Color = new(0.9529f, 0.46666f, 0.203921569f, 1f) });
Controls.Add(new Label() { Location = new(34,395), Text = "Email"});
Controls.Add(new Textbox() { Location = new(41,431), Size = new(401,41), InsideColor = new(28,28,28,255), BorderColor = Color4.DarkCyan });
Controls.Add(new Label() { Location = new(34,521), Text = "Password" });

View File

@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GraphicsManager" Version="1.0.0-alpha7" />
<PackageReference Include="GraphicsManager" Version="1.0.0-alpha9" />
<PackageReference Include="Luski.net" Version="1.1.0-alpha" />
</ItemGroup>