-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatMode.cs
244 lines (221 loc) · 8.39 KB
/
ChatMode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;
using WindowsInput.Native;
using XSOverlay;
// ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
namespace KeyboardOSC;
public static class ChatMode
{
private static bool _isSilentMsg;
private static bool _isFirstMsg;
private static string _currentText = "";
private static string _lastMsg = "";
private static readonly ManualLogSource Logger = Plugin.PluginLogger;
private static TextMeshProUGUI _oscBarText;
private static TextMeshProUGUI _charCounter;
private static List<KeyboardKey> _currentlyDownStickyKeys = new();
private static Timer _eventsTimer = new(1300);
public static void HandleKey(KeyboardKey.VirtualKeyEventData eventData)
{
var scanCode = eventData.Sender.UsingRawVirtualKeyCode
? (uint) eventData.KeyCode[0]
: eventData.Sender.ScanCode[0];
var shiftedField = AccessTools.Field(typeof(KeyboardKey), "IsShifted");
var altedField = AccessTools.Field(typeof(KeyboardKey), "IsAlted");
var isShifted = (bool) shiftedField.GetValue(eventData.Sender);
var isAlted = (bool) altedField.GetValue(eventData.Sender); // altGr
foreach (var key in eventData.KeyCode)
{
var character = Tools.ConvertVirtualKeyToUnicode(key, scanCode, isShifted, isAlted);
ProcessKey(key, eventData, character);
}
}
private static void ProcessKey(VirtualKeyCode key, KeyboardKey.VirtualKeyEventData data, string character)
{
var isCtrlHeld = _currentlyDownStickyKeys
.Any(k => k.Key[0] is VirtualKeyCode.LCONTROL or VirtualKeyCode.RCONTROL);
var sendTyping = PluginSettings.GetSetting<bool>("TypingIndicator").Value;
var liveSendMode = PluginSettings.GetSetting<bool>("LiveSend").Value;
switch (key)
{
// backspace/delete keys
case VirtualKeyCode.BACK or VirtualKeyCode.DELETE:
{
if (_currentText.Length <= 0) return;
if (isCtrlHeld)
{
var lastSpaceIndex = _currentText.LastIndexOf(' ');
_currentText = lastSpaceIndex >= 0 ? _currentText.Substring(0, lastSpaceIndex) : "";
UpdateChatText(_currentText);
#if DEBUG
Logger.LogInfo("bulk deleting chat text: " + _currentText);
#endif
if (sendTyping) SendTyping(_currentText.Length != 0);
if (liveSendMode) _eventsTimer.Start();
return;
}
_currentText = _currentText.Remove(key is VirtualKeyCode.DELETE ? 0 : _currentText.Length - 1, 1);
if (sendTyping) SendTyping(_currentText.Length != 0);
if (liveSendMode) _eventsTimer.Start();
UpdateChatText(_currentText);
return;
}
// silent switch (no sound on send, no typing indicator)
case VirtualKeyCode.TAB:
_isSilentMsg = !_isSilentMsg;
UpdateChatColor();
Logger.LogInfo($"Silent mode: {_isSilentMsg}");
return;
// clear shortcut
case VirtualKeyCode.ESCAPE:
ClearInput();
Logger.LogInfo("Input cleared");
return;
case VirtualKeyCode.END:
Tools.SendOsc("/chatbox/input", string.Empty, true, false);
Logger.LogInfo("Chatbox cleared");
return;
case VirtualKeyCode.INSERT:
_currentText = _lastMsg;
UpdateChatText(_currentText);
_isFirstMsg = true;
Logger.LogInfo("Inserted last input");
return;
// copy + paste
case VirtualKeyCode.VK_C:
if (!isCtrlHeld) break;
GUIUtility.systemCopyBuffer = _currentText;
return;
case VirtualKeyCode.VK_V:
if (!isCtrlHeld) break;
_currentText += GUIUtility.systemCopyBuffer;
UpdateChatText(_currentText);
return;
// Send message (or clear for continuous)
case VirtualKeyCode.RETURN:
if (liveSendMode)
{
Logger.LogInfo($"Sending message: {_currentText.ReplaceShortcodes()} [^-^]");
_lastMsg = _currentText;
SendMessage(true);
ClearInput();
}
else
{
Logger.LogInfo($"Sending message: {_currentText.ReplaceShortcodes()}");
SendMessage();
ClearInput();
}
return;
}
// Normal character inputs
if (sendTyping) SendTyping(_currentText.Length != 0);
if (liveSendMode)
{
_eventsTimer.Start();
if (_currentText.IsNullOrWhiteSpace())
_isFirstMsg = true;
}
_currentText += character;
UpdateChatText(_currentText);
}
private static void TimerElapsed(object sender, ElapsedEventArgs e)
{
if (_isSilentMsg) return;
Logger.LogInfo("Timer elapsed, sending message");
if (_currentText.IsNullOrWhiteSpace())
{
Tools.SendOsc("/chatbox/input", string.Empty, true, false);
SendTyping(false);
}
var sendTyping = PluginSettings.GetSetting<bool>("TypingIndicator").Value;
SendMessage(true);
if (sendTyping) SendTyping(true);
}
private static void SendMessage(bool liveSend = false)
{
if (liveSend)
{
_eventsTimer.Stop();
Tools.SendOsc("/chatbox/input", _currentText.ReplaceShortcodes(), true, !_isSilentMsg || _isFirstMsg);
SendTyping(false);
_isFirstMsg = false;
}
else
{
Tools.SendOsc("/chatbox/input", _currentText.ReplaceShortcodes(), true, !_isSilentMsg);
SendTyping(false);
_lastMsg = _currentText;
ClearInput();
}
}
private static void ClearInput()
{
UpdateChatText(string.Empty);
_currentText = string.Empty;
_isSilentMsg = false;
UpdateChatColor();
Plugin.ReleaseStickyKeys.Invoke(Plugin.Instance.inputHandler, null);
}
private static void SendTyping(bool typing)
{
if (typing && _isSilentMsg) return;
Tools.SendOsc("/chatbox/typing", typing);
}
public static void Setup(TextMeshProUGUI barText, TextMeshProUGUI charCounter)
{
_oscBarText = barText;
_charCounter = charCounter;
_eventsTimer.Elapsed += TimerElapsed;
var stickyKeysField = AccessTools.Field(typeof(KeyboardInputHandler), "CurrentlyDownStickyKeys");
_currentlyDownStickyKeys = (List<KeyboardKey>) stickyKeysField.GetValue(Plugin.Instance.inputHandler);
}
private static void UpdateChatColor()
{
_oscBarText.color =
_isSilentMsg ? UIThemeHandler.Instance.T_WarningTone : UIThemeHandler.Instance.T_ConstrastingTone;
_charCounter.color = _currentText.Length switch
{
>= 120 => UIThemeHandler.Instance.T_ErrTone,
>= 85 => UIThemeHandler.Instance.T_WarningTone,
_ => UIThemeHandler.Instance.T_ConstrastingTone
};
}
private static void UpdateChatText(string text)
{
if (text.Length > 144)
{
text = text.Substring(0, 144);
}
XSTools.SetTMPUIText(_charCounter, $"{text.Length}/144");
UpdateChatColor();
XSTools.SetTMPUIText(_oscBarText, text);
}
private static string ReplaceShortcodes(this string input)
{
var shortcodes = new Dictionary<string, string>
{
{"//shrug", "¯\\_(ツ)_/¯"},
{"//happy", "(¬‿¬)"},
{"//table", "┬─┬"},
{"//music", "🎵"},
{"//cookie", "🍪"},
{"//star", "⭐"},
{"//hrt", "💗"},
{"//2hrt", "💕"},
{"//skull", "💀"},
{"//skull2", "☠"},
};
foreach (var shortcode in shortcodes)
{
input = input.Replace(shortcode.Key, shortcode.Value);
}
return input;
}
}