@margoengine/ui 0.0.0-stage → 0.1.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.
Files changed (61) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +708 -3
  4. package/dist/basic.d.ts +87 -0
  5. package/dist/collections.d.ts +180 -0
  6. package/dist/color.d.ts +24 -0
  7. package/dist/controls.d.ts +130 -0
  8. package/dist/curved.d.ts +41 -0
  9. package/dist/curved.js +105 -0
  10. package/dist/dom-CWJhpotq.js +340 -0
  11. package/dist/dom-host.d.ts +67 -0
  12. package/dist/dom-host.js +563 -0
  13. package/dist/dom-spec.d.ts +21 -0
  14. package/dist/dom.d.ts +63 -0
  15. package/dist/draw.d.ts +107 -0
  16. package/dist/editor.d.ts +83 -0
  17. package/dist/editor.js +323 -0
  18. package/dist/errors-CDgOgzCd.js +7 -0
  19. package/dist/focus.d.ts +12 -0
  20. package/dist/forms.d.ts +114 -0
  21. package/dist/forms.js +354 -0
  22. package/dist/gl-bb768Mje.js +165 -0
  23. package/dist/gl-load-wwKmojhb.js +5 -0
  24. package/dist/gl-load.d.ts +3 -0
  25. package/dist/gl.d.ts +30 -0
  26. package/dist/i18n.d.ts +69 -0
  27. package/dist/i18n.js +117 -0
  28. package/dist/image.d.ts +74 -0
  29. package/dist/image.js +295 -0
  30. package/dist/index.d.ts +36 -0
  31. package/dist/index.js +4 -0
  32. package/dist/input-dom.d.ts +14 -0
  33. package/dist/input.d.ts +92 -0
  34. package/dist/input.js +315 -0
  35. package/dist/interaction.d.ts +106 -0
  36. package/dist/keyboard.d.ts +40 -0
  37. package/dist/keyboard.js +132 -0
  38. package/dist/layout.d.ts +116 -0
  39. package/dist/markup.d.ts +89 -0
  40. package/dist/markup.js +369 -0
  41. package/dist/meters-yaZRtwsX.js +1401 -0
  42. package/dist/meters.d.ts +72 -0
  43. package/dist/mirror.d.ts +30 -0
  44. package/dist/overlays.d.ts +89 -0
  45. package/dist/painter.d.ts +115 -0
  46. package/dist/pass-CAn29pqc.js +15 -0
  47. package/dist/react.d.ts +99 -0
  48. package/dist/react.js +246 -0
  49. package/dist/renderer-Bdhc_0Cu.js +1653 -0
  50. package/dist/renderer.d.ts +92 -0
  51. package/dist/rsc/react.js +3 -0
  52. package/dist/shader.d.ts +6 -0
  53. package/dist/theme.d.ts +104 -0
  54. package/dist/ui.d.ts +328 -0
  55. package/dist/virtual.d.ts +81 -0
  56. package/dist/widget.d.ts +196 -0
  57. package/dist/world-gl.d.ts +32 -0
  58. package/dist/world-gl.js +195 -0
  59. package/dist/world.d.ts +216 -0
  60. package/dist/world.js +561 -0
  61. package/package.json +119 -6
