Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

247 righe
5.0 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/rendering/renderer'
  4. ], function (
  5. events,
  6. renderer
  7. ) {
  8. return {
  9. axes: {
  10. horizontal: {
  11. negative: ['left', 'a', 'q', 'z'],
  12. positive: ['right', 'd', 'e', 'c']
  13. },
  14. vertical: {
  15. negative: ['up', 'w', 'q', 'e'],
  16. positive: ['down', 's', 'x', 'z', 'c']
  17. }
  18. },
  19. //Certain keys should always register even if they don't get emitted
  20. modifiers: [
  21. 'shift', 'ctrl'
  22. ],
  23. mappings: {
  24. 8: 'backspace',
  25. 9: 'tab',
  26. 13: 'enter',
  27. 16: 'shift',
  28. 17: 'ctrl',
  29. 27: 'esc',
  30. 37: 'left',
  31. 38: 'up',
  32. 39: 'right',
  33. 40: 'down',
  34. 46: 'del',
  35. //hacks for mac cmd key
  36. 224: 'ctrl',
  37. 91: 'ctrl',
  38. 93: 'ctrl'
  39. },
  40. mouse: {
  41. button: null,
  42. x: 0,
  43. y: 0
  44. },
  45. mouseRaw: null,
  46. keys: {},
  47. enabled: true,
  48. init: function () {
  49. $(window).on('keydown', this.events.keyboard.keyDown.bind(this));
  50. $(window).on('keyup', this.events.keyboard.keyUp.bind(this));
  51. events.on('onSceneMove', this.events.mouse.mouseMove.bind(this));
  52. $('.ui-container')
  53. .on('mousedown', this.events.mouse.mouseDown.bind(this))
  54. .on('mouseup', this.events.mouse.mouseUp.bind(this))
  55. .on('mousemove', this.events.mouse.mouseMove.bind(this))
  56. .on('touchstart', this.events.touch.touchStart.bind(this))
  57. .on('touchmove', this.events.touch.touchMove.bind(this))
  58. .on('touchend', this.events.touch.touchEnd.bind(this))
  59. .on('touchcancel', this.events.touch.touchCancel.bind(this));
  60. },
  61. resetKeys: function () {
  62. for (let k in this.keys)
  63. events.emit('onKeyUp', k);
  64. this.keys = {};
  65. },
  66. getMapping: function (charCode) {
  67. if (charCode >= 97)
  68. return (charCode - 96).toString();
  69. return (
  70. this.mappings[charCode] ||
  71. String.fromCharCode(charCode).toLowerCase()
  72. );
  73. },
  74. isKeyDown: function (key, noConsume) {
  75. if (this.keys.has(key)) {
  76. if (noConsume)
  77. return true;
  78. let down = this.keys[key];
  79. this.keys[key] = 2;
  80. return (down === 1);
  81. } return false;
  82. },
  83. getAxis: function (axisName) {
  84. let axis = this.axes[axisName];
  85. if (!axis)
  86. return 0;
  87. let result = 0;
  88. for (let i = 0; i < axis.negative.length; i++) {
  89. if (this.keys[axis.negative[i]]) {
  90. result--;
  91. break;
  92. }
  93. }
  94. for (let i = 0; i < axis.positive.length; i++) {
  95. if (this.keys[axis.positive[i]]) {
  96. result++;
  97. break;
  98. }
  99. }
  100. return result;
  101. },
  102. events: {
  103. keyboard: {
  104. keyDown: function (e) {
  105. if (!this.enabled)
  106. return;
  107. let key = this.getMapping(e.which);
  108. let isModifier = this.modifiers.indexOf(key) > -1;
  109. let isBody = e.target === document.body;
  110. if (!isModifier && !isBody)
  111. return true;
  112. if ((e.keyCode === 9) || (e.keyCode === 8) || (e.keyCode === 122))
  113. e.preventDefault();
  114. if (this.keys.has(key))
  115. this.keys[key] = 2;
  116. else {
  117. this.keys[key] = 1;
  118. if (isBody || isModifier) {
  119. let keyEvent = {
  120. key: key,
  121. consumed: false
  122. };
  123. events.emit('onUiKeyDown', keyEvent);
  124. if (!keyEvent.consumed)
  125. events.emit('onCanvasKeyDown', keyEvent);
  126. events.emit('onKeyDown', key);
  127. }
  128. }
  129. if (key === 'backspace')
  130. return false;
  131. else if (e.key === 'F11')
  132. events.emit('onToggleFullscreen');
  133. },
  134. keyUp: function (e) {
  135. if (!this.enabled)
  136. return;
  137. let key = this.getMapping(e.which);
  138. let isModifier = this.modifiers.indexOf(key) > -1;
  139. let isBody = e.target === document.body;
  140. if (!isModifier && !isBody)
  141. return;
  142. delete this.keys[key];
  143. events.emit('onKeyUp', key);
  144. }
  145. },
  146. mouse: {
  147. mouseDown: function (e) {
  148. let el = $(e.target);
  149. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  150. return;
  151. let button = e.button;
  152. this.mouse.button = button;
  153. this.mouse.down = true;
  154. this.mouse.event = e;
  155. events.emit('mouseDown', this.mouse);
  156. },
  157. mouseUp: function (e) {
  158. let el = $(e.target);
  159. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  160. return;
  161. this.mouse.button = null;
  162. this.mouse.down = false;
  163. events.emit('mouseUp', this.mouse);
  164. },
  165. mouseMove: function (e) {
  166. if (e)
  167. this.mouseRaw = e;
  168. else
  169. e = this.mouseRaw;
  170. if (!e)
  171. return;
  172. let el = $(e.target);
  173. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  174. return;
  175. this.mouse.x = e.offsetX + (renderer.pos.x);
  176. this.mouse.y = e.offsetY + (renderer.pos.y);
  177. }
  178. },
  179. touch: {
  180. touchStart: function (e) {
  181. let touch = e.touches[0];
  182. events.emit('onTouchStart', {
  183. x: touch.clientX,
  184. y: touch.clientY,
  185. worldX: touch.clientX + renderer.pos.x,
  186. worldY: touch.clientY + renderer.pos.y
  187. });
  188. },
  189. touchMove: function (e) {
  190. let touch = e.touches[0];
  191. events.emit('onTouchMove', {
  192. x: touch.clientX,
  193. y: touch.clientY
  194. });
  195. },
  196. touchEnd: function (e) {
  197. events.emit('onTouchEnd');
  198. },
  199. touchCancel: function (e) {
  200. events.emit('onTouchCancel');
  201. }
  202. }
  203. }
  204. };
  205. });