library_godot_display.js 21 KB

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