180 lines
4.8 KiB
C#
Raw Normal View History

2021-07-07 13:36:25 +02:00
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
2022-05-22 15:55:26 +08:00
using RageCoop.Core;
2021-07-07 13:36:25 +02:00
using GTA;
using GTA.Native;
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-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-03-28 15:08:30 +02:00
public bool Focused
2021-07-07 13:36:25 +02:00
{
get { return CurrentFocused; }
set
{
2021-07-12 20:28:26 -03:00
if (value && Hidden)
{
Hidden = false;
}
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; }
private bool Hidden
{
get { return CurrentHidden; }
set
{
2021-08-26 17:01:32 +02:00
if (value)
{
2021-08-26 17:01:32 +02: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;
}
}
2021-07-07 13:36:25 +02:00
public Chat()
{
MainScaleForm = new Scaleform("multiplayer_chat");
}
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
{
2021-11-19 22:08:15 +01:00
if ((Util.GetTickCount64() - LastMessageTime) > 15000 && !Focused && !Hidden)
2021-07-12 20:28:26 -03:00
{
Hidden = true;
}
if (!Hidden)
{
MainScaleForm.Render2D();
}
2021-07-07 13:36:25 +02:00
if (!CurrentFocused)
{
return;
}
Function.Call(Hash.DISABLE_ALL_CONTROL_ACTIONS, 0);
}
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;
}
if (key == Keys.PageUp)
{
MainScaleForm.CallFunction("PAGE_UP");
}
else if (key == Keys.PageDown)
{
MainScaleForm.CallFunction("PAGE_DOWN");
}
string keyChar = GetCharFromKey(key, Game.IsKeyPressed(Keys.ShiftKey), false);
if (keyChar.Length == 0)
{
return;
}
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
}
return;
case (char)13:
MainScaleForm.CallFunction("ADD_TEXT", "ENTER");
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,
2021-07-07 13:36:25 +02:00
[Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
StringBuilder receivingBuffer,
int bufferSize, uint flags, IntPtr kblayout);
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
{
StringBuilder buf = new StringBuilder(256);
byte[] keyboardState = new byte[256];
if (shift)
{
keyboardState[(int)Keys.ShiftKey] = 0xff;
}
if (altGr)
{
keyboardState[(int)Keys.ControlKey] = 0xff;
keyboardState[(int)Keys.Menu] = 0xff;
}
ToUnicodeEx((uint)key, 0, keyboardState, buf, 256, 0, InputLanguage.CurrentInputLanguage.Handle);
return buf.ToString();
}
}
}