index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. var buttons = require('./buttons.js');
  3. var device = require('./device.js');
  4. var mouse = require('./mouse.js');
  5. var pointer = require('./pointer.js');
  6. function _define_property(obj, key, value) {
  7. if (key in obj) {
  8. Object.defineProperty(obj, key, {
  9. value: value,
  10. enumerable: true,
  11. configurable: true,
  12. writable: true
  13. });
  14. } else {
  15. obj[key] = value;
  16. }
  17. return obj;
  18. }
  19. class PointerHost {
  20. isKeyPressed(keyDef) {
  21. return this.devices.get(keyDef.pointerType).isPressed(keyDef);
  22. }
  23. async press(instance, keyDef, position) {
  24. this.devices.get(keyDef.pointerType).addPressed(keyDef);
  25. this.buttons.down(keyDef);
  26. const pointerName = this.getPointerName(keyDef);
  27. const pointer = keyDef.pointerType === 'touch' ? this.pointers.new(pointerName, keyDef.pointerType, this.buttons) : this.pointers.get(pointerName);
  28. // TODO: deprecate the following implicit setting of position
  29. pointer.position = position;
  30. if (pointer.pointerType !== 'touch') {
  31. this.mouse.position = position;
  32. }
  33. if (pointer.pointerType === 'touch') {
  34. pointer.init(instance);
  35. }
  36. pointer.down(instance, keyDef.button);
  37. if (pointer.pointerType !== 'touch') {
  38. this.mouse.down(instance, keyDef, pointer.isPrevented);
  39. }
  40. }
  41. async move(instance, pointerName, position) {
  42. const pointer = this.pointers.get(pointerName);
  43. // In (some?) browsers this order of events can be observed.
  44. // This interweaving of events is probably unnecessary.
  45. // While the order of mouse (or pointer) events is defined per spec,
  46. // the order in which they interweave/follow on a user interaction depends on the implementation.
  47. const pointermove = pointer.move(instance, position);
  48. const mousemove = pointer.pointerType === 'touch' ? undefined : this.mouse.move(instance, position, pointer.isPrevented);
  49. pointermove === null || pointermove === undefined ? undefined : pointermove.leave();
  50. mousemove === null || mousemove === undefined ? undefined : mousemove.leave();
  51. pointermove === null || pointermove === undefined ? undefined : pointermove.enter();
  52. mousemove === null || mousemove === undefined ? undefined : mousemove.enter();
  53. pointermove === null || pointermove === undefined ? undefined : pointermove.move();
  54. mousemove === null || mousemove === undefined ? undefined : mousemove.move();
  55. }
  56. async release(instance, keyDef, position) {
  57. const device = this.devices.get(keyDef.pointerType);
  58. device.removePressed(keyDef);
  59. this.buttons.up(keyDef);
  60. const pointer = this.pointers.get(this.getPointerName(keyDef));
  61. const isPrevented = pointer.isPrevented;
  62. // TODO: deprecate the following implicit setting of position
  63. pointer.position = position;
  64. if (pointer.pointerType !== 'touch') {
  65. this.mouse.position = position;
  66. }
  67. if (device.countPressed === 0) {
  68. pointer.up(instance, keyDef.button);
  69. }
  70. if (pointer.pointerType === 'touch') {
  71. pointer.release(instance);
  72. }
  73. if (pointer.pointerType === 'touch' && !pointer.isMultitouch) {
  74. const mousemove = this.mouse.move(instance, position, isPrevented);
  75. mousemove === null || mousemove === undefined ? undefined : mousemove.leave();
  76. mousemove === null || mousemove === undefined ? undefined : mousemove.enter();
  77. mousemove === null || mousemove === undefined ? undefined : mousemove.move();
  78. this.mouse.down(instance, keyDef, isPrevented);
  79. }
  80. if (!pointer.isMultitouch) {
  81. const mousemove = this.mouse.move(instance, position, isPrevented);
  82. mousemove === null || mousemove === undefined ? undefined : mousemove.leave();
  83. mousemove === null || mousemove === undefined ? undefined : mousemove.enter();
  84. mousemove === null || mousemove === undefined ? undefined : mousemove.move();
  85. this.mouse.up(instance, keyDef, isPrevented);
  86. }
  87. }
  88. getPointerName(keyDef) {
  89. return keyDef.pointerType === 'touch' ? keyDef.name : keyDef.pointerType;
  90. }
  91. getPreviousPosition(pointerName) {
  92. return this.pointers.has(pointerName) ? this.pointers.get(pointerName).position : undefined;
  93. }
  94. resetClickCount() {
  95. this.mouse.resetClickCount();
  96. }
  97. getMouseTarget(instance) {
  98. var _this_mouse_position_target;
  99. return (_this_mouse_position_target = this.mouse.position.target) !== null && _this_mouse_position_target !== undefined ? _this_mouse_position_target : instance.config.document.body;
  100. }
  101. setMousePosition(position) {
  102. this.mouse.position = position;
  103. this.pointers.get('mouse').position = position;
  104. }
  105. constructor(system){
  106. _define_property(this, "system", undefined);
  107. _define_property(this, "mouse", undefined);
  108. _define_property(this, "buttons", undefined);
  109. _define_property(this, "devices", new class {
  110. get(k) {
  111. var _this_registry, _k;
  112. var _;
  113. return (_ = (_this_registry = this.registry)[_k = k]) !== null && _ !== undefined ? _ : _this_registry[_k] = new device.Device();
  114. }
  115. constructor(){
  116. _define_property(this, "registry", {});
  117. }
  118. }());
  119. _define_property(this, "pointers", new class {
  120. new(pointerName, pointerType, buttons) {
  121. const isPrimary = pointerType !== 'touch' || !Object.values(this.registry).some((p)=>p.pointerType === 'touch' && !p.isCancelled);
  122. if (!isPrimary) {
  123. Object.values(this.registry).forEach((p)=>{
  124. if (p.pointerType === pointerType && !p.isCancelled) {
  125. p.isMultitouch = true;
  126. }
  127. });
  128. }
  129. this.registry[pointerName] = new pointer.Pointer({
  130. pointerId: this.nextId++,
  131. pointerType,
  132. isPrimary
  133. }, buttons);
  134. return this.registry[pointerName];
  135. }
  136. get(pointerName) {
  137. if (!this.has(pointerName)) {
  138. throw new Error(`Trying to access pointer "${pointerName}" which does not exist.`);
  139. }
  140. return this.registry[pointerName];
  141. }
  142. has(pointerName) {
  143. return pointerName in this.registry;
  144. }
  145. constructor(){
  146. _define_property(this, "registry", {});
  147. _define_property(this, "nextId", 1);
  148. }
  149. }());
  150. this.system = system;
  151. this.buttons = new buttons.Buttons();
  152. this.mouse = new mouse.Mouse();
  153. this.pointers.new('mouse', 'mouse', this.buttons);
  154. }
  155. }
  156. exports.PointerHost = PointerHost;