// Furniture and devices in the plan: each piece with its symbol, the handles of the selected one, and the camera's field of view.
import { nothing, svg, type TemplateResult } from "lit";
import { entityKind, entityName, kindOf } from "../../../devices.ts";
import { pluginDrawsWedge } from "../../../plugins/registry.ts";
import { furnitureName } from "../../../furniture-names.ts";
import { iconPath } from "../../../icons.ts";
import { deviceIcon } from "../fields.ts";
import { ENERGY_DEVICES, isLamp, noteDue, type Floor, type Furniture, type Placement } from "../../../model.ts";
import { furnitureSymbol } from "../../furniture2d.ts";
import { drawOrder } from "../ops/items.ts";
import type { CanvasContext } from "./context.ts";
import { formatLength } from "../../../units.ts";
import { furnitureEntities } from "../../../entities/furniture-links.ts";
import { fixtureGlow } from "../../../entities/kinds.ts";
import type { HomeAssistant } from "../../../types.ts";
type Layer = TemplateResult | typeof nothing;
const RAD = Math.PI / 180;
/** The turn handle: a small circle on a stem, with a turning arrow. */
const turnHandle = (attribute: "data-rotate" | "data-aim", id: string, from: [number, number], to: [number, number]): TemplateResult => {
const [hx, hy] = to;
const arrow = svg``;
const stem = svg`${arrow}`;
return attribute === "data-rotate" ? svg`${stem}` : svg`${stem}`;
};
/** The four corner handles that resize a piece of furniture. */
function resizeHandles(ctx: CanvasContext, item: Furniture): TemplateResult[] {
const a = item.rotation * RAD;
return ([[-1, -1], [1, -1], [1, 1], [-1, 1]] as const).map(([sx, sz]) => {
const [x, y] = ctx.toScreen([item.x + (sx * item.w * Math.cos(a)) / 2 - (sz * item.d * Math.sin(a)) / 2, item.z + (sx * item.w * Math.sin(a)) / 2 + (sz * item.d * Math.cos(a)) / 2]);
return svg``;
});
}
/** Auto-linked entities per floor, worked out again only when the floor or the states change (a drag redraws often). */
const linkCache = new WeakMap }>();
function floorLinks(hass: HomeAssistant, floor: Floor): ReturnType {
const hit = linkCache.get(floor);
if (hit && hit.states === hass.states) return hit.links;
const links = furnitureEntities(hass, [floor]);
linkCache.set(floor, { states: hass.states, links });
return links;
}
/** A lamp lights up in the plan as in 3D: with its light set by hand or found in the room, or with one of its further lights (#111). */
export function lampLit(hass: HomeAssistant | undefined, floor: Floor, item: Furniture): boolean {
if (!hass || !isLamp(item.type)) return false;
const main = floorLinks(hass, floor).get(item.id)?.entity;
return !!fixtureGlow(main ? hass.states[main] : undefined, (item.extra_lights ?? []).map((id) => hass.states[id]));
}
export function furnitureLayer(ctx: CanvasContext, floor: Floor, selectedId: string | null): Layer {
const { host, scale } = ctx;
const one = (item: Furniture) => {
const selected = item.id === selectedId;
const movable = selected && host.admin && !item.locked;
const [cx, cy] = ctx.toScreen([item.x, item.z]);
const a = item.rotation * RAD;
const labelled = Math.min(item.w, item.d) * scale > 44;
const lit = lampLit(host.hass, floor, item);
const energy = (ENERGY_DEVICES as readonly string[]).includes(item.type);
const klass = `nf-furn${selected ? " nf-furn-sel" : ""}${lit ? " nf-furn-lit" : ""}${energy ? " nf-energy-item" : ""}`;
// the handle sits in front of the piece, and dragging it turns the piece
const reach = item.d / 2 + Math.max(0.3, 26 / scale);
const handleAt = ctx.toScreen([item.x - Math.sin(a) * reach, item.z + Math.cos(a) * reach]);
const stemFrom = ctx.toScreen([item.x - Math.sin(a) * (item.d / 2), item.z + Math.cos(a) * (item.d / 2)]);
// the size is written behind the piece, away from the turn handle
const [lx, ly] = ctx.toScreen([item.x + Math.sin(a) * (item.d / 2 + 18 / scale), item.z - Math.cos(a) * (item.d / 2 + 18 / scale)]);
return svg`
${furnitureSymbol(item.type, item.w, item.d, item)}
${labelled ? svg`${furnitureName(host.hass, item.type)}` : nothing}
${movable ? resizeHandles(ctx, item) : nothing}
${selected ? svg`${formatLength(host.hass, item.w)} × ${formatLength(host.hass, item.d)}` : nothing}
${selected && item.locked ? svg`🔒` : nothing}
${movable ? turnHandle("data-rotate", item.id, stemFrom, handleAt) : nothing}`;
};
return svg`${drawOrder(floor).map(one)}`;
}
/** The device pins of the floor. */
export function devicesLayer(ctx: CanvasContext, floor: Floor, selectedId: string | null): Layer {
const { host } = ctx;
const one = (placed: Placement) => {
// a plugin may take on entities NextFloor does not know (cameras)
const kind = kindOf(placed.entity_id) ?? (host.hass ? entityKind(host.hass, placed.entity_id) : null);
if (!kind) return nothing;
const [x, y] = ctx.toScreen([placed.x, placed.z]);
const on = host.hass?.states[placed.entity_id]?.state === "on";
const selected = placed.entity_id === selectedId;
// a device a plugin draws a wedge for can be turned by a handle in front of it
const turn = (placed.rotation ?? 0) * RAD;
const aim = selected && host.admin && !placed.locked && pluginDrawsWedge(placed.entity_id, host.hass?.states[placed.entity_id]);
const handle: [number, number] = [x - Math.sin(turn) * 46, y + Math.cos(turn) * 46];
const icon = deviceIcon(host, placed.entity_id, placed.icon);
return svg`${aim ? turnHandle("data-aim", placed.entity_id, [x, y], handle) : nothing}
${entityName(host.hass, placed.entity_id)}
${icon
? svg``
: svg``}
${selected && placed.locked ? svg`🔒` : nothing}`;
};
return svg`${floor.placements.map(one)}`;
}
/** The notes of a floor: a small sticky note at its spot (orange once it is due), the selected one ringed. */
export function notesLayer(ctx: CanvasContext, floor: Floor, selectedId: string | null): Layer {
return svg`${(floor.notes ?? []).map((note) => {
const [x, y] = ctx.toScreen([note.x, note.z]);
return svg`
${note.text}
✎
`;
})}`;
}