144 lines
5.8 KiB
C#
144 lines
5.8 KiB
C#
using GraphicsManager.Enums;
|
|
using GraphicsManager.Interfaces;
|
|
using GraphicsManager.Objects;
|
|
using Luski.GUI.MainScreen.Interfaces;
|
|
using Luski.net;
|
|
using Luski.net.Enums;
|
|
using Luski.net.JsonTypes;
|
|
using OpenTK.Mathematics;
|
|
using OpenTK.Windowing.Common;
|
|
using OpenTK.Windowing.GraphicsLibraryFramework;
|
|
|
|
namespace Luski.GUI.MainScreen.UI;
|
|
|
|
public class Chat : UserControl
|
|
{
|
|
public FlowLayout MessageFlow;
|
|
private UserControl titlecon, typecon;
|
|
private Label title, desc;
|
|
private Textbox tb;
|
|
private SocketTextChannel? Channel = null;
|
|
public Chat(MainScreen screen)
|
|
{
|
|
screen.MainShow += ScreenOnMainShow;
|
|
Size = new((int)(754 * Globals.Settings.Scale), (int)(667 * Globals.Settings.Scale));
|
|
BackgroundColor = new(50, 50, 50, 255);
|
|
Anchor = ObjectAnchor.All;
|
|
Controls.Add(MessageFlow = new()
|
|
{
|
|
Size = new((int)(754 * Globals.Settings.Scale), (int)(586 * Globals.Settings.Scale)),
|
|
Location = new(0, (int)(40 * Globals.Settings.Scale)),
|
|
BackgroundColor = new(40,40,40,255),
|
|
Anchor = ObjectAnchor.All,
|
|
HScrollPixels = Globals.Settings.PerScrollPixels
|
|
});
|
|
Controls.Add(titlecon = new UserControl(){Anchor = ObjectAnchor.Left | ObjectAnchor.Top | ObjectAnchor.Right, Size = new((int)(754 * Globals.Settings.Scale), (int)(40 * Globals.Settings.Scale)), BackgroundColor = BackgroundColor});
|
|
Controls.Add(typecon = new UserControl(){Anchor = ObjectAnchor.Left | ObjectAnchor.Bottom | ObjectAnchor.Right, Location = new(0, (int)(626 * Globals.Settings.Scale)), Size = new((int)(754 * Globals.Settings.Scale), (int)(41 * Globals.Settings.Scale)), BackgroundColor = BackgroundColor});
|
|
titlecon.Controls.Add(title = new Label()
|
|
{
|
|
Font = Globals.DefaultFont, Location = new(
|
|
(int)((20 - (Globals.DefaultFont.PixelHeight / 2)) * Globals.Settings.Scale),
|
|
(int)((20 * Globals.Settings.Scale) - (Globals.DefaultFont.PixelHeight / 2)))
|
|
});
|
|
titlecon.Controls.Add(desc = new Label(){ Font = Globals.DefaultFont, Color = new(161,161,161,255), Location = new(title.Location.X + title.Size.X + 5, title.Location.Y)});
|
|
typecon.Controls.Add(tb = new Textbox()
|
|
{
|
|
Font = Globals.DefaultFont,
|
|
InsideColor = new(28, 28, 28, 255),
|
|
BorderColor = Color4.DarkCyan,
|
|
Location = new((int)(7.5 * Globals.Settings.Scale)),
|
|
Size = new((int)(739 * Globals.Settings.Scale), (int)(26 * Globals.Settings.Scale)),
|
|
Anchor = ObjectAnchor.All
|
|
});
|
|
tb.KeyPress += TbOnKeyPress;
|
|
}
|
|
|
|
private Task ScreenOnMainShow()
|
|
{
|
|
Globals.Luski.MessageReceived += LuskiOnMessageReceived;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private Task TbOnKeyPress(KeyboardKeyEventArgs arg)
|
|
{
|
|
if (arg.Key != Keys.Enter && arg.Key != Keys.KeyPadEnter) return Task.CompletedTask;
|
|
Thread t = new(() => Thr());
|
|
t.Start();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void Thr()
|
|
{
|
|
Globals.Luski.SendMessage(tb.Text, Channel!.Id, CancellationToken.None);
|
|
Window!.Invoke(new Action(() => { tb.Text = string.Empty; }));
|
|
}
|
|
|
|
private Task LuskiOnMessageReceived(SocketMessage arg)
|
|
{
|
|
if (Channel!.Id != arg.ChannelID) return Task.CompletedTask;
|
|
IRenderObject? reff = null;
|
|
if (MessageFlow.Controls.Length > 0) reff = MessageFlow.Controls[MessageFlow.Controls.Length - 1];
|
|
AddMessage(arg);
|
|
if (reff is null || (reff.Location.Y + reff.Size.Y <= MessageFlow.Size.Y && reff.Location.X >= 0)) Window.Invoke(new Action(() => { MessageFlow.ScrollToBottom();}));
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
MessageFlow.Controls.Clear();
|
|
lastm = null;
|
|
lastUser = null;
|
|
}
|
|
|
|
public void UpdateTitle(IChannelPick channelPick)
|
|
{
|
|
if (Channel is not null && Channel!.Id == channelPick.Channel.Id) return;
|
|
Channel = channelPick.Channel;
|
|
title.Text = channelPick.Channel.Title;
|
|
tb.WatermarkFont = Globals.DefaultFont;
|
|
if (channelPick.Channel.Type == ChannelType.DM)
|
|
title.Text = (channelPick.Channel as SocketDMChannel)!.User.Username;
|
|
tb.WatermarkText = "Message " + title.Text;
|
|
if (channelPick.Channel.Description is not null)
|
|
{
|
|
desc.Visible = true;
|
|
desc.Text = channelPick.Channel.Description;
|
|
desc.Location = new(title.Location.X + title.Size.X + 5, title.Location.Y);
|
|
}
|
|
else
|
|
{
|
|
desc.Visible = false;
|
|
}
|
|
}
|
|
|
|
private SocketMessage? lastm = null;
|
|
private long? lastUser = null;
|
|
private ChatMessage? LastChatMessage = null;
|
|
|
|
public void AddMessage(SocketMessage Message)
|
|
{
|
|
bool hasbeentenmin = false;
|
|
if (lastm is not null)
|
|
hasbeentenmin =
|
|
new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(lastm.Id >> 22).ToLocalTime().AddMinutes(10) <
|
|
new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(Message.Id >> 22).ToLocalTime();
|
|
lastm = Message;
|
|
if (lastUser is null || lastUser != Message.AuthorID || hasbeentenmin)
|
|
{
|
|
if (Window is null || !Window.InvokeRequired)
|
|
MessageFlow.Controls.Add(LastChatMessage = new ChatMessage(Message));
|
|
else
|
|
Window!.Invoke(new Action(() => { MessageFlow.Controls.Add(LastChatMessage = new ChatMessage(Message)); Window.DrawFrame(); }));
|
|
}
|
|
else
|
|
{
|
|
if (Window is null || !Window.InvokeRequired)
|
|
LastChatMessage!.AddMessage(Message);
|
|
else
|
|
Window!.Invoke(new Action(() => { LastChatMessage!.AddMessage(Message); Window!.DrawFrame(); }));
|
|
}
|
|
|
|
lastUser = Message.AuthorID;
|
|
}
|
|
} |