81 lines
2.5 KiB
JavaScript
81 lines
2.5 KiB
JavaScript
"use strict";
|
|
|
|
(function (global) {
|
|
function bindMouseInput(ctx) {
|
|
if (!ctx?.canvas) return false;
|
|
const { canvas, world, uiCache } = ctx;
|
|
|
|
canvas.addEventListener("contextmenu", (e) => e.preventDefault());
|
|
canvas.addEventListener("mousedown", (e) => {
|
|
if (e.button === 0 && world.tool === "water_hose") {
|
|
const p = ctx.screenToWorldEvent(e);
|
|
if (!p.inside) return;
|
|
e.preventDefault();
|
|
ctx.beginHose(p, e.clientX, e.clientY);
|
|
return;
|
|
}
|
|
if (e.button === 0 && world.tool === "pinch") {
|
|
const p = ctx.screenToWorldEvent(e);
|
|
if (!p.inside) return;
|
|
if (!ctx.beginGrab(p, e.clientX, e.clientY, { notifyNoTarget: true })) return;
|
|
e.preventDefault();
|
|
canvas.style.cursor = "grabbing";
|
|
return;
|
|
}
|
|
if (e.button !== 2) return;
|
|
e.preventDefault();
|
|
ctx.beginPan(e.clientX, e.clientY, e.button);
|
|
});
|
|
|
|
window.addEventListener("mouseup", (e) => {
|
|
if (ctx.endHose()) return;
|
|
if (ctx.endGrab(e.clientX, e.clientY)) return;
|
|
ctx.endPan(e.button);
|
|
});
|
|
|
|
window.addEventListener("mousemove", (e) => {
|
|
if (uiCache.hosing) {
|
|
const p = ctx.screenToWorldEvent(e);
|
|
ctx.moveHose(p, e.clientX, e.clientY);
|
|
return;
|
|
}
|
|
if (uiCache.grabbing) {
|
|
const p = ctx.screenToWorldEvent(e);
|
|
const totalMove = Math.hypot(e.clientX - (uiCache.grabStartX || e.clientX), e.clientY - (uiCache.grabStartY || e.clientY));
|
|
if (totalMove > 3) uiCache.grabMoved = true;
|
|
ctx.moveGrab(p, e.clientX, e.clientY);
|
|
return;
|
|
}
|
|
ctx.movePan(e.clientX, e.clientY);
|
|
});
|
|
|
|
canvas.addEventListener("click", (e) => {
|
|
if (uiCache.hoseMoved) {
|
|
uiCache.hoseMoved = false;
|
|
return;
|
|
}
|
|
if (uiCache.grabMoved) {
|
|
uiCache.grabMoved = false;
|
|
return;
|
|
}
|
|
if (uiCache.panMoved) {
|
|
uiCache.panMoved = false;
|
|
return;
|
|
}
|
|
const p = ctx.screenToWorldEvent(e);
|
|
if (!p.inside) return;
|
|
window.TarinaiCommands?.dispatch?.(world, { type: "pointer.primary", x: p.x, y: p.y });
|
|
renderStats();
|
|
});
|
|
|
|
canvas.addEventListener("mousemove", (e) => ctx.updatePointerFromEvent(e));
|
|
canvas.addEventListener("wheel", resizeFieldFromWheel, { passive: false });
|
|
canvas.addEventListener("mouseleave", () => {
|
|
world.pointer.inside = false;
|
|
world.pointer.motion = 0;
|
|
});
|
|
return true;
|
|
}
|
|
|
|
global.TarinaiMouseInput = Object.freeze({ bindMouseInput });
|
|
})(window);
|