package/dist/world.js ADDED
@@ -0,0 +1,561 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { n as e, r as t } from "./errors-CDgOgzCd.js";
5
+ import { t as n } from "./pass-CAn29pqc.js";
6
+ import { a as r, c as i, n as a, o, r as s } from "./dom-CWJhpotq.js";
7
+ import { MargoError as c } from "@margoengine/math/errors";
8
+ import { unprojectPoint as l } from "@margoengine/math";
9
+ //#region ../shared/require-webgpu.js
10
+ function u(e, t) {
11
+ if (e.backend === "webgl2" || !e.gpu) throw new c("unsupported", `${t} needs WebGPU; see https://margoengine.com/errors#webgpu`);
12
+ }
13
+ //#endregion
14
+ //#region ../shared/mipmaps.js
15
+ var d = (e) => `
16
+ @vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {
17
+ return vec4f(array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i],0,1);
18
+ }
19
+ @group(0) @binding(0) var image:texture_2d<f32>;
20
+ @fragment fn fragment(@builtin(position) clip:vec4f)->@location(0) vec4f {
21
+ let src=textureDimensions(image);
22
+ let dst=max(src/2u,vec2u(1u));let g=vec2u(clip.xy);
23
+ let a=vec2f(g*src)/vec2f(dst);let b=vec2f((g+1u)*src)/vec2f(dst);let end=min(src,((g+1u)*src+dst-1u)/dst);
24
+ var sum=vec4f(0.0);var sc=vec3f(0.0);
25
+ for(var y=g.y*src.y/dst.y;y<end.y;y++){
26
+ let wy=min(b.y,f32(y+1u))-max(a.y,f32(y));
27
+ for(var x=g.x*src.x/dst.x;x<end.x;x++){
28
+ let w=(min(b.x,f32(x+1u))-max(a.x,f32(x)))*wy;let t=textureLoad(image,vec2u(x,y),0);
29
+ sum+=w*t;sc+=(w*t.a)*t.rgb;
30
+ }
31
+ }
32
+ let area=(b.x-a.x)*(b.y-a.y);
33
+ ${e ? "return vec4f(select(sum.rgb/area,sc/sum.a,sum.a>0.0),sum.a/area);" : "return sum/area;"}
34
+ }
35
+ `;
36
+ function f(e, t, n, r, i = 1, a = "plain") {
37
+ if (n < 2) return;
38
+ let o = t.format, s = a === "weighted", c = s ? o + ":weighted" : o, l = r.get(c);
39
+ if (!l) {
40
+ let t = e.createShaderModule({ code: d(s) });
41
+ r.set(c, l = e.createRenderPipeline({
42
+ layout: "auto",
43
+ vertex: {
44
+ module: t,
45
+ entryPoint: "vertex"
46
+ },
47
+ fragment: {
48
+ module: t,
49
+ entryPoint: "fragment",
50
+ targets: [{ format: o }]
51
+ }
52
+ }));
53
+ }
54
+ let u = e.createCommandEncoder();
55
+ for (let r = 0; r < i; r++) for (let a = 1; a < n; a++) {
56
+ let n = (e) => t.createView(i > 1 ? {
57
+ format: o,
58
+ dimension: "2d",
59
+ baseMipLevel: e,
60
+ mipLevelCount: 1,
61
+ baseArrayLayer: r,
62
+ arrayLayerCount: 1
63
+ } : {
64
+ baseMipLevel: e,
65
+ mipLevelCount: 1
66
+ }), s = e.createBindGroup({
67
+ layout: l.getBindGroupLayout(0),
68
+ entries: [{
69
+ binding: 0,
70
+ resource: n(a - 1)
71
+ }]
72
+ }), c = u.beginRenderPass({ colorAttachments: [{
73
+ view: n(a),
74
+ loadOp: "clear",
75
+ storeOp: "store",
76
+ clearValue: {
77
+ r: 0,
78
+ g: 0,
79
+ b: 0,
80
+ a: 0
81
+ }
82
+ }] });
83
+ c.setPipeline(l), c.setBindGroup(0, s), c.draw(3), c.end();
84
+ }
85
+ e.queue.submit([u.finish()]);
86
+ }
87
+ //#endregion
88
+ //#region ../shared/view-rect.js
89
+ function p(e) {
90
+ return e?.view?.viewport;
91
+ }
92
+ function m(e, t) {
93
+ let n = p(t);
94
+ if (!n) return;
95
+ e.setViewport(n[0], n[1], n[2], n[3], 0, 1);
96
+ let r = t.view.scissor ?? n;
97
+ e.setScissorRect(r[0], r[1], r[2], r[3]);
98
+ }
99
+ //#endregion
100
+ //#region src/world.ts
101
+ var h = class {
102
+ get surface() {
103
+ return [
104
+ 0,
105
+ 0,
106
+ 1,
107
+ 1
108
+ ];
109
+ }
110
+ constructor(n, r = {}) {
111
+ if (this.ui = n, this.matrix = new Float32Array([
112
+ 1,
113
+ 0,
114
+ 0,
115
+ 0,
116
+ 0,
117
+ 1,
118
+ 0,
119
+ 0,
120
+ 0,
121
+ 0,
122
+ 1,
123
+ 0,
124
+ 0,
125
+ 0,
126
+ 0,
127
+ 1
128
+ ]), this.visible = !0, !n || typeof n.draw != "function") throw t("WorldPanel needs a Ui: new WorldPanel(new Ui())", "ui");
129
+ this.cssWidth = n.width * n.cssScale, this.cssHeight = n.height * n.cssScale, this.pixelRatio = r.pixelRatio ?? 1, this.width = r.width ?? 1, this.height = r.height ?? this.width * this.cssHeight / this.cssWidth, this.doubleSided = r.doubleSided ?? !1;
130
+ for (let t of [
131
+ this.pixelRatio,
132
+ this.width,
133
+ this.height
134
+ ]) if (!(t > 0) || !Number.isFinite(t)) throw e("WorldPanel width, height and pixelRatio must be finite and above 0; pass numbers such as { width: 1.2, pixelRatio: 2 }", "ui");
135
+ this.setPose(r.position ?? [
136
+ 0,
137
+ 0,
138
+ 0
139
+ ], r.rotation ?? [
140
+ 0,
141
+ 0,
142
+ 0,
143
+ 1
144
+ ]), n.resize(this.cssWidth, this.cssHeight, this.pixelRatio);
145
+ }
146
+ setPose(t, n) {
147
+ let [r, i, a, o] = n;
148
+ if (![
149
+ ...t,
150
+ r,
151
+ i,
152
+ a,
153
+ o
154
+ ].every(Number.isFinite) || Math.abs(r * r + i * i + a * a + o * o - 1) > .001) throw e("WorldPanel position must be finite and rotation a unit quaternion [x, y, z, w]; normalize the rotation", "ui");
155
+ let s = this.matrix;
156
+ s[0] = 1 - 2 * (i * i + a * a), s[1] = 2 * (r * i + a * o), s[2] = 2 * (r * a - i * o), s[4] = 2 * (r * i - a * o), s[5] = 1 - 2 * (r * r + a * a), s[6] = 2 * (i * a + r * o), s[8] = 2 * (r * a + i * o), s[9] = 2 * (i * a - r * o), s[10] = 1 - 2 * (r * r + i * i), s[12] = t[0], s[13] = t[1], s[14] = t[2], s[3] = s[7] = s[11] = 0, s[15] = 1;
157
+ }
158
+ side(e) {
159
+ let t = this.matrix;
160
+ return Math.sign((e[0] - t[12]) * t[8] + (e[1] - t[13]) * t[9] + (e[2] - t[14]) * t[10]);
161
+ }
162
+ intersect(e) {
163
+ let t = this.matrix, n = e.origin, r = e.direction, i = t[8], a = t[9], o = t[10], s = (n[0] - t[12]) * i + (n[1] - t[13]) * a + (n[2] - t[14]) * o, c = r[0] * i + r[1] * a + r[2] * o;
164
+ if (!(Math.abs(c) > 1e-12) || !Number.isFinite(s)) return null;
165
+ let l = -s / c;
166
+ if (!(l >= 0)) return null;
167
+ let u = s < 0;
168
+ if (u && !this.doubleSided) return null;
169
+ let d = n[0] + r[0] * l, f = n[1] + r[1] * l, p = n[2] + r[2] * l, m = d - t[12], h = f - t[13], g = p - t[14], _ = (m * t[0] + h * t[1] + g * t[2]) / (t[0] * t[0] + t[1] * t[1] + t[2] * t[2]), v = (m * t[4] + h * t[5] + g * t[6]) / (t[4] * t[4] + t[5] * t[5] + t[6] * t[6]), y = _ / this.width + .5, b = .5 - v / this.height;
170
+ return u && (y = 1 - y), {
171
+ distance: l,
172
+ point: [
173
+ d,
174
+ f,
175
+ p
176
+ ],
177
+ x: y * this.cssWidth,
178
+ y: b * this.cssHeight,
179
+ inside: y >= 0 && y <= 1 && b >= 0 && b <= 1,
180
+ back: u
181
+ };
182
+ }
183
+ pointerMove(e, t) {
184
+ let n = this.intersect(e);
185
+ return n ? this.ui.pointerMove(n.x, n.y, t) : (this.ui.pointerLeave(), !1);
186
+ }
187
+ pointerDown(e, t) {
188
+ let n = this.intersect(e);
189
+ return !!n && n.inside && this.ui.pointerDown(n.x, n.y, t);
190
+ }
191
+ pointerUp(e, t) {
192
+ let n = this.intersect(e);
193
+ return n ? this.ui.pointerUp(n.x, n.y, t) : this.ui.pointerUp(-1e9, -1e9, t);
194
+ }
195
+ };
196
+ function g(e, t, n, r, i) {
197
+ let a = l({
198
+ x: t / r * 2 - 1,
199
+ y: 1 - n / i * 2,
200
+ z: .5
201
+ }, e, {
202
+ x: 0,
203
+ y: 0,
204
+ z: 0
205
+ }), o = e.position;
206
+ if (!a) return {
207
+ origin: [
208
+ o[0],
209
+ o[1],
210
+ o[2]
211
+ ],
212
+ direction: [
213
+ 0,
214
+ 0,
215
+ -1
216
+ ]
217
+ };
218
+ let s = a.x - o[0], c = a.y - o[1], u = a.z - o[2], d = Math.hypot(s, c, u) || 1;
219
+ return {
220
+ origin: [
221
+ o[0],
222
+ o[1],
223
+ o[2]
224
+ ],
225
+ direction: [
226
+ s / d,
227
+ c / d,
228
+ u / d
229
+ ]
230
+ };
231
+ }
232
+ function _(e) {
233
+ let t = Math.hypot(e[8], e[9], e[10]) || 1;
234
+ return {
235
+ origin: [
236
+ e[12],
237
+ e[13],
238
+ e[14]
239
+ ],
240
+ direction: [
241
+ -e[8] / t,
242
+ -e[9] / t,
243
+ -e[10] / t
244
+ ]
245
+ };
246
+ }
247
+ function v(e, t) {
248
+ let n = null;
249
+ for (let r of e) {
250
+ if (!r.visible) continue;
251
+ let e = r.intersect(t);
252
+ e?.inside && (!n || e.distance < n.hit.distance) && (n = {
253
+ panel: r,
254
+ hit: e
255
+ });
256
+ }
257
+ return n;
258
+ }
259
+ var y = "rgba8unorm-srgb", b = class c {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
260
+ static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
261
+ let n = new c(t);
262
+ return e.use(n), n;
263
+ }
264
+ constructor(e = {}) {
265
+ if (this.stats = {
266
+ panels: 0,
267
+ repaints: 0,
268
+ drawCalls: 0
269
+ }, this.panels = /* @__PURE__ */ new Set(), this.gpu = /* @__PURE__ */ new Map(), this.pointers = /* @__PURE__ */ new Map(), this.descriptor = n(), this.cameras = [], this.cameraData = /* @__PURE__ */ new Float32Array(20), this.panelData = /* @__PURE__ */ new Float32Array(24), this.mipCache = /* @__PURE__ */ new Map(), this.pipelines = /* @__PURE__ */ new Map(), this.frameDrawn = -1, this.frame = 0, this.execute = (e) => {
270
+ let t = this.stats;
271
+ if (t.panels = t.drawCalls = 0, this.frameDrawn !== this.frame && (t.repaints = 0), e.backend !== "webgpu" || !this.device || e.device !== this.device || !this.panels.size) return;
272
+ let n = e.camera.position, r = [];
273
+ for (let e of this.panels) {
274
+ if (!e.visible) continue;
275
+ let t = this.resources(e);
276
+ this.frameDrawn !== this.frame && this.paint(e, t), (e.doubleSided || e.side(n) > 0) && r.push([e, t]);
277
+ }
278
+ if (this.frameDrawn = this.frame, !r.length) return;
279
+ let i = e.format.endsWith("-srgb") || e.format.includes("float") ? 1 : 0, a = this.panelData;
280
+ for (let [e, t] of r) a.set(e.matrix), a[16] = e.width, a[17] = e.height, a[18] = i, a[19] = +!!e.doubleSided, a.set(e.surface, 20), this.device.queue.writeBuffer(t.uniform, 0, a);
281
+ this.descriptor.colorAttachments[0].view = e.color, this.descriptor.depthStencilAttachment.view = e.depth;
282
+ let o = e.encoder.beginRenderPass(this.descriptor);
283
+ m(o, e), o.setPipeline(this.pipeline(e)), o.setBindGroup(1, this.camera(e.view?.index ?? 0, e));
284
+ for (let [e, n] of r) o.setBindGroup(0, n.group), o.draw(6 * e.surface[2] * e.surface[3]), t.drawCalls++, t.panels++;
285
+ o.end();
286
+ }, this.name = e.name ?? "ui-world", this.order = e.order ?? 25, !this.name || typeof this.name != "string" || !Number.isFinite(this.order)) throw t("WorldPanels needs a non-empty name and a finite order; pass a name string and an order number", "ui");
287
+ this.input = e.input ?? !0, this.signal = e.signal;
288
+ }
289
+ add(...e) {
290
+ for (let n of e) {
291
+ if (!(n instanceof h)) throw t("WorldPanels.add takes WorldPanel objects; wrap the Ui first: new WorldPanel(ui)", "ui");
292
+ this.panels.add(n);
293
+ }
294
+ return this;
295
+ }
296
+ remove(e) {
297
+ this.panels.delete(e), this.release(e);
298
+ for (let t of this.pointers.values()) t.hover === e && (t.hover = null), t.captured === e && (t.captured = null);
299
+ }
300
+ get list() {
301
+ return [...this.panels];
302
+ }
303
+ pointer(e, t, n = {}) {
304
+ let r = n.id ?? 0, i = this.pointers.get(r);
305
+ i || this.pointers.set(r, i = {
306
+ hover: null,
307
+ captured: null,
308
+ pressed: !1
309
+ });
310
+ let a = i.pressed;
311
+ if (i.pressed = t, i.captured) {
312
+ let r = i.captured;
313
+ return t ? r.pointerMove(e, n) || !0 : (i.captured = null, r.pointerUp(e, n));
314
+ }
315
+ let o = v(this.panels, e), s = o?.panel ?? null;
316
+ if (i.hover && i.hover !== s && i.hover.ui.pointerLeave(), i.hover = s, !s) return !1;
317
+ let c = s.ui.pointerMove(o.hit.x, o.hit.y, n);
318
+ return t && !a && s.ui.pointerDown(o.hit.x, o.hit.y, n) ? (i.captured = s, !0) : !t && a ? s.ui.pointerUp(o.hit.x, o.hit.y, n) : c;
319
+ }
320
+ controller(e, t = 1) {
321
+ let n = e.targetRayMatrix;
322
+ if (!n[15]) {
323
+ let e = this.pointers.get(t);
324
+ return e?.captured && e.captured.ui.pointerCancel(), e?.hover?.ui.pointerLeave(), this.pointers.delete(t), !1;
325
+ }
326
+ return this.pointer(_(n), e.buttons[1] > 0, {
327
+ id: t,
328
+ type: "pen"
329
+ });
330
+ }
331
+ cancel() {
332
+ for (let e of this.pointers.values()) e.captured?.ui.pointerCancel();
333
+ for (let e of this.pointers.values()) e.hover?.ui.pointerLeave();
334
+ this.pointers.clear();
335
+ }
336
+ setup(e) {
337
+ u(e, "WorldPanels"), this.engine = e;
338
+ let t = e.canvas;
339
+ this.input && t && !this.detach && (this.detach = this.listen(t));
340
+ let n = this.device = e.gpu, r = GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT;
341
+ this.panelLayout = n.createBindGroupLayout({ entries: [
342
+ {
343
+ binding: 0,
344
+ visibility: r,
345
+ buffer: {
346
+ type: "uniform",
347
+ minBindingSize: 96
348
+ }
349
+ },
350
+ {
351
+ binding: 1,
352
+ visibility: GPUShaderStage.FRAGMENT,
353
+ sampler: { type: "filtering" }
354
+ },
355
+ {
356
+ binding: 2,
357
+ visibility: GPUShaderStage.FRAGMENT,
358
+ texture: { sampleType: "float" }
359
+ }
360
+ ] }), this.cameraLayout = n.createBindGroupLayout({ entries: [{
361
+ binding: 0,
362
+ visibility: GPUShaderStage.VERTEX,
363
+ buffer: {
364
+ type: "uniform",
365
+ minBindingSize: 80
366
+ }
367
+ }] }), this.pipelineLayout = n.createPipelineLayout({ bindGroupLayouts: [this.panelLayout, this.cameraLayout] }), this.module = n.createShaderModule({ code: x }), this.sampler = n.createSampler({
368
+ magFilter: "linear",
369
+ minFilter: "linear",
370
+ mipmapFilter: "linear",
371
+ maxAnisotropy: 4,
372
+ addressModeU: "clamp-to-edge",
373
+ addressModeV: "clamp-to-edge"
374
+ });
375
+ }
376
+ registerPasses(e) {
377
+ e.add({
378
+ name: `${this.name}:draw`,
379
+ order: this.order,
380
+ execute: this.execute
381
+ });
382
+ }
383
+ onPreUpdate(e) {
384
+ this.frame++;
385
+ for (let t of this.panels) t.ui.update(e);
386
+ }
387
+ listen(e) {
388
+ let t = [], n = (t, n) => {
389
+ let r = this.engine.camera;
390
+ return g(r, t, n, e.clientWidth || 1, e.clientHeight || 1);
391
+ }, i = /* @__PURE__ */ new Set();
392
+ return a({
393
+ pointerMove: (e, t, r) => this.pointer(n(e, t), i.has(r.id ?? 0), r),
394
+ pointerDown: (e, t, r) => {
395
+ if (r.button) return !1;
396
+ i.add(r.id ?? 0);
397
+ let a = this.pointer(n(e, t), !0, r);
398
+ return a || i.delete(r.id ?? 0), a;
399
+ },
400
+ pointerUp: (e, t, r) => i.delete(r.id ?? 0) ? this.pointer(n(e, t), !1, r) : !1,
401
+ pointerCancel: () => {
402
+ i.clear(), this.cancel();
403
+ },
404
+ pointerLeave: () => {
405
+ for (let e of this.pointers.values()) e.captured || e.hover?.ui.pointerLeave();
406
+ },
407
+ wheel: (e, t, r) => {
408
+ let i = v(this.panels, n(e, t));
409
+ return !!i && i.panel.ui.wheel(i.hit.x, i.hit.y, r);
410
+ }
411
+ }, e, r(e, t)), s(t, this.signal);
412
+ }
413
+ resources(t) {
414
+ let n = this.gpu.get(t);
415
+ if (n) return n;
416
+ let r = this.device, a = Math.round(t.cssWidth * t.pixelRatio), o = Math.round(t.cssHeight * t.pixelRatio), s = r.limits.maxTextureDimension2D;
417
+ if (a > s || o > s || a < 1 || o < 1) throw e(`A WorldPanel texture of ${a} by ${o} pixels does not fit this GPU, whose limit is ${s} a side; lower the UI size or pixelRatio`, "ui");
418
+ let c = Math.floor(Math.log2(Math.max(a, o))) + 1, l = r.createTexture({
419
+ size: [a, o],
420
+ format: y,
421
+ mipLevelCount: c,
422
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT
423
+ }), u = r.createBuffer({
424
+ size: 96,
425
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
426
+ }), d = new i({
427
+ atlasSize: 1024,
428
+ colorAtlasSize: 512,
429
+ maxPages: 16
430
+ }, {
431
+ rects: 0,
432
+ glyphs: 0,
433
+ drawCalls: 0,
434
+ uploads: 0
435
+ });
436
+ return d.setup(r), n = {
437
+ texture: l,
438
+ view: l.createView({
439
+ baseMipLevel: 0,
440
+ mipLevelCount: 1
441
+ }),
442
+ levels: c,
443
+ painter: d,
444
+ uniform: u,
445
+ group: r.createBindGroup({
446
+ layout: this.panelLayout,
447
+ entries: [
448
+ {
449
+ binding: 0,
450
+ resource: { buffer: u }
451
+ },
452
+ {
453
+ binding: 1,
454
+ resource: this.sampler
455
+ },
456
+ {
457
+ binding: 2,
458
+ resource: l.createView()
459
+ }
460
+ ]
461
+ }),
462
+ painted: -1,
463
+ atlas: ""
464
+ }, this.gpu.set(t, n), n;
465
+ }
466
+ paint(e, t) {
467
+ let n = e.ui.draw();
468
+ if (t.painted === n.version && t.atlas === t.painter.atlasKey) return;
469
+ let r = this.device, i = t.painter, a = i.prepare(n), o = r.createCommandEncoder(), s = o.beginRenderPass({ colorAttachments: [{
470
+ view: t.view,
471
+ loadOp: "clear",
472
+ storeOp: "store",
473
+ clearValue: [
474
+ 0,
475
+ 0,
476
+ 0,
477
+ 0
478
+ ]
479
+ }] });
480
+ a && i.encode(s, {
481
+ width: t.texture.width,
482
+ height: t.texture.height,
483
+ format: y,
484
+ samples: 1
485
+ }), s.end(), r.queue.submit([o.finish()]), t.levels > 1 && f(r, t.texture, t.levels, this.mipCache), t.painted = n.version, t.atlas = i.atlasKey, this.stats.repaints++;
486
+ }
487
+ camera(e, t) {
488
+ let n = this.cameras[e];
489
+ if (!n) {
490
+ let t = this.device.createBuffer({
491
+ size: 80,
492
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
493
+ });
494
+ n = this.cameras[e] = {
495
+ buffer: t,
496
+ group: this.device.createBindGroup({
497
+ layout: this.cameraLayout,
498
+ entries: [{
499
+ binding: 0,
500
+ resource: { buffer: t }
501
+ }]
502
+ })
503
+ };
504
+ }
505
+ let r = t.camera, i = this.cameraData;
506
+ return i.set(r.matrix), i[16] = r.position[0], i[17] = r.position[1], i[18] = r.position[2], i[19] = 1, this.device.queue.writeBuffer(n.buffer, 0, i), n.group;
507
+ }
508
+ pipeline(e) {
509
+ let t = e.descriptor.depthStencil.format, n = e.descriptor.multisample?.count ?? 1, r = `${e.format}|${t}|${n}`, i = this.pipelines.get(r);
510
+ if (!i) {
511
+ let a = this.module;
512
+ i = this.device.createRenderPipeline({
513
+ layout: this.pipelineLayout,
514
+ primitive: {
515
+ topology: "triangle-list",
516
+ cullMode: "none"
517
+ },
518
+ vertex: {
519
+ module: a,
520
+ entryPoint: "panelVertex"
521
+ },
522
+ fragment: {
523
+ module: a,
524
+ entryPoint: "panelFragment",
525
+ targets: [{
526
+ format: e.format,
527
+ blend: o
528
+ }]
529
+ },
530
+ multisample: { count: n },
531
+ depthStencil: {
532
+ format: t,
533
+ depthWriteEnabled: !1,
534
+ depthCompare: "greater-equal"
535
+ }
536
+ }), this.pipelines.set(r, i);
537
+ }
538
+ return i;
539
+ }
540
+ release(e) {
541
+ let t = this.gpu.get(e);
542
+ t && (t.texture.destroy(), t.uniform.destroy(), t.painter.release(!0), this.gpu.delete(e));
543
+ }
544
+ onDeviceRestored(e) {
545
+ for (let e of [...this.gpu.keys()]) this.release(e);
546
+ for (let e of this.cameras) e?.buffer.destroy();
547
+ this.cameras.length = 0, this.pipelines.clear(), this.mipCache.clear(), this.setup(e);
548
+ }
549
+ dispose() {
550
+ let e = this.engine;
551
+ this.detach?.(), this.detach = void 0, this.cancel();
552
+ for (let e of [...this.gpu.keys()]) this.release(e);
553
+ for (let e of this.cameras) e?.buffer.destroy();
554
+ this.cameras.length = 0, this.pipelines.clear(), this.mipCache.clear(), this.panels.clear(), this.device = this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
555
+ }
556
+ [Symbol.dispose ?? Symbol.for("Symbol.dispose")]() {
557
+ this.dispose();
558
+ }
559
+ }, x = "\nstruct Panel { model: mat4x4f, size: vec2f, linear: f32, pad: f32, surface: vec4f }\nstruct View { viewProj: mat4x4f, eye: vec4f }\n@group(0) @binding(0) var<uniform> panel: Panel;\n@group(0) @binding(1) var panelSampler: sampler;\n@group(0) @binding(2) var panelTexture: texture_2d<f32>;\n@group(1) @binding(0) var<uniform> view: View;\n\nstruct Out { @builtin(position) clip: vec4f, @location(0) uv: vec2f, @location(1) facing: f32 }\n@vertex fn panelVertex(@builtin(vertex_index) vi: u32) -> Out {\n let corner = vi % 6u;\n let cell = vi / 6u;\n let columns = u32(panel.surface.z);\n let c = (vec2f(f32(cell % columns), f32(cell / columns)) + vec2f(f32((0x32u >> corner) & 1u), f32((0x2cu >> corner) & 1u))) / panel.surface.zw;\n var local = vec3f((c.x - 0.5) * panel.size.x, (0.5 - c.y) * panel.size.y, 0.0);\n var normal = vec3f(0.0, 0.0, 1.0);\n if (panel.surface.y > 0.5) {\n let r = panel.surface.x;\n let a = local.x / r;\n let b = select(0.0, local.y / r, panel.surface.y > 1.5);\n normal = vec3f(sin(a) * cos(b), sin(b), cos(a) * cos(b));\n local = vec3f(r * normal.x, select(local.y, r * normal.y, panel.surface.y > 1.5), r * (normal.z - 1.0));\n }\n let world = panel.model * vec4f(local, 1.0);\n // From behind, the UI is mirrored back to reading order.\n var o: Out;\n o.clip = view.viewProj * world;\n o.facing = dot(view.eye.xyz - world.xyz, (panel.model * vec4f(normal, 0.0)).xyz);\n o.uv = c;\n return o;\n}\nfn toSrgb(c: vec3f) -> vec3f {\n return select(1.055 * pow(c, vec3f(1.0 / 2.4)) - 0.055, c * 12.92, c <= vec3f(0.0031308));\n}\n@fragment fn panelFragment(i: Out) -> @location(0) vec4f {\n if (i.facing < 0.0 && panel.pad < 0.5) { discard; }\n let uv = vec2f(select(i.uv.x, 1.0 - i.uv.x, i.facing < 0.0), i.uv.y);\n let c = textureSample(panelTexture, panelSampler, uv);\n if (panel.linear > 0.5 || c.a <= 0.0) { return c; }\n return vec4f(toSrgb(c.rgb / c.a) * c.a, c.a);\n}\n";
560
+ //#endregion
561
+ export { h as WorldPanel, b as WorldPanels, v as pickPanel, _ as rayFromMatrix, g as screenRay };
package/package.json CHANGED
@@ -1,6 +1,119 @@
1
- {
2
- "name": "@margoengine/ui",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@margoengine/ui",
3
+ "version": "0.1.0",
4
+ "description": "Game UI drawn in the engine: menus, HUDs, health bars, inventories, dialogs and toasts with flex layout, theming, keyboard, gamepad and touch navigation, screen reader support and React components.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "webgpu",
8
+ "3d",
9
+ "ui",
10
+ "hud",
11
+ "game-ui",
12
+ "accessibility"
13
+ ],
14
+ "homepage": "https://margoengine.com",
15
+ "bugs": {
16
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
21
+ "directory": "packages/ui"
22
+ },
23
+ "license": "Apache-2.0",
24
+ "author": "Scott Campbell",
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ },
32
+ "./react": {
33
+ "types": "./dist/react.d.ts",
34
+ "react-server": "./dist/rsc/react.js",
35
+ "import": "./dist/react.js"
36
+ },
37
+ "./world": {
38
+ "types": "./dist/world.d.ts",
39
+ "import": "./dist/world.js"
40
+ },
41
+ "./input": {
42
+ "types": "./dist/input.d.ts",
43
+ "import": "./dist/input.js"
44
+ },
45
+ "./markup": {
46
+ "types": "./dist/markup.d.ts",
47
+ "import": "./dist/markup.js"
48
+ },
49
+ "./image": {
50
+ "types": "./dist/image.d.ts",
51
+ "import": "./dist/image.js"
52
+ },
53
+ "./forms": {
54
+ "types": "./dist/forms.d.ts",
55
+ "import": "./dist/forms.js"
56
+ },
57
+ "./world-gl": {
58
+ "types": "./dist/world-gl.d.ts",
59
+ "import": "./dist/world-gl.js"
60
+ },
61
+ "./curved": {
62
+ "types": "./dist/curved.d.ts",
63
+ "import": "./dist/curved.js"
64
+ },
65
+ "./keyboard": {
66
+ "types": "./dist/keyboard.d.ts",
67
+ "import": "./dist/keyboard.js"
68
+ },
69
+ "./i18n": {
70
+ "types": "./dist/i18n.d.ts",
71
+ "import": "./dist/i18n.js"
72
+ },
73
+ "./editor": {
74
+ "types": "./dist/editor.d.ts",
75
+ "import": "./dist/editor.js"
76
+ },
77
+ "./dom": {
78
+ "types": "./dist/dom-host.d.ts",
79
+ "import": "./dist/dom-host.js"
80
+ }
81
+ },
82
+ "files": [
83
+ "dist",
84
+ "LICENSE",
85
+ "NOTICE"
86
+ ],
87
+ "publishConfig": {
88
+ "access": "public"
89
+ },
90
+ "peerDependencies": {
91
+ "@margoengine/core": "^0.1.0",
92
+ "react": "^18.0.0 || ^19.0.0"
93
+ },
94
+ "peerDependenciesMeta": {
95
+ "react": {
96
+ "optional": true
97
+ }
98
+ },
99
+ "dependencies": {
100
+ "@margoengine/input": "^0.1.0",
101
+ "@margoengine/math": "^0.1.0",
102
+ "@margoengine/text": "^0.1.0"
103
+ },
104
+ "scripts": {
105
+ "build": "node ../../scripts/build-optional.js ui",
106
+ "test": "npm run build && node --test tests/*.test.js",
107
+ "test:browser": "npm run build && node tests/ui-browser.mjs",
108
+ "prepack": "npm run build",
109
+ "test:browser:world": "npm run build && node tests/world-browser.mjs",
110
+ "test:browser:input": "npm run build && node tests/text-input-browser.mjs",
111
+ "test:browser:image": "npm run build && node tests/image-browser.mjs",
112
+ "test:browser:forms": "npm run build && node tests/forms-browser.mjs",
113
+ "test:browser:world-gl": "npm run build && node tests/world-gl-browser.mjs",
114
+ "test:browser:keyboard": "npm run build && node tests/keyboard-browser.mjs",
115
+ "test:browser:i18n": "npm run build && node tests/i18n-browser.mjs",
116
+ "test:browser:editor": "npm run build && node tests/editor-browser.mjs",
117
+ "test:browser:dom": "npm run build && node tests/dom-host-browser.mjs"
118
+ }
119
+ }