@johnoscott/isometric-engine 0.12.6 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ import { buildSvg } from "../../iso-core/svg-dom.js";
2
+ import { boxSilhouette } from "../../iso-objects/geometry.js";
3
+ import { applyStep } from "../splice.js";
4
+ import { h } from "./icons.js";
5
+ const paletteLooks = (K) => (palette, data, ui, theme) => {
6
+ palette.classList.add("looks");
7
+ let soon = 0;
8
+ return function draw() {
9
+ if (soon) return;
10
+ soon = window.setTimeout(() => {
11
+ soon = 0;
12
+ const b = [...palette.querySelectorAll(".pal-item:not([data-look])")].find((q) => !q.hidden && !q.closest("[hidden]")), d = data();
13
+ if (!b || !d) return;
14
+ const s = lookOf(K, ui.palette[Number(b.dataset.entry)], d, theme);
15
+ b.dataset.look = s ? "object" : "icon";
16
+ if (s) b.querySelector(".icon")?.replaceWith(h("span", { class: `${ui.rootClass} pal-look` }, s));
17
+ draw();
18
+ }, 0);
19
+ };
20
+ };
21
+ function lookOf(K, item, data, theme) {
22
+ const k = K.kinds.find((x) => x.kind === item.kind);
23
+ if (!k) return void 0;
24
+ const name = K.commands.freshName("", item.name ?? k.singular), path = [...k.section, name];
25
+ const bare = Object.entries(data ?? {}).filter(([, v]) => v === null || typeof v !== "object").map(([n, v]) => `${JSON.stringify(n)}: ${JSON.stringify(v)}
26
+ `).join("");
27
+ let d;
28
+ try {
29
+ d = K.trial(K.commands.insertCmd(path.slice(0, -1), name, item.value?.() ?? k.template()).build(bare).steps.reduce((t, st) => applyStep(t, st), bare), theme);
30
+ } catch {
31
+ }
32
+ const id = d?.pinId.get(name), it = id ? d.scene.layout.byId.get(id) : void 0;
33
+ if (!d || d.diags.length || !id || !it) return void 0;
34
+ const s = buildSvg(d.svg), all = [...s.querySelectorAll("[data-id]")];
35
+ if (!all.some((e) => e.getAttribute("data-id") === id)) return void 0;
36
+ for (const e of all) if (e.getAttribute("data-id") !== id) e.remove();
37
+ for (const e of s.querySelectorAll("text, title, desc, .g-labels, .g-floor")) e.remove();
38
+ const pts = boxSilhouette(it.box), xs = pts.map((q) => q[0]), ys = pts.map((q) => q[1]), m = 3;
39
+ s.setAttribute("viewBox", [Math.min(...xs) - m, Math.min(...ys) - m, Math.max(...xs) - Math.min(...xs) + 2 * m, Math.max(...ys) - Math.min(...ys) + 2 * m].map((v) => v.toFixed(1)).join(" "));
40
+ for (const a of ["width", "height", "role", "tabindex", "aria-label"]) s.removeAttribute(a);
41
+ for (const e of s.querySelectorAll("[tabindex]")) {
42
+ e.removeAttribute("tabindex");
43
+ e.removeAttribute("role");
44
+ }
45
+ s.setAttribute("preserveAspectRatio", "xMidYMid meet");
46
+ s.setAttribute("aria-hidden", "true");
47
+ s.setAttribute("focusable", "false");
48
+ s.classList.add("pal-scene");
49
+ return s;
50
+ }
51
+ export {
52
+ paletteLooks
53
+ };
@@ -1,3 +1,4 @@
1
+ import { paletteLooks } from "./palette-look.js";
1
2
  import { toPointer } from "../path.js";
2
3
  import { outline } from "../outline.js";
3
4
  import { commandsFor } from "../commands.js";
