InputSystem.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import SystemBase from '../Base/SystemBase'
  2. class InputSystem extends SystemBase {
  3. private downX:number = -1;
  4. private downY:number = -1;
  5. private delay: number = 300;
  6. private timestamp: number = 0;
  7. constructor() {
  8. super(InputSystem.name)
  9. this.init()
  10. console.log(this)
  11. }
  12. protected ctx() {
  13. return this;
  14. }
  15. private init() {
  16. document.addEventListener('mousedown', this.mousedown.bind(this));
  17. document.addEventListener('mouseup', this.mouseup.bind(this));
  18. document.addEventListener('mousemove', this.mousemove.bind(this));
  19. }
  20. private mousedown(e: MouseEvent) {
  21. this.downX = e.clientX
  22. this.downY = e.clientY
  23. this.dispatchEvent({ type: 'mousedown', event: e })
  24. this.timestamp = Date.now();
  25. }
  26. private mouseup(e: MouseEvent) {
  27. this.dispatchEvent({ type: 'mouseup', event: e })
  28. if (Date.now() - this.timestamp < this.delay) {
  29. // 模拟点击事件
  30. this.dispatchEvent({ type: 'click', event: e })
  31. }
  32. this.downX = -1
  33. this.downY = -1
  34. }
  35. private mousemove(e: MouseEvent) {
  36. this.dispatchEvent({ type: 'mousemove', event: e })
  37. if(this.downX>-1 || this.downY>-1){
  38. this.dispatchEvent({ type: 'mousedrag', event: e })
  39. }
  40. }
  41. }
  42. export default InputSystem