Skip to content

Commit

Permalink
Merge pull request #151 from kou-yeung/feature/make_dyn_call_20240515
Browse files Browse the repository at this point in the history
support WASM table ( #149 )
  • Loading branch information
kou-yeung authored May 15, 2024
2 parents 1003214 + 9308d1c commit 63119e6
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 139 deletions.
250 changes: 130 additions & 120 deletions Assets/WebGLSupport/WebGLInput/WebGLInput.jslib
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
var WebGLInput = {
$instances: [],
WebGLInputInit : function() {
// Remove the `Runtime` object from "v1.37.27: 12/24/2017"
// if Runtime not defined. create and add functon!!
if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
},
WebGLInputInit : function() {
// use WebAssembly.Table : makeDynCall
// when enable. dynCall is undefined
if(typeof dynCall === "undefined")
{
// make Runtime.dynCall to undefined
Runtime = { dynCall : undefined }
}
else
{
// Remove the `Runtime` object from "v1.37.27: 12/24/2017"
// if Runtime not defined. create and add functon!!
if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
}
},
WebGLInputCreate: function (canvasId, x, y, width, height, fontsize, text, placeholder, isMultiLine, isPassword, isHidden, isMobile) {

var container = document.getElementById(UTF8ToString(canvasId));
Expand All @@ -17,172 +27,172 @@ var WebGLInput = {
container = canvas.parentNode;
}

if(canvas)
{
var scaleX = container.offsetWidth / canvas.width;
var scaleY = container.offsetHeight / canvas.height;
if(canvas)
{
var scaleX = container.offsetWidth / canvas.width;
var scaleY = container.offsetHeight / canvas.height;

if(scaleX && scaleY)
{
x *= scaleX;
width *= scaleX;
y *= scaleY;
height *= scaleY;
}
}
if(scaleX && scaleY)
{
x *= scaleX;
width *= scaleX;
y *= scaleY;
height *= scaleY;
}
}

var input = document.createElement(isMultiLine?"textarea":"input");
input.style.position = "absolute";

if(isMobile) {
input.style.bottom = 1 + "vh";
input.style.left = 5 + "vw";
input.style.width = 90 + "vw";
input.style.height = (isMultiLine? 18 : 10) + "vh";
input.style.fontSize = 5 + "vh";
input.style.borderWidth = 5 + "px";
input.style.borderColor = "#000000";
} else {
input.style.top = y + "px";
input.style.left = x + "px";
input.style.width = width + "px";
input.style.height = height + "px";
input.style.fontSize = fontsize + "px";
}
if(isMobile) {
input.style.bottom = 1 + "vh";
input.style.left = 5 + "vw";
input.style.width = 90 + "vw";
input.style.height = (isMultiLine? 18 : 10) + "vh";
input.style.fontSize = 5 + "vh";
input.style.borderWidth = 5 + "px";
input.style.borderColor = "#000000";
} else {
input.style.top = y + "px";
input.style.left = x + "px";
input.style.width = width + "px";
input.style.height = height + "px";
input.style.fontSize = fontsize + "px";
}

input.style.outlineWidth = 1 + 'px';
input.style.opacity = isHidden?0:1;
input.style.resize = 'none'; // for textarea
input.style.padding = '0px 1px';
input.style.cursor = "default";
input.style.touchAction = 'none';
input.style.outlineWidth = 1 + 'px';
input.style.opacity = isHidden?0:1;
input.style.resize = 'none'; // for textarea
input.style.padding = '0px 1px';
input.style.cursor = "default";
input.style.touchAction = 'none';

input.spellcheck = false;
input.value = UTF8ToString(text);
input.placeholder = UTF8ToString(placeholder);
input.style.outlineColor = 'black';
if(isPassword){
input.type = 'password';
}
input.spellcheck = false;
input.value = UTF8ToString(text);
input.placeholder = UTF8ToString(placeholder);
input.style.outlineColor = 'black';
if(isPassword){
input.type = 'password';
}

if(isMobile) {
document.body.appendChild(input);
} else {
container.appendChild(input);
}
if(isMobile) {
document.body.appendChild(input);
} else {
container.appendChild(input);
}
return instances.push(input) - 1;
},
WebGLInputEnterSubmit: function(id, falg){
var input = instances[id];
// for enter key
input.addEventListener('keydown', function(e) {
if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
if(falg)
{
e.preventDefault();
input.blur();
}
}
});
},
WebGLInputTab:function(id, cb) {
var input = instances[id];
// for tab key
WebGLInputEnterSubmit: function(id, falg){
var input = instances[id];
// for enter key
input.addEventListener('keydown', function(e) {
if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
if(falg)
{
e.preventDefault();
input.blur();
}
}
});
},
WebGLInputTab:function(id, cb) {
var input = instances[id];
// for tab key
input.addEventListener('keydown', function (e) {
if ((e.which && e.which === 9) || (e.keyCode && e.keyCode === 9)) {
e.preventDefault();

// if enable tab text
if(input.enableTabText){
// if enable tab text
if(input.enableTabText){
var val = input.value;
var start = input.selectionStart;
var end = input.selectionEnd;
input.value = val.substr(0, start) + '\t' + val.substr(end, val.length);
input.setSelectionRange(start + 1, start + 1);
input.oninput(); // call oninput to exe ValueChange function!!
} else {
Runtime.dynCall("vii", cb, [id, e.shiftKey ? -1 : 1]);
}
} else {
(!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, e.shiftKey ? -1 : 1]) : {{{ makeDynCall("vii", "cb") }}}(id, e.shiftKey ? -1 : 1);
}
}
});
},
WebGLInputFocus: function(id){
var input = instances[id];
input.focus();
});
},
WebGLInputFocus: function(id){
var input = instances[id];
input.focus();
},
WebGLInputOnFocus: function (id, cb) {
var input = instances[id];
input.onfocus = function () {
Runtime.dynCall("vi", cb, [id]);
(!!Runtime.dynCall) ? Runtime.dynCall("vi", cb, [id]) : {{{ makeDynCall("vi", "cb") }}}(id);
};
},
WebGLInputOnBlur: function (id, cb) {
var input = instances[id];
input.onblur = function () {
Runtime.dynCall("vi", cb, [id]);
(!!Runtime.dynCall) ? Runtime.dynCall("vi", cb, [id]) : {{{ makeDynCall("vi", "cb") }}}(id);
};
},
WebGLInputIsFocus: function (id) {
return instances[id] === document.activeElement;
},
WebGLInputOnValueChange:function(id, cb){
WebGLInputIsFocus: function (id) {
return instances[id] === document.activeElement;
},
WebGLInputOnValueChange:function(id, cb){
var input = instances[id];
input.oninput = function () {
var returnStr = input.value;
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
Runtime.dynCall("vii", cb, [id, buffer]);
var returnStr = input.value;
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
(!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, buffer]) : {{{ makeDynCall("vii", "cb") }}}(id, buffer);
};
},
WebGLInputOnEditEnd:function(id, cb){
WebGLInputOnEditEnd:function(id, cb){
var input = instances[id];
input.onchange = function () {
var returnStr = input.value;
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
Runtime.dynCall("vii", cb, [id, buffer]);
var returnStr = input.value;
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
(!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, buffer]) : {{{ makeDynCall("vii", "cb") }}}(id, buffer);
};
},
WebGLInputSelectionStart:function(id){
WebGLInputSelectionStart:function(id){
var input = instances[id];
return input.selectionStart;
},
WebGLInputSelectionEnd:function(id){
return input.selectionStart;
},
WebGLInputSelectionEnd:function(id){
var input = instances[id];
return input.selectionEnd;
},
WebGLInputSelectionDirection:function(id){
return input.selectionEnd;
},
WebGLInputSelectionDirection:function(id){
var input = instances[id];
return (input.selectionDirection == "backward")?-1:1;
},
WebGLInputSetSelectionRange:function(id, start, end){
var input = instances[id];
input.setSelectionRange(start, end);
},
WebGLInputMaxLength:function(id, maxlength){
return (input.selectionDirection == "backward")?-1:1;
},
WebGLInputSetSelectionRange:function(id, start, end){
var input = instances[id];
input.maxLength = maxlength;
},
WebGLInputText:function(id, text){
input.setSelectionRange(start, end);
},
WebGLInputMaxLength:function(id, maxlength){
var input = instances[id];
input.maxLength = maxlength;
},
WebGLInputText:function(id, text){
var input = instances[id];
input.value = UTF8ToString(text);
},
WebGLInputDelete:function(id){
input.value = UTF8ToString(text);
},
WebGLInputDelete:function(id){
var input = instances[id];
input.parentNode.removeChild(input);
instances[id] = null;
},
WebGLInputEnableTabText:function(id, enable) {
WebGLInputEnableTabText:function(id, enable) {
var input = instances[id];
input.enableTabText = enable;
},
WebGLInputForceBlur:function(id) {
input.enableTabText = enable;
},
WebGLInputForceBlur:function(id) {
var input = instances[id];
input.blur();
},
input.blur();
},
}

autoAddDeps(WebGLInput, '$instances');
Expand Down
Loading

0 comments on commit 63119e6

Please sign in to comment.