@@ -79,6 +80,7 @@ function setupFor(a, v, textPane) {
79
80
  palette: v.palette ? v.palette.map((e) => ({ kind: e.kind, label: e.label, icon: e.icon ?? icon(e.kind), name: e.name, ...e.section === void 0 ? {} : { section: e.section }, ...e.value === void 0 ? {} : { value: () => structuredClone(e.value) } })) : a.kinds.map((k) => ({ kind: k.kind, icon: icon(k.kind), label: k.singular[0].toUpperCase() + k.singular.slice(1) })),
80
81
  ...v.paletteLayout === void 0 ? {} : { paletteLayout: v.paletteLayout },
81
82
  ...v.doubleClick === void 0 ? {} : { doubleClick: v.doubleClick },
83
+ ...v.dragMotion === void 0 ? {} : { dragMotion: v.dragMotion },
82
84
  text: {
83
85
  stage: "The file, drawn: arrow keys and Tab move through what is drawn; + and \u2212 zoom, 0 fits",
84
86
  dragGroup: "A drag on nothing",
@@ -109,6 +111,7 @@ function setupFor(a, v, textPane) {
109
111
  return r.read === void 0 || r.diags.some((d) => d.severity === "error") ? void 0 : v.draw(r.read, r.read.data, theme);
110
112
  }
111
113
  };
114
+ if (v.paletteLook === "object") canvas.looks = paletteLooks(canvas);
112
115
  const kit = {
113
116
  async check(text) {
114
117
  const r = analyse(a, text, { fileName: v.fileName });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnoscott/isometric-engine",
3
- "version": "0.12.6",
3
+ "version": "0.14.1",
4
4
  "description": "A generic isometric diagram engine: render (L0–L2) and editor (L0–L3), with a 2D view and a YAML diagram format.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -0,0 +1,89 @@
1
+ // Making way for a dragged thing: which of the things beside it would step aside, and where to. Pure arithmetic on boxes in the
2
+ // container's own grid; the canvas says when (its dwell) and draws it (ui/drag-way.ts).
3
+ // - Only what the dragged thing's cells overlap, and what those then overlap, moves: at most MAX_CHAIN things, all the same way.
4
+ // - The fewest whole cells; among equals, the way the dragged thing was going; never back towards where it came from.
5
+ // - Clear of everything else and of the dragged thing, inside the container's margin. If there is no such place, undefined.
6
+
7
+ /** a drop that overlaps waits this long on one cell before anything shows (ms) */
8
+ export const DWELL_HINT = 150;
9
+ /** and this long, from the same start, before the things in its way give way (ms) */
10
+ export const DWELL_GIVE = 700;
11
+ /** what gave way goes back this long after the dragged thing no longer needs the place (ms) */
12
+ export const BACK_AFTER = 250;
13
+ /** a thing's glide to its place, or back (ms) */
14
+ export const GLIDE = 180;
15
+ /** the jiggle: how far (screen px), and how often (a second) */
16
+ export const JIGGLE = { px: 1.5, hz: 8 };
17
+ /** the most things that may step aside for one drop */
18
+ export const MAX_CHAIN = 3;
19
+
20
+ export interface Rect { x0: number; y0: number; x1: number; y1: number }
21
+ export interface Beside extends Rect {
22
+ id: string;
23
+ /** whether it may be pinned somewhere else */
24
+ movable: boolean;
25
+ }
26
+ export interface WayOptions {
27
+ /** where the dragged thing would be */
28
+ foot: Rect;
29
+ /** everything else in the container */
30
+ others: Beside[];
31
+ /** the way it was going: its last step, or from where it started */
32
+ going: [number, number];
33
+ /** what a thing that steps aside must stay in (the container, less its margin); none: anywhere */
34
+ bounds?: Rect;
35
+ max?: number;
36
+ }
37
+
38
+ const E = 1e-6;
39
+ export const hits = (a: Rect, b: Rect) => a.x0 < b.x1 - E && a.x1 > b.x0 + E && a.y0 < b.y1 - E && a.y1 > b.y0 + E;
40
+ const at = (b: Rect, [dx, dy]: [number, number]): Rect => ({ x0: b.x0 + dx, y0: b.y0 + dy, x1: b.x1 + dx, y1: b.y1 + dy });
41
+
42
+ /** The steps (whole cells) of what would move, by id; undefined: nothing can make way. */
43
+ export function makeWay({ foot, others, going, bounds, max = MAX_CHAIN }: WayOptions): Map<string, [number, number]> | undefined {
44
+ const first = others.filter(o => hits(o, foot));
45
+ if (!first.length) return new Map();
46
+ if (first.some(o => !o.movable)) return undefined;
47
+ const ways: [number, number][] = [[1, 0], [-1, 0], [0, 1], [0, -1]];
48
+ let best: { steps: Map<string, [number, number]>; cells: number; along: number } | undefined;
49
+ for (const w of ways) {
50
+ const along = w[0] * going[0] + w[1] * going[1];
51
+ // back to where it came from: against its larger component
52
+ if (along < 0 && Math.abs(along) >= Math.max(Math.abs(going[0]), Math.abs(going[1])) - E) continue;
53
+ const steps = push(foot, others, w, bounds, max);
54
+ if (!steps) continue;
55
+ const cells = [...steps.values()].reduce((n, [dx, dy]) => n + Math.abs(dx) + Math.abs(dy), 0);
56
+ if (!best || cells < best.cells || (cells === best.cells && along > best.along + E)) best = { steps, cells, along };
57
+ }
58
+ return best?.steps;
59
+ }
60
+ /** all that is in the way, pushed one way by whole cells until each is clear of what pushes it */
61
+ function push(foot: Rect, others: Beside[], w: [number, number], bounds: Rect | undefined, max: number): Map<string, [number, number]> | undefined {
62
+ const far = new Map<string, number>(); // cells, by id
63
+ const byId = new Map<string, Beside>(others.map(o => [o.id, o]));
64
+ const boxOf = (o: Beside) => at(o, [w[0] * (far.get(o.id) ?? 0), w[1] * (far.get(o.id) ?? 0)]);
65
+ // what pushes: the dragged thing first, then each that moved (again, if it moved again)
66
+ const queue: (string | undefined)[] = [undefined];
67
+ for (let guard = 0; queue.length; guard++) {
68
+ if (guard > 100) return undefined;
69
+ const id = queue.shift(), by = id === undefined ? foot : boxOf(byId.get(id)!);
70
+ for (const o of others) {
71
+ if (o.id === id) continue;
72
+ const b = boxOf(o);
73
+ if (!hits(b, by)) continue;
74
+ if (!o.movable) return undefined;
75
+ const gap = w[0] > 0 ? by.x1 - b.x0 : w[0] < 0 ? b.x1 - by.x0 : w[1] > 0 ? by.y1 - b.y0 : b.y1 - by.y0;
76
+ far.set(o.id, (far.get(o.id) ?? 0) + Math.max(1, Math.ceil(gap - E)));
77
+ if (far.size > max) return undefined;
78
+ if (!queue.includes(o.id)) queue.push(o.id);
79
+ }
80
+ }
81
+ const steps = new Map([...far].map(([id, n]) => [id, [w[0] * n, w[1] * n] as [number, number]]));
82
+ // clear of everything, the dragged thing too, and inside
83
+ for (const id of steps.keys()) {
84
+ const o = byId.get(id)!, b = boxOf(o);
85
+ if (hits(b, foot) || others.some(q => q.id !== id && hits(b, boxOf(q)))) return undefined;
86
+ if (bounds && (b.x0 < bounds.x0 - E || b.y0 < bounds.y0 - E || b.x1 > bounds.x1 + E || b.y1 > bounds.y1 + E)) return undefined;
87
+ }
88
+ return steps;
89
+ }
@@ -260,6 +260,10 @@ body.read-only .palette { display: none; }
260
260
  .pal-grip { flex: none; width: 6px; margin: 0 -3px; cursor: col-resize; z-index: 3; position: relative; }
261
261
  .pal-grip:hover, .pal-grip:focus-visible { background: color-mix(in srgb, var(--accent) 30%, transparent); outline: 0; }
262
262
  body.read-only .pal-dock, body.read-only .pal-grip { display: none; }
263
+ .palette.looks .pal-item { height: auto; min-height: 40px; }
264
+ .palette.looks .pal-look { display: block; flex: 0 0 44px; width: 44px; height: 36px; margin: 0; padding: 0; background: none; pointer-events: none; overflow: hidden; }
265
+ .palette.looks .pal-scene { display: block; width: 100%; height: 100%; }
266
+ .palette.looks .pal-item > .icon { margin: 0 15px; }
263
267
  .pal-ghost { position: absolute; z-index: 5; pointer-events: none; display: flex; align-items: center; gap: var(--sp-1); padding: 2px var(--sp-2); border: var(--bw) solid var(--accent); border-radius: var(--r-sm); background: var(--surface); color: var(--ink); font-size: var(--fs-sm); box-shadow: var(--shadow-2, none); }
264
268
  .pal-ghost .icon { width: 14px; height: 14px; }
265
269
  .pal-ghost.bad { border-color: var(--bad); color: var(--bad); }