-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathIMEHandler.cs
More file actions
224 lines (178 loc) · 7.82 KB
/
IMEHandler.cs
File metadata and controls
224 lines (178 loc) · 7.82 KB
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
#if WINDOWS
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Rampastring.XNAUI;
using Rampastring.XNAUI.Input;
using Rampastring.XNAUI.XNAControls;
namespace TSMapEditor.UI.IME;
public abstract class IMEHandler : IIMEHandler
{
bool IIMEHandler.TextCompositionEnabled => TextCompositionEnabled;
public abstract bool TextCompositionEnabled { get; protected set; }
private XNATextBox? _IMEFocus = null;
public XNATextBox? IMEFocus
{
get => _IMEFocus;
protected set
{
_IMEFocus = value;
Debug.Assert(!_IMEFocus?.IMEDisabled ?? true, "IME focus should not be assigned from a textbox with IME disabled");
}
}
private string _composition = string.Empty;
public string Composition
{
get => _composition;
protected set
{
string old = _composition;
_composition = value;
OnCompositionChanged(old, value);
}
}
public bool CompositionEmpty => string.IsNullOrEmpty(_composition);
/// <summary>
/// Indicates whether an IME event has been received ever. Used to distinguish IME users from non-IME users.
/// </summary>
protected bool IMEEventReceived = false;
protected bool LastActionIMEChatInput = true;
private void OnCompositionChanged(string oldValue, string newValue)
{
//Debug.WriteLine($"IME: OnCompositionChanged: {newValue.Length - oldValue.Length}");
IMEEventReceived = true;
// It seems that OnIMETextInput() is always triggered after OnCompositionChanged(). We expect such a behavior.
LastActionIMEChatInput = false;
}
protected ConcurrentDictionary<XNATextBox, Action<char>?> TextBoxHandleChatInputCallbacks = [];
public virtual int CompositionCursorPosition { get; set; }
public static IMEHandler Create(Game game)
{
return new WinFormsIMEHandler(game);
}
public abstract void SetTextInputRectangle(Rectangle rectangle);
public abstract void StartTextComposition();
public abstract void StopTextComposition();
protected virtual void OnIMETextInput(char character)
{
//Debug.WriteLine($"IME: OnIMETextInput: {character} {(short)character}; IMEFocus is null? {IMEFocus == null}");
LastActionIMEChatInput = true;
if (IMEFocus != null)
{
TextBoxHandleChatInputCallbacks.TryGetValue(IMEFocus, out var handleChatInput);
handleChatInput?.Invoke(character);
}
}
public void SetIMETextInputRectangle(WindowManager manager)
{
// When the client window resizes, we should call SetIMETextInputRectangle()
if (manager.SelectedControl is XNATextBox textBox)
SetIMETextInputRectangle(textBox);
}
private void SetIMETextInputRectangle(XNATextBox sender)
{
WindowManager windowManager = sender.WindowManager;
Rectangle textBoxRect = sender.RenderRectangle();
double scaleRatio = windowManager.ScaleRatio;
Rectangle rect = new()
{
X = (int)(textBoxRect.X * scaleRatio + windowManager.SceneXPosition),
Y = (int)(textBoxRect.Y * scaleRatio + windowManager.SceneYPosition),
Width = (int)(textBoxRect.Width * scaleRatio),
Height = (int)(textBoxRect.Height * scaleRatio)
};
// The following code returns a more accurate location based on the current InputPosition.
// However, as SetIMETextInputRectangle() does not automatically update with changes in InputPosition
// (e.g., due to scrolling or mouse clicks altering the textbox's input position without shifting focus),
// accuracy becomes inconsistent. Sometimes it's precise, other times it's off,
// which is arguably worse than a consistent but manageable inaccuracy.
// This inconsistency could lead to a confusing user experience,
// as the input rectangle's position may not reliably reflect the current input position.
// Therefore, unless whenever InputPosition is changed, SetIMETextInputRectangle() is raised
// -- which requires more time to investigate and test, it's commented out for now.
//var vec = Renderer.GetTextDimensions(
// sender.Text.Substring(sender.TextStartPosition, sender.InputPosition),
// sender.FontIndex);
//rect.X += (int)(vec.X * scaleRatio);
SetTextInputRectangle(rect);
}
void IIMEHandler.OnSelectedChanged(XNATextBox sender)
{
if (sender.WindowManager.SelectedControl == sender)
{
StopTextComposition();
if (!sender.IMEDisabled && sender.Enabled && sender.Visible)
{
IMEFocus = sender;
// Update the location of IME based on the textbox
SetIMETextInputRectangle(sender);
StartTextComposition();
}
else
{
IMEFocus = null;
}
}
else if (sender.WindowManager.SelectedControl is not XNATextBox)
{
// Disable IME since the current selected control is not XNATextBox
IMEFocus = null;
StopTextComposition();
}
// Note: if sender.WindowManager.SelectedControl != sender and is XNATextBox,
// another OnSelectedChanged() will be triggered,
// so we do not need to handle this case
}
void IIMEHandler.RegisterXNATextBox(XNATextBox sender, Action<char>? handleCharInput)
=> TextBoxHandleChatInputCallbacks[sender] = handleCharInput;
void IIMEHandler.KillXNATextBox(XNATextBox sender)
=> TextBoxHandleChatInputCallbacks.TryRemove(sender, out _);
bool IIMEHandler.HandleScrollLeftKey(XNATextBox sender)
=> !CompositionEmpty;
bool IIMEHandler.HandleScrollRightKey(XNATextBox sender)
=> !CompositionEmpty;
bool IIMEHandler.HandleBackspaceKey(XNATextBox sender)
{
bool handled = !LastActionIMEChatInput;
LastActionIMEChatInput = true;
//Debug.WriteLine($"IME: HandleBackspaceKey: handled: {handled}");
return handled;
}
bool IIMEHandler.HandleDeleteKey(XNATextBox sender)
{
bool handled = !LastActionIMEChatInput;
LastActionIMEChatInput = true;
//Debug.WriteLine($"IME: HandleDeleteKey: handled: {handled}");
return handled;
}
bool IIMEHandler.GetDrawCompositionText(XNATextBox sender, out string composition, out int compositionCursorPosition)
{
if (IMEFocus != sender || CompositionEmpty)
{
composition = string.Empty;
compositionCursorPosition = 0;
return false;
}
composition = Composition;
compositionCursorPosition = CompositionCursorPosition;
return true;
}
bool IIMEHandler.HandleCharInput(XNATextBox sender, char input)
=> TextCompositionEnabled;
bool IIMEHandler.HandleEnterKey(XNATextBox sender)
=> false;
bool IIMEHandler.HandleEscapeKey(XNATextBox sender)
{
//Debug.WriteLine($"IME: HandleEscapeKey: handled: {IMEEventReceived}");
// This method disables the ESC handling of the TextBox as long as the user has ever used IME.
// This is because IME users often use ESC to cancel composition. Even if currently the composition is empty,
// the user still expects ESC to cancel composition rather than deleting the whole sentence.
// For example, the user might mistakenly hit ESC key twice to cancel composition -- deleting the whole sentence is definitely a heavy punishment for such a small mistake.
// Note: "!CompositionEmpty => IMEEventReceived" should hold, but just in case
return IMEEventReceived || !CompositionEmpty;
}
void IIMEHandler.OnTextChanged(XNATextBox sender) { }
}
#endif