114 lines
4.6 KiB
JavaScript
114 lines
4.6 KiB
JavaScript
"use strict";
|
|
|
|
(function (global) {
|
|
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);
|
|
}
|
|
|
|
function groundTip(worldRef = null) {
|
|
const def = groundDef(worldRef);
|
|
return global.TarinaiGround.tooltipText(def.id);
|
|
}
|
|
|
|
function updateGroundUI(worldRef = null) {
|
|
const uiRef = global.ui || {};
|
|
const def = groundDef(worldRef);
|
|
const label = def.label || "\u5730\u9762";
|
|
const statEl = uiRef.statGroundType || document.getElementById("statGroundType");
|
|
const btnEl = uiRef.groundTypeBtn || document.getElementById("groundTypeBtn");
|
|
if (statEl) setTextIfChanged(statEl, "groundType", label);
|
|
if (btnEl) {
|
|
const desc = def.description || "\u30af\u30ea\u30c3\u30af\u3067\u5207\u308a\u66ff\u3048";
|
|
btnEl.dataset.tip = desc;
|
|
btnEl.removeAttribute("title");
|
|
btnEl.setAttribute("aria-label", desc ? `\u5730\u9762\u3092\u5207\u308a\u66ff\u3048\u308b\u3002\u73fe\u5728\u306f${label}\u3002${desc}` : `\u5730\u9762\u3092\u5207\u308a\u66ff\u3048\u308b\u3002\u73fe\u5728\u306f${label}`);
|
|
global.TarinaiTooltips.refresh(btnEl);
|
|
}
|
|
}
|
|
|
|
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("\u5730\u9762\u3092\u307e\u3060\u5207\u308a\u66ff\u3048\u3089\u308c\u307e\u305b\u3093\u3002");
|
|
return;
|
|
}
|
|
global.audio?.uiClick?.();
|
|
currentWorld.cycleGroundType();
|
|
global.TarinaiRender.invalidateRenderCaches("ground-cycle");
|
|
if (global.uiCache) {
|
|
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");
|
|
if (!btn) return;
|
|
btn.dataset.groundToggle = "1";
|
|
if (container) container.dataset.groundToggle = "1";
|
|
if (btn.dataset.groundBound !== "1") {
|
|
btn.dataset.groundBound = "1";
|
|
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);
|
|
});
|
|
}
|
|
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));
|
|
}
|
|
|
|
global.TarinaiGroundUI = Object.freeze({
|
|
bind: bindGroundUI,
|
|
update: updateGroundUI,
|
|
});
|
|
})(window);
|