library_godot_display.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*************************************************************************/
  2. /* library_godot_display.js */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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, multiline, 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. const elem = multiline ? GodotDisplayVK.textarea : GodotDisplayVK.textinput;
  81. elem.readonly = false;
  82. elem.disabled = false;
  83. elem.value = text;
  84. elem.style.display = 'block';
  85. elem.focus();
  86. elem.setSelectionRange(start, end);
  87. },
  88. hide: function () {
  89. if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
  90. return;
  91. }
  92. [GodotDisplayVK.textinput, GodotDisplayVK.textarea].forEach(function (elem) {
  93. elem.blur();
  94. elem.style.display = 'none';
  95. elem.value = '';
  96. });
  97. },
  98. updateSize: function () {
  99. if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
  100. return;
  101. }
  102. const rect = GodotConfig.canvas.getBoundingClientRect();
  103. function update(elem) {
  104. elem.style.left = `${rect.left}px`;
  105. elem.style.top = `${rect.top}px`;
  106. elem.style.width = `${rect.width}px`;
  107. elem.style.height = `${rect.height}px`;
  108. }
  109. update(GodotDisplayVK.textinput);
  110. update(GodotDisplayVK.textarea);
  111. },
  112. clear: function () {
  113. if (GodotDisplayVK.textinput) {
  114. GodotDisplayVK.textinput.remove();
  115. GodotDisplayVK.textinput = null;
  116. }
  117. if (GodotDisplayVK.textarea) {
  118. GodotDisplayVK.textarea.remove();
  119. GodotDisplayVK.textarea = null;
  120. }
  121. },
  122. },
  123. };
  124. mergeInto(LibraryManager.library, GodotDisplayVK);
  125. /*
  126. * Display server cursor helper.
  127. * Keeps track of cursor status and custom shapes.
  128. */
  129. const GodotDisplayCursor = {
  130. $GodotDisplayCursor__deps: ['$GodotOS', '$GodotConfig'],
  131. $GodotDisplayCursor__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayCursor.clear(); resolve(); });',
  132. $GodotDisplayCursor: {
  133. shape: 'auto',
  134. visible: true,
  135. cursors: {},
  136. set_style: function (style) {
  137. GodotConfig.canvas.style.cursor = style;
  138. },
  139. set_shape: function (shape) {
  140. GodotDisplayCursor.shape = shape;
  141. let css = shape;
  142. if (shape in GodotDisplayCursor.cursors) {
  143. const c = GodotDisplayCursor.cursors[shape];
  144. css = `url("${c.url}") ${c.x} ${c.y}, auto`;
  145. }
  146. if (GodotDisplayCursor.visible) {
  147. GodotDisplayCursor.set_style(css);
  148. }
  149. },
  150. clear: function () {
  151. GodotDisplayCursor.set_style('');
  152. GodotDisplayCursor.shape = 'auto';
  153. GodotDisplayCursor.visible = true;
  154. Object.keys(GodotDisplayCursor.cursors).forEach(function (key) {
  155. URL.revokeObjectURL(GodotDisplayCursor.cursors[key]);
  156. delete GodotDisplayCursor.cursors[key];
  157. });
  158. },
  159. lockPointer: function () {
  160. const canvas = GodotConfig.canvas;
  161. if (canvas.requestPointerLock) {
  162. canvas.requestPointerLock();
  163. }
  164. },
  165. releasePointer: function () {
  166. if (document.exitPointerLock) {
  167. document.exitPointerLock();
  168. }
  169. },
  170. isPointerLocked: function () {
  171. return document.pointerLockElement === GodotConfig.canvas;
  172. },
  173. },
  174. };
  175. mergeInto(LibraryManager.library, GodotDisplayCursor);
  176. const GodotDisplayScreen = {
  177. $GodotDisplayScreen__deps: ['$GodotConfig', '$GodotOS', '$GL', 'emscripten_webgl_get_current_context'],
  178. $GodotDisplayScreen: {
  179. desired_size: [0, 0],
  180. hidpi: true,
  181. getPixelRatio: function () {
  182. return GodotDisplayScreen.hidpi ? window.devicePixelRatio || 1 : 1;
  183. },
  184. isFullscreen: function () {
  185. const elem = document.fullscreenElement || document.mozFullscreenElement
  186. || document.webkitFullscreenElement || document.msFullscreenElement;
  187. if (elem) {
  188. return elem === GodotConfig.canvas;
  189. }
  190. // But maybe knowing the element is not supported.
  191. return document.fullscreen || document.mozFullScreen
  192. || document.webkitIsFullscreen;
  193. },
  194. hasFullscreen: function () {
  195. return document.fullscreenEnabled || document.mozFullScreenEnabled
  196. || document.webkitFullscreenEnabled;
  197. },
  198. requestFullscreen: function () {
  199. if (!GodotDisplayScreen.hasFullscreen()) {
  200. return 1;
  201. }
  202. const canvas = GodotConfig.canvas;
  203. try {
  204. const promise = (canvas.requestFullscreen || canvas.msRequestFullscreen
  205. || canvas.mozRequestFullScreen || canvas.mozRequestFullscreen
  206. || canvas.webkitRequestFullscreen
  207. ).call(canvas);
  208. // Some browsers (Safari) return undefined.
  209. // For the standard ones, we need to catch it.
  210. if (promise) {
  211. promise.catch(function () {
  212. // nothing to do.
  213. });
  214. }
  215. } catch (e) {
  216. return 1;
  217. }
  218. return 0;
  219. },
  220. exitFullscreen: function () {
  221. if (!GodotDisplayScreen.isFullscreen()) {
  222. return 0;
  223. }
  224. try {
  225. const promise = document.exitFullscreen();
  226. if (promise) {
  227. promise.catch(function () {
  228. // nothing to do.
  229. });
  230. }
  231. } catch (e) {
  232. return 1;
  233. }
  234. return 0;
  235. },
  236. _updateGL: function () {
  237. const gl_context_handle = _emscripten_webgl_get_current_context(); // eslint-disable-line no-undef
  238. const gl = GL.getContext(gl_context_handle);
  239. if (gl) {
  240. GL.resizeOffscreenFramebuffer(gl);
  241. }
  242. },
  243. updateSize: function () {
  244. const isFullscreen = GodotDisplayScreen.isFullscreen();
  245. const wantsFullWindow = GodotConfig.canvas_resize_policy === 2;
  246. const noResize = GodotConfig.canvas_resize_policy === 0;
  247. const wwidth = GodotDisplayScreen.desired_size[0];
  248. const wheight = GodotDisplayScreen.desired_size[1];
  249. const canvas = GodotConfig.canvas;
  250. let width = wwidth;
  251. let height = wheight;
  252. if (noResize) {
  253. // Don't resize canvas, just update GL if needed.
  254. if (canvas.width !== width || canvas.height !== height) {
  255. GodotDisplayScreen.desired_size = [canvas.width, canvas.height];
  256. GodotDisplayScreen._updateGL();
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. const scale = GodotDisplayScreen.getPixelRatio();
  262. if (isFullscreen || wantsFullWindow) {
  263. // We need to match screen size.
  264. width = window.innerWidth * scale;
  265. height = window.innerHeight * scale;
  266. }
  267. const csw = `${width / scale}px`;
  268. const csh = `${height / scale}px`;
  269. if (canvas.style.width !== csw || canvas.style.height !== csh || canvas.width !== width || canvas.height !== height) {
  270. // Size doesn't match.
  271. // Resize canvas, set correct CSS pixel size, update GL.
  272. canvas.width = width;
  273. canvas.height = height;
  274. canvas.style.width = csw;
  275. canvas.style.height = csh;
  276. GodotDisplayScreen._updateGL();
  277. return 1;
  278. }
  279. return 0;
  280. },
  281. },
  282. };
  283. mergeInto(LibraryManager.library, GodotDisplayScreen);
  284. /**
  285. * Display server interface.
  286. *
  287. * Exposes all the functions needed by DisplayServer implementation.
  288. */
  289. const GodotDisplay = {
  290. $GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'],
  291. $GodotDisplay: {
  292. window_icon: '',
  293. findDPI: function () {
  294. function testDPI(dpi) {
  295. return window.matchMedia(`(max-resolution: ${dpi}dpi)`).matches;
  296. }
  297. function bisect(low, high, func) {
  298. const mid = parseInt(((high - low) / 2) + low, 10);
  299. if (high - low <= 1) {
  300. return func(high) ? high : low;
  301. }
  302. if (func(mid)) {
  303. return bisect(low, mid, func);
  304. }
  305. return bisect(mid, high, func);
  306. }
  307. try {
  308. const dpi = bisect(0, 800, testDPI);
  309. return dpi >= 96 ? dpi : 96;
  310. } catch (e) {
  311. return 96;
  312. }
  313. },
  314. },
  315. godot_js_display_is_swap_ok_cancel__sig: 'i',
  316. godot_js_display_is_swap_ok_cancel: function () {
  317. const win = (['Windows', 'Win64', 'Win32', 'WinCE']);
  318. const plat = navigator.platform || '';
  319. if (win.indexOf(plat) !== -1) {
  320. return 1;
  321. }
  322. return 0;
  323. },
  324. godot_js_tts_is_speaking__sig: 'i',
  325. godot_js_tts_is_speaking: function () {
  326. return window.speechSynthesis.speaking;
  327. },
  328. godot_js_tts_is_paused__sig: 'i',
  329. godot_js_tts_is_paused: function () {
  330. return window.speechSynthesis.paused;
  331. },
  332. godot_js_tts_get_voices__sig: 'vi',
  333. godot_js_tts_get_voices: function (p_callback) {
  334. const func = GodotRuntime.get_func(p_callback);
  335. try {
  336. const arr = [];
  337. const voices = window.speechSynthesis.getVoices();
  338. for (let i = 0; i < voices.length; i++) {
  339. arr.push(`${voices[i].lang};${voices[i].name}`);
  340. }
  341. const c_ptr = GodotRuntime.allocStringArray(arr);
  342. func(arr.length, c_ptr);
  343. GodotRuntime.freeStringArray(c_ptr, arr.length);
  344. } catch (e) {
  345. // Fail graciously.
  346. }
  347. },
  348. godot_js_tts_speak__sig: 'viiiffii',
  349. godot_js_tts_speak: function (p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_callback) {
  350. const func = GodotRuntime.get_func(p_callback);
  351. function listener_end(evt) {
  352. evt.currentTarget.cb(1 /*TTS_UTTERANCE_ENDED*/, evt.currentTarget.id, 0);
  353. }
  354. function listener_start(evt) {
  355. evt.currentTarget.cb(0 /*TTS_UTTERANCE_STARTED*/, evt.currentTarget.id, 0);
  356. }
  357. function listener_error(evt) {
  358. evt.currentTarget.cb(2 /*TTS_UTTERANCE_CANCELED*/, evt.currentTarget.id, 0);
  359. }
  360. function listener_bound(evt) {
  361. evt.currentTarget.cb(3 /*TTS_UTTERANCE_BOUNDARY*/, evt.currentTarget.id, evt.charIndex);
  362. }
  363. const utterance = new SpeechSynthesisUtterance(GodotRuntime.parseString(p_text));
  364. utterance.rate = p_rate;
  365. utterance.pitch = p_pitch;
  366. utterance.volume = p_volume / 100.0;
  367. utterance.addEventListener('end', listener_end);
  368. utterance.addEventListener('start', listener_start);
  369. utterance.addEventListener('error', listener_error);
  370. utterance.addEventListener('boundary', listener_bound);
  371. utterance.id = p_utterance_id;
  372. utterance.cb = func;
  373. const voice = GodotRuntime.parseString(p_voice);
  374. const voices = window.speechSynthesis.getVoices();
  375. for (let i = 0; i < voices.length; i++) {
  376. if (voices[i].name === voice) {
  377. utterance.voice = voices[i];
  378. break;
  379. }
  380. }
  381. window.speechSynthesis.resume();
  382. window.speechSynthesis.speak(utterance);
  383. },
  384. godot_js_tts_pause__sig: 'v',
  385. godot_js_tts_pause: function () {
  386. window.speechSynthesis.pause();
  387. },
  388. godot_js_tts_resume__sig: 'v',
  389. godot_js_tts_resume: function () {
  390. window.speechSynthesis.resume();
  391. },
  392. godot_js_tts_stop__sig: 'v',
  393. godot_js_tts_stop: function () {
  394. window.speechSynthesis.cancel();
  395. window.speechSynthesis.resume();
  396. },
  397. godot_js_display_alert__sig: 'vi',
  398. godot_js_display_alert: function (p_text) {
  399. window.alert(GodotRuntime.parseString(p_text)); // eslint-disable-line no-alert
  400. },
  401. godot_js_display_screen_dpi_get__sig: 'i',
  402. godot_js_display_screen_dpi_get: function () {
  403. return GodotDisplay.findDPI();
  404. },
  405. godot_js_display_pixel_ratio_get__sig: 'f',
  406. godot_js_display_pixel_ratio_get: function () {
  407. return GodotDisplayScreen.getPixelRatio();
  408. },
  409. godot_js_display_fullscreen_request__sig: 'i',
  410. godot_js_display_fullscreen_request: function () {
  411. return GodotDisplayScreen.requestFullscreen();
  412. },
  413. godot_js_display_fullscreen_exit__sig: 'i',
  414. godot_js_display_fullscreen_exit: function () {
  415. return GodotDisplayScreen.exitFullscreen();
  416. },
  417. godot_js_display_desired_size_set__sig: 'vii',
  418. godot_js_display_desired_size_set: function (width, height) {
  419. GodotDisplayScreen.desired_size = [width, height];
  420. GodotDisplayScreen.updateSize();
  421. },
  422. godot_js_display_size_update__sig: 'i',
  423. godot_js_display_size_update: function () {
  424. const updated = GodotDisplayScreen.updateSize();
  425. if (updated) {
  426. GodotDisplayVK.updateSize();
  427. }
  428. return updated;
  429. },
  430. godot_js_display_screen_size_get__sig: 'vii',
  431. godot_js_display_screen_size_get: function (width, height) {
  432. const scale = GodotDisplayScreen.getPixelRatio();
  433. GodotRuntime.setHeapValue(width, window.screen.width * scale, 'i32');
  434. GodotRuntime.setHeapValue(height, window.screen.height * scale, 'i32');
  435. },
  436. godot_js_display_window_size_get: function (p_width, p_height) {
  437. GodotRuntime.setHeapValue(p_width, GodotConfig.canvas.width, 'i32');
  438. GodotRuntime.setHeapValue(p_height, GodotConfig.canvas.height, 'i32');
  439. },
  440. godot_js_display_has_webgl__sig: 'ii',
  441. godot_js_display_has_webgl: function (p_version) {
  442. if (p_version !== 1 && p_version !== 2) {
  443. return false;
  444. }
  445. try {
  446. return !!document.createElement('canvas').getContext(p_version === 2 ? 'webgl2' : 'webgl');
  447. } catch (e) { /* Not available */ }
  448. return false;
  449. },
  450. /*
  451. * Canvas
  452. */
  453. godot_js_display_canvas_focus__sig: 'v',
  454. godot_js_display_canvas_focus: function () {
  455. GodotConfig.canvas.focus();
  456. },
  457. godot_js_display_canvas_is_focused__sig: 'i',
  458. godot_js_display_canvas_is_focused: function () {
  459. return document.activeElement === GodotConfig.canvas;
  460. },
  461. /*
  462. * Touchscreen
  463. */
  464. godot_js_display_touchscreen_is_available__sig: 'i',
  465. godot_js_display_touchscreen_is_available: function () {
  466. return 'ontouchstart' in window;
  467. },
  468. /*
  469. * Clipboard
  470. */
  471. godot_js_display_clipboard_set__sig: 'ii',
  472. godot_js_display_clipboard_set: function (p_text) {
  473. const text = GodotRuntime.parseString(p_text);
  474. if (!navigator.clipboard || !navigator.clipboard.writeText) {
  475. return 1;
  476. }
  477. navigator.clipboard.writeText(text).catch(function (e) {
  478. // Setting OS clipboard is only possible from an input callback.
  479. GodotRuntime.error('Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:', e);
  480. });
  481. return 0;
  482. },
  483. godot_js_display_clipboard_get__sig: 'ii',
  484. godot_js_display_clipboard_get: function (callback) {
  485. const func = GodotRuntime.get_func(callback);
  486. try {
  487. navigator.clipboard.readText().then(function (result) {
  488. const ptr = GodotRuntime.allocString(result);
  489. func(ptr);
  490. GodotRuntime.free(ptr);
  491. }).catch(function (e) {
  492. // Fail graciously.
  493. });
  494. } catch (e) {
  495. // Fail graciously.
  496. }
  497. },
  498. /*
  499. * Window
  500. */
  501. godot_js_display_window_title_set__sig: 'vi',
  502. godot_js_display_window_title_set: function (p_data) {
  503. document.title = GodotRuntime.parseString(p_data);
  504. },
  505. godot_js_display_window_icon_set__sig: 'vii',
  506. godot_js_display_window_icon_set: function (p_ptr, p_len) {
  507. let link = document.getElementById('-gd-engine-icon');
  508. if (link === null) {
  509. link = document.createElement('link');
  510. link.rel = 'icon';
  511. link.id = '-gd-engine-icon';
  512. document.head.appendChild(link);
  513. }
  514. const old_icon = GodotDisplay.window_icon;
  515. const png = new Blob([GodotRuntime.heapSlice(HEAPU8, p_ptr, p_len)], { type: 'image/png' });
  516. GodotDisplay.window_icon = URL.createObjectURL(png);
  517. link.href = GodotDisplay.window_icon;
  518. if (old_icon) {
  519. URL.revokeObjectURL(old_icon);
  520. }
  521. },
  522. /*
  523. * Cursor
  524. */
  525. godot_js_display_cursor_set_visible__sig: 'vi',
  526. godot_js_display_cursor_set_visible: function (p_visible) {
  527. const visible = p_visible !== 0;
  528. if (visible === GodotDisplayCursor.visible) {
  529. return;
  530. }
  531. GodotDisplayCursor.visible = visible;
  532. if (visible) {
  533. GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
  534. } else {
  535. GodotDisplayCursor.set_style('none');
  536. }
  537. },
  538. godot_js_display_cursor_is_hidden__sig: 'i',
  539. godot_js_display_cursor_is_hidden: function () {
  540. return !GodotDisplayCursor.visible;
  541. },
  542. godot_js_display_cursor_set_shape__sig: 'vi',
  543. godot_js_display_cursor_set_shape: function (p_string) {
  544. GodotDisplayCursor.set_shape(GodotRuntime.parseString(p_string));
  545. },
  546. godot_js_display_cursor_set_custom_shape__sig: 'viiiii',
  547. godot_js_display_cursor_set_custom_shape: function (p_shape, p_ptr, p_len, p_hotspot_x, p_hotspot_y) {
  548. const shape = GodotRuntime.parseString(p_shape);
  549. const old_shape = GodotDisplayCursor.cursors[shape];
  550. if (p_len > 0) {
  551. const png = new Blob([GodotRuntime.heapSlice(HEAPU8, p_ptr, p_len)], { type: 'image/png' });
  552. const url = URL.createObjectURL(png);
  553. GodotDisplayCursor.cursors[shape] = {
  554. url: url,
  555. x: p_hotspot_x,
  556. y: p_hotspot_y,
  557. };
  558. } else {
  559. delete GodotDisplayCursor.cursors[shape];
  560. }
  561. if (shape === GodotDisplayCursor.shape) {
  562. GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
  563. }
  564. if (old_shape) {
  565. URL.revokeObjectURL(old_shape.url);
  566. }
  567. },
  568. godot_js_display_cursor_lock_set__sig: 'vi',
  569. godot_js_display_cursor_lock_set: function (p_lock) {
  570. if (p_lock) {
  571. GodotDisplayCursor.lockPointer();
  572. } else {
  573. GodotDisplayCursor.releasePointer();
  574. }
  575. },
  576. godot_js_display_cursor_is_locked__sig: 'i',
  577. godot_js_display_cursor_is_locked: function () {
  578. return GodotDisplayCursor.isPointerLocked() ? 1 : 0;
  579. },
  580. /*
  581. * Listeners
  582. */
  583. godot_js_display_fullscreen_cb__sig: 'vi',
  584. godot_js_display_fullscreen_cb: function (callback) {
  585. const canvas = GodotConfig.canvas;
  586. const func = GodotRuntime.get_func(callback);
  587. function change_cb(evt) {
  588. if (evt.target === canvas) {
  589. func(GodotDisplayScreen.isFullscreen());
  590. }
  591. }
  592. GodotEventListeners.add(document, 'fullscreenchange', change_cb, false);
  593. GodotEventListeners.add(document, 'mozfullscreenchange', change_cb, false);
  594. GodotEventListeners.add(document, 'webkitfullscreenchange', change_cb, false);
  595. },
  596. godot_js_display_window_blur_cb__sig: 'vi',
  597. godot_js_display_window_blur_cb: function (callback) {
  598. const func = GodotRuntime.get_func(callback);
  599. GodotEventListeners.add(window, 'blur', function () {
  600. func();
  601. }, false);
  602. },
  603. godot_js_display_notification_cb__sig: 'viiiii',
  604. godot_js_display_notification_cb: function (callback, p_enter, p_exit, p_in, p_out) {
  605. const canvas = GodotConfig.canvas;
  606. const func = GodotRuntime.get_func(callback);
  607. const notif = [p_enter, p_exit, p_in, p_out];
  608. ['mouseover', 'mouseleave', 'focus', 'blur'].forEach(function (evt_name, idx) {
  609. GodotEventListeners.add(canvas, evt_name, function () {
  610. func(notif[idx]);
  611. }, true);
  612. });
  613. },
  614. godot_js_display_setup_canvas__sig: 'viiii',
  615. godot_js_display_setup_canvas: function (p_width, p_height, p_fullscreen, p_hidpi) {
  616. const canvas = GodotConfig.canvas;
  617. GodotEventListeners.add(canvas, 'contextmenu', function (ev) {
  618. ev.preventDefault();
  619. }, false);
  620. GodotEventListeners.add(canvas, 'webglcontextlost', function (ev) {
  621. alert('WebGL context lost, please reload the page'); // eslint-disable-line no-alert
  622. ev.preventDefault();
  623. }, false);
  624. GodotDisplayScreen.hidpi = !!p_hidpi;
  625. switch (GodotConfig.canvas_resize_policy) {
  626. case 0: // None
  627. GodotDisplayScreen.desired_size = [canvas.width, canvas.height];
  628. break;
  629. case 1: // Project
  630. GodotDisplayScreen.desired_size = [p_width, p_height];
  631. break;
  632. default: // Full window
  633. // Ensure we display in the right place, the size will be handled by updateSize
  634. canvas.style.position = 'absolute';
  635. canvas.style.top = 0;
  636. canvas.style.left = 0;
  637. break;
  638. }
  639. GodotDisplayScreen.updateSize();
  640. if (p_fullscreen) {
  641. GodotDisplayScreen.requestFullscreen();
  642. }
  643. },
  644. /*
  645. * Virtual Keyboard
  646. */
  647. godot_js_display_vk_show__sig: 'viiii',
  648. godot_js_display_vk_show: function (p_text, p_multiline, p_start, p_end) {
  649. const text = GodotRuntime.parseString(p_text);
  650. const start = p_start > 0 ? p_start : 0;
  651. const end = p_end > 0 ? p_end : start;
  652. GodotDisplayVK.show(text, p_multiline, start, end);
  653. },
  654. godot_js_display_vk_hide__sig: 'v',
  655. godot_js_display_vk_hide: function () {
  656. GodotDisplayVK.hide();
  657. },
  658. godot_js_display_vk_available__sig: 'i',
  659. godot_js_display_vk_available: function () {
  660. return GodotDisplayVK.available();
  661. },
  662. godot_js_display_tts_available__sig: 'i',
  663. godot_js_display_tts_available: function () {
  664. return 'speechSynthesis' in window;
  665. },
  666. godot_js_display_vk_cb__sig: 'vi',
  667. godot_js_display_vk_cb: function (p_input_cb) {
  668. const input_cb = GodotRuntime.get_func(p_input_cb);
  669. if (GodotDisplayVK.available()) {
  670. GodotDisplayVK.init(input_cb);
  671. }
  672. },
  673. };
  674. autoAddDeps(GodotDisplay, '$GodotDisplay');
  675. mergeInto(LibraryManager.library, GodotDisplay);