-
Notifications
You must be signed in to change notification settings - Fork 2
/
mouseInput.ts
127 lines (106 loc) · 3.39 KB
/
mouseInput.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @ 创建者: FBplus
* @ 创建时间: 2022-06-07 17:01:01
* @ 修改者: FBplus
* @ 修改时间: 2022-07-28 17:49:21
* @ 详情: 鼠标操作
*/
import * as pc from "playcanvas";
import { InputEventsMap } from "@/utils/common/InputEventsMap";
import { Tool } from "@/utils/helpers/toolBase";
// import { tool } from "@/utils/helpers/useToolHelper";
/**
* 鼠标输入选项
*/
export interface MouseInputOptions { clickError: number };
// @tool("MouseInputer")
export class MouseInputer extends Tool<MouseInputOptions, InputEventsMap>
{
// 默认选项
protected toolOptionsDefault: MouseInputOptions = {
clickError: 1
};
private mouseDownVec: pc.Vec2;
private mouseMoveVec: pc.Vec2;
private mouseUpVec: pc.Vec2;
private isDragging: boolean;
constructor(option?: MouseInputOptions)
{
super();
this.setOptions(option);
this.mouseDownVec = new pc.Vec2();
this.mouseMoveVec = new pc.Vec2();
this.mouseUpVec = new pc.Vec2();
}
private onMouseDown(event: pc.MouseEvent): void
{
this.isDragging = true;
this.mouseDownVec.set(event.x, event.y);
this.mouseMoveVec.set(event.x, event.y);
this.fire("down", {
x: this.mouseDownVec.x,
y: this.mouseDownVec.y
});
}
private onMouseMove(event: pc.MouseEvent): void
{
const dx = event.x - this.mouseMoveVec.x;
const dy = event.y - this.mouseMoveVec.y;
this.mouseMoveVec.set(event.x, event.y);
this.fire("move", {
x: this.mouseMoveVec.x,
y: this.mouseMoveVec.y,
dx: dx,
dy: dy
});
if (this.isDragging) {
this.fire("dragging", {
x: this.mouseMoveVec.x,
y: this.mouseMoveVec.y,
dx: dx,
dy: dy,
ox: this.mouseDownVec.x,
oy: this.mouseDownVec.y
});
}
}
private onMouseUp(event: pc.MouseEvent): void
{
this.mouseUpVec.set(event.x, event.y);
if (this.mouseUpVec.distance(this.mouseDownVec) < this.toolOptions.clickError) {
this.fire("click", {
x: this.mouseUpVec.x,
y: this.mouseUpVec.y
});
}
this.fire("up", {
x: this.mouseUpVec.x,
y: this.mouseUpVec.y
});
if (this.isDragging) {
this.fire("dragEnd", {
x: this.mouseUpVec.x,
y: this.mouseUpVec.y
});
}
this.isDragging = false;
}
private onMouseWheel(event: pc.MouseEvent): void
{
this.fire("pinch", { delta: event.wheelDelta, event: event.event });
}
protected override onEnable(): void
{
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.on(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
}
protected override onDisable(): void
{
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.off(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
this.app.mouse.off(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.off(pc.EVENT_MOUSEUP, this.onMouseUp, this);
}
}