-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouseEvents.ts
119 lines (104 loc) · 2.91 KB
/
mouseEvents.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
import type { User } from "@/types/general";
import type { Camera } from "@/types/canvasTypes";
import { isColliding } from "./utilities";
function getUserCollision(
users: User[],
myPlayerId: string,
worldX: number,
worldY: number,
playerWidth: number,
playerHeight: number
): boolean {
for (const user of users) {
if (
user.id !== myPlayerId &&
isColliding(
{
x: worldX - 36,
y: worldY - 64,
width: playerWidth,
height: playerHeight,
},
{ x: user.x, y: user.y, width: playerWidth, height: playerHeight }
)
) {
return true;
}
}
return false;
}
function handleRightClick(
e: MouseEvent,
camera: Camera,
users: User[],
myPlayerId: string
): void {
const { clientX: mouseX, clientY: mouseY } = e;
const worldX = mouseX / camera.zoomFactor + camera.cameraX;
const worldY = mouseY / camera.zoomFactor + camera.cameraY;
const mousePos = { mouseX, mouseY };
const worldPos = { worldX, worldY };
const playerWidth = (18 * camera.zoomFactor) / 3;
const playerHeight = (22 * camera.zoomFactor) / 3;
// Check if the right-click is on any user, if so, don't emit right click event
const collision = getUserCollision(
users,
myPlayerId,
worldX,
worldY,
playerWidth,
playerHeight
);
if (!collision) {
// Emit right click event to Space.vue
emitter.emit("rightClick", { mousePos, worldPos });
}
e.preventDefault();
}
export function rightClickEventListener(
canvas: HTMLCanvasElement,
camera: Camera,
users: User[],
myPlayerId: string
): void {
canvas.addEventListener("contextmenu", (e) =>
handleRightClick(e, camera, users, myPlayerId)
);
}
// Override zoom and ctrl+scroll
function handleWheelEvent(
e: WheelEvent,
camera: { cameraX: number; cameraY: number; zoomFactor: number }
): void {
if (e.ctrlKey) {
e.preventDefault();
const zoomSpeed = 0.1;
const zoomIncrement = e.deltaY < 0 ? zoomSpeed : -zoomSpeed;
// Limit zoom to 0.5 - 1.5
camera.zoomFactor = clampZoom(camera.zoomFactor + zoomIncrement, 0.5, 1.5);
console.log(camera.zoomFactor);
}
}
export function wheelEventListener(
canvas: HTMLCanvasElement,
camera: { cameraX: number; cameraY: number; zoomFactor: number }
): void {
canvas.addEventListener("wheel", (e) => handleWheelEvent(e, camera));
}
// For limiting zoom
function clampZoom(
zoomFactor: number,
minZoom: number,
maxZoom: number
): number {
return Math.min(Math.max(zoomFactor, minZoom), maxZoom);
}
// Listen to double click
/* canvas.addEventListener("dblclick", (e: MouseEvent) => {
const x = e.clientX + cameraX - 8;
const y = e.clientY + cameraY - 25;
// Emit double click event
emitter.emit("doubleClick", { x, y });
myPlayer.x = x;
myPlayer.y = y;
}); */