256 lines
12 KiB
JavaScript
256 lines
12 KiB
JavaScript
/* Редактор мебели: расстановка вручную на плане комнаты.
|
|
* Данные: localStorage["house3d_furniture_v1"] = { room: [{type,x,y,w,h,rot}] } (метры, y от юга)
|
|
* 3D-карта читает те же данные и строит мебель.
|
|
*/
|
|
const F_ROOMS = {
|
|
sanuzel: "Санузел", tualet: "СУ 2", kabinet: "Спальня", spalnia: "Данина комната",
|
|
danina_komnata: "Кабинет", kitchen: "Кухня-гостиная", koridor: "Холл",
|
|
prihozhaya: "Прихожая", tambur: "Тамбур", garderob: "Гардеробная",
|
|
kladovaia: "Кладовая", postirochnaia: "Тех.пом.", terrasa: "Терраса", kryltso: "Крыльцо",
|
|
};
|
|
const F_POLY = {
|
|
sanuzel: [[5.15,8.6],[5.15,7.25],[3.38,7.25],[3.38,5.68],[4.67,5.68],[6.55,5.68],[6.55,8.6]],
|
|
tualet: [[6.55,6.55],[7.75,6.55],[7.75,5.68],[6.55,5.68]],
|
|
kabinet: [[8.68,8.6],[8.68,5.85],[9.47,5.85],[9.47,5.68],[12.1,5.68],[12.1,8.6],[8.68,8.6]],
|
|
spalnia: [[12.1,8.6],[12.1,4.45],[8.68,4.45],[8.68,5.68],[12.1,5.68]],
|
|
danina_komnata: [[5.67,8.6],[8.68,8.6],[8.68,4.45],[5.67,4.45]],
|
|
kitchen: [[0.19,8.6],[3.38,8.6],[3.38,5.85],[3.72,5.85],[3.72,4.45],[0.19,4.45]],
|
|
koridor: [[3.72,5.85],[3.72,4.45],[9.45,4.45],[9.4,5.85]],
|
|
prihozhaya: [[3.72,4.45],[5.5,4.45],[5.5,2.38],[3.72,2.38]],
|
|
tambur: [[3.72,2.38],[5.5,2.38],[5.5,0.12],[3.72,0.12]],
|
|
garderob: [[6.55,8.6],[6.55,7.25],[9.0,7.25],[9.0,8.6]],
|
|
kladovaia: [[7.75,7.25],[9.0,7.25],[9.0,5.68],[7.75,5.68]],
|
|
postirochnaia: [[3.41,10.05],[5.1,10.05],[5.1,6.24],[3.41,6.24]],
|
|
terrasa: [[-2.45,8.6],[-0.02,8.6],[-0.02,0.12],[-2.45,0.12]],
|
|
kryltso: [[3.72,0.12],[5.67,0.12],[5.67,-1.54],[3.72,-1.54]],
|
|
};
|
|
const F_TYPES = {
|
|
bath: { n: "Ванна", w: 1.7, h: 0.75, z: 0.55, c: 0xf2f2ee },
|
|
wc: { n: "Унитаз", w: 0.4, h: 0.45, z: 0.42, c: 0xf2f2ee },
|
|
sink: { n: "Раковина", w: 0.6, h: 0.45, z: 0.85, c: 0x8b5e3c },
|
|
shower: { n: "Душ", w: 0.3, h: 0.3, z: 2.0, c: 0x16181a },
|
|
shelf: { n: "Полки", w: 0.25, h: 0.9, z: 1.6, c: 0x8b5e3c },
|
|
wardrobe: { n: "Шкаф", w: 1.0, h: 0.6, z: 2.2, c: 0xe6e1d6 },
|
|
bed: { n: "Кровать", w: 1.6, h: 2.0, z: 0.5, c: 0x8e9a9e },
|
|
desk: { n: "Стол", w: 1.6, h: 0.6, z: 0.75, c: 0xa9825a },
|
|
chair: { n: "Стул", w: 0.45, h: 0.45, z: 0.45, c: 0x1e2024 },
|
|
sofa: { n: "Диван", w: 2.0, h: 0.9, z: 0.75, c: 0x4a3f45 },
|
|
tv: { n: "ТВ", w: 1.2, h: 0.1, z: 1.1, c: 0x16181a },
|
|
washer: { n: "Стиралка", w: 0.6, h: 0.6, z: 0.85, c: 0xf2f2ee },
|
|
fridge: { n: "Холодильник", w: 0.65, h: 0.65, z: 1.8, c: 0x9aa3a8 },
|
|
stove: { n: "Плита", w: 0.6, h: 0.6, z: 0.85, c: 0x16181a },
|
|
rug: { n: "Коврик", w: 1.2, h: 0.8, z: 0.02, c: 0xc8b49a },
|
|
door: { n: "Дверь", w: 0.9, h: 0.06, z: 2.05, c: 0xf2f2ee },
|
|
window: { n: "Окно", w: 1.2, h: 0.06, z: 1.1, c: 0xc9d4da },
|
|
};
|
|
const F_KEY = "house3d_furniture_v1";
|
|
|
|
class FurnitureCard extends HTMLElement {
|
|
constructor() {
|
|
super();
|
|
this._room = "sanuzel";
|
|
this._data = {};
|
|
try { this._data = JSON.parse(localStorage.getItem(F_KEY) || "{}") || {}; } catch (e) { this._data = {}; }
|
|
this._sel = null;
|
|
this.attachShadow({ mode: "open" });
|
|
}
|
|
|
|
setConfig(c) { this._config = c || {}; if (c && c.room) this._room = c.room; }
|
|
set hass(h) { this._hass = h; if (!this._rendered) { this._rendered = true; this._render(); } }
|
|
|
|
_items() { return (this._data[this._room] = this._data[this._room] || []); }
|
|
_save() { localStorage.setItem(F_KEY, JSON.stringify(this._data)); }
|
|
|
|
_bounds() {
|
|
const p = F_POLY[this._room] || [];
|
|
return {
|
|
x0: Math.min(...p.map((q) => q[0])), x1: Math.max(...p.map((q) => q[0])),
|
|
y0: Math.min(...p.map((q) => q[1])), y1: Math.max(...p.map((q) => q[1])),
|
|
};
|
|
}
|
|
|
|
_render() {
|
|
try { this._renderSafe(); } catch (err) {
|
|
this.shadowRoot.innerHTML = '<div style=\'padding:16px;color:#ffb4b4;font:14px system-ui\'>Ошибка редактора: ' + (err && err.message ? err.message : err) + '</div>';
|
|
}
|
|
}
|
|
|
|
_renderSafe() {
|
|
const b = this._bounds();
|
|
const W = 720, H = 520;
|
|
const pad = 30;
|
|
const sx = (W - pad * 2) / Math.max(0.6, b.x1 - b.x0);
|
|
const sy = (H - pad * 2) / Math.max(0.6, b.y1 - b.y0);
|
|
const s = Math.min(sx, sy);
|
|
const P = (x, y) => [pad + (x - b.x0) * s, pad + (b.y1 - y) * s];
|
|
this._P = P;
|
|
this._s = s;
|
|
|
|
const poly = (F_POLY[this._room] || []).map(([x, y]) => P(x, y).join(",")).join(" ");
|
|
const items = this._items().map((it, i) => {
|
|
const [x0, y0] = P(it.x, it.y + it.h);
|
|
const [x1, y1] = P(it.x + it.w, it.y);
|
|
const t = F_TYPES[it.type] || { n: it.type, c: 0x888888 };
|
|
const col = "#" + (t.c >>> 0).toString(16).padStart(6, "0");
|
|
const sel = this._sel === i ? " sel" : "";
|
|
return `<g class="item${sel}" data-i="${i}">
|
|
<rect x="${x0}" y="${y0}" width="${Math.max(4, x1 - x0)}" height="${Math.max(4, y1 - y0)}"
|
|
fill="${col}" fill-opacity="0.85" stroke="#cfe4ff" stroke-width="1.5"/>
|
|
<text x="${x0 + 4}" y="${y0 + 14}">${t.n}</text>
|
|
<rect class="h" x="${x1 - 7}" y="${y1 - 7}" width="14" height="14" fill="#3d8bff" stroke="#fff"/>
|
|
</g>`;
|
|
}).join("");
|
|
|
|
const palette = Object.entries(F_TYPES).map(([k, v]) =>
|
|
`<button class="p" data-add="${k}">${v.n}</button>`).join("");
|
|
|
|
this.shadowRoot.innerHTML = `<style>
|
|
:host{display:block;height:100%}
|
|
.app{height:100%;display:grid;grid-template-columns:1fr 250px;gap:12px;padding:12px;
|
|
background:#0b1016;color:#e8eef5;font-family:'Segoe UI',system-ui,sans-serif;box-sizing:border-box}
|
|
.stage{background:#0f151c;border:1px solid #1e2a36;border-radius:16px;position:relative;overflow:hidden}
|
|
svg{width:100%;height:100%;display:block;touch-action:none}
|
|
.room{fill:#1a2431;stroke:#3a5670;stroke-width:3}
|
|
.item{cursor:move}
|
|
.item.sel rect{stroke:#ffb84d;stroke-width:2.5}
|
|
.item text{font-size:12px;fill:#dce8f5;pointer-events:none}
|
|
.h{cursor:nwse-resize}
|
|
.side{display:flex;flex-direction:column;gap:8px;min-height:0}
|
|
.tabs{display:flex;flex-wrap:wrap;gap:6px}
|
|
.tab{padding:8px 10px;border-radius:10px;border:1px solid #22303d;background:#141c25;color:#9fb6c9;font-size:13px;cursor:pointer}
|
|
.tab.sel{border-color:#3d8bff;color:#dceaff;background:#12233a}
|
|
.pal{flex:1;overflow:auto;display:grid;grid-template-columns:1fr 1fr;gap:6px;align-content:start}
|
|
.p{min-height:46px;border-radius:10px;border:1px solid #2a3b4c;background:#0e1620;color:#cfe4ff;font-size:13px;cursor:pointer}
|
|
.row{display:flex;gap:6px}
|
|
.row button{flex:1;min-height:48px;border-radius:12px;border:1px solid #2a3b4c;background:#122033;color:#cfe4ff;font-size:14px;cursor:pointer}
|
|
.row button.warn{border-color:#7a3b3b;color:#ffc9c9}
|
|
.hint{font-size:12px;color:#7f93a8;line-height:1.35}
|
|
</style>
|
|
<div class="app">
|
|
<div class="stage">
|
|
<svg viewBox="0 0 ${W} ${H}">
|
|
<polygon class="room" points="${poly}"/>
|
|
${items}
|
|
</svg>
|
|
</div>
|
|
<div class="side">
|
|
<div class="tabs">${Object.entries(F_ROOMS).map(([k, v]) =>
|
|
`<button class="tab${k === this._room ? " sel" : ""}" data-room="${k}">${v}</button>`).join("")}</div>
|
|
<div class="pal">${palette}</div>
|
|
<div class="row">
|
|
<button data-act="rot">Повернуть</button>
|
|
<button data-act="del" class="warn">Удалить</button>
|
|
</div>
|
|
<div class="row">
|
|
<button data-act="save">Сохранить</button>
|
|
<button data-act="json">Скачать JSON</button>
|
|
</div>
|
|
<div class="hint">Тяните предмет мышью/пальцем. Уголок справа-снизу — размер. «Сохранить» — 3D соберёт по этой расстановке.</div>
|
|
</div>
|
|
</div>`;
|
|
|
|
this._bind(b);
|
|
}
|
|
|
|
_bind(b) {
|
|
const svg = this.shadowRoot.querySelector("svg");
|
|
const toM = (ev) => {
|
|
const r = svg.getBoundingClientRect();
|
|
const vx = (ev.clientX - r.left) / r.width * 720;
|
|
const vy = (ev.clientY - r.top) / r.height * 520;
|
|
const x = b.x0 + (vx - 30) / this._s;
|
|
const y = b.y1 - (vy - 30) / this._s;
|
|
return { x, y };
|
|
};
|
|
|
|
let drag = null;
|
|
svg.addEventListener("pointerdown", (ev) => {
|
|
const g = ev.target.closest(".item");
|
|
if (!g) { this._sel = null; this._render(); return; }
|
|
const i = +g.dataset.i;
|
|
this._sel = i;
|
|
const it = this._items()[i];
|
|
const m = toM(ev);
|
|
drag = { i, mode: ev.target.classList.contains("h") ? "size" : "move", x: m.x, y: m.y, it: { ...it } };
|
|
svg.setPointerCapture(ev.pointerId);
|
|
this._render();
|
|
});
|
|
|
|
svg.addEventListener("pointermove", (ev) => {
|
|
if (!drag) return;
|
|
const m = toM(ev);
|
|
const dx = m.x - drag.x, dy = m.y - drag.y;
|
|
const it = this._items()[drag.i];
|
|
if (drag.mode === "move") {
|
|
it.x = +(drag.it.x + dx).toFixed(2);
|
|
it.y = +(drag.it.y + dy).toFixed(2);
|
|
} else {
|
|
it.w = +Math.max(0.1, drag.it.w + dx).toFixed(2);
|
|
it.h = +Math.max(0.1, drag.it.h + dy).toFixed(2);
|
|
}
|
|
this._redrawItems();
|
|
});
|
|
|
|
const stop = () => { if (drag) { drag = null; this._save(); } };
|
|
svg.addEventListener("pointerup", stop);
|
|
svg.addEventListener("pointercancel", stop);
|
|
|
|
this.shadowRoot.querySelectorAll("[data-room]").forEach((el) =>
|
|
el.addEventListener("click", () => { this._room = el.dataset.room; this._sel = null; this._render(); }));
|
|
this.shadowRoot.querySelectorAll("[data-add]").forEach((el) =>
|
|
el.addEventListener("click", () => {
|
|
const t = el.dataset.add;
|
|
const d = F_TYPES[t];
|
|
this._items().push({ type: t, x: +(b.x0 + 0.2).toFixed(2), y: +(b.y0 + 0.2).toFixed(2),
|
|
w: d.w, h: d.h, rot: 0 });
|
|
this._sel = this._items().length - 1;
|
|
this._save(); this._render();
|
|
}));
|
|
this.shadowRoot.querySelector("[data-act=rot]").addEventListener("click", () => {
|
|
if (this._sel == null) return;
|
|
const it = this._items()[this._sel];
|
|
const w = it.w; it.w = it.h; it.h = w;
|
|
this._save(); this._render();
|
|
});
|
|
this.shadowRoot.querySelector("[data-act=del]").addEventListener("click", () => {
|
|
if (this._sel == null) return;
|
|
this._items().splice(this._sel, 1);
|
|
this._sel = null; this._save(); this._render();
|
|
});
|
|
this.shadowRoot.querySelector("[data-act=save]").addEventListener("click", () => {
|
|
this._save();
|
|
document.dispatchEvent(new CustomEvent("house3d-furniture", { detail: { saved: true } }));
|
|
const el = this.shadowRoot.querySelector(".hint");
|
|
if (el) el.textContent = "Сохранено. Обновите 3D-карту (Ctrl+F5).";
|
|
});
|
|
this.shadowRoot.querySelector("[data-act=json]").addEventListener("click", () => {
|
|
const blob = new Blob([JSON.stringify(this._data, null, 1)], { type: "application/json" });
|
|
const a = document.createElement("a");
|
|
a.href = URL.createObjectURL(blob);
|
|
a.download = "furniture.json";
|
|
a.click();
|
|
});
|
|
}
|
|
|
|
_redrawItems() {
|
|
const b = this._bounds();
|
|
const P = this._P;
|
|
this.shadowRoot.querySelectorAll(".item").forEach((g) => {
|
|
const it = this._items()[+g.dataset.i];
|
|
const [x0, y0] = P(it.x, it.y + it.h);
|
|
const [x1, y1] = P(it.x + it.w, it.y);
|
|
const r = g.querySelector("rect");
|
|
r.setAttribute("x", x0); r.setAttribute("y", y0);
|
|
r.setAttribute("width", Math.max(4, x1 - x0)); r.setAttribute("height", Math.max(4, y1 - y0));
|
|
const h = g.querySelector(".h");
|
|
h.setAttribute("x", x1 - 7); h.setAttribute("y", y1 - 7);
|
|
const t = g.querySelector("text");
|
|
t.setAttribute("x", x0 + 4); t.setAttribute("y", y0 + 14);
|
|
});
|
|
}
|
|
|
|
getCardSize() { return 20; }
|
|
}
|
|
|
|
customElements.define("furniture-card", FurnitureCard);
|