feat: reveal device cards progressively with camera zoom

This commit is contained in:
jhartworks committed 2026-11-12 15:41:35 +01:00
1 parent e62f987563
commit a2a119d3ee
18 files changed
+458 -265

No files matched your search

+14 -3
View File
@@ -42,6 +42,7 @@ import { isHeatPump, readHeatPump, type HeatPumpInfo } from "../live/heat-pump.t
import { coverageColor, isAccessPoint, networkHealth, readAccessPoint } from "../live/wifi.ts";
import type { FurnitureColors } from "../furniture-colors.ts";
import { renderLhCards, liveCardStyles, unstackLhCards, type LiveCard } from "../live/cards.ts";
import { cardDetail, cardVisibleAtZoom } from "../live/card-visibility.ts";
import { readCar, type CarInfo } from "../live/car.ts";
import { mediaNow, multiroom, type MediaNow } from "../live/media.ts";
import { SHOW_PRESENCE } from "../flags.ts";
@@ -2697,17 +2698,27 @@ export class NfView3d extends LitElement {
if (JSON.stringify(cards) !== JSON.stringify(this._liveCards)) this._liveCards = cards;
v.setLiveAnchors(
cards.map((c) => c.at),
cards.length ? (i, x, y, visible) => this.placeLhCard(i, x, y, visible) : null,
cards.length ? (i, x, y, visible, density) => this.placeLhCard(i, x, y, visible, density) : null,
);
}
/** Called by the viewer after every frame: moves a card to its point without a Lit render. */
private placeLhCard(i: number, x: number, y: number, visible: boolean): void {
private placeLhCard(i: number, x: number, y: number, visible: boolean, density: number): void {
const el = this.renderRoot.querySelector<HTMLElement>(`.live-card[data-i="${i}"]`);
if (!el) return;
const card = this._liveCards[i], key = `${card.kind}:${card.at.floorId}:${card.at.x}:${card.at.z}`;
const wasVisible = el.dataset.zoomKey === key && el.dataset.zoomVisible === "1";
const active = this.shadowRoot?.activeElement;
const focused = !!active && el.contains(active);
const zoomVisible = focused || cardVisibleAtZoom(card, density, wasVisible);
el.dataset.zoomKey = key;
el.dataset.zoomVisible = zoomVisible ? "1" : "0";
el.dataset.density = String(Math.round(density));
el.dataset.priority = String(focused ? 110 : cardDetail(card).priority);
el.dataset.focused = focused ? "1" : "0";
el.dataset.x = String(x);
el.dataset.y = String(y);
el.style.visibility = visible ? "visible" : "hidden";
el.style.visibility = visible && zoomVisible ? "visible" : "hidden";
// the last card of the frame: lay them all out so none covers another
if (i === this._liveCards.length - 1) unstackLhCards(this.renderRoot);
}
+41
View File
@@ -0,0 +1,41 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import type { FurniturePowerCard } from "./furniture-power-card.ts";
import type { HouseCard, HydronicCard } from "./cards.ts";
import { cardDetail, cardVisibleAtZoom } from "./card-visibility.ts";
const power=(w: number|null): FurniturePowerCard=>({kind:"power",id:"wallbox",name:"Wallbox",powerEntity:"sensor.power",power:w,at:{floorId:"f",x:0,y:1,z:0}});
const house: HouseCard={kind:"house",at:{floorId:"f",x:0,y:5,z:0},summary:{grid:0,solar:0,battery:null,soc:null,tariff:null,consumption:0},autarky:null};
const hydraulic: HydronicCard={kind:"hydronic",at:{floorId:"f",x:0,y:2,z:0},readOnly:false,item:{id:"h",type:"heating_manifold",x:0,z:0,w:1.5,d:.35,h:1.1,rotation:0,variant:null}};
test("distant views retain the summary while small device cards wait for more zoom",()=>{
assert.equal(cardVisibleAtZoom(house,0,false),true);
for(const card of [power(0),power(7400),hydraulic]) assert.equal(cardVisibleAtZoom(card,15,false),false);
assert.equal(cardVisibleAtZoom(hydraulic,50,false),true);
assert.equal(cardVisibleAtZoom(power(7400),60,false),true);
assert.equal(cardVisibleAtZoom(power(0),60,false),false);
assert.equal(cardVisibleAtZoom(power(0),90,false),true);
});
test("live consumption and export appear before idle devices, without discarding the zero value",()=>{
assert.ok(cardDetail(power(7400)).priority>cardDetail(power(0)).priority);
assert.equal(cardVisibleAtZoom(power(-1500),60,false),true);
assert.equal(cardVisibleAtZoom(power(0),90,false),true);
assert.equal(power(0).power,0);
assert.equal(cardVisibleAtZoom(power(null),40,false),false);
});
test("hysteresis keeps a visible card stable near its boundary and restores it after zooming back in",()=>{
const c=power(0),threshold=cardDetail(c).density;
assert.equal(cardVisibleAtZoom(c,threshold,false),false);
assert.equal(cardVisibleAtZoom(c,threshold,true),true);
assert.equal(cardVisibleAtZoom(c,threshold*.85,true),false);
assert.equal(cardVisibleAtZoom(c,threshold*1.15,false),true);
for(const density of [NaN,Infinity,-1,0]) assert.equal(cardVisibleAtZoom(c,density,true),false);
});
test("charging telemetry follows detail zoom even without a power link",()=>{
const c={...power(null),powerEntity:null,readings:[{id:"soc",entity:"sensor.soc",label:"SoC",icon:"🔋",text:"0 %"}]};
assert.equal(cardVisibleAtZoom(c,30,false),false);
assert.equal(cardVisibleAtZoom(c,70,false),true);
});
+25
View File
@@ -0,0 +1,25 @@
import type { LiveCard } from "./cards.ts";
/** Screen pixels per plan metre at the anchor, independent of building size and camera angle. */
export function cardDetail(card: LiveCard): { density: number; priority: number } {
switch (card.kind) {
case "house": return { density:0,priority:100 };
case "roomclimate": return { density:24,priority:80 };
case "hydronic": return { density:42,priority:45 };
case "power": {
const active=card.power!==null&&Math.abs(card.power)>1;
return active ? {density:48,priority:60} : card.readings?.length ? {density:56,priority:30} : {density:card.power===null?64:72,priority:20};
}
case "car": return {density:card.car.charging?36:60,priority:card.car.charging?60:20};
case "printer": return {density:card.printer.printing?36:60,priority:card.printer.printing?60:20};
case "heatpump": return {density:card.pump.mode!=="idle"?36:60,priority:card.pump.mode!=="idle"?60:20};
case "media": return {density:48,priority:40};
}
}
/** A small dead band prevents cards flickering at a zoom boundary. Summary cards keep their existing visibility. */
export function cardVisibleAtZoom(card: LiveCard, density: number, previouslyVisible: boolean): boolean {
const limit=cardDetail(card).density;
if (!limit) return true;
return Number.isFinite(density) && density >= limit * (previouslyVisible ? .88 : 1.12);
}
+9 -3
View File
@@ -246,7 +246,7 @@ function mediaCard(hass: HomeAssistant | undefined, c: MediaCard, i: number, act
* before it (lower on screen first) moves up above it. Its thread to the anchor stretches.
*/
export function unstackLhCards(root: ParentNode): void {
const cards = [...root.querySelectorAll<HTMLElement>(".live-card")].filter((el) => el.style.visibility !== "hidden");
const cards = [...root.querySelectorAll<HTMLElement>(".live-card")].filter((el) => el.style.visibility !== "hidden" && el.offsetWidth > 0);
const placed: { x0: number; x1: number; y0: number; y1: number }[] = [];
// a card near the edge slides back into the view (on a phone it was cut off); its thread still points home
const width = cards[0]?.parentElement?.clientWidth ?? 0;
@@ -258,7 +258,7 @@ export function unstackLhCards(root: ParentNode): void {
el.style.setProperty("--live-dx", `${Math.round(anchor - x)}px`);
return { el, x, y: Number(el.dataset.y), w, h: el.offsetHeight };
})
.sort((a, b) => b.y - a.y);
.sort((a, b) => Number(b.el.dataset.priority || 0) - Number(a.el.dataset.priority || 0) || b.y - a.y);
for (const it of items) {
let bottom = it.y;
// Multi-reading equipment cards use nearby free columns before stacking out of view.
@@ -278,8 +278,14 @@ export function unstackLhCards(root: ParentNode): void {
if (!hit) break;
bottom = hit.y0 - 8;
}
placed.push({ x0: it.x - it.w / 2, x1: it.x + it.w / 2, y0: bottom - it.h, y1: bottom });
const lift = it.y - bottom;
// Device details wait for a closer view instead of forming long towers or leaving the stage.
const detail = !it.el.classList.contains("live-card-house") && !it.el.classList.contains("live-card-roomclimate") && it.el.dataset.focused !== "1";
const multi = it.el.classList.contains("live-card-hydronic") || it.el.classList.contains("live-card-charging");
if (detail && height > 0 && (bottom-it.h < 8 || bottom > height-8 || (!multi && Math.abs(lift) > Math.max(100,Math.min(180,height*.25))))) {
it.el.style.visibility="hidden"; continue;
}
placed.push({ x0: it.x - it.w / 2, x1: it.x + it.w / 2, y0: bottom - it.h, y1: bottom });
it.el.style.setProperty("--live-dx", `${Math.round(Number(it.el.dataset.x) - it.x)}px`);
it.el.style.transform = `translate(${Math.round(it.x)}px, ${Math.round(bottom)}px) translate(-50%, -100%)`;
it.el.style.setProperty("--live-thread", `${Math.round(18 + lift)}px`);
+7 -4
View File
@@ -267,7 +267,7 @@ export interface LivePictureInput {
export type LiveScreenInput = Omit<LiveScreen, "x" | "z" | "rotation" | "rect"> & { furnitureId: string };
/** Called after every frame for each card anchor: where it is on screen (CSS pixels) and whether it shows. */
export type LiveAnchorCallback = (index: number, x: number, y: number, visible: boolean) => void;
export type LiveAnchorCallback = (index: number, x: number, y: number, visible: boolean, pixelsPerMeter: number) => void;
export interface PersonPin {
id: string;
@@ -1274,11 +1274,14 @@ export class NextFloorViewer {
const cb = this.liveAnchorCb;
if (!cb || !this.liveAnchors.length) return;
const v = new Vector3();
const view = this.camera.matrixWorldInverse.elements, focal = this.size.h * this.camera.projectionMatrix.elements[5] / 2;
this.liveAnchors.forEach((p, i) => {
const l = this.liftOf(p.floorId);
v.set(p.x, l.y + p.y, p.z).project(this.camera);
const visible = l.visible && v.z < 1 && Math.abs(v.x) < 1.15 && Math.abs(v.y) < 1.15;
cb(i, ((v.x + 1) / 2) * this.size.w, ((1 - v.y) / 2) * this.size.h, visible);
v.set(p.x, l.y + p.y, p.z);
const depth = -(view[2]*v.x+view[6]*v.y+view[10]*v.z+view[14]);
v.project(this.camera);
const visible = l.visible && v.z > -1 && v.z < 1 && Math.abs(v.x) < 1.15 && Math.abs(v.y) < 1.15;
cb(i, ((v.x + 1) / 2) * this.size.w, ((1 - v.y) / 2) * this.size.h, visible, depth > 0 ? focal / depth : 0);
});
}