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.
 
 
 

296 righe
6.2 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. numericalKeyCodeMappings: {
  24. Digit1: 49,
  25. Digit2: 50,
  26. Digit3: 51,
  27. Digit4: 52,
  28. Digit5: 53
  29. },
  30. mappings: {
  31. 8: 'backspace',
  32. 9: 'tab',
  33. 13: 'enter',
  34. 16: 'shift',
  35. 17: 'ctrl',
  36. 27: 'esc',
  37. 37: 'left',
  38. 38: 'up',
  39. 39: 'right',
  40. 40: 'down',
  41. 46: 'del',
  42. //hacks for mac cmd key
  43. 224: 'ctrl',
  44. 91: 'ctrl',
  45. 93: 'ctrl'
  46. },
  47. mouse: {
  48. button: null,
  49. x: 0,
  50. y: 0
  51. },
  52. mouseRaw: null,
  53. keys: {},
  54. enabled: true,
  55. init: function () {
  56. $(window).on('keydown', this.events.keyboard.keyDown.bind(this));
  57. $(window).on('keyup', this.events.keyboard.keyUp.bind(this));
  58. events.on('onSceneMove', this.events.mouse.mouseMove.bind(this));
  59. $('.ui-container')
  60. .on('mousedown', this.events.mouse.mouseDown.bind(this))
  61. .on('mouseup', this.events.mouse.mouseUp.bind(this))
  62. .on('mousemove', this.events.mouse.mouseMove.bind(this))
  63. .on('touchstart', this.events.touch.touchStart.bind(this))
  64. .on('touchmove', this.events.touch.touchMove.bind(this))
  65. .on('touchend', this.events.touch.touchEnd.bind(this))
  66. .on('touchcancel', this.events.touch.touchCancel.bind(this));
  67. if (isMobile)
  68. require(['plugins/shake.js'], this.onLoadShake.bind(this));
  69. },
  70. onLoadShake: function (shake) {
  71. let shaker = new shake({
  72. threshold: 5,
  73. timeout: 1000
  74. });
  75. shaker.start();
  76. window.addEventListener('shake', this.events.mobile.onShake.bind(this), false);
  77. },
  78. resetKeys: function () {
  79. for (let k in this.keys)
  80. events.emit('onKeyUp', k);
  81. this.keys = {};
  82. },
  83. getMapping: function (charCode) {
  84. if (charCode >= 97)
  85. return (charCode - 96).toString();
  86. return (
  87. this.mappings[charCode] ||
  88. String.fromCharCode(charCode).toLowerCase()
  89. );
  90. },
  91. isKeyDown: function (key, noConsume) {
  92. if (this.keys.has(key)) {
  93. if (noConsume)
  94. return true;
  95. let down = this.keys[key];
  96. this.keys[key] = 2;
  97. return (down === 1);
  98. } return false;
  99. },
  100. getAxis: function (axisName) {
  101. let axis = this.axes[axisName];
  102. if (!axis)
  103. return 0;
  104. let result = 0;
  105. for (let i = 0; i < axis.negative.length; i++) {
  106. if (this.keys[axis.negative[i]]) {
  107. result--;
  108. break;
  109. }
  110. }
  111. for (let i = 0; i < axis.positive.length; i++) {
  112. if (this.keys[axis.positive[i]]) {
  113. result++;
  114. break;
  115. }
  116. }
  117. return result;
  118. },
  119. events: {
  120. keyboard: {
  121. keyDown: function (e) {
  122. if (!this.enabled)
  123. return;
  124. let code = this.numericalKeyCodeMappings[e.code] || e.which;
  125. let key = this.getMapping(code);
  126. let isModifier = this.modifiers.indexOf(key) > -1;
  127. let isBody = e.target === document.body;
  128. if (!isModifier && !isBody)
  129. return true;
  130. if ((e.keyCode === 9) || (e.keyCode === 8) || (e.keyCode === 122))
  131. e.preventDefault();
  132. if (this.keys.has(key))
  133. this.keys[key] = 2;
  134. else {
  135. this.keys[key] = 1;
  136. if (isBody || isModifier) {
  137. let keyEvent = {
  138. key: key,
  139. consumed: false
  140. };
  141. events.emit('onUiKeyDown', keyEvent);
  142. if (!keyEvent.consumed)
  143. events.emit('onCanvasKeyDown', keyEvent);
  144. events.emit('onKeyDown', key);
  145. }
  146. }
  147. if (key === 'backspace')
  148. return false;
  149. else if (e.key === 'F11')
  150. events.emit('onToggleFullscreen');
  151. },
  152. keyUp: function (e) {
  153. if (!this.enabled)
  154. return;
  155. let key = this.getMapping(e.which);
  156. let isModifier = this.modifiers.indexOf(key) > -1;
  157. let isBody = e.target === document.body;
  158. if (!isModifier && !isBody)
  159. return;
  160. delete this.keys[key];
  161. events.emit('onKeyUp', key);
  162. }
  163. },
  164. mouse: {
  165. mouseDown: function (e) {
  166. let el = $(e.target);
  167. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  168. return;
  169. let button = e.button;
  170. this.mouse.button = button;
  171. this.mouse.down = true;
  172. this.mouse.event = e;
  173. //This is needed for casting targetted spells on Mobile...it's hacky.
  174. this.mouse.worldX = e.pageX + renderer.pos.x;
  175. this.mouse.worldY = e.pageY + renderer.pos.y;
  176. events.emit('mouseDown', this.mouse);
  177. },
  178. mouseUp: function (e) {
  179. let el = $(e.target);
  180. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  181. return;
  182. this.mouse.button = null;
  183. this.mouse.down = false;
  184. events.emit('mouseUp', this.mouse);
  185. },
  186. mouseMove: function (e) {
  187. if (e)
  188. this.mouseRaw = e;
  189. else
  190. e = this.mouseRaw;
  191. if (!e)
  192. return;
  193. let el = $(e.target);
  194. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  195. return;
  196. this.mouse.x = e.offsetX + renderer.pos.x;
  197. this.mouse.y = e.offsetY + renderer.pos.y;
  198. }
  199. },
  200. touch: {
  201. touchStart: function (e) {
  202. let el = $(e.target);
  203. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  204. return;
  205. let touch = e.touches[0];
  206. events.emit('onTouchStart', {
  207. x: touch.clientX,
  208. y: touch.clientY,
  209. worldX: touch.clientX + renderer.pos.x,
  210. worldY: touch.clientY + renderer.pos.y
  211. });
  212. },
  213. touchMove: function (e) {
  214. let el = $(e.target);
  215. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  216. return;
  217. let touch = e.touches[0];
  218. events.emit('onTouchMove', {
  219. x: touch.clientX,
  220. y: touch.clientY,
  221. touches: e.touches.length
  222. });
  223. },
  224. touchEnd: function (e) {
  225. let el = $(e.target);
  226. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  227. return;
  228. events.emit('onTouchEnd');
  229. },
  230. touchCancel: function (e) {
  231. let el = $(e.target);
  232. if ((!el.hasClass('ui-container')) || (el.hasClass('blocking')))
  233. return;
  234. events.emit('onTouchCancel');
  235. }
  236. },
  237. mobile: {
  238. onShake: function (e) {
  239. events.emit('onShake', e);
  240. }
  241. }
  242. }
  243. };
  244. });