You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

247 lines
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. $('.canvas-container');
  57. .on('touchstart', this.events.touch.touchStart.bind(this))
  58. .on('touchmove', this.events.touch.touchMove.bind(this))
  59. .on('touchend', this.events.touch.touchEnd.bind(this))
  60. .on('touchcancel', this.events.touch.touchCancel.bind(this));
  61. },
  62. resetKeys: function () {
  63. for (let k in this.keys)
  64. events.emit('onKeyUp', k);
  65. this.keys = {};
  66. },
  67. getMapping: function (charCode) {
  68. if (charCode >= 97)
  69. return (charCode - 96).toString();
  70. return (
  71. this.mappings[charCode] ||
  72. String.fromCharCode(charCode).toLowerCase()
  73. );
  74. },
  75. isKeyDown: function (key, noConsume) {
  76. if (this.keys.has(key)) {
  77. if (noConsume)
  78. return true;
  79. let down = this.keys[key];
  80. this.keys[key] = 2;
  81. return (down === 1);
  82. } return false;
  83. },
  84. getAxis: function (axisName) {
  85. let axis = this.axes[axisName];
  86. if (!axis)
  87. return 0;
  88. let result = 0;
  89. for (let i = 0; i < axis.negative.length; i++) {
  90. if (this.keys[axis.negative[i]]) {
  91. result--;
  92. break;
  93. }
  94. }
  95. for (let i = 0; i < axis.positive.length; i++) {
  96. if (this.keys[axis.positive[i]]) {
  97. result++;
  98. break;
  99. }
  100. }
  101. return result;
  102. },
  103. events: {
  104. keyboard: {
  105. keyDown: function (e) {
  106. if (!this.enabled)
  107. return;
  108. let key = this.getMapping(e.which);
  109. let isModifier = this.modifiers.indexOf(key) > -1;
  110. let isBody = e.target === document.body;
  111. if (!isModifier && !isBody)
  112. return true;
  113. if ((e.keyCode === 9) || (e.keyCode === 8) || (e.keyCode === 122))
  114. e.preventDefault();
  115. if (this.keys.has(key))
  116. this.keys[key] = 2;
  117. else {
  118. this.keys[key] = 1;
  119. if (isBody || isModifier) {
  120. let keyEvent = {
  121. key: key,
  122. consumed: false
  123. };
  124. events.emit('onUiKeyDown', keyEvent);
  125. if (!keyEvent.consumed)
  126. events.emit('onCanvasKeyDown', keyEvent);
  127. events.emit('onKeyDown', key);
  128. }
  129. }
  130. if (key === 'backspace')
  131. return false;
  132. else if (e.key === 'F11')
  133. events.emit('onToggleFullscreen');
  134. },
  135. keyUp: function (e) {
  136. if (!this.enabled)
  137. return;
  138. let key = this.getMapping(e.which);
  139. let isModifier = this.modifiers.indexOf(key) > -1;
  140. let isBody = e.target === document.body;
  141. if (!isModifier && !isBody)
  142. return;
  143. delete this.keys[key];
  144. events.emit('onKeyUp', key);
  145. }
  146. },
  147. mouse: {
  148. mouseDown: function (e) {
  149. let el = $(e.target);
  150. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  151. return;
  152. let button = e.button;
  153. this.mouse.button = button;
  154. this.mouse.down = true;
  155. this.mouse.event = e;
  156. events.emit('mouseDown', this.mouse);
  157. },
  158. mouseUp: function (e) {
  159. let el = $(e.target);
  160. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  161. return;
  162. this.mouse.button = null;
  163. this.mouse.down = false;
  164. events.emit('mouseUp', this.mouse);
  165. },
  166. mouseMove: function (e) {
  167. if (e)
  168. this.mouseRaw = e;
  169. else
  170. e = this.mouseRaw;
  171. if (!e)
  172. return;
  173. let el = $(e.target);
  174. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  175. return;
  176. this.mouse.x = e.offsetX + (renderer.pos.x);
  177. this.mouse.y = e.offsetY + (renderer.pos.y);
  178. }
  179. },
  180. touch: {
  181. touchStart: function (e) {
  182. let touch = e.touches[0];
  183. events.emit('onTouchStart', {
  184. x: touch.clientX,
  185. y: touch.clientY
  186. });
  187. },
  188. touchMove: function (e) {
  189. let touch = e.touches[0];
  190. events.emit('onTouchMove', {
  191. x: touch.clientX,
  192. y: touch.clientY
  193. });
  194. },
  195. touchEnd: function (e) {
  196. events.emit('onTouchEnd');
  197. },
  198. touchCancel: function (e) {
  199. events.emit('onTouchCancel');
  200. }
  201. }
  202. }
  203. };
  204. });