library_godot_display.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /**************************************************************************/
  2. /* library_godot_display.js */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. const GodotDisplayVK = {
  31. $GodotDisplayVK__deps: ['$GodotRuntime', '$GodotConfig', '$GodotEventListeners'],
  32. $GodotDisplayVK__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayVK.clear(); resolve(); });',
  33. $GodotDisplayVK: {
  34. textinput: null,
  35. textarea: null,
  36. available: function () {
  37. return GodotConfig.virtual_keyboard && 'ontouchstart' in window;
  38. },
  39. init: function (input_cb) {
  40. function create(what) {
  41. const elem = document.createElement(what);
  42. elem.style.display = 'none';
  43. elem.style.position = 'absolute';
  44. elem.style.zIndex = '-1';
  45. elem.style.background = 'transparent';
  46. elem.style.padding = '0px';
  47. elem.style.margin = '0px';
  48. elem.style.overflow = 'hidden';
  49. elem.style.width = '0px';
  50. elem.style.height = '0px';
  51. elem.style.border = '0px';
  52. elem.style.outline = 'none';
  53. elem.readonly = true;
  54. elem.disabled = true;
  55. GodotEventListeners.add(elem, 'input', function (evt) {
  56. const c_str = GodotRuntime.allocString(elem.value);
  57. input_cb(c_str, elem.selectionEnd);
  58. GodotRuntime.free(c_str);
  59. }, false);
  60. GodotEventListeners.add(elem, 'blur', function (evt) {
  61. elem.style.display = 'none';
  62. elem.readonly = true;
  63. elem.disabled = true;
  64. }, false);
  65. GodotConfig.canvas.insertAdjacentElement('beforebegin', elem);
  66. return elem;
  67. }
  68. GodotDisplayVK.textinput = create('input');
  69. GodotDisplayVK.textarea = create('textarea');
  70. GodotDisplayVK.updateSize();
  71. },
  72. show: function (text, type, start, end) {
  73. if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
  74. return;
  75. }
  76. if (GodotDisplayVK.textinput.style.display !== '' || GodotDisplayVK.textarea.style.display !== '') {
  77. GodotDisplayVK.hide();
  78. }
  79. GodotDisplayVK.updateSize();
  80. let elem = GodotDisplayVK.textinput;
  81. switch (type) {
  82. case 0: // KEYBOARD_TYPE_DEFAULT
  83. elem.type = 'text';
  84. elem.inputmode = '';
  85. break;
  86. case 1: // KEYBOARD_TYPE_MULTILINE
  87. elem = GodotDisplayVK.textarea;
  88. break;
  89. case 2: // KEYBOARD_TYPE_NUMBER
  90. elem.type = 'text';
  91. elem.inputmode = 'numeric';
  92. break;
  93. case 3: // KEYBOARD_TYPE_NUMBER_DECIMAL
  94. elem.type = 'text';
  95. elem.inputmode = 'decimal';
  96. break;
  97. case 4: // KEYBOARD_TYPE_PHONE
  98. elem.type = 'tel';
  99. elem.inputmode = '';
  100. break;
  101. case 5: // KEYBOARD_TYPE_EMAIL_ADDRESS
  102. elem.type = 'email';
  103. elem.inputmode = '';
  104. break;
  105. case 6: // KEYBOARD_TYPE_PASSWORD
  106. elem.type = 'password';
  107. elem.inputmode = '';
  108. break;
  109. case 7: // KEYBOARD_TYPE_URL
  110. elem.type = 'url';
  111. elem.inputmode = '';
  112. break;
  113. default:
  114. elem.type = 'text';
  115. elem.inputmode = '';
  116. break;
  117. }
  118. elem.readonly = false;
  119. elem.disabled = false;
  120. elem.value = text;
  121. elem.style.display = 'block';
  122. elem.focus();
  123. elem.setSelectionRange(start, end);
  124. },
  125. hide: function () {
  126. if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
  127. return;
  128. }
  129. [GodotDisplayVK.textinput, GodotDisplayVK.textarea].forEach(function (elem) {
  130. elem.blur();
  131. elem.style.display = 'none';
  132. elem.value = '';
  133. });
  134. },
  135. updateSize: function () {
  136. if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
  137. return;
  138. }
  139. const rect = GodotConfig.canvas.getBoundingClientRect();
  140. function update(elem) {
  141. elem.style.left = `${rect.left}px`;
  142. elem.style.top = `${rect.top}px`;
  143. elem.style.width = `${rect.width}px`;
  144. elem.style.height = `${rect.height}px`;
  145. }
  146. update(GodotDisplayVK.textinput);
  147. update(GodotDisplayVK.textarea);
  148. },
  149. clear: function () {
  150. if (GodotDisplayVK.textinput) {
  151. GodotDisplayVK.textinput.remove();
  152. GodotDisplayVK.textinput = null;
  153. }
  154. if (GodotDisplayVK.textarea) {
  155. GodotDisplayVK.textarea.remove();
  156. GodotDisplayVK.textarea = null;
  157. }
  158. },
  159. },
  160. };
  161. mergeInto(LibraryManager.library, GodotDisplayVK);
  162. /*
  163. * Display server cursor helper.
  164. * Keeps track of cursor status and custom shapes.
  165. */
  166. const GodotDisplayCursor = {
  167. $GodotDisplayCursor__deps: ['$GodotOS', '$GodotConfig'],
  168. $GodotDisplayCursor__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayCursor.clear(); resolve(); });',
  169. $GodotDisplayCursor: {
  170. shape: 'default',
  171. visible: true,
  172. cursors: {},
  173. set_style: function (style) {
  174. GodotConfig.canvas.style.cursor = style;
  175. },
  176. set_shape: function (shape) {
  177. GodotDisplayCursor.shape = shape;
  178. let css = shape;
  179. if (shape in GodotDisplayCursor.cursors) {
  180. const c = GodotDisplayCursor.cursors[shape];
  181. css = `url("${c.url}") ${c.x} ${c.y}, default`;
  182. }
  183. if (GodotDisplayCursor.visible) {
  184. GodotDisplayCursor.set_style(css);
  185. }
  186. },
  187. clear: function () {
  188. GodotDisplayCursor.set_style('');
  189. GodotDisplayCursor.shape = 'default';
  190. GodotDisplayCursor.visible = true;
  191. Object.keys(GodotDisplayCursor.cursors).forEach(function (key) {
  192. URL.revokeObjectURL(GodotDisplayCursor.cursors[key]);
  193. delete GodotDisplayCursor.cursors[key];
  194. });
  195. },
  196. lockPointer: function () {
  197. const canvas = GodotConfig.canvas;
  198. if (canvas.requestPointerLock) {
  199. canvas.requestPointerLock();
  200. }
  201. },
  202. releasePointer: function () {
  203. if (document.exitPointerLock) {
  204. document.exitPointerLock();
  205. }
  206. },
  207. isPointerLocked: function () {
  208. return document.pointerLockElement === GodotConfig.canvas;
  209. },
  210. },
  211. };
  212. mergeInto(LibraryManager.library, GodotDisplayCursor);
  213. const GodotDisplayScreen = {
  214. $GodotDisplayScreen__deps: ['$GodotConfig', '$GodotOS', '$GL', 'emscripten_webgl_get_current_context'],
  215. $GodotDisplayScreen: {
  216. desired_size: [0, 0],
  217. hidpi: true,
  218. getPixelRatio: function () {
  219. return GodotDisplayScreen.hidpi ? window.devicePixelRatio || 1 : 1;
  220. },
  221. isFullscreen: function () {
  222. const elem = document.fullscreenElement || document.mozFullscreenElement
  223. || document.webkitFullscreenElement || document.msFullscreenElement;
  224. if (elem) {
  225. return elem === GodotConfig.canvas;
  226. }
  227. // But maybe knowing the element is not supported.
  228. return document.fullscreen || document.mozFullScreen
  229. || document.webkitIsFullscreen;
  230. },
  231. hasFullscreen: function () {
  232. return document.fullscreenEnabled || document.mozFullScreenEnabled
  233. || document.webkitFullscreenEnabled;
  234. },
  235. requestFullscreen: function () {
  236. if (!GodotDisplayScreen.hasFullscreen()) {
  237. return 1;
  238. }
  239. const canvas = GodotConfig.canvas;
  240. try {
  241. const promise = (canvas.requestFullscreen || canvas.msRequestFullscreen
  242. || canvas.mozRequestFullScreen || canvas.mozRequestFullscreen
  243. || canvas.webkitRequestFullscreen
  244. ).call(canvas);
  245. // Some browsers (Safari) return undefined.
  246. // For the standard ones, we need to catch it.
  247. if (promise) {
  248. promise.catch(function () {
  249. // nothing to do.
  250. });
  251. }
  252. } catch (e) {
  253. return 1;
  254. }
  255. return 0;
  256. },
  257. exitFullscreen: function () {
  258. if (!GodotDisplayScreen.isFullscreen()) {
  259. return 0;
  260. }
  261. try {
  262. const promise = document.exitFullscreen();
  263. if (promise) {
  264. promise.catch(function () {
  265. // nothing to do.
  266. });
  267. }
  268. } catch (e) {
  269. return 1;
  270. }
  271. return 0;
  272. },
  273. _updateGL: function () {
  274. const gl_context_handle = _emscripten_webgl_get_current_context();
  275. const gl = GL.getContext(gl_context_handle);
  276. if (gl) {
  277. GL.resizeOffscreenFramebuffer(gl);
  278. }
  279. },
  280. updateSize: function () {
  281. const isFullscreen = GodotDisplayScreen.isFullscreen();
  282. const wantsFullWindow = GodotConfig.canvas_resize_policy === 2;
  283. const noResize = GodotConfig.canvas_resize_policy === 0;
  284. const dWidth = GodotDisplayScreen.desired_size[0];
  285. const dHeight = GodotDisplayScreen.desired_size[1];
  286. const canvas = GodotConfig.canvas;
  287. let width = dWidth;
  288. let height = dHeight;
  289. if (noResize) {
  290. // Don't resize canvas, just update GL if needed.
  291. if (canvas.width !== width || canvas.height !== height) {
  292. GodotDisplayScreen.desired_size = [canvas.width, canvas.height];
  293. GodotDisplayScreen._updateGL();
  294. return 1;
  295. }
  296. return 0;
  297. }
  298. const scale = GodotDisplayScreen.getPixelRatio();
  299. if (isFullscreen || wantsFullWindow) {
  300. // We need to match screen size.
  301. width = window.innerWidth * scale;
  302. height = window.innerHeight * scale;
  303. }
  304. const csw = `${width / scale}px`;
  305. const csh = `${height / scale}px`;
  306. if (canvas.style.width !== csw || canvas.style.height !== csh || canvas.width !== width || canvas.height !== height) {
  307. // Size doesn't match.
  308. // Resize canvas, set correct CSS pixel size, update GL.
  309. canvas.width = width;
  310. canvas.height = height;
  311. canvas.style.width = csw;
  312. canvas.style.height = csh;
  313. GodotDisplayScreen._updateGL();
  314. return 1;
  315. }
  316. return 0;
  317. },
  318. },
  319. };
  320. mergeInto(LibraryManager.library, GodotDisplayScreen);
  321. /**
  322. * Display server interface.
  323. *
  324. * Exposes all the functions needed by DisplayServer implementation.
  325. */
  326. const GodotDisplay = {
  327. $GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'],
  328. $GodotDisplay: {
  329. window_icon: '',
  330. getDPI: function () {
  331. // devicePixelRatio is given in dppx
  332. // https://drafts.csswg.org/css-values/#resolution
  333. // > due to the 1:96 fixed ratio of CSS *in* to CSS *px*, 1dppx is equivalent to 96dpi.
  334. const dpi = Math.round(window.devicePixelRatio * 96);
  335. return dpi >= 96 ? dpi : 96;
  336. },
  337. },
  338. godot_js_display_is_swap_ok_cancel__proxy: 'sync',
  339. godot_js_display_is_swap_ok_cancel__sig: 'i',
  340. godot_js_display_is_swap_ok_cancel: function () {
  341. const win = (['Windows', 'Win64', 'Win32', 'WinCE']);
  342. const plat = navigator.platform || '';
  343. if (win.indexOf(plat) !== -1) {
  344. return 1;
  345. }
  346. return 0;
  347. },
  348. godot_js_tts_is_speaking__proxy: 'sync',
  349. godot_js_tts_is_speaking__sig: 'i',
  350. godot_js_tts_is_speaking: function () {
  351. return window.speechSynthesis.speaking;
  352. },
  353. godot_js_tts_is_paused__proxy: 'sync',
  354. godot_js_tts_is_paused__sig: 'i',
  355. godot_js_tts_is_paused: function () {
  356. return window.speechSynthesis.paused;
  357. },
  358. godot_js_tts_get_voices__proxy: 'sync',
  359. godot_js_tts_get_voices__sig: 'vi',
  360. godot_js_tts_get_voices: function (p_callback) {
  361. const func = GodotRuntime.get_func(p_callback);
  362. try {
  363. const arr = [];
  364. const voices = window.speechSynthesis.getVoices();
  365. for (let i = 0; i < voices.length; i++) {
  366. arr.push(`${voices[i].lang};${voices[i].name}`);
  367. }
  368. const c_ptr = GodotRuntime.allocStringArray(arr);
  369. func(arr.length, c_ptr);
  370. GodotRuntime.freeStringArray(c_ptr, arr.length);
  371. } catch (e) {
  372. // Fail graciously.
  373. }
  374. },
  375. godot_js_tts_speak__proxy: 'sync',
  376. godot_js_tts_speak__sig: 'viiiffii',
  377. godot_js_tts_speak: function (p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_callback) {
  378. const func = GodotRuntime.get_func(p_callback);
  379. function listener_end(evt) {
  380. evt.currentTarget.cb(1 /* TTS_UTTERANCE_ENDED */, evt.currentTarget.id, 0);
  381. }
  382. function listener_start(evt) {
  383. evt.currentTarget.cb(0 /* TTS_UTTERANCE_STARTED */, evt.currentTarget.id, 0);
  384. }
  385. function listener_error(evt) {
  386. evt.currentTarget.cb(2 /* TTS_UTTERANCE_CANCELED */, evt.currentTarget.id, 0);
  387. }
  388. function listener_bound(evt) {
  389. evt.currentTarget.cb(3 /* TTS_UTTERANCE_BOUNDARY */, evt.currentTarget.id, evt.charIndex);
  390. }
  391. const utterance = new SpeechSynthesisUtterance(GodotRuntime.parseString(p_text));
  392. utterance.rate = p_rate;
  393. utterance.pitch = p_pitch;
  394. utterance.volume = p_volume / 100.0;
  395. utterance.addEventListener('end', listener_end);
  396. utterance.addEventListener('start', listener_start);
  397. utterance.addEventListener('error', listener_error);
  398. utterance.addEventListener('boundary', listener_bound);
  399. utterance.id = p_utterance_id;
  400. utterance.cb = func;
  401. const voice = GodotRuntime.parseString(p_voice);
  402. const voices = window.speechSynthesis.getVoices();
  403. for (let i = 0; i < voices.length; i++) {
  404. if (voices[i].name === voice) {
  405. utterance.voice = voices[i];
  406. break;
  407. }
  408. }
  409. window.speechSynthesis.resume();
  410. window.speechSynthesis.speak(utterance);
  411. },
  412. godot_js_tts_pause__proxy: 'sync',
  413. godot_js_tts_pause__sig: 'v',
  414. godot_js_tts_pause: function () {
  415. window.speechSynthesis.pause();
  416. },
  417. godot_js_tts_resume__proxy: 'sync',
  418. godot_js_tts_resume__sig: 'v',
  419. godot_js_tts_resume: function () {
  420. window.speechSynthesis.resume();
  421. },
  422. godot_js_tts_stop__proxy: 'sync',
  423. godot_js_tts_stop__sig: 'v',
  424. godot_js_tts_stop: function () {
  425. window.speechSynthesis.cancel();
  426. window.speechSynthesis.resume();
  427. },
  428. godot_js_display_alert__proxy: 'sync',
  429. godot_js_display_alert__sig: 'vi',
  430. godot_js_display_alert: function (p_text) {
  431. window.alert(GodotRuntime.parseString(p_text)); // eslint-disable-line no-alert
  432. },
  433. godot_js_display_screen_dpi_get__proxy: 'sync',
  434. godot_js_display_screen_dpi_get__sig: 'i',
  435. godot_js_display_screen_dpi_get: function () {
  436. return GodotDisplay.getDPI();
  437. },
  438. godot_js_display_pixel_ratio_get__proxy: 'sync',
  439. godot_js_display_pixel_ratio_get__sig: 'f',
  440. godot_js_display_pixel_ratio_get: function () {
  441. return GodotDisplayScreen.getPixelRatio();
  442. },
  443. godot_js_display_fullscreen_request__proxy: 'sync',
  444. godot_js_display_fullscreen_request__sig: 'i',
  445. godot_js_display_fullscreen_request: function () {
  446. return GodotDisplayScreen.requestFullscreen();
  447. },
  448. godot_js_display_fullscreen_exit__proxy: 'sync',
  449. godot_js_display_fullscreen_exit__sig: 'i',
  450. godot_js_display_fullscreen_exit: function () {
  451. return GodotDisplayScreen.exitFullscreen();
  452. },
  453. godot_js_display_desired_size_set__proxy: 'sync',
  454. godot_js_display_desired_size_set__sig: 'vii',
  455. godot_js_display_desired_size_set: function (width, height) {
  456. GodotDisplayScreen.desired_size = [width, height];
  457. GodotDisplayScreen.updateSize();
  458. },
  459. godot_js_display_size_update__proxy: 'sync',
  460. godot_js_display_size_update__sig: 'i',
  461. godot_js_display_size_update: function () {
  462. const updated = GodotDisplayScreen.updateSize();
  463. if (updated) {
  464. GodotDisplayVK.updateSize();
  465. }
  466. return updated;
  467. },
  468. godot_js_display_screen_size_get__proxy: 'sync',
  469. godot_js_display_screen_size_get__sig: 'vii',
  470. godot_js_display_screen_size_get: function (width, height) {
  471. const scale = GodotDisplayScreen.getPixelRatio();
  472. GodotRuntime.setHeapValue(width, window.screen.width * scale, 'i32');
  473. GodotRuntime.setHeapValue(height, window.screen.height * scale, 'i32');
  474. },
  475. godot_js_display_window_size_get__proxy: 'sync',
  476. godot_js_display_window_size_get__sig: 'vii',
  477. godot_js_display_window_size_get: function (p_width, p_height) {
  478. GodotRuntime.setHeapValue(p_width, GodotConfig.canvas.width, 'i32');
  479. GodotRuntime.setHeapValue(p_height, GodotConfig.canvas.height, 'i32');
  480. },
  481. godot_js_display_has_webgl__proxy: 'sync',
  482. godot_js_display_has_webgl__sig: 'ii',
  483. godot_js_display_has_webgl: function (p_version) {
  484. if (p_version !== 1 && p_version !== 2) {
  485. return false;
  486. }
  487. try {
  488. return !!document.createElement('canvas').getContext(p_version === 2 ? 'webgl2' : 'webgl');
  489. } catch (e) { /* Not available */ }
  490. return false;
  491. },
  492. /*
  493. * Canvas
  494. */
  495. godot_js_display_canvas_focus__proxy: 'sync',
  496. godot_js_display_canvas_focus__sig: 'v',
  497. godot_js_display_canvas_focus: function () {
  498. GodotConfig.canvas.focus();
  499. },
  500. godot_js_display_canvas_is_focused__proxy: 'sync',
  501. godot_js_display_canvas_is_focused__sig: 'i',
  502. godot_js_display_canvas_is_focused: function () {
  503. return document.activeElement === GodotConfig.canvas;
  504. },
  505. /*
  506. * Touchscreen
  507. */
  508. godot_js_display_touchscreen_is_available__proxy: 'sync',
  509. godot_js_display_touchscreen_is_available__sig: 'i',
  510. godot_js_display_touchscreen_is_available: function () {
  511. return 'ontouchstart' in window;
  512. },
  513. /*
  514. * Clipboard
  515. */
  516. godot_js_display_clipboard_set__proxy: 'sync',
  517. godot_js_display_clipboard_set__sig: 'ii',
  518. godot_js_display_clipboard_set: function (p_text) {
  519. const text = GodotRuntime.parseString(p_text);
  520. if (!navigator.clipboard || !navigator.clipboard.writeText) {
  521. return 1;
  522. }
  523. navigator.clipboard.writeText(text).catch(function (e) {
  524. // Setting OS clipboard is only possible from an input callback.
  525. GodotRuntime.error('Setting OS clipboard is only possible from an input callback for the Web platform. Exception:', e);
  526. });
  527. return 0;
  528. },
  529. godot_js_display_clipboard_get__proxy: 'sync',
  530. godot_js_display_clipboard_get__sig: 'ii',
  531. godot_js_display_clipboard_get: function (callback) {
  532. const func = GodotRuntime.get_func(callback);
  533. try {
  534. navigator.clipboard.readText().then(function (result) {
  535. const ptr = GodotRuntime.allocString(result);
  536. func(ptr);
  537. GodotRuntime.free(ptr);
  538. }).catch(function (e) {
  539. // Fail graciously.
  540. });
  541. } catch (e) {
  542. // Fail graciously.
  543. }
  544. },
  545. /*
  546. * Window
  547. */
  548. godot_js_display_window_title_set__proxy: 'sync',
  549. godot_js_display_window_title_set__sig: 'vi',
  550. godot_js_display_window_title_set: function (p_data) {
  551. document.title = GodotRuntime.parseString(p_data);
  552. },
  553. godot_js_display_window_icon_set__proxy: 'sync',
  554. godot_js_display_window_icon_set__sig: 'vii',
  555. godot_js_display_window_icon_set: function (p_ptr, p_len) {
  556. let link = document.getElementById('-gd-engine-icon');
  557. const old_icon = GodotDisplay.window_icon;
  558. if (p_ptr) {
  559. if (link === null) {
  560. link = document.createElement('link');
  561. link.rel = 'icon';
  562. link.id = '-gd-engine-icon';
  563. document.head.appendChild(link);
  564. }
  565. const png = new Blob([GodotRuntime.heapSlice(HEAPU8, p_ptr, p_len)], { type: 'image/png' });
  566. GodotDisplay.window_icon = URL.createObjectURL(png);
  567. link.href = GodotDisplay.window_icon;
  568. } else {
  569. if (link) {
  570. link.remove();
  571. }
  572. GodotDisplay.window_icon = null;
  573. }
  574. if (old_icon) {
  575. URL.revokeObjectURL(old_icon);
  576. }
  577. },
  578. /*
  579. * Cursor
  580. */
  581. godot_js_display_cursor_set_visible__proxy: 'sync',
  582. godot_js_display_cursor_set_visible__sig: 'vi',
  583. godot_js_display_cursor_set_visible: function (p_visible) {
  584. const visible = p_visible !== 0;
  585. if (visible === GodotDisplayCursor.visible) {
  586. return;
  587. }
  588. GodotDisplayCursor.visible = visible;
  589. if (visible) {
  590. GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
  591. } else {
  592. GodotDisplayCursor.set_style('none');
  593. }
  594. },
  595. godot_js_display_cursor_is_hidden__proxy: 'sync',
  596. godot_js_display_cursor_is_hidden__sig: 'i',
  597. godot_js_display_cursor_is_hidden: function () {
  598. return !GodotDisplayCursor.visible;
  599. },
  600. godot_js_display_cursor_set_shape__proxy: 'sync',
  601. godot_js_display_cursor_set_shape__sig: 'vi',
  602. godot_js_display_cursor_set_shape: function (p_string) {
  603. GodotDisplayCursor.set_shape(GodotRuntime.parseString(p_string));
  604. },
  605. godot_js_display_cursor_set_custom_shape__proxy: 'sync',
  606. godot_js_display_cursor_set_custom_shape__sig: 'viiiii',
  607. godot_js_display_cursor_set_custom_shape: function (p_shape, p_ptr, p_len, p_hotspot_x, p_hotspot_y) {
  608. const shape = GodotRuntime.parseString(p_shape);
  609. const old_shape = GodotDisplayCursor.cursors[shape];
  610. if (p_len > 0) {
  611. const png = new Blob([GodotRuntime.heapSlice(HEAPU8, p_ptr, p_len)], { type: 'image/png' });
  612. const url = URL.createObjectURL(png);
  613. GodotDisplayCursor.cursors[shape] = {
  614. url: url,
  615. x: p_hotspot_x,
  616. y: p_hotspot_y,
  617. };
  618. } else {
  619. delete GodotDisplayCursor.cursors[shape];
  620. }
  621. if (shape === GodotDisplayCursor.shape) {
  622. GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
  623. }
  624. if (old_shape) {
  625. URL.revokeObjectURL(old_shape.url);
  626. }
  627. },
  628. godot_js_display_cursor_lock_set__proxy: 'sync',
  629. godot_js_display_cursor_lock_set__sig: 'vi',
  630. godot_js_display_cursor_lock_set: function (p_lock) {
  631. if (p_lock) {
  632. GodotDisplayCursor.lockPointer();
  633. } else {
  634. GodotDisplayCursor.releasePointer();
  635. }
  636. },
  637. godot_js_display_cursor_is_locked__proxy: 'sync',
  638. godot_js_display_cursor_is_locked__sig: 'i',
  639. godot_js_display_cursor_is_locked: function () {
  640. return GodotDisplayCursor.isPointerLocked() ? 1 : 0;
  641. },
  642. /*
  643. * Listeners
  644. */
  645. godot_js_display_fullscreen_cb__proxy: 'sync',
  646. godot_js_display_fullscreen_cb__sig: 'vi',
  647. godot_js_display_fullscreen_cb: function (callback) {
  648. const canvas = GodotConfig.canvas;
  649. const func = GodotRuntime.get_func(callback);
  650. function change_cb(evt) {
  651. if (evt.target === canvas) {
  652. func(GodotDisplayScreen.isFullscreen());
  653. }
  654. }
  655. GodotEventListeners.add(document, 'fullscreenchange', change_cb, false);
  656. GodotEventListeners.add(document, 'mozfullscreenchange', change_cb, false);
  657. GodotEventListeners.add(document, 'webkitfullscreenchange', change_cb, false);
  658. },
  659. godot_js_display_window_blur_cb__proxy: 'sync',
  660. godot_js_display_window_blur_cb__sig: 'vi',
  661. godot_js_display_window_blur_cb: function (callback) {
  662. const func = GodotRuntime.get_func(callback);
  663. GodotEventListeners.add(window, 'blur', function () {
  664. func();
  665. }, false);
  666. },
  667. godot_js_display_notification_cb__proxy: 'sync',
  668. godot_js_display_notification_cb__sig: 'viiiii',
  669. godot_js_display_notification_cb: function (callback, p_enter, p_exit, p_in, p_out) {
  670. const canvas = GodotConfig.canvas;
  671. const func = GodotRuntime.get_func(callback);
  672. const notif = [p_enter, p_exit, p_in, p_out];
  673. ['mouseover', 'mouseleave', 'focus', 'blur'].forEach(function (evt_name, idx) {
  674. GodotEventListeners.add(canvas, evt_name, function () {
  675. func(notif[idx]);
  676. }, true);
  677. });
  678. },
  679. godot_js_display_setup_canvas__proxy: 'sync',
  680. godot_js_display_setup_canvas__sig: 'viiii',
  681. godot_js_display_setup_canvas: function (p_width, p_height, p_fullscreen, p_hidpi) {
  682. const canvas = GodotConfig.canvas;
  683. GodotEventListeners.add(canvas, 'contextmenu', function (ev) {
  684. ev.preventDefault();
  685. }, false);
  686. GodotEventListeners.add(canvas, 'webglcontextlost', function (ev) {
  687. alert('WebGL context lost, please reload the page'); // eslint-disable-line no-alert
  688. ev.preventDefault();
  689. }, false);
  690. GodotDisplayScreen.hidpi = !!p_hidpi;
  691. switch (GodotConfig.canvas_resize_policy) {
  692. case 0: // None
  693. GodotDisplayScreen.desired_size = [canvas.width, canvas.height];
  694. break;
  695. case 1: // Project
  696. GodotDisplayScreen.desired_size = [p_width, p_height];
  697. break;
  698. default: // Full window
  699. // Ensure we display in the right place, the size will be handled by updateSize
  700. canvas.style.position = 'absolute';
  701. canvas.style.top = 0;
  702. canvas.style.left = 0;
  703. break;
  704. }
  705. GodotDisplayScreen.updateSize();
  706. if (p_fullscreen) {
  707. GodotDisplayScreen.requestFullscreen();
  708. }
  709. },
  710. /*
  711. * Virtual Keyboard
  712. */
  713. godot_js_display_vk_show__proxy: 'sync',
  714. godot_js_display_vk_show__sig: 'viiii',
  715. godot_js_display_vk_show: function (p_text, p_type, p_start, p_end) {
  716. const text = GodotRuntime.parseString(p_text);
  717. const start = p_start > 0 ? p_start : 0;
  718. const end = p_end > 0 ? p_end : start;
  719. GodotDisplayVK.show(text, p_type, start, end);
  720. },
  721. godot_js_display_vk_hide__proxy: 'sync',
  722. godot_js_display_vk_hide__sig: 'v',
  723. godot_js_display_vk_hide: function () {
  724. GodotDisplayVK.hide();
  725. },
  726. godot_js_display_vk_available__proxy: 'sync',
  727. godot_js_display_vk_available__sig: 'i',
  728. godot_js_display_vk_available: function () {
  729. return GodotDisplayVK.available();
  730. },
  731. godot_js_display_tts_available__proxy: 'sync',
  732. godot_js_display_tts_available__sig: 'i',
  733. godot_js_display_tts_available: function () {
  734. return 'speechSynthesis' in window;
  735. },
  736. godot_js_display_vk_cb__proxy: 'sync',
  737. godot_js_display_vk_cb__sig: 'vi',
  738. godot_js_display_vk_cb: function (p_input_cb) {
  739. const input_cb = GodotRuntime.get_func(p_input_cb);
  740. if (GodotDisplayVK.available()) {
  741. GodotDisplayVK.init(input_cb);
  742. }
  743. },
  744. };
  745. autoAddDeps(GodotDisplay, '$GodotDisplay');
  746. mergeInto(LibraryManager.library, GodotDisplay);