hhmm
This commit is contained in:
parent
434f07cc4e
commit
16074232aa
40 changed files with 1509 additions and 732 deletions
115
js/ui_ground.js
115
js/ui_ground.js
|
|
@ -1,57 +1,118 @@
|
|||
"use strict";
|
||||
|
||||
(function (global) {
|
||||
function groundDef(worldRef = global.world) {
|
||||
const id = worldRef?.groundType || "soil";
|
||||
return global.TarinaiGround?.definition?.(id) || (typeof GROUND_TYPES !== "undefined" ? GROUND_TYPES[id] || GROUND_TYPES.soil : { id: "soil", label: "土", description: "いつものじめん" });
|
||||
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 groundTip(worldRef = global.world) {
|
||||
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: "いつもの地面" });
|
||||
}
|
||||
|
||||
function groundTip(worldRef = null) {
|
||||
const def = groundDef(worldRef);
|
||||
if (global.TarinaiGround?.tooltipText) return global.TarinaiGround.tooltipText(def.id);
|
||||
return `${def.label || "じめん"}\n${def.description || "クリックで切り替え"}`;
|
||||
return `${def.label || "地面"}\n${def.description || "クリックで切り替え"}`;
|
||||
}
|
||||
|
||||
function updateGroundUI(worldRef = global.world) {
|
||||
function updateGroundUI(worldRef = null) {
|
||||
const uiRef = global.ui || {};
|
||||
const def = groundDef(worldRef);
|
||||
const label = def.label || "土";
|
||||
if (uiRef.statGroundType) setTextIfChanged(uiRef.statGroundType, "groundType", label);
|
||||
if (uiRef.groundTypeBtn) {
|
||||
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 || "クリックで切り替え";
|
||||
uiRef.groundTypeBtn.dataset.tip = desc;
|
||||
uiRef.groundTypeBtn.removeAttribute("title");
|
||||
uiRef.groundTypeBtn.setAttribute("aria-label", desc ? `地面を切り替える。現在は${label}。${desc}` : `地面を切り替える。現在は${label}`);
|
||||
btnEl.dataset.tip = desc;
|
||||
btnEl.removeAttribute("title");
|
||||
btnEl.setAttribute("aria-label", desc ? `地面を切り替える。現在は${label}。${desc}` : `地面を切り替える。現在は${label}`);
|
||||
}
|
||||
}
|
||||
|
||||
function bindGroundUI(worldRef = global.world) {
|
||||
const btn = global.ui?.groundTypeBtn;
|
||||
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");
|
||||
if (!btn) return;
|
||||
btn.dataset.groundToggle = "1";
|
||||
if (container) container.dataset.groundToggle = "1";
|
||||
if (btn.dataset.groundBound !== "1") {
|
||||
btn.dataset.groundBound = "1";
|
||||
btn.addEventListener("click", (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
const currentWorld = global.world || worldRef;
|
||||
audio.uiClick?.();
|
||||
currentWorld?.cycleGroundType?.();
|
||||
if (global.uiCache) {
|
||||
global.uiCache.lastStatsGroundType = "";
|
||||
if (global.uiCache.stats) global.uiCache.stats.groundType = "";
|
||||
}
|
||||
updateGroundUI(currentWorld);
|
||||
global.renderStats?.();
|
||||
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);
|
||||
});
|
||||
}
|
||||
global.TarinaiTooltips?.bind?.(btn, () => groundTip(worldRef));
|
||||
updateGroundUI(worldRef);
|
||||
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,
|
||||
tip: groundTip,
|
||||
cycle: cycleGroundFromEvent,
|
||||
});
|
||||
global.bindGroundUI = bindGroundUI;
|
||||
})(window);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue