tarinai/js/ui_ground.js

118 lines
4.9 KiB
JavaScript
Raw Normal View History

2026-06-25 14:51:58 +09:00
"use strict";
(function (global) {
2026-06-25 17:43:24 +09:00
let boundGroundWorld = null;
function activeWorld(fallback = null) {
if (fallback?.cycleGroundType) return fallback;
if (boundGroundWorld?.cycleGroundType) return boundGroundWorld;
if (global.world?.cycleGroundType) return global.world;
try {
if (typeof world !== "undefined" && world?.cycleGroundType) return world;
} catch (_) { /* global lexical world may not exist yet */ }
return null;
}
function groundDef(worldRef = null) {
const worldObj = activeWorld(worldRef);
const id = worldObj?.groundType || "soil";
return global.TarinaiGround?.definition?.(id) || (typeof GROUND_TYPES !== "undefined" ? GROUND_TYPES[id] || GROUND_TYPES.soil : { id: "soil", label: "土", description: "いつもの地面" });
2026-06-25 14:51:58 +09:00
}
2026-06-25 17:43:24 +09:00
function groundTip(worldRef = null) {
2026-06-25 14:51:58 +09:00
const def = groundDef(worldRef);
if (global.TarinaiGround?.tooltipText) return global.TarinaiGround.tooltipText(def.id);
2026-06-25 17:43:24 +09:00
return `${def.label || "地面"}\n${def.description || "クリックで切り替え"}`;
2026-06-25 14:51:58 +09:00
}
2026-06-25 17:43:24 +09:00
function updateGroundUI(worldRef = null) {
2026-06-25 14:51:58 +09:00
const uiRef = global.ui || {};
const def = groundDef(worldRef);
const label = def.label || "土";
2026-06-25 17:43:24 +09:00
const statEl = uiRef.statGroundType || document.getElementById("statGroundType");
const btnEl = uiRef.groundTypeBtn || document.getElementById("groundTypeBtn");
if (statEl) setTextIfChanged(statEl, "groundType", label);
if (btnEl) {
2026-06-25 14:51:58 +09:00
const desc = def.description || "クリックで切り替え";
2026-06-25 17:43:24 +09:00
btnEl.dataset.tip = desc;
btnEl.removeAttribute("title");
btnEl.setAttribute("aria-label", desc ? `地面を切り替える。現在は${label}${desc}` : `地面を切り替える。現在は${label}`);
2026-06-25 14:51:58 +09:00
}
}
2026-06-25 17:43:24 +09:00
function cycleGroundFromEvent(event = null, fallbackWorld = null) {
event?.preventDefault?.();
event?.stopPropagation?.();
event?.stopImmediatePropagation?.();
const now = (global.performance?.now?.() || Date.now());
if (now - (cycleGroundFromEvent.lastAt || 0) < 120) return;
cycleGroundFromEvent.lastAt = now;
const currentWorld = activeWorld(fallbackWorld);
if (!currentWorld?.cycleGroundType) {
if (typeof showToast === "function") showToast("地面をまだ切り替えられません。");
return;
}
global.audio?.uiClick?.();
currentWorld.cycleGroundType();
global.TarinaiRender?.invalidateRenderCaches?.("ground-cycle");
if (global.uiCache) {
global.uiCache.lastStatsGroundType = "";
if (global.uiCache.stats) global.uiCache.stats.groundType = "";
global.uiCache.lastChartDraw = "";
}
updateGroundUI(currentWorld);
global.renderStats?.();
global.render?.();
}
function isGroundToggleTarget(target) {
return Boolean(target?.closest?.("[data-ground-toggle], #groundTypeBtn, .ground-stat"));
}
function bindGroundUI(worldRef = null) {
const worldObj = activeWorld(worldRef);
if (worldObj) boundGroundWorld = worldObj;
const btn = global.ui?.groundTypeBtn || document.getElementById("groundTypeBtn");
const container = btn?.closest?.(".ground-stat") || document.querySelector(".ground-stat");
2026-06-25 14:51:58 +09:00
if (!btn) return;
2026-06-25 17:43:24 +09:00
btn.dataset.groundToggle = "1";
if (container) container.dataset.groundToggle = "1";
2026-06-25 14:51:58 +09:00
if (btn.dataset.groundBound !== "1") {
btn.dataset.groundBound = "1";
2026-06-25 17:43:24 +09:00
btn.addEventListener("pointerup", (event) => cycleGroundFromEvent(event, worldObj), { passive: false });
btn.addEventListener("click", (event) => cycleGroundFromEvent(event, worldObj), { passive: false });
}
if (container && container.dataset.groundBound !== "1") {
container.dataset.groundBound = "1";
container.addEventListener("pointerup", (event) => {
if (!isGroundToggleTarget(event.target)) return;
cycleGroundFromEvent(event, worldObj);
}, { passive: false });
container.addEventListener("click", (event) => {
if (!isGroundToggleTarget(event.target)) return;
cycleGroundFromEvent(event, worldObj);
}, { passive: false });
container.addEventListener("keydown", (event) => {
if (event.key !== "Enter" && event.key !== " ") return;
cycleGroundFromEvent(event, worldObj);
2026-06-25 14:51:58 +09:00
});
}
2026-06-25 17:43:24 +09:00
if (document.body && document.body.dataset.groundCaptureBound !== "1") {
document.body.dataset.groundCaptureBound = "1";
document.addEventListener("click", (event) => {
if (!isGroundToggleTarget(event.target)) return;
cycleGroundFromEvent(event, activeWorld(worldObj));
}, { capture: true, passive: false });
}
global.TarinaiTooltips?.bind?.(btn, () => groundTip(activeWorld(worldObj)));
updateGroundUI(activeWorld(worldObj));
2026-06-25 14:51:58 +09:00
}
global.TarinaiGroundUI = Object.freeze({
bind: bindGroundUI,
update: updateGroundUI,
tip: groundTip,
2026-06-25 17:43:24 +09:00
cycle: cycleGroundFromEvent,
2026-06-25 14:51:58 +09:00
});
global.bindGroundUI = bindGroundUI;
})(window);