tarinai/js/ui_input_mouse.js
2026-07-18 13:06:02 +09:00

59 lines
2 KiB
JavaScript

"use strict";
(function (global) {
function bindMouseInput(ctx) {
if (!ctx?.canvas) return false;
const { canvas, world, uiCache } = ctx;
const actions = global.TarinaiPointerActionSystem.create(ctx);
canvas.addEventListener("contextmenu", (e) => e.preventDefault());
window.addEventListener("contextmenu", (e) => {
const now = performance?.now?.() ?? Date.now();
if (uiCache.panning || now <= Number(uiCache.suppressContextMenuUntil || 0)) e.preventDefault();
}, true);
canvas.addEventListener("mousedown", (e) => {
if (e.button === 0) {
const p = ctx.screenToWorldEvent(e);
const started = actions?.beginToolAt?.(p, e.clientX, e.clientY, { notifyNoTarget: world.tool === "pinch" });
if (started) {
e.preventDefault();
return;
}
}
if (e.button !== 2) return;
e.preventDefault();
ctx.beginPan(e.clientX, e.clientY, e.button);
});
window.addEventListener("mouseup", (e) => {
if (actions?.endActive?.(e.clientX, e.clientY)) return;
ctx.endPan(e.button);
});
window.addEventListener("mousemove", (e) => {
const p = ctx.screenToWorldEvent(e);
if (actions?.moveActive?.(p, e.clientX, e.clientY)) return;
ctx.movePan(e.clientX, e.clientY);
});
canvas.addEventListener("click", (e) => {
if (actions?.shouldSuppressClick?.()) return;
const p = ctx.screenToWorldEvent(e);
actions?.dispatchTapAt?.(p);
});
canvas.addEventListener("mousemove", (e) => ctx.updatePointerFromEvent(e));
canvas.addEventListener("wheel", resizeFieldFromWheel, { passive: false });
canvas.addEventListener("mouseleave", () => {
world.pointer.inside = false;
world.pointer.motion = 0;
world.hoverGrabTarget = null;
world.hoverDeleteTarget = null;
world.hoverGiveTarget = null;
world.hoverPokeTarget = null;
});
return true;
}
global.TarinaiMouseInput = Object.freeze({ bindMouseInput });
})(window);