tarinai/js/ui_input_mouse.js

59 lines
2 KiB
JavaScript
Raw Normal View History

2026-06-25 14:51:58 +09:00
"use strict";
(function (global) {
function bindMouseInput(ctx) {
if (!ctx?.canvas) return false;
const { canvas, world, uiCache } = ctx;
2026-06-29 16:21:58 +09:00
const actions = global.TarinaiPointerActionSystem.create(ctx);
2026-06-25 14:51:58 +09:00
canvas.addEventListener("contextmenu", (e) => e.preventDefault());
2026-07-18 13:06:02 +09:00
window.addEventListener("contextmenu", (e) => {
const now = performance?.now?.() ?? Date.now();
if (uiCache.panning || now <= Number(uiCache.suppressContextMenuUntil || 0)) e.preventDefault();
}, true);
2026-06-25 14:51:58 +09:00
canvas.addEventListener("mousedown", (e) => {
2026-06-28 23:07:40 +09:00
if (e.button === 0) {
2026-06-26 19:04:32 +09:00
const p = ctx.screenToWorldEvent(e);
2026-06-28 23:07:40 +09:00
const started = actions?.beginToolAt?.(p, e.clientX, e.clientY, { notifyNoTarget: world.tool === "pinch" });
if (started) {
e.preventDefault();
return;
}
2026-06-25 14:51:58 +09:00
}
if (e.button !== 2) return;
e.preventDefault();
ctx.beginPan(e.clientX, e.clientY, e.button);
});
window.addEventListener("mouseup", (e) => {
2026-06-28 23:07:40 +09:00
if (actions?.endActive?.(e.clientX, e.clientY)) return;
2026-06-25 14:51:58 +09:00
ctx.endPan(e.button);
});
window.addEventListener("mousemove", (e) => {
2026-06-28 23:07:40 +09:00
const p = ctx.screenToWorldEvent(e);
if (actions?.moveActive?.(p, e.clientX, e.clientY)) return;
2026-06-25 14:51:58 +09:00
ctx.movePan(e.clientX, e.clientY);
});
canvas.addEventListener("click", (e) => {
2026-06-28 23:07:40 +09:00
if (actions?.shouldSuppressClick?.()) return;
2026-06-25 14:51:58 +09:00
const p = ctx.screenToWorldEvent(e);
2026-06-28 23:07:40 +09:00
actions?.dispatchTapAt?.(p);
2026-06-25 14:51:58 +09:00
});
canvas.addEventListener("mousemove", (e) => ctx.updatePointerFromEvent(e));
canvas.addEventListener("wheel", resizeFieldFromWheel, { passive: false });
canvas.addEventListener("mouseleave", () => {
world.pointer.inside = false;
world.pointer.motion = 0;
2026-06-27 21:50:05 +09:00
world.hoverGrabTarget = null;
world.hoverDeleteTarget = null;
2026-07-11 21:13:39 +09:00
world.hoverGiveTarget = null;
2026-07-15 14:44:29 +09:00
world.hoverPokeTarget = null;
2026-06-25 14:51:58 +09:00
});
return true;
}
global.TarinaiMouseInput = Object.freeze({ bindMouseInput });
})(window);