|
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
|
|
|
using Unity.UIWidgets.gestures; |
|
|
|
using Unity.UIWidgets.ui; |
|
|
|
using UnityEngine; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public delegate RawInputKeyResponse GlobalKeyEventHandlerDelegate(RawKeyEvent rawEvt, bool enableCustomAction = false); |
|
|
|
|
|
|
|
public delegate RawInputKeyResponse GlobalKeyEventHandlerDelegate(RawKeyEvent rawEvt, |
|
|
|
bool enableCustomAction = false); |
|
|
|
|
|
|
|
public class RawInputKeyResponse { |
|
|
|
public readonly bool swallow; |
|
|
|
public readonly char input; |
|
|
|
|
|
|
this.input = input; |
|
|
|
this.inputAction = inputAction; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
false, |
|
|
|
false, |
|
|
|
evt.data.unityEvent.character, |
|
|
|
null); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
interface KeyboardDelegate: IDisposable { |
|
|
|
interface KeyboardDelegate : IDisposable { |
|
|
|
|
|
|
|
|
|
|
|
void setStyle(Dictionary<string, object> args); |
|
|
|
void setIMEPos(Offset imeGlobalPos); |
|
|
|
|
|
|
|
|
|
|
bool imeRequired(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TextEditingValue _value; |
|
|
|
|
|
|
|
public void show() { |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setEditableSizeAndTransform(Dictionary<string, object> args) { |
|
|
|
//todo
|
|
|
|
//todo
|
|
|
|
//todo
|
|
|
|
//todo
|
|
|
|
} |
|
|
|
|
|
|
|
Offset _editorWindowPosToScreenPos(Offset position) { |
|
|
|
|
|
|
|
|
|
|
public void setIMEPos(Offset imeGlobalPos) { |
|
|
|
var uiWidgetWindowAdapter = Window.instance; |
|
|
|
Offset screenPos = uiWidgetWindowAdapter != null |
|
|
|
? uiWidgetWindowAdapter.windowPosToScreenPos(imeGlobalPos) |
|
|
|
Offset screenPos = uiWidgetWindowAdapter != null |
|
|
|
? uiWidgetWindowAdapter.windowPosToScreenPos(imeGlobalPos) |
|
|
|
|
|
|
|
|
|
|
|
Input.compositionCursorPos = new Vector2(screenPos.dx, screenPos.dy); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
if (_client == 0) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var currentEvent = PointerEventConverter.KeyEvent; |
|
|
|
Debug.Log($"event: {currentEvent}"); |
|
|
|
var oldValue = _value; |
|
|
|
|
|
|
|
|
|
|
|
while (!PointerEventConverter.KeyEvent.isEmpty()) { |
|
|
|
var currentEvent = PointerEventConverter.KeyEvent.Dequeue(); |
|
|
|
Debug.Log($"event: {currentEvent} c: {currentEvent.character}"); |
|
|
|
var oldValue = _value; |
|
|
|
if (currentEvent.modifiers == EventModifiers.FunctionKey) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
if (currentEvent != null && currentEvent.type == EventType.KeyDown) { |
|
|
|
var response = TextInput._handleGlobalInputKey(_client, |
|
|
|
new RawKeyDownEvent(new RawKeyEventData(currentEvent))); |
|
|
|
if (currentEvent != null && currentEvent.type == EventType.KeyDown) { |
|
|
|
var response = TextInput._handleGlobalInputKey(_client, |
|
|
|
new RawKeyDownEvent(new RawKeyEventData(currentEvent))); |
|
|
|
if (response.swallow) { |
|
|
|
if (response.inputAction != null) { |
|
|
|
Window.instance.run( |
|
|
|
() => { TextInput._performAction(_client, response.inputAction.Value); }); |
|
|
|
} |
|
|
|
if (response.swallow) { |
|
|
|
if (response.inputAction != null) { |
|
|
|
Window.instance.run(() => { TextInput._performAction(_client, response.inputAction.Value); }); |
|
|
|
if (_validateCharacter(response.input)) { |
|
|
|
_value = _value.insert(new string(response.input, 1)); |
|
|
|
} |
|
|
|
|
|
|
|
if (_validateCharacter(response.input)) { |
|
|
|
_value = _value.insert(new string(response.input, 1)); |
|
|
|
else if (currentEvent.keyCode == KeyCode.Backspace) { |
|
|
|
if (_value.selection.isValid) { |
|
|
|
_value = _value.deleteSelection(true); |
|
|
|
} |
|
|
|
} else if (currentEvent.keyCode == KeyCode.Backspace) { |
|
|
|
if (_value.selection.isValid) { |
|
|
|
_value = _value.deleteSelection(true); |
|
|
|
else if (currentEvent.character != '\0') { |
|
|
|
_value = _value.clearCompose(); |
|
|
|
char ch = currentEvent.character; |
|
|
|
if (ch == '\r' || ch == 3) { |
|
|
|
ch = '\n'; |
|
|
|
} |
|
|
|
|
|
|
|
if (ch == '\n') { |
|
|
|
Window.instance.run(() => { TextInput._performAction(_client, TextInputAction.newline); }); |
|
|
|
} |
|
|
|
|
|
|
|
if (_validateCharacter(ch)) { |
|
|
|
_value = _value.insert(new string(ch, 1)); |
|
|
|
} |
|
|
|
} else if (currentEvent.character != '\0') { |
|
|
|
_value = _value.clearCompose(); |
|
|
|
char ch = currentEvent.character; |
|
|
|
if (ch == '\r' || ch == 3) { |
|
|
|
ch = '\n'; |
|
|
|
else if (!string.IsNullOrEmpty(currentEvent.keyCode.ToString())) { |
|
|
|
isIMEInput = true; |
|
|
|
Debug.Log($"event:? {currentEvent}"); |
|
|
|
_value = _value.compose(currentEvent.keyCode.ToString()); |
|
|
|
if (ch == '\n') { |
|
|
|
Window.instance.run(() => { TextInput._performAction(_client, TextInputAction.newline); }); |
|
|
|
} |
|
|
|
|
|
|
|
if (_validateCharacter(ch)) { |
|
|
|
_value = _value.insert(new string(ch, 1)); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (!string.IsNullOrEmpty(currentEvent.keyCode.ToString())) { |
|
|
|
isIMEInput = true; |
|
|
|
Debug.Log($"event:? {currentEvent}"); |
|
|
|
_value = _value.compose(currentEvent.keyCode.ToString()); |
|
|
|
currentEvent.Use(); |
|
|
|
|
|
|
|
currentEvent.Use(); |
|
|
|
} |
|
|
|
if (_value != oldValue) { |
|
|
|
if (this.isIMEInput) { |
|
|
|
var isIMEInput = this.isIMEInput; |
|
|
|
Timer.create(TimeSpan.Zero, () => { |
|
|
|
TextInput._updateEditingState(_client, _value, isIMEInput); |
|
|
|
}); |
|
|
|
this.isIMEInput = false; |
|
|
|
} |
|
|
|
else { |
|
|
|
Timer.create(TimeSpan.Zero, () => { |
|
|
|
TextInput._updateEditingState(_client, _value); |
|
|
|
}); |
|
|
|
if (_value != oldValue) { |
|
|
|
if (this.isIMEInput) { |
|
|
|
var isIMEInput = this.isIMEInput; |
|
|
|
Timer.create(TimeSpan.Zero, |
|
|
|
() => { TextInput._updateEditingState(_client, _value, isIMEInput); }); |
|
|
|
this.isIMEInput = false; |
|
|
|
} |
|
|
|
else { |
|
|
|
Timer.create(TimeSpan.Zero, () => { TextInput._updateEditingState(_client, _value); }); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
return ch >= ' ' || ch == '\t' || ch == '\r' || ch == 10 || ch == '\n'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class UnityTouchScreenKeyboardDelegate : KeyboardDelegate, TextInputUpdateListener { |
|
|
|
int _client; |
|
|
|
string _lastCompositionString; |
|
|
|
|
|
|
: TextSelection.collapsed(0) |
|
|
|
); |
|
|
|
var changed = _value != newValue; |
|
|
|
|
|
|
|
|
|
|
|
_value = newValue; |
|
|
|
if (changed) { |
|
|
|
Window.instance.run(() => { |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
abstract class AbstractUIWidgetsKeyboardDelegate : KeyboardDelegate { |
|
|
|
|
|
|
|
UIWidgetsMessageManager.instance. |
|
|
|
AddChannelMessageDelegate("TextInput", _handleMethodCall); |
|
|
|
UIWidgetsMessageManager.instance.AddChannelMessageDelegate("TextInput", _handleMethodCall); |
|
|
|
|
|
|
|
|
|
|
|
UIWidgetsMessageManager.instance. |
|
|
|
RemoveChannelMessageDelegate("TextInput", _handleMethodCall); |
|
|
|
UIWidgetsMessageManager.instance.RemoveChannelMessageDelegate("TextInput", _handleMethodCall); |
|
|
|
} |
|
|
|
|
|
|
|
public abstract void show(); |
|
|
|
|
|
|
public abstract void setEditingState(TextEditingValue value); |
|
|
|
|
|
|
|
public void setEditableSizeAndTransform(Dictionary<string, object> args) { |
|
|
|
throw new NotImplementedException(); |
|
|
|
} |
|
|
|
|
|
|
public abstract void setClient(int client, TextInputConfiguration configuration); |
|
|
|
|
|
|
|
public abstract void clearClient(); |
|
|
|
|
|
|
|
public virtual bool imeRequired() { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*if (TextInput._currentConnection == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
throw new UIWidgetsError($"unknown method ${method}"); |
|
|
|
} |
|
|
|
}*/ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if UNITY_WEBGL
|
|
|
|
class UIWidgetsWebGLKeyboardDelegate : AbstractUIWidgetsKeyboardDelegate { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if UNITY_IOS || UNITY_ANDROID
|
|
|
|
class UIWidgetsTouchScreenKeyboardDelegate : AbstractUIWidgetsKeyboardDelegate { |
|
|
|
|
|
|
|
|
|
|
#elif UNITY_ANDROID
|
|
|
|
internal static void UIWidgetsTextInputShow() { |
|
|
|
using ( |
|
|
|
AndroidJavaClass pluginClass = new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
AndroidJavaClass pluginClass = |
|
|
|
new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
) { |
|
|
|
pluginClass.CallStatic("show"); |
|
|
|
} |
|
|
|
|
|
|
using ( |
|
|
|
AndroidJavaClass pluginClass = new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
AndroidJavaClass pluginClass = |
|
|
|
new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
) { |
|
|
|
pluginClass.CallStatic("hide"); |
|
|
|
} |
|
|
|
|
|
|
using ( |
|
|
|
AndroidJavaClass pluginClass = new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
AndroidJavaClass pluginClass = |
|
|
|
new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
) { |
|
|
|
|
|
|
|
pluginClass.CallStatic("setClient", client, configuration); |
|
|
|
|
|
|
internal static void UIWidgetsTextInputSetTextInputEditingState(string jsonText) { |
|
|
|
using ( |
|
|
|
AndroidJavaClass pluginClass = new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
AndroidJavaClass pluginClass = |
|
|
|
new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
) { |
|
|
|
pluginClass.CallStatic("setEditingState", jsonText); |
|
|
|
} |
|
|
|
|
|
|
using ( |
|
|
|
AndroidJavaClass pluginClass = new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
AndroidJavaClass pluginClass = |
|
|
|
new AndroidJavaClass("com.unity.uiwidgets.plugin.editing.TextInputPlugin") |
|
|
|
) { |
|
|
|
pluginClass.CallStatic("clearClient"); |
|
|
|
} |
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
} |