| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import SystemBase from '../Base/SystemBase'
- class InputSystem extends SystemBase {
- private downX:number = -1;
- private downY:number = -1;
- private delay: number = 300;
- private timestamp: number = 0;
- constructor() {
- super(InputSystem.name)
- this.init()
- console.log(this)
- }
- protected ctx() {
- return this;
- }
- private init() {
- document.addEventListener('mousedown', this.mousedown.bind(this));
- document.addEventListener('mouseup', this.mouseup.bind(this));
- document.addEventListener('mousemove', this.mousemove.bind(this));
- }
- private mousedown(e: MouseEvent) {
- this.downX = e.clientX
- this.downY = e.clientY
- this.dispatchEvent({ type: 'mousedown', event: e })
- this.timestamp = Date.now();
- }
- private mouseup(e: MouseEvent) {
- this.dispatchEvent({ type: 'mouseup', event: e })
- if (Date.now() - this.timestamp < this.delay) {
- // 模拟点击事件
- this.dispatchEvent({ type: 'click', event: e })
- }
- this.downX = -1
- this.downY = -1
- }
- private mousemove(e: MouseEvent) {
- this.dispatchEvent({ type: 'mousemove', event: e })
- if(this.downX>-1 || this.downY>-1){
- this.dispatchEvent({ type: 'mousedrag', event: e })
- }
- }
- }
- export default InputSystem
|