首次提交
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#if UNITY_WEBGL && FAIRYGUI_WEBGL_TEXT_INPUT
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using AOT;
|
||||
using FairyGUI;
|
||||
using UnityEngine;
|
||||
|
||||
public static class WebGLTextInput
|
||||
{
|
||||
static WebGLTextInput()
|
||||
{
|
||||
WebGLTextInputInit(OnInput, OnBlur);
|
||||
}
|
||||
|
||||
public static void Start(InputTextField target)
|
||||
{
|
||||
WebGLTextInputSetText(target.text,
|
||||
!target.textField.singleLine,
|
||||
ColorUtility.ToHtmlStringRGBA(target.textFormat.color),
|
||||
target.textFormat.size,
|
||||
target.textFormat.font,
|
||||
target.maxLength);
|
||||
|
||||
WebGLInput.captureAllKeyboardInput = false;
|
||||
|
||||
SyncTransform(target);
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
{
|
||||
WebGLTextInputHide();
|
||||
}
|
||||
|
||||
public static void SyncTransform(InputTextField target)
|
||||
{
|
||||
Rect rect = target.TransformRect(new Rect(0, 0, target.width, target.height), null);
|
||||
rect.min = StageCamera.main.WorldToScreenPoint(rect.min);
|
||||
rect.max = StageCamera.main.WorldToScreenPoint(rect.max);
|
||||
rect.y = Screen.height - rect.y - rect.height;
|
||||
|
||||
WebGLTextInputShow(rect.x, rect.y, target.width, target.height,
|
||||
rect.width / target.width, rect.height / target.height);
|
||||
}
|
||||
|
||||
[MonoPInvokeCallback(typeof(Action<string>))]
|
||||
static void OnInput(string value)
|
||||
{
|
||||
var focus = Stage.inst.focus as InputTextField;
|
||||
if (focus != null)
|
||||
focus.ReplaceText(value);
|
||||
}
|
||||
|
||||
[MonoPInvokeCallback(typeof(Action))]
|
||||
static void OnBlur()
|
||||
{
|
||||
WebGLInput.captureAllKeyboardInput = true;
|
||||
|
||||
var focus = Stage.inst.focus as InputTextField;
|
||||
if (focus != null)
|
||||
Stage.inst.SetFocus(null, true);
|
||||
}
|
||||
|
||||
[DllImport("__Internal")]
|
||||
public static extern void WebGLTextInputInit(Action<string> onInputCallback, Action onBlurCallback);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
public static extern void WebGLTextInputSetText(string text, bool multiline, string color, int fontSize, string fontFace, int maxLength);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
public static extern void WebGLTextInputShow(float x, float y, float width, float height, float scaleX, float scaleY);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
public static extern void WebGLTextInputHide();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13531b8bb4ecf47a995ffa3e9c54ab0c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,107 @@
|
||||
var WebGLTextInput = {
|
||||
$canvasEle: null,
|
||||
$containerEle: null,
|
||||
$inputEle: null,
|
||||
$textareaEle: null,
|
||||
$callbacks: null,
|
||||
|
||||
WebGLTextInputInit: function (onInputCallback, onBlurCallback) {
|
||||
callbacks = { onInputCallback: onInputCallback, onBlurCallback: onBlurCallback };
|
||||
canvasEle = document.getElementById('unity-canvas');
|
||||
},
|
||||
|
||||
WebGLTextInputSetText: function (text, multiline, color, fontSize, fontFace, maxLength) {
|
||||
if (containerEle == null) {
|
||||
InitInput(inputEle = document.createElement("input"));
|
||||
InitInput(textareaEle = document.createElement("textarea"));
|
||||
|
||||
containerEle = document.createElement("div");
|
||||
containerEle.style.cssText = "transform-origin:0 0;-webkit-transform-origin:0 0;-moz-transform-origin:0 0;-o-transform-origin:0 0;";
|
||||
containerEle.style.position = "absolute";
|
||||
containerEle.style.zIndex = '1E5';
|
||||
containerEle.name = "WebGLTextInput";
|
||||
canvasEle.parentElement.appendChild(containerEle);
|
||||
}
|
||||
|
||||
inputEle.parentElement && (containerEle.removeChild(inputEle));
|
||||
textareaEle.parentElement && (containerEle.removeChild(textareaEle));
|
||||
|
||||
var current = multiline ? textareaEle : inputEle;
|
||||
containerEle.appendChild(current);
|
||||
containerEle.style.display = "";
|
||||
|
||||
current.maxLength = maxLength <= 0 ? 1E5 : maxLength;
|
||||
current.value = UTF8ToString(text);
|
||||
current.style.color = color;
|
||||
current.style.fontSize = fontSize + 'px';
|
||||
current.style.fontFamily = fontFace;
|
||||
current.focus();
|
||||
},
|
||||
|
||||
WebGLTextInputShow: function (x, y, width, height, scaleX, scaleY) {
|
||||
containerEle.style.top = y + "px";
|
||||
containerEle.style.left = x + "px";
|
||||
containerEle.style.width = width + "px";
|
||||
containerEle.style.height = height + "px";
|
||||
containerEle.style.transform = "scale(" + scaleX + "," + scaleY + ")";
|
||||
},
|
||||
|
||||
WebGLTextInputHide: function () {
|
||||
containerEle.style.display = "none";
|
||||
},
|
||||
|
||||
|
||||
$InitInput: function (input) {
|
||||
var style = input.style;
|
||||
style.position = "absolute";
|
||||
style.width = "100%";
|
||||
style.height = "100%";
|
||||
style.overflow = "hidden";
|
||||
style.resize = 'none';
|
||||
style.backgroundColor = 'transparent';
|
||||
style.border = 'none';
|
||||
style.outline = 'none';
|
||||
|
||||
var t = callbacks;
|
||||
input.addEventListener('input', function () {
|
||||
var returnStr = input.value;
|
||||
var bufferSize = lengthBytesUTF8(returnStr) + 1;
|
||||
var buffer = _malloc(bufferSize);
|
||||
stringToUTF8(returnStr, buffer, bufferSize);
|
||||
dynCall("vi", t.onInputCallback, [buffer]);
|
||||
});
|
||||
|
||||
input.addEventListener('blur', function () {
|
||||
if (input.parentElement)
|
||||
input.parentElement.style.display = "none";
|
||||
dynCall("v", t.onBlurCallback);
|
||||
});
|
||||
|
||||
if (input.tagName == "INPUT") {
|
||||
input.addEventListener('keydown', function (e) {
|
||||
if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
|
||||
e.preventDefault();
|
||||
input.blur();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var stopEvent = function (e) {
|
||||
if (e.type == 'touchmove')
|
||||
e.preventDefault();
|
||||
e.stopPropagation && e.stopPropagation();
|
||||
}
|
||||
|
||||
input.addEventListener('mousemove', stopEvent, { passive: false });
|
||||
input.addEventListener('mousedown', stopEvent, { passive: false });
|
||||
input.addEventListener('touchmove', stopEvent, { passive: false });
|
||||
},
|
||||
};
|
||||
|
||||
autoAddDeps(WebGLTextInput, '$canvasEle');
|
||||
autoAddDeps(WebGLTextInput, '$containerEle');
|
||||
autoAddDeps(WebGLTextInput, '$inputEle');
|
||||
autoAddDeps(WebGLTextInput, '$textareaEle');
|
||||
autoAddDeps(WebGLTextInput, "$callbacks");
|
||||
autoAddDeps(WebGLTextInput, '$InitInput');
|
||||
mergeInto(LibraryManager.library, WebGLTextInput);
|
||||
@@ -0,0 +1,36 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3613e6a08c3e07c47937410768813692
|
||||
timeCreated: 1565948029
|
||||
licenseType: Pro
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
Facebook: WebGL
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
- first:
|
||||
WebGL: WebGL
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user