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.
 
 
 

164 lines
3.2 KiB

  1. define([
  2. 'js/events',
  3. 'js/renderer',
  4. 'js/constants'
  5. ], function (
  6. events,
  7. renderer,
  8. constants
  9. ) {
  10. return {
  11. mappings: {
  12. 8: 'backspace',
  13. 9: 'tab',
  14. 13: 'enter',
  15. 16: 'shift',
  16. 17: 'ctrl',
  17. 27: 'esc',
  18. 37: 'left',
  19. 38: 'up',
  20. 39: 'right',
  21. 40: 'down',
  22. 46: 'del'
  23. },
  24. mouse: {
  25. button: null,
  26. x: 0,
  27. y: 0,
  28. raw: null
  29. },
  30. keys: {},
  31. init: function () {
  32. $(window).on('keydown', this.events.keyboard.onKeyDown.bind(this));
  33. $(window).on('keyup', this.events.keyboard.onKeyUp.bind(this));
  34. $('canvas')
  35. .on('mousedown', this.events.mouse.onMouseDown.bind(this))
  36. .on('mouseup', this.events.mouse.onMouseUp.bind(this))
  37. .on('mousemove', this.events.mouse.onMouseMove.bind(this))
  38. .on('mousewheel', this.events.mouse.onMouseWheel.bind(this));
  39. },
  40. resetKeys: function () {
  41. for (let k in this.keys)
  42. events.emit('onKeyUp', k);
  43. this.keys = {};
  44. },
  45. getMapping: function (charCode) {
  46. if (charCode >= 97)
  47. return (charCode - 96).toString();
  48. return (
  49. this.mappings[charCode] ||
  50. String.fromCharCode(charCode).toLowerCase()
  51. );
  52. },
  53. isKeyDown: function (key, consume) {
  54. let down = this.keys[key];
  55. if (down != null) {
  56. if (!consume)
  57. return true;
  58. this.keys[key] = 2;
  59. return (down == 1);
  60. } return false;
  61. },
  62. events: {
  63. keyboard: {
  64. onKeyDown: function (e) {
  65. if (e.target != document.body)
  66. return true;
  67. if ((e.keyCode == 9) || (e.keyCode == 8) || (e.keyCode == 122))
  68. e.preventDefault();
  69. let key = this.getMapping(e.which);
  70. if (this.keys[key] != null)
  71. this.keys[key] = 2;
  72. else {
  73. this.keys[key] = 1;
  74. events.emit('onKeyDown', key);
  75. }
  76. if (key == 'backspace')
  77. return false;
  78. },
  79. onKeyUp: function (e) {
  80. if (e.target != document.body)
  81. return;
  82. let key = this.getMapping(e.which);
  83. delete this.keys[key];
  84. events.emit('onKeyUp', key);
  85. }
  86. },
  87. mouse: {
  88. onMouseDown: function (e) {
  89. let el = $(e.target);
  90. if ((!el.hasClass('canvas')) || (el.hasClass('blocking')))
  91. return;
  92. let button = e.button;
  93. this.mouse.button = button;
  94. this.mouse.down = true;
  95. this.mouse.event = e;
  96. events.emit('onMouseDown', this.mouse);
  97. },
  98. onMouseUp: function (e) {
  99. let el = $(e.target);
  100. if ((!el.hasClass('canvas')) || (el.hasClass('blocking')))
  101. return;
  102. let button = e.button;
  103. this.mouse.down = false;
  104. events.emit('onMouseUp', this.mouse);
  105. this.mouse.button = null;
  106. },
  107. onMouseMove: function (e) {
  108. if (e)
  109. this.mouse.raw = e;
  110. else
  111. e = this.mouse.raw;
  112. if (!e)
  113. return;
  114. let el = $(e.target);
  115. if ((!el.hasClass('canvas')) || (el.hasClass('blocking')))
  116. return;
  117. let x = ~~((renderer.pos.x + (e.offsetX / renderer.currentZoom)) / constants.gridSize);
  118. let y = ~~((renderer.pos.y + (e.offsetY / renderer.currentZoom)) / constants.gridSize);
  119. this.mouse.x = x;
  120. this.mouse.y = y;
  121. events.emit('onMouseMove', this.mouse);
  122. },
  123. onMouseWheel: function (e) {
  124. events.emit('onMouseWheel', {
  125. delta: (e.deltaY > 0) ? 1 : -1
  126. });
  127. }
  128. }
  129. }
  130. };
  131. });