WebGLInput.jslib 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var WebGLInput = {
  2. $instances: [],
  3. WebGLInputInit : function() {
  4. // use WebAssembly.Table : makeDynCall
  5. // when enable. dynCall is undefined
  6. if(typeof dynCall === "undefined")
  7. {
  8. // make Runtime.dynCall to undefined
  9. Runtime = { dynCall : undefined }
  10. }
  11. else
  12. {
  13. // Remove the `Runtime` object from "v1.37.27: 12/24/2017"
  14. // if Runtime not defined. create and add functon!!
  15. if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
  16. }
  17. },
  18. WebGLInputCreate: function (canvasId, x, y, width, height, fontsize, text, placeholder, isMultiLine, isPassword, isHidden, isMobile) {
  19. var container = document.getElementById(UTF8ToString(canvasId));
  20. var canvas = container.getElementsByTagName('canvas')[0];
  21. // if container is null and have canvas
  22. if (!container && canvas)
  23. {
  24. // set the container to canvas.parentNode
  25. container = canvas.parentNode;
  26. }
  27. if(canvas)
  28. {
  29. var scaleX = container.offsetWidth / canvas.width;
  30. var scaleY = container.offsetHeight / canvas.height;
  31. if(scaleX && scaleY)
  32. {
  33. x *= scaleX;
  34. width *= scaleX;
  35. y *= scaleY;
  36. height *= scaleY;
  37. }
  38. }
  39. var input = document.createElement(isMultiLine?"textarea":"input");
  40. input.style.position = "absolute";
  41. if(isMobile) {
  42. input.style.bottom = 1 + "vh";
  43. input.style.left = 5 + "vw";
  44. input.style.width = 90 + "vw";
  45. input.style.height = (isMultiLine? 18 : 10) + "vh";
  46. input.style.fontSize = 5 + "vh";
  47. input.style.borderWidth = 5 + "px";
  48. input.style.borderColor = "#000000";
  49. } else {
  50. input.style.top = y + "px";
  51. input.style.left = x + "px";
  52. input.style.width = width + "px";
  53. input.style.height = height + "px";
  54. input.style.fontSize = fontsize + "px";
  55. }
  56. input.style.outlineWidth = 1 + 'px';
  57. input.style.opacity = isHidden?0:1;
  58. input.style.resize = 'none'; // for textarea
  59. input.style.padding = '0px 1px';
  60. input.style.cursor = "default";
  61. input.style.touchAction = 'none';
  62. input.spellcheck = false;
  63. input.value = UTF8ToString(text);
  64. input.placeholder = UTF8ToString(placeholder);
  65. input.style.outlineColor = 'black';
  66. if(isPassword){
  67. input.type = 'password';
  68. }
  69. if(isMobile) {
  70. document.body.appendChild(input);
  71. } else {
  72. container.appendChild(input);
  73. }
  74. return instances.push(input) - 1;
  75. },
  76. WebGLInputEnterSubmit: function(id, falg){
  77. var input = instances[id];
  78. // for enter key
  79. input.addEventListener('keydown', function(e) {
  80. if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
  81. if(falg)
  82. {
  83. e.preventDefault();
  84. input.blur();
  85. }
  86. }
  87. });
  88. },
  89. WebGLInputTab:function(id, cb) {
  90. var input = instances[id];
  91. // for tab key
  92. input.addEventListener('keydown', function (e) {
  93. if ((e.which && e.which === 9) || (e.keyCode && e.keyCode === 9)) {
  94. e.preventDefault();
  95. // if enable tab text
  96. if(input.enableTabText){
  97. var val = input.value;
  98. var start = input.selectionStart;
  99. var end = input.selectionEnd;
  100. input.value = val.substr(0, start) + '\t' + val.substr(end, val.length);
  101. input.setSelectionRange(start + 1, start + 1);
  102. input.oninput(); // call oninput to exe ValueChange function!!
  103. } else {
  104. (!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, e.shiftKey ? -1 : 1]) : {{{ makeDynCall("vii", "cb") }}}(id, e.shiftKey ? -1 : 1);
  105. }
  106. }
  107. });
  108. },
  109. WebGLInputFocus: function(id){
  110. var input = instances[id];
  111. input.focus();
  112. },
  113. WebGLInputOnFocus: function (id, cb) {
  114. var input = instances[id];
  115. input.onfocus = function () {
  116. (!!Runtime.dynCall) ? Runtime.dynCall("vi", cb, [id]) : {{{ makeDynCall("vi", "cb") }}}(id);
  117. };
  118. },
  119. WebGLInputOnBlur: function (id, cb) {
  120. var input = instances[id];
  121. input.onblur = function () {
  122. (!!Runtime.dynCall) ? Runtime.dynCall("vi", cb, [id]) : {{{ makeDynCall("vi", "cb") }}}(id);
  123. };
  124. },
  125. WebGLInputIsFocus: function (id) {
  126. return instances[id] === document.activeElement;
  127. },
  128. WebGLInputOnValueChange:function(id, cb){
  129. var input = instances[id];
  130. input.oninput = function () {
  131. var returnStr = input.value;
  132. var bufferSize = lengthBytesUTF8(returnStr) + 1;
  133. var buffer = _malloc(bufferSize);
  134. stringToUTF8(returnStr, buffer, bufferSize);
  135. (!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, buffer]) : {{{ makeDynCall("vii", "cb") }}}(id, buffer);
  136. };
  137. },
  138. WebGLInputOnEditEnd:function(id, cb){
  139. var input = instances[id];
  140. input.onchange = function () {
  141. var returnStr = input.value;
  142. var bufferSize = lengthBytesUTF8(returnStr) + 1;
  143. var buffer = _malloc(bufferSize);
  144. stringToUTF8(returnStr, buffer, bufferSize);
  145. (!!Runtime.dynCall) ? Runtime.dynCall("vii", cb, [id, buffer]) : {{{ makeDynCall("vii", "cb") }}}(id, buffer);
  146. };
  147. },
  148. WebGLInputOnKeyboardEvent:function(id, cb){
  149. var input = instances[id];
  150. var func = function(mode, e) {
  151. if (e instanceof KeyboardEvent){
  152. var bufferSize = lengthBytesUTF8(e.key) + 1;
  153. var key = _malloc(bufferSize);
  154. stringToUTF8(e.key, key, bufferSize);
  155. var code = e.code;
  156. var shift = e.shiftKey ? 1 : 0;
  157. var ctrl = e.ctrlKey ? 1 : 0;
  158. var alt = e.altKey ? 1 : 0;
  159. (!!Runtime.dynCall) ? Runtime.dynCall("viiiiiii", cb, [id, mode, key, code, shift, ctrl, alt]) : {{{ makeDynCall("viiiiiii", "cb") }}}(id, mode, key, code, shift, ctrl, alt);
  160. }
  161. }
  162. input.addEventListener('keydown', function(e) { func(1, e); });
  163. input.addEventListener('keyup', function(e) { func(2, e); });
  164. },
  165. WebGLInputSelectionStart:function(id){
  166. var input = instances[id];
  167. return input.selectionStart;
  168. },
  169. WebGLInputSelectionEnd:function(id){
  170. var input = instances[id];
  171. return input.selectionEnd;
  172. },
  173. WebGLInputSelectionDirection:function(id){
  174. var input = instances[id];
  175. return (input.selectionDirection == "backward")?-1:1;
  176. },
  177. WebGLInputSetSelectionRange:function(id, start, end){
  178. var input = instances[id];
  179. input.setSelectionRange(start, end);
  180. },
  181. WebGLInputMaxLength:function(id, maxlength){
  182. var input = instances[id];
  183. input.maxLength = maxlength;
  184. },
  185. WebGLInputText:function(id, text){
  186. var input = instances[id];
  187. input.value = UTF8ToString(text);
  188. },
  189. WebGLInputDelete:function(id){
  190. var input = instances[id];
  191. input.parentNode.removeChild(input);
  192. instances[id] = null;
  193. },
  194. WebGLInputEnableTabText:function(id, enable) {
  195. var input = instances[id];
  196. input.enableTabText = enable;
  197. },
  198. WebGLInputForceBlur:function(id) {
  199. var input = instances[id];
  200. input.blur();
  201. },
  202. }
  203. autoAddDeps(WebGLInput, '$instances');
  204. mergeInto(LibraryManager.library, WebGLInput);