Files
RAGECOOP-V/Client/Scripts/Networking/Chat.cs

154 lines
4.3 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2021-07-07 13:36:25 +02:00
using System.Runtime.InteropServices;
using System.Text;
2022-10-23 19:02:39 +08:00
using GTA;
using GTA.Native;
2021-07-07 13:36:25 +02:00
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
2021-07-07 13:36:25 +02:00
{
internal class Chat
2021-07-07 13:36:25 +02:00
{
private readonly Scaleform MainScaleForm;
2022-10-23 19:02:39 +08:00
public Chat()
{
MainScaleForm = new Scaleform("multiplayer_chat");
}
2022-03-28 15:08:30 +02:00
public string CurrentInput { get; set; }
2021-07-07 13:36:25 +02:00
private bool CurrentFocused { get; set; }
2022-10-23 19:02:39 +08:00
2022-03-28 15:08:30 +02:00
public bool Focused
2021-07-07 13:36:25 +02:00
{
2022-09-06 21:46:35 +08:00
get => CurrentFocused;
2021-07-07 13:36:25 +02:00
set
{
2022-10-23 19:02:39 +08:00
if (value && Hidden) Hidden = false;
2021-07-12 20:28:26 -03:00
2021-07-07 13:36:25 +02:00
MainScaleForm.CallFunction("SET_FOCUS", value ? 2 : 1, 2, "ALL");
CurrentFocused = value;
}
}
2021-11-19 22:08:15 +01:00
private ulong LastMessageTime { get; set; }
2021-07-12 20:28:26 -03:00
private bool CurrentHidden { get; set; }
2022-10-23 19:02:39 +08:00
2021-07-12 20:28:26 -03:00
private bool Hidden
{
2022-09-06 21:46:35 +08:00
get => CurrentHidden;
2021-07-12 20:28:26 -03:00
set
{
2021-08-26 17:01:32 +02:00
if (value)
{
2022-10-23 19:02:39 +08:00
if (!CurrentHidden) MainScaleForm.CallFunction("hide");
}
2021-08-26 17:01:32 +02:00
else if (CurrentHidden)
{
MainScaleForm.CallFunction("showFeed");
}
2021-07-12 20:28:26 -03:00
CurrentHidden = value;
}
}
2022-03-28 15:08:30 +02:00
public void Init()
2021-07-07 13:36:25 +02:00
{
MainScaleForm.CallFunction("SET_FOCUS", 2, 2, "ALL");
MainScaleForm.CallFunction("SET_FOCUS", 1, 2, "ALL");
}
2022-03-28 15:08:30 +02:00
public void Clear()
2021-07-07 13:36:25 +02:00
{
MainScaleForm.CallFunction("RESET");
}
2022-03-28 15:08:30 +02:00
public void Tick()
2021-07-07 13:36:25 +02:00
{
2022-10-23 19:02:39 +08:00
if (Util.GetTickCount64() - LastMessageTime > 15000 && !Focused && !Hidden) Hidden = true;
2021-07-12 20:28:26 -03:00
2022-10-23 19:02:39 +08:00
if (!Hidden) MainScaleForm.Render2D();
2021-07-07 13:36:25 +02:00
2022-10-23 19:02:39 +08:00
if (!CurrentFocused) return;
2021-07-07 13:36:25 +02:00
Call(DISABLE_ALL_CONTROL_ACTIONS, 0);
2021-07-07 13:36:25 +02:00
}
2022-03-28 15:08:30 +02:00
public void AddMessage(string sender, string msg)
2021-07-07 13:36:25 +02:00
{
MainScaleForm.CallFunction("ADD_MESSAGE", sender + ":", msg);
2021-11-19 22:08:15 +01:00
LastMessageTime = Util.GetTickCount64();
2021-07-12 20:28:26 -03:00
Hidden = false;
2021-07-07 13:36:25 +02:00
}
2022-03-28 15:08:30 +02:00
public void OnKeyDown(Keys key)
2021-07-07 13:36:25 +02:00
{
if (key == Keys.Escape)
{
Focused = false;
CurrentInput = "";
return;
}
2022-07-20 17:50:01 +08:00
2021-07-07 13:36:25 +02:00
if (key == Keys.PageUp)
MainScaleForm.CallFunction("PAGE_UP");
2022-10-23 19:02:39 +08:00
else if (key == Keys.PageDown) MainScaleForm.CallFunction("PAGE_DOWN");
2021-07-07 13:36:25 +02:00
2022-10-23 19:02:39 +08:00
var keyChar = GetCharFromKey(key, Game.IsKeyPressed(Keys.ShiftKey), false);
2021-07-07 13:36:25 +02:00
2022-10-23 19:02:39 +08:00
if (keyChar.Length == 0) return;
2021-07-07 13:36:25 +02:00
switch (keyChar[0])
{
case (char)8:
if (CurrentInput?.Length > 0)
2021-07-07 13:36:25 +02:00
{
2021-07-12 20:28:26 -03:00
CurrentInput = CurrentInput.Remove(CurrentInput.Length - 1);
MainScaleForm.CallFunction("DELETE_TEXT");
2021-07-07 13:36:25 +02:00
}
2022-10-23 19:02:39 +08:00
2021-07-07 13:36:25 +02:00
return;
case (char)13:
MainScaleForm.CallFunction("ADD_TEXT", "ENTER");
2022-10-23 19:02:39 +08:00
if (!string.IsNullOrWhiteSpace(CurrentInput)) Networking.SendChatMessage(CurrentInput);
2021-07-07 13:36:25 +02:00
Focused = false;
CurrentInput = "";
return;
default:
CurrentInput += keyChar;
MainScaleForm.CallFunction("ADD_TEXT", keyChar);
return;
}
}
[DllImport("user32.dll")]
2022-03-28 15:08:30 +02:00
public static extern int ToUnicodeEx(uint virtualKeyCode, uint scanCode, byte[] keyboardState,
2022-10-23 19:02:39 +08:00
[Out] [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
2021-07-07 13:36:25 +02:00
StringBuilder receivingBuffer,
int bufferSize, uint flags, IntPtr kblayout);
[DllImport("user32.dll")]
static extern IntPtr GetKeyboardLayout(uint idThread);
2022-03-28 15:08:30 +02:00
public static string GetCharFromKey(Keys key, bool shift, bool altGr)
2021-07-07 13:36:25 +02:00
{
2022-10-23 19:02:39 +08:00
var buf = new StringBuilder(256);
var keyboardState = new byte[256];
2021-07-07 13:36:25 +02:00
2022-10-23 19:02:39 +08:00
if (shift) keyboardState[(int)Keys.ShiftKey] = 0xff;
2021-07-07 13:36:25 +02:00
if (altGr)
{
keyboardState[(int)Keys.ControlKey] = 0xff;
keyboardState[(int)Keys.Menu] = 0xff;
}
ToUnicodeEx((uint)key, 0, keyboardState, buf, 256, 0, GetKeyboardLayout(0));
2021-07-07 13:36:25 +02:00
return buf.ToString();
}
}
2022-10-23 19:02:39 +08:00
}