@johnmorrisdotca/wakusei-planets 0.31.0 → 0.32.0

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.
package/dist/element.d.ts CHANGED
@@ -3,6 +3,8 @@ import "./tables/ceres.ts";
3
3
  import "./tables/dwarfs.ts";
4
4
  import { type BodyId, type Look } from "./facts.ts";
5
5
  import { type StarLook, type StarNumbers } from "./star.ts";
6
+ import type { Credit, DataSource } from "./data-door.ts";
7
+ import { type DoorHost } from "./door/host.ts";
6
8
  export { TEXTURE_BASE, TEXTURE_FAR_BASE, TILES_BASE } from "./scene/textures.ts";
7
9
  import type { DetailStats } from "./scene/detail.ts";
8
10
  import { type ThreeParts } from "./scene/body.ts";
@@ -49,6 +51,25 @@ export type WakuseiState = "line" | "loading" | "still" | "live" | "lost" | "una
49
51
  export declare class WakuseiBody extends HTMLElement {
50
52
  #private;
51
53
  static observedAttributes: string[];
54
+ /**
55
+ * The outside sources of map for this body (the data door): each carries its own licence and credit, and a map source for the body takes the place of the folder of tiles. Assign a list to replace them; a source that
56
+ * `validateSource` refuses, or that is not a map source, throws a `TypeError` and changes nothing. A property, not an attribute: data is not text.
57
+ */
58
+ get sources(): readonly DataSource[];
59
+ set sources(list: readonly DataSource[]);
60
+ /** Adds one source (checked at once: a `TypeError` for an empty credit, no licence, a clashing id or a source that is not a map source). Returns the function that removes it. */
61
+ addSource(source: DataSource): () => void;
62
+ /** Removes a source by id: its loads are aborted and its tiles freed, and its credit goes. Returns whether there was one. */
63
+ removeSource(id: string): boolean;
64
+ /** Switches a source off (`false`) or on, for a layers panel. Off aborts its loads, frees its tiles and drops its credit. */
65
+ enableSource(id: string, on: boolean): void;
66
+ /**
67
+ * The sources whose data is in the last drawn frame, the package's own first: the surface picture, the finer map tiles when a window of them is on the body, and each source of yours that gave tiles. `creditLine(credits, "en" | "ja")`
68
+ * from `@johnmorrisdotca/wakusei-planets/door` writes them as a line with the licences. `wakusei-creditschange` fires when this set changes (not at each frame) with `detail: { credits }`.
69
+ */
70
+ credits(): Credit[];
71
+ /** What the sources have done: tile loads started, failed and refused, and the bytes held. */
72
+ get sourceStats(): ReturnType<DoorHost["book"]["stats"]>;
52
73
  /** The moment the dated picture shows, in milliseconds since 1970, or `undefined` for a portrait with no date. */
53
74
  get time(): number | undefined;
54
75
  /** The radius of the body's disc on the screen in CSS pixels, at the picture's own size: what `grab-radius` on the shared view controls wants for a drag that keeps the touched point under the pointer. */
package/dist/element.js CHANGED
@@ -11,7 +11,9 @@ import { starLook } from "./star.js";
11
11
  import { starLineSvg } from "./star-line.js";
12
12
  import { detailOf, lookOf, starLookOf } from "./scene/look.js";
13
13
  import { createLive, defaultTurn, REDUCED_TURN } from "./live.js";
14
- import { texturePaths, tilesBase } from "./scene/textures.js";
14
+ import { mapSourceFor, texturePaths, tilesBase } from "./scene/textures.js";
15
+ import { createDoorHost } from "./door/host.js";
16
+ import { ownCredit } from "./door/own.js";
15
17
  export { TEXTURE_BASE, TEXTURE_FAR_BASE, TILES_BASE } from "./scene/textures.js";
16
18
  import { mapWidthOf, ringWidthOf, zoomLimit } from "./scene/reach.js";
17
19
  import { createBodyItem } from "./scene/body.js";
@@ -92,7 +94,7 @@ const TEMPLATE = `<style>
92
94
  * ```
93
95
  */
94
96
  export class WakuseiBody extends HTMLElement {
95
- static observedAttributes = ["body", "star", "quality", ...STAR_ATTRS, "look", "size", "time", "tilt", "pitch", "phase", "meridian", "turn", "rate", "moons", "range", "scale-to", "span-km", "fit", "live", "texture-base", "texture-far-base", "tiles", "lang", "labels"];
97
+ static observedAttributes = ["body", "star", "quality", ...STAR_ATTRS, "look", "size", "time", "tilt", "pitch", "phase", "meridian", "turn", "rate", "moons", "range", "scale-to", "span-km", "fit", "live", "texture-base", "texture-far-base", "tiles", "data", "lang", "labels"];
96
98
  #stage;
97
99
  #state = "line";
98
100
  #renderer;
@@ -133,6 +135,28 @@ export class WakuseiBody extends HTMLElement {
133
135
  #limitKey = "";
134
136
  #tileLevels;
135
137
  #tiles;
138
+ #door = createDoorHost(this, {
139
+ own: () => {
140
+ // the body's own pictures and, where a window of the package's own tiles is on it, those
141
+ const own = [];
142
+ if (this.look !== "line" && this.#view !== undefined && this.#textureWidth > 0)
143
+ own.push(ownCredit("wakusei-pictures", 1));
144
+ const shown = this.#view?.scene?.detail;
145
+ if (shown?.source !== undefined && this.#door.book.get(shown.source.id) === undefined)
146
+ own.push(ownCredit(shown.source.id, shown.used));
147
+ return own;
148
+ },
149
+ used: () => {
150
+ const shown = this.#view?.scene?.detail;
151
+ return shown?.source === undefined ? new Map() : new Map([[shown.source.id, shown.used]]);
152
+ },
153
+ }, { kinds: ["tiles"] },
154
+ // a source added, removed or switched, or a tile arrived: look at the view again (the window is chosen at a sync), then draw
155
+ () => {
156
+ if (this.#view?.live === true)
157
+ this.#sync();
158
+ this.#view?.invalidate();
159
+ });
136
160
  #hover = () => void this.goLive();
137
161
  #onContext = (state) => {
138
162
  if (state === "unavailable")
@@ -149,6 +173,39 @@ export class WakuseiBody extends HTMLElement {
149
173
  this.#resume();
150
174
  this.#syncClock();
151
175
  };
176
+ /**
177
+ * The outside sources of map for this body (the data door): each carries its own licence and credit, and a map source for the body takes the place of the folder of tiles. Assign a list to replace them; a source that
178
+ * `validateSource` refuses, or that is not a map source, throws a `TypeError` and changes nothing. A property, not an attribute: data is not text.
179
+ */
180
+ get sources() {
181
+ return this.#door.sources();
182
+ }
183
+ set sources(list) {
184
+ this.#door.setSources(list);
185
+ }
186
+ /** Adds one source (checked at once: a `TypeError` for an empty credit, no licence, a clashing id or a source that is not a map source). Returns the function that removes it. */
187
+ addSource(source) {
188
+ return this.#door.book.addSource(source);
189
+ }
190
+ /** Removes a source by id: its loads are aborted and its tiles freed, and its credit goes. Returns whether there was one. */
191
+ removeSource(id) {
192
+ return this.#door.book.removeSource(id);
193
+ }
194
+ /** Switches a source off (`false`) or on, for a layers panel. Off aborts its loads, frees its tiles and drops its credit. */
195
+ enableSource(id, on) {
196
+ this.#door.book.enableSource(id, on);
197
+ }
198
+ /**
199
+ * The sources whose data is in the last drawn frame, the package's own first: the surface picture, the finer map tiles when a window of them is on the body, and each source of yours that gave tiles. `creditLine(credits, "en" | "ja")`
200
+ * from `@johnmorrisdotca/wakusei-planets/door` writes them as a line with the licences. `wakusei-creditschange` fires when this set changes (not at each frame) with `detail: { credits }`.
201
+ */
202
+ credits() {
203
+ return this.#door.credits();
204
+ }
205
+ /** What the sources have done: tile loads started, failed and refused, and the bytes held. */
206
+ get sourceStats() {
207
+ return this.#door.book.stats();
208
+ }
152
209
  /** The moment the dated picture shows, in milliseconds since 1970, or `undefined` for a portrait with no date. */
153
210
  get time() {
154
211
  const r = this.#request();
@@ -197,6 +254,7 @@ export class WakuseiBody extends HTMLElement {
197
254
  root.innerHTML = TEMPLATE;
198
255
  this.#stage = root.getElementById("stage");
199
256
  this.#startedAt = performance.now();
257
+ this.#door.book.ownOnly(this.getAttribute("data") === "own");
200
258
  this.#reduce = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
201
259
  this.#clockFromAttributes();
202
260
  if (!this.hasAttribute("role"))
@@ -254,6 +312,8 @@ export class WakuseiBody extends HTMLElement {
254
312
  this.#clockFromAttributes();
255
313
  this.#syncClock(true);
256
314
  }
315
+ if (name === "data")
316
+ this.#door.book.ownOnly(this.getAttribute("data") === "own");
257
317
  if (name === "live")
258
318
  this.#applyHover();
259
319
  if (name === "live" || name === "look" || name === "body")
@@ -705,7 +765,8 @@ export class WakuseiBody extends HTMLElement {
705
765
  ...texturePaths(this),
706
766
  wantedWidth: (discPx) => discPx * Math.min(2, window.devicePixelRatio || 1) * 1.6,
707
767
  tiles: {
708
- base: () => tilesBase(this),
768
+ source: (body) => mapSourceFor(this.#door.book, tilesBase(this), body),
769
+ book: this.#door.book,
709
770
  ratio: () => window.devicePixelRatio || 1,
710
771
  allowed: () => (navigator.deviceMemory ?? 8) > 2,
711
772
  onLevels: (levels) => {
@@ -713,9 +774,16 @@ export class WakuseiBody extends HTMLElement {
713
774
  this.#refreshLimits();
714
775
  },
715
776
  },
716
- redraw: () => this.#view?.invalidate(),
777
+ redraw: () => {
778
+ // the tile levels were read, or a window is ready: look at the view again (the window is chosen at a sync), then draw
779
+ if (this.#view?.live === true)
780
+ this.#sync();
781
+ this.#view?.invalidate();
782
+ this.#door.announce();
783
+ },
717
784
  report: (note) => {
718
785
  this.#textureWidth = note.width;
786
+ this.#door.announce();
719
787
  if (note.failed === true)
720
788
  this.setAttribute("data-fallback", "shader");
721
789
  else
@@ -787,6 +855,7 @@ export class WakuseiBody extends HTMLElement {
787
855
  this.#sceneState = state;
788
856
  scene.apply(state);
789
857
  this.#textureWidth = scene.textureWidth;
858
+ this.#door.announce();
790
859
  }
791
860
  // --- moving ---------------------------------------------------------------------------------------------------------------------------------------
792
861
  #frame() {
@@ -822,6 +891,7 @@ export class WakuseiBody extends HTMLElement {
822
891
  this.#state = state;
823
892
  this.setAttribute("data-state", state);
824
893
  this.dispatchEvent(new CustomEvent("wakusei-state", { detail: { state } }));
894
+ this.#door.announce();
825
895
  if (state === "unavailable")
826
896
  this.#drawLine();
827
897
  }
@@ -3,6 +3,8 @@ import type * as THREE from "three";
3
3
  import type { Pose } from "../ephemeris.ts";
4
4
  import { type BodyFacts, type BodyId } from "../facts.ts";
5
5
  import { type BodyLook } from "./look.ts";
6
+ import type { SourceBook } from "../door/book.ts";
7
+ import type { BodyMapSource } from "../door/map.ts";
6
8
  import { type DetailStats } from "./detail.ts";
7
9
  import type { TileLevel } from "./tiles.ts";
8
10
  export { fallbackLook } from "./fallback.ts";
@@ -70,7 +72,8 @@ export interface BodyItemOptions {
70
72
  }): void;
71
73
  /** Finer map tiles for a close zoom (see `scene/detail.ts`): where they are (`undefined` for none), the pixel ratio, whether this device may use them, and what to do when the tile set's levels for the body are known. */
72
74
  tiles?: {
73
- base(): string | undefined;
75
+ source(body: string): BodyMapSource | undefined;
76
+ book: SourceBook;
74
77
  ratio(): number;
75
78
  allowed(): boolean;
76
79
  onLevels?(levels: readonly TileLevel[] | undefined): void;
@@ -205,7 +205,8 @@ export function createBodyItem(options) {
205
205
  const detail = windowUniforms === undefined || options.tiles === undefined
206
206
  ? undefined
207
207
  : createDetail(T, {
208
- base: () => options.tiles.base(),
208
+ source: () => options.tiles.source(look.painted.map),
209
+ book: options.tiles.book,
209
210
  body: look.painted.map,
210
211
  ratio: () => options.tiles.ratio(),
211
212
  allowed: () => options.tiles.allowed(),
@@ -1,29 +1,35 @@
1
1
  import type * as THREE from "three";
2
+ import type { SourceInfo } from "../data-door.ts";
3
+ import { type SourceBook } from "../door/book.ts";
4
+ import { forgetManifests as forgetFolders, type BodyMapSource } from "../door/map.ts";
2
5
  import { type Axes, type Seen, type TileLevel, type ViewBox } from "./tiles.ts";
3
6
  type Three = Pick<typeof THREE, "ClampToEdgeWrapping" | "DataTexture" | "LinearFilter" | "NoColorSpace">;
4
7
  /** Milliseconds a window that the view no longer needs is kept (the view may come straight back) before its texture is freed. */
5
8
  export declare const DETAIL_KEEP_MS = 4000;
6
9
  /** Milliseconds a changed view must stay changed before a new window is fetched, so a drag or a pinch does not fetch what it passes. */
7
10
  export declare const DETAIL_SETTLE_MS = 140;
8
- /** Decoded tiles kept (about a megabyte each), those of the window first. */
9
- export declare const DETAIL_TILES_KEPT = 48;
11
+ /** Decoded tiles a view's store keeps by default (about a megabyte and a half each), those of the window first: the view's 48 MB budget (`TILE_BUDGET_BYTES`). */
12
+ export declare const DETAIL_TILES_KEPT = 34;
10
13
  /** What a window is doing, for a page or a test. */
11
14
  export interface DetailStats {
12
- /** A readable tile set was found (a body with levels in it, or not). */
15
+ /** The source's levels for this body were read (a body with levels in it, or not). */
13
16
  ready: boolean;
14
17
  /** The level the view wants now, or `null` when the body's own picture is sharp enough. */
15
18
  level: number | null;
16
19
  /** The level on the body now, or `null`. */
17
20
  shown: number | null;
18
- /** Tiles held decoded, and tiles being fetched. */
21
+ /** Tiles of the window held decoded, and tiles of it being fetched. */
19
22
  held: number;
20
23
  loading: number;
21
- /** Tiles or windows that could not be made and are not tried again for a while. */
24
+ /** Windows that could not be made: they are tried again only when the view changes. */
22
25
  failed: number;
23
26
  /** Tile fetches made. */
24
27
  requested: number;
25
28
  /** The width and height in pixels of the window's picture now, or 0. */
26
29
  pixels: number;
30
+ /** The source whose tiles are on the body now, and how many tiles of the window it gave: for the credit line. `undefined` for none. */
31
+ source: SourceInfo | undefined;
32
+ used: number;
27
33
  }
28
34
  /** What a window gives the body when one is ready or let go: the picture and where it lies, or `null` for none. */
29
35
  export interface DetailWindow {
@@ -44,8 +50,10 @@ export interface DetailWindow {
44
50
  texel: number;
45
51
  }
46
52
  export interface DetailDeps {
47
- /** Where the tile set is, ending with a slash, or `undefined` for none. Read at every draw. */
48
- base: () => string | undefined;
53
+ /** The map source the body is drawn from now, or `undefined` for none (the body's own 2048 pixel picture). Read at every draw, so a source added, removed or switched off is taken up at the next one. */
54
+ source: () => BodyMapSource | undefined;
55
+ /** The view's sources: their tiles are held in its one store, under its one budget. */
56
+ book: SourceBook;
49
57
  /** The body's name in the tile set. */
50
58
  body: string;
51
59
  /** Device pixels to a CSS pixel. */
@@ -54,17 +62,17 @@ export interface DetailDeps {
54
62
  allowed: () => boolean;
55
63
  /** Ask for one draw. */
56
64
  redraw: () => void;
57
- /** Told when the tile set's levels for this body are known (`undefined` when it has none), so the zoom range can follow. */
65
+ /** Told when the source's levels for this body are known (`undefined` when it has none), so the zoom range can follow. */
58
66
  onLevels?: (levels: readonly TileLevel[] | undefined) => void;
59
67
  /** Put the window on the body, or take it off (`null`). */
60
68
  apply: (window: DetailWindow | null) => void;
61
69
  now?: () => number;
62
70
  }
63
71
  export interface Detail {
64
- /** Called at each draw with where the camera is and the body's axes (an orthographic camera looking straight at the body, as `<wakusei-body>` has). The tile set is read at the first call. */
72
+ /** Called at each draw with where the camera is and the body's axes (an orthographic camera looking straight at the body, as `<wakusei-body>` has). The source's levels are read at the first call. */
65
73
  sync(box: ViewBox, axes: Axes, viewWidth: number): void;
66
74
  /**
67
- * Called at each draw of a body seen by any camera (`seenRegion`): what the camera sees of it, or `undefined` for none. The tile set is read only once the 2048 pixel map is not sharp enough for the view.
75
+ * Called at each draw of a body seen by any camera (`seenRegion`): what the camera sees of it, or `undefined` for none. The source's levels are read only once the 2048 pixel map is not sharp enough for the view.
68
76
  */
69
77
  follow(seen: Seen | undefined): void;
70
78
  /** Called at a draw when the body is too small in the view to want tiles at all: nothing is read, and what is held is let go after a few seconds. */
@@ -72,8 +80,17 @@ export interface Detail {
72
80
  readonly stats: DetailStats;
73
81
  dispose(): void;
74
82
  }
75
- /** Forget what was read of the tile sets (for a test that serves different ones). */
76
- export declare function forgetManifests(): void;
83
+ /**
84
+ * Forget what was read of the tile folders (for a test that serves different ones).
85
+ *
86
+ * @example
87
+ * ```ts no-run
88
+ * import { forgetManifests } from "@johnmorrisdotca/wakusei-planets/door";
89
+ *
90
+ * forgetManifests();
91
+ * ```
92
+ */
93
+ export declare const forgetManifests: typeof forgetFolders;
77
94
  /** Make the window of finer map for one body. */
78
95
  export declare function createDetail(T: Three, deps: DetailDeps): Detail;
79
96
  export {};