Fix mouse events
This commit is contained in:
parent
31f77c22da
commit
6d7d65dc67
41
mouse.ts
41
mouse.ts
@ -13,8 +13,11 @@ const mouseButtonsDown = {
|
|||||||
[M.RIGHT]: false,
|
[M.RIGHT]: false,
|
||||||
[M.MIDDLE]: false,
|
[M.MIDDLE]: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type MouseEventType = "click" | "down" | "up" | "move" | "dblclick";
|
||||||
|
|
||||||
const mouseEvents: Array<{
|
const mouseEvents: Array<{
|
||||||
type: "click" | "down" | "up" | "move" | "dblclick",
|
type: MouseEventType,
|
||||||
button: typeof M[keyof typeof M],
|
button: typeof M[keyof typeof M],
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
@ -27,37 +30,59 @@ let mouseY = 0;
|
|||||||
|
|
||||||
const eventPixelCoords = (evt: WindowMouseEvent) => {
|
const eventPixelCoords = (evt: WindowMouseEvent) => {
|
||||||
const {width, height} = gameWindow.size;
|
const {width, height} = gameWindow.size;
|
||||||
const pixX = Math.floor(128*evt.clientX/width);
|
const min = Math.min(width, height);
|
||||||
const pixY = Math.floor(128*evt.clientY/height);
|
const pixX = Math.floor(128*(evt.clientX-(width-min)/2)/min);
|
||||||
|
const pixY = Math.floor(128*(evt.clientY-(height-min)/2)/min);
|
||||||
|
if (pixX < 0 || pixX > 128 || pixY < 0 || pixY > 128) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
x: pixX,
|
x: pixX,
|
||||||
y: pixY,
|
y: pixY,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pushEvent = (type: MouseEventType, evt: WindowMouseEvent, extra?: Partial<typeof mouseEvents[0]>) => {
|
||||||
|
const coords = eventPixelCoords(evt);
|
||||||
|
if (!coords) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mouseEvents.push({type, button: evt.button, ...coords, ...(extra ?? {})});
|
||||||
|
}
|
||||||
|
|
||||||
|
const evtInBounds = (evt: WindowMouseEvent) => {
|
||||||
|
return !!eventPixelCoords(evt);
|
||||||
|
}
|
||||||
|
|
||||||
addEventListener("dblclick", (evt) => {
|
addEventListener("dblclick", (evt) => {
|
||||||
mouseEvents.push({type: "dblclick", button: evt.button, ...eventPixelCoords(evt)});
|
pushEvent("dblclick", evt);
|
||||||
});
|
});
|
||||||
|
|
||||||
addEventListener("click", (evt) => {
|
addEventListener("click", (evt) => {
|
||||||
mouseEvents.push({type: "click", button: evt.button, ...eventPixelCoords(evt)});
|
pushEvent("click", evt);
|
||||||
});
|
});
|
||||||
|
|
||||||
addEventListener("mousedown", (evt) => {
|
addEventListener("mousedown", (evt) => {
|
||||||
|
if (evtInBounds(evt)) {
|
||||||
mouseButtonsDown[evt.button] = true;
|
mouseButtonsDown[evt.button] = true;
|
||||||
mouseEvents.push({type: "down", button: evt.button, ...eventPixelCoords(evt)});
|
}
|
||||||
|
pushEvent("down", evt);
|
||||||
});
|
});
|
||||||
|
|
||||||
addEventListener("mouseup", (evt) => {
|
addEventListener("mouseup", (evt) => {
|
||||||
|
if (evtInBounds(evt)) {
|
||||||
mouseButtonsDown[evt.button] = false;
|
mouseButtonsDown[evt.button] = false;
|
||||||
mouseEvents.push({type: "up", button: evt.button, ...eventPixelCoords(evt)});
|
}
|
||||||
|
pushEvent("up", evt);
|
||||||
});
|
});
|
||||||
|
|
||||||
addEventListener("mousemove", (evt) => {
|
addEventListener("mousemove", (evt) => {
|
||||||
const coords = eventPixelCoords(evt);
|
const coords = eventPixelCoords(evt);
|
||||||
mouseEvents.push({type: "move", button: evt.button, ...eventPixelCoords(evt), prevX: mouseX, prevY: mouseY});
|
pushEvent("up", evt, {prevX: mouseX, prevY: mouseY});
|
||||||
|
if (coords) {
|
||||||
mouseX = coords.x;
|
mouseX = coords.x;
|
||||||
mouseY = coords.y;
|
mouseY = coords.y;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const mousePos = () => {
|
export const mousePos = () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user