library_godot_input.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*************************************************************************/
  2. /* library_godot_input.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. const GodotInput = {
  31. $GodotInput__deps: ['$GodotRuntime', '$GodotConfig', '$GodotDisplayListeners'],
  32. $GodotInput: {
  33. getModifiers: function (evt) {
  34. return (evt.shiftKey + 0) + ((evt.altKey + 0) << 1) + ((evt.ctrlKey + 0) << 2) + ((evt.metaKey + 0) << 3);
  35. },
  36. computePosition: function (evt, rect) {
  37. const canvas = GodotConfig.canvas;
  38. const rw = canvas.width / rect.width;
  39. const rh = canvas.height / rect.height;
  40. const x = (evt.clientX - rect.x) * rw;
  41. const y = (evt.clientY - rect.y) * rh;
  42. return [x, y];
  43. },
  44. },
  45. godot_js_display_mouse_move_cb__sig: 'vi',
  46. godot_js_display_mouse_move_cb: function (callback) {
  47. const func = GodotRuntime.get_func(callback);
  48. const canvas = GodotConfig.canvas;
  49. function move_cb(evt) {
  50. const rect = canvas.getBoundingClientRect();
  51. const pos = GodotInput.computePosition(evt, rect);
  52. // Scale movement
  53. const rw = canvas.width / rect.width;
  54. const rh = canvas.height / rect.height;
  55. const rel_pos_x = evt.movementX * rw;
  56. const rel_pos_y = evt.movementY * rh;
  57. const modifiers = GodotInput.getModifiers(evt);
  58. func(pos[0], pos[1], rel_pos_x, rel_pos_y, modifiers);
  59. }
  60. GodotDisplayListeners.add(window, 'mousemove', move_cb, false);
  61. },
  62. godot_js_display_mouse_wheel_cb__sig: 'vi',
  63. godot_js_display_mouse_wheel_cb: function (callback) {
  64. const func = GodotRuntime.get_func(callback);
  65. function wheel_cb(evt) {
  66. if (func(evt['deltaX'] || 0, evt['deltaY'] || 0)) {
  67. evt.preventDefault();
  68. }
  69. }
  70. GodotDisplayListeners.add(GodotConfig.canvas, 'wheel', wheel_cb, false);
  71. },
  72. godot_js_display_mouse_button_cb__sig: 'vi',
  73. godot_js_display_mouse_button_cb: function (callback) {
  74. const func = GodotRuntime.get_func(callback);
  75. const canvas = GodotConfig.canvas;
  76. function button_cb(p_pressed, evt) {
  77. const rect = canvas.getBoundingClientRect();
  78. const pos = GodotInput.computePosition(evt, rect);
  79. const modifiers = GodotInput.getModifiers(evt);
  80. if (func(p_pressed, evt.button, pos[0], pos[1], modifiers)) {
  81. evt.preventDefault();
  82. }
  83. }
  84. GodotDisplayListeners.add(canvas, 'mousedown', button_cb.bind(null, 1), false);
  85. GodotDisplayListeners.add(window, 'mouseup', button_cb.bind(null, 0), false);
  86. },
  87. godot_js_display_touch_cb__sig: 'viii',
  88. godot_js_display_touch_cb: function (callback, ids, coords) {
  89. const func = GodotRuntime.get_func(callback);
  90. const canvas = GodotConfig.canvas;
  91. function touch_cb(type, evt) {
  92. const rect = canvas.getBoundingClientRect();
  93. const touches = evt.changedTouches;
  94. for (let i = 0; i < touches.length; i++) {
  95. const touch = touches[i];
  96. const pos = GodotInput.computePosition(touch, rect);
  97. GodotRuntime.setHeapValue(coords + (i * 2), pos[0], 'double');
  98. GodotRuntime.setHeapValue(coords + (i * 2 + 8), pos[1], 'double');
  99. GodotRuntime.setHeapValue(ids + i, touch.identifier, 'i32');
  100. }
  101. func(type, touches.length);
  102. if (evt.cancelable) {
  103. evt.preventDefault();
  104. }
  105. }
  106. GodotDisplayListeners.add(canvas, 'touchstart', touch_cb.bind(null, 0), false);
  107. GodotDisplayListeners.add(canvas, 'touchend', touch_cb.bind(null, 1), false);
  108. GodotDisplayListeners.add(canvas, 'touchcancel', touch_cb.bind(null, 1), false);
  109. GodotDisplayListeners.add(canvas, 'touchmove', touch_cb.bind(null, 2), false);
  110. },
  111. godot_js_display_key_cb__sig: 'viii',
  112. godot_js_display_key_cb: function (callback, code, key) {
  113. const func = GodotRuntime.get_func(callback);
  114. function key_cb(pressed, evt) {
  115. const modifiers = GodotInput.getModifiers(evt);
  116. GodotRuntime.stringToHeap(evt.code, code, 32);
  117. GodotRuntime.stringToHeap(evt.key, key, 32);
  118. func(pressed, evt.repeat, modifiers);
  119. evt.preventDefault();
  120. }
  121. GodotDisplayListeners.add(GodotConfig.canvas, 'keydown', key_cb.bind(null, 1), false);
  122. GodotDisplayListeners.add(GodotConfig.canvas, 'keyup', key_cb.bind(null, 0), false);
  123. },
  124. };
  125. autoAddDeps(GodotInput, '$GodotInput');
  126. mergeInto(LibraryManager.library, GodotInput);