/* Редактор мебели: расстановка вручную на плане комнаты. * Данные: 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 = '
Ошибка редактора: ' + (err && err.message ? err.message : err) + '
'; } } _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 ` ${t.n} `; }).join(""); const palette = Object.entries(F_TYPES).map(([k, v]) => ``).join(""); this.shadowRoot.innerHTML = `
${items}
${Object.entries(F_ROOMS).map(([k, v]) => ``).join("")}
${palette}
Тяните предмет мышью/пальцем. Уголок справа-снизу — размер. «Сохранить» — 3D соберёт по этой расстановке.
`; 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);