import { LitElement, html, render } from "lit"; import { customElement, query, state } from "lit/decorators.js"; import territory_patterns from "../../resources/territory_patterns.json"; import "./components/Difficulties"; import "./components/Maps"; @customElement("territory-patterns-input") export class territoryPatternsModal extends LitElement { @query("o-modal") private modalEl!: HTMLElement & { open: () => void; close: () => void; }; @query("#territory-patterns-input_") private previewButton!: HTMLElement; @state() private selectedPattern = localStorage.getItem("territoryPattern") || ""; @state() private buttonWidth: number = 100; private resizeObserver: ResizeObserver; constructor() { super(); this.resizeObserver = new ResizeObserver((entries) => { for (const entry of entries) { if (entry.target.classList.contains("preview-container")) { this.buttonWidth = entry.contentRect.width; } } }); } connectedCallback() { super.connectedCallback(); this.updateComplete.then(() => { const containers = this.renderRoot.querySelectorAll(".preview-container"); containers.forEach((container) => this.resizeObserver.observe(container)); this.updatePreview(); }); } disconnectedCallback() { super.disconnectedCallback(); this.resizeObserver.disconnect(); } createRenderRoot() { return this; } render() { return html`
${Object.entries(territory_patterns.patterns).map( ([key, pattern]) => html` `, )}
`; } public open() { this.modalEl?.open(); } public close() { this.modalEl?.close(); } private selectPattern(patternKey: string) { this.selectedPattern = patternKey; localStorage.setItem("territoryPattern", patternKey); this.updatePreview(); this.close(); } private updatePreview() { if (!this.previewButton || !this.selectedPattern) return; const pattern = territory_patterns.patterns[this.selectedPattern]; if (!pattern) return; const fixedHeight = 48; const fixedWidth = 48; const cellCountX = pattern.tileWidth; const cellCountY = pattern.tileHeight; const cellSize = Math.min( fixedHeight / cellCountY, fixedWidth / cellCountX, ); const previewHTML = html`
${pattern.pattern.flat().map( (cell) => html`
`, )}
`; render(previewHTML, this.previewButton); } }