2023-05-05 16:02:23 -07:00
|
|
|
import { WindowMouseEvent } from "https://deno.land/x/dwm@0.3.3/mod.ts";
|
|
|
|
import { gameWindow } from "./window.ts";
|
|
|
|
|
|
|
|
export const M = {
|
|
|
|
NONE: -1,
|
|
|
|
LEFT: 0,
|
|
|
|
RIGHT: 1,
|
|
|
|
MIDDLE: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
const mouseButtonsDown = {
|
|
|
|
[M.LEFT]: false,
|
|
|
|
[M.RIGHT]: false,
|
|
|
|
[M.MIDDLE]: false,
|
|
|
|
};
|
2023-05-14 14:25:02 -07:00
|
|
|
|
|
|
|
type MouseEventType = "click" | "down" | "up" | "move" | "dblclick";
|
|
|
|
|
2023-05-05 16:02:23 -07:00
|
|
|
const mouseEvents: Array<{
|
2023-05-14 14:25:02 -07:00
|
|
|
type: MouseEventType,
|
2023-05-05 16:02:23 -07:00
|
|
|
button: typeof M[keyof typeof M],
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
prevX?: number,
|
|
|
|
prevY?: number
|
|
|
|
}> = [];
|
|
|
|
|
|
|
|
let mouseX = 0;
|
|
|
|
let mouseY = 0;
|
|
|
|
|
|
|
|
const eventPixelCoords = (evt: WindowMouseEvent) => {
|
|
|
|
const {width, height} = gameWindow.size;
|
2023-05-14 14:25:02 -07:00
|
|
|
const min = Math.min(width, 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;
|
|
|
|
}
|
2023-05-05 16:02:23 -07:00
|
|
|
return {
|
|
|
|
x: pixX,
|
|
|
|
y: pixY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 14:25:02 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-05-08 23:14:01 -07:00
|
|
|
addEventListener("dblclick", (evt) => {
|
2023-05-14 14:25:02 -07:00
|
|
|
pushEvent("dblclick", evt);
|
2023-05-08 23:14:01 -07:00
|
|
|
});
|
2023-05-05 16:02:23 -07:00
|
|
|
|
|
|
|
addEventListener("click", (evt) => {
|
2023-05-14 14:25:02 -07:00
|
|
|
pushEvent("click", evt);
|
2023-05-05 16:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
addEventListener("mousedown", (evt) => {
|
2023-05-14 14:25:02 -07:00
|
|
|
if (evtInBounds(evt)) {
|
|
|
|
mouseButtonsDown[evt.button] = true;
|
|
|
|
}
|
|
|
|
pushEvent("down", evt);
|
2023-05-05 16:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
addEventListener("mouseup", (evt) => {
|
2023-05-14 14:25:02 -07:00
|
|
|
if (evtInBounds(evt)) {
|
|
|
|
mouseButtonsDown[evt.button] = false;
|
|
|
|
}
|
|
|
|
pushEvent("up", evt);
|
2023-05-05 16:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
addEventListener("mousemove", (evt) => {
|
|
|
|
const coords = eventPixelCoords(evt);
|
2023-05-14 14:25:02 -07:00
|
|
|
pushEvent("up", evt, {prevX: mouseX, prevY: mouseY});
|
|
|
|
if (coords) {
|
|
|
|
mouseX = coords.x;
|
|
|
|
mouseY = coords.y;
|
|
|
|
}
|
2023-05-05 16:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
export const mousePos = () => {
|
|
|
|
return {
|
|
|
|
x: mouseX,
|
|
|
|
y: mouseY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getMouseX = () => {
|
|
|
|
return mouseX;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getMouseY = () => {
|
|
|
|
return mouseY;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const refreshMouse = () => {
|
|
|
|
mouseEvents.length = 0;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:20:58 -07:00
|
|
|
export const mouseDown = (button: number = M.LEFT) => {
|
|
|
|
return mouseEvents.some(ev => ev.button === button && ev.type === "down");
|
|
|
|
}
|
|
|
|
|
2023-05-05 16:02:23 -07:00
|
|
|
export const mouseClick = (button: number = M.LEFT) => {
|
|
|
|
return mouseEvents.some(ev => ev.button === button && ev.type === "click");
|
|
|
|
}
|
|
|
|
|
2023-05-08 23:14:01 -07:00
|
|
|
export const mouseDoubleClick = (button: number = M.LEFT) => {
|
|
|
|
return mouseEvents.some(ev => ev.button === button && ev.type === "dblclick");
|
|
|
|
}
|
|
|
|
|
2023-05-05 16:02:23 -07:00
|
|
|
export const mouseHeld = (button: number = M.LEFT) => {
|
|
|
|
return mouseButtonsDown[button];
|
|
|
|
}
|