@opsydyn/effect-three-foldkit 0.4.0 → 1.0.0-next.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 (118) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +29 -23
  3. package/dist/boxSelect.d.ts +91 -117
  4. package/dist/boxSelect.d.ts.map +1 -1
  5. package/dist/browser.d.ts +3 -0
  6. package/dist/browser.d.ts.map +1 -0
  7. package/dist/browser.js +32 -0
  8. package/dist/browser.js.map +10 -0
  9. package/dist/element.d.ts +13 -869
  10. package/dist/element.d.ts.map +1 -1
  11. package/dist/index.d.ts +17 -17
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +173 -2383
  14. package/dist/index.js.map +14 -38
  15. package/dist/inspector.d.ts +4 -21
  16. package/dist/inspector.d.ts.map +1 -1
  17. package/dist/measuringTape.d.ts +39 -46
  18. package/dist/measuringTape.d.ts.map +1 -1
  19. package/dist/mount.d.ts +17 -11
  20. package/dist/mount.d.ts.map +1 -1
  21. package/dist/overlayView.d.ts +7 -18
  22. package/dist/overlayView.d.ts.map +1 -1
  23. package/dist/pivotControls.d.ts +64 -76
  24. package/dist/pivotControls.d.ts.map +1 -1
  25. package/dist/transformGizmo.d.ts +113 -125
  26. package/dist/transformGizmo.d.ts.map +1 -1
  27. package/dist/view.d.ts +37 -4
  28. package/dist/view.d.ts.map +1 -1
  29. package/dist/viewportGizmo.d.ts +107 -119
  30. package/dist/viewportGizmo.d.ts.map +1 -1
  31. package/package.json +15 -10
  32. package/src/boxSelect.ts +12 -133
  33. package/src/browser.ts +16 -0
  34. package/src/element.ts +19 -106
  35. package/src/index.ts +35 -16
  36. package/src/inspector.ts +70 -93
  37. package/src/measuringTape.ts +28 -92
  38. package/src/mount.ts +53 -286
  39. package/src/overlayView.ts +36 -57
  40. package/src/pivotControls.ts +8 -94
  41. package/src/transformGizmo.ts +8 -108
  42. package/src/view.ts +89 -51
  43. package/src/viewportGizmo.ts +8 -97
  44. package/dist/assets.d.ts +0 -53
  45. package/dist/assets.d.ts.map +0 -1
  46. package/dist/dom/animationFrames.d.ts +0 -12
  47. package/dist/dom/animationFrames.d.ts.map +0 -1
  48. package/dist/dom/canvas.d.ts +0 -26
  49. package/dist/dom/canvas.d.ts.map +0 -1
  50. package/dist/dom/latch.d.ts +0 -15
  51. package/dist/dom/latch.d.ts.map +0 -1
  52. package/dist/dom/models.d.ts +0 -10
  53. package/dist/dom/models.d.ts.map +0 -1
  54. package/dist/dom/overlays.d.ts +0 -24
  55. package/dist/dom/overlays.d.ts.map +0 -1
  56. package/dist/dom/pointer.d.ts +0 -49
  57. package/dist/dom/pointer.d.ts.map +0 -1
  58. package/dist/dom/postprocessing.d.ts +0 -22
  59. package/dist/dom/postprocessing.d.ts.map +0 -1
  60. package/dist/dom/property.d.ts +0 -15
  61. package/dist/dom/property.d.ts.map +0 -1
  62. package/dist/dom/reconciled.d.ts +0 -10
  63. package/dist/dom/reconciled.d.ts.map +0 -1
  64. package/dist/dom/renderSettings.d.ts +0 -58
  65. package/dist/dom/renderSettings.d.ts.map +0 -1
  66. package/dist/dom/typedScenes.d.ts +0 -14
  67. package/dist/dom/typedScenes.d.ts.map +0 -1
  68. package/dist/dom/updates.d.ts +0 -8
  69. package/dist/dom/updates.d.ts.map +0 -1
  70. package/dist/dom/viewport.d.ts +0 -40
  71. package/dist/dom/viewport.d.ts.map +0 -1
  72. package/dist/dom/webgl.d.ts +0 -12
  73. package/dist/dom/webgl.d.ts.map +0 -1
  74. package/dist/dom/webgpu.d.ts +0 -24
  75. package/dist/dom/webgpu.d.ts.map +0 -1
  76. package/dist/dom/webgpuPostprocessing.d.ts +0 -4
  77. package/dist/dom/webgpuPostprocessing.d.ts.map +0 -1
  78. package/dist/dom/xr.d.ts +0 -53
  79. package/dist/dom/xr.d.ts.map +0 -1
  80. package/dist/dom/xrGlide.d.ts +0 -49
  81. package/dist/dom/xrGlide.d.ts.map +0 -1
  82. package/dist/dom/xrHub.d.ts +0 -45
  83. package/dist/dom/xrHub.d.ts.map +0 -1
  84. package/dist/dom/xrInputs.d.ts +0 -40
  85. package/dist/dom/xrInputs.d.ts.map +0 -1
  86. package/dist/dom/xrSpaces.d.ts +0 -33
  87. package/dist/dom/xrSpaces.d.ts.map +0 -1
  88. package/dist/dom/xrTarget.d.ts +0 -36
  89. package/dist/dom/xrTarget.d.ts.map +0 -1
  90. package/dist/environment.d.ts +0 -64
  91. package/dist/environment.d.ts.map +0 -1
  92. package/dist/xr.d.ts +0 -34
  93. package/dist/xr.d.ts.map +0 -1
  94. package/src/assets.ts +0 -151
  95. package/src/dom/animationFrames.ts +0 -32
  96. package/src/dom/canvas.ts +0 -54
  97. package/src/dom/latch.ts +0 -36
  98. package/src/dom/models.ts +0 -37
  99. package/src/dom/overlays.ts +0 -63
  100. package/src/dom/pointer.ts +0 -163
  101. package/src/dom/postprocessing.ts +0 -116
  102. package/src/dom/property.ts +0 -43
  103. package/src/dom/reconciled.ts +0 -51
  104. package/src/dom/renderSettings.ts +0 -117
  105. package/src/dom/typedScenes.ts +0 -21
  106. package/src/dom/updates.ts +0 -14
  107. package/src/dom/viewport.ts +0 -97
  108. package/src/dom/webgl.ts +0 -231
  109. package/src/dom/webgpu.ts +0 -220
  110. package/src/dom/webgpuPostprocessing.ts +0 -102
  111. package/src/dom/xr.ts +0 -211
  112. package/src/dom/xrGlide.ts +0 -114
  113. package/src/dom/xrHub.ts +0 -118
  114. package/src/dom/xrInputs.ts +0 -309
  115. package/src/dom/xrSpaces.ts +0 -287
  116. package/src/dom/xrTarget.ts +0 -71
  117. package/src/environment.ts +0 -78
  118. package/src/xr.ts +0 -36
package/src/mount.ts CHANGED
@@ -1,305 +1,72 @@
1
- /**
2
- * The scene Mount (ADR-002): runs one scene for the lifetime of its element inside
3
- * Foldkit's runtime. Everything acquired here is released, in reverse order, when the
4
- * element unmounts.
5
- *
6
- * @module
7
- */
8
- import {
9
- createGatedFrames,
10
- FrameSource,
11
- type GatedFrames,
12
- type PhysicsEngine,
13
- type PointerInput,
14
- ResourceRegistry,
15
- type SceneEvent,
16
- type SceneDescription,
17
- SceneRuntime,
18
- SceneSchema,
19
- } from '@opsydyn/effect-three';
20
- import { type Cause, Data, Effect, Option, Queue, Schema, Stream } from 'effect';
21
- import { Mount } from 'foldkit';
22
- import type { ViewState } from 'foldkit/mount';
1
+ /** Connects an Effect UI mount scope and arguments to the browser scene host. */
2
+ import { Mount } from '@effect/ui';
3
+ import { SceneSchema, type SceneDescription } from '@opsydyn/effect-three';
4
+ import { mountSceneHost, type Environment } from '@opsydyn/effect-three/browser';
5
+ import { Effect, Schema, type Scope, Stream } from 'effect';
23
6
 
24
- import {
25
- canvasIn,
26
- reconciledDispatch,
27
- removal,
28
- sceneErrorDispatch,
29
- sceneEventDispatch,
30
- } from './dom/canvas.ts';
31
- import { applying, createLatch, type Latch, routedWhileOpen } from './dom/latch.ts';
32
- import { containOverlays, createOverlayWriter, type OverlayWriter } from './dom/overlays.ts';
33
- import { claimsGestures, listenForPointer, touchActionSync } from './dom/pointer.ts';
34
- import { interceptProperty } from './dom/property.ts';
35
- import { typedScene } from './dom/typedScenes.ts';
36
- import { afterUpdate } from './dom/updates.ts';
37
- import {
38
- measureViewport,
39
- observePixelRatio,
40
- observeReducedMotion,
41
- observeSize,
42
- observeVisibility,
43
- } from './dom/viewport.ts';
44
- import { createFrameGate, type FrameGate, xrTarget } from './dom/xrTarget.ts';
45
7
  import { SceneLifecycle } from './element.ts';
46
- import type { Environment } from './environment.ts';
47
-
48
- type SceneHandle = SceneRuntime.SceneHandle;
49
-
50
- /** Everything one mounted scene needs, gathered so helpers take one argument. */
51
- type Mounted = {
52
- readonly environment: Environment;
53
- readonly element: Element;
54
- readonly canvas: HTMLCanvasElement;
55
- readonly overlays: OverlayWriter;
56
- readonly handle: SceneHandle;
57
- readonly live: Latch;
58
- readonly frames: GatedFrames;
59
- readonly gate: FrameGate;
60
- };
61
-
62
- const decodeUnknownScene = Schema.decodeUnknownEffect(SceneSchema.SceneDescription);
63
-
64
- /** Typed scenes from `view` skip validation; anything else is Schema-decoded. */
65
- const decodeScene = (value: unknown) =>
66
- Option.fromNullishOr(typedScene(value)).pipe(
67
- Option.match({ onNone: () => decodeUnknownScene(value), onSome: Effect.succeed }),
68
- );
69
-
70
- const isLive = Schema.is(Schema.Literal('Live'));
71
-
72
- const acquireCanvas = (element: Element) => {
73
- const create = Effect.sync(canvasIn(element));
74
- return Effect.acquireRelease(create, (canvas) => Effect.sync(removal(canvas)));
75
- };
76
-
77
- const acquireSceneUpdates = () => {
78
- const create = Queue.sliding<unknown>(1);
79
- return Effect.acquireRelease(create, Queue.shutdown);
80
- };
81
-
82
- const interception = (element: Element, updates: Queue.Queue<unknown>) => () =>
83
- interceptProperty(element, 'scene', (value) => Queue.offerUnsafe(updates, value));
84
-
85
- const acquireInterception = (element: Element, updates: Queue.Queue<unknown>) => {
86
- const intercept = Effect.sync(interception(element, updates));
87
- return Effect.acquireRelease(intercept, (intercepted) => Effect.sync(intercepted.restore));
88
- };
89
-
90
- const pointerListening =
91
- (
92
- canvas: HTMLElement,
93
- onInput: (input: PointerInput) => ReadonlyArray<SceneEvent>,
94
- claimsGestures: () => boolean,
95
- ) =>
96
- () =>
97
- listenForPointer(canvas, onInput, claimsGestures);
98
-
99
- const acquirePointer = (canvas: HTMLElement, mounted: Mounted) => {
100
- const listen = Effect.sync(
101
- pointerListening(
102
- canvas,
103
- routedWhileOpen(mounted.live, mounted.handle.pointer),
104
- claimsGestures(mounted.handle.graph),
105
- ),
106
- );
107
- return Effect.acquireRelease(listen, Effect.sync);
108
- };
109
-
110
- /** Makes the scene presentable in the page's immersive sessions while it is mounted. */
111
- const xrRegistration = (mounted: Mounted) => () =>
112
- mounted.environment.xrHub.register(
113
- xrTarget({
114
- element: mounted.element,
115
- canvas: mounted.canvas,
116
- live: mounted.live,
117
- gate: mounted.gate,
118
- settings: mounted.handle.graph.xr,
119
- draw: mounted.handle.drawNow,
120
- scene: mounted.handle.graph.scene,
121
- follow: mounted.handle.graph.follow,
122
- lend: mounted.handle.graph.store.lendGeometry,
123
- input: mounted.handle.xrInput,
124
- }),
125
- );
126
8
 
127
- const acquireXrTarget = (mounted: Mounted) => {
128
- const register = Effect.sync(xrRegistration(mounted));
129
- return Effect.acquireRelease(register, Effect.sync);
9
+ /** Arguments supplied by a component whenever it renders the scene. */
10
+ export type SceneMountArgs = {
11
+ readonly scene: SceneDescription;
12
+ readonly live?: boolean;
130
13
  };
131
14
 
132
- /** A stream of values from a DOM observer that returns a disconnect function. */
133
- const connectObserver =
134
- <A>(observe: (emit: (value: A) => void) => () => void) =>
135
- (queue: Queue.Queue<A, Cause.Done>) => {
136
- const connect = Effect.sync(applying(observe, (value: A) => Queue.offerUnsafe(queue, value)));
137
- return Effect.acquireRelease(connect, Effect.sync);
138
- };
15
+ export type SceneMount<R = never> = Mount.Factory<
16
+ SceneMountArgs,
17
+ SceneLifecycle,
18
+ HTMLElement,
19
+ R | Scope.Scope
20
+ >;
139
21
 
140
- const observed = <A>(observe: (emit: (value: A) => void) => () => void) =>
141
- Stream.callback<A>(connectObserver(observe));
22
+ const lifecycleFailure = (reason: string) => SceneLifecycle.FailedScene({ reason });
142
23
 
143
- const applyUpdate = (mounted: Mounted) => (value: unknown) => {
144
- const reportError = sceneErrorDispatch(mounted.element);
145
- const reportReconciled = afterUpdate(
146
- reconciledDispatch(mounted.element, mounted.environment.store),
147
- [touchActionSync(mounted.canvas, mounted.handle.graph), mounted.overlays.rewrite],
148
- );
149
- const decoded = decodeScene(value);
150
- const applied = decoded.pipe(
151
- Effect.flatMap(mounted.handle.update),
152
- Effect.flatMap((ops) => Effect.sync(reportReconciled(ops))),
153
- );
154
- return applied.pipe(
155
- Effect.catchTags({
156
- SchemaError: (error) =>
157
- Effect.sync(reportError({ _tag: 'MalformedScene', issue: error.message })),
158
- InvalidScene: ({ error }) => Effect.sync(reportError(error)),
159
- }),
160
- );
161
- };
162
-
163
- const resize = (mounted: Mounted) => () =>
164
- mounted.handle.resize(
165
- measureViewport(
166
- mounted.element,
167
- mounted.environment.host.devicePixelRatio,
168
- mounted.environment.pixelRatio,
169
- ),
170
- );
171
-
172
- const watchers = (
173
- mounted: Mounted,
174
- updates: Queue.Queue<unknown>,
175
- viewStateChanges: Stream.Stream<ViewState>,
176
- ) => {
177
- const dispatchEvent = sceneEventDispatch(mounted.element, mounted.live);
178
- const sizes = observed<void>((emit) => observeSize(mounted.element, emit));
179
- const ratios = observed<void>((emit) => observePixelRatio(mounted.environment.host, emit));
180
- const visibility = observed<boolean>((emit) => observeVisibility(mounted.element, emit));
181
- const reducedMotion = observed<boolean>((emit) =>
182
- observeReducedMotion(mounted.environment.host, emit),
183
- );
184
- return [
185
- Stream.runForEach(Stream.fromQueue(updates), applyUpdate(mounted)),
186
- Stream.runForEach(mounted.handle.events, (event) => Effect.sync(dispatchEvent(event))),
187
- Stream.runForEach(viewStateChanges, (state) =>
188
- Effect.sync(applying(mounted.live.set, isLive(state))),
189
- ),
190
- Stream.runForEach(sizes, resize(mounted)),
191
- Stream.runForEach(ratios, resize(mounted)),
192
- Stream.runForEach(visibility, (visible) =>
193
- Effect.sync(applying(mounted.gate.setVisible, visible)),
194
- ),
195
- Stream.runForEach(reducedMotion, (reduced) => mounted.handle.setAnimating(!reduced)),
196
- ];
24
+ const recoverStartup = {
25
+ InvalidScene: ({ error }: { readonly error: { readonly _tag: string } }) =>
26
+ Effect.succeed(lifecycleFailure(`invalid scene: ${error._tag}`)),
27
+ RendererUnavailable: () => Effect.succeed(lifecycleFailure('renderer unavailable')),
28
+ PhysicsUnavailable: () => Effect.succeed(lifecycleFailure('physics unavailable')),
197
29
  };
198
30
 
199
- /** The physics engine could not be loaded (Rapier's WebAssembly, say). */
200
- class PhysicsUnavailable extends Data.TaggedError('PhysicsUnavailable')<{
201
- readonly cause: unknown;
202
- }> {}
203
-
204
- const sceneRuntimeFor =
205
- (
206
- environment: Environment,
207
- element: Element,
208
- canvas: HTMLCanvasElement,
209
- overlays: OverlayWriter,
210
- physics: PhysicsEngine | undefined,
211
- ) =>
212
- (scene: SceneDescription) => {
213
- const frames = createGatedFrames(environment.frames);
214
- const started = SceneRuntime.make({
215
- scene,
216
- renderer: environment.renderer(canvas),
217
- behaviours: environment.behaviours,
218
- physics,
219
- labels: environment.labels,
220
- placeOverlays: overlays.place,
221
- viewport: measureViewport(element, environment.host.devicePixelRatio, environment.pixelRatio),
222
- }).pipe(
223
- Effect.provideService(FrameSource.FrameSource, FrameSource.fromScheduler(frames)),
224
- Effect.provideService(ResourceRegistry.ResourceRegistry, { store: environment.store }),
225
- );
226
- return Effect.map(started, (handle) => ({ handle, frames }));
227
- };
228
-
229
- function* runScene(
31
+ const startScene = (
230
32
  environment: Environment,
231
- element: Element,
232
- viewStateChanges: Stream.Stream<ViewState>,
233
- ) {
234
- const canvas = yield* acquireCanvas(element);
235
- const updates = yield* acquireSceneUpdates();
236
- const intercepted = yield* acquireInterception(element, updates);
237
- const initial = yield* decodeScene(intercepted.initial);
238
- const overlays = createOverlayWriter(element);
239
- yield* Effect.sync(containOverlays(element));
240
- const physics = yield* Effect.tryPromise({
241
- try: () => environment.physics,
242
- catch: (cause) => new PhysicsUnavailable({ cause }),
243
- });
244
- const { handle, frames } = yield* sceneRuntimeFor(
245
- environment,
246
- element,
247
- canvas,
248
- overlays,
249
- physics,
250
- )(initial);
251
- const mounted: Mounted = {
33
+ element: HTMLElement,
34
+ { scene, live }: SceneMountArgs,
35
+ changes: Stream.Stream<SceneMountArgs>,
36
+ ) => {
37
+ const updates = Stream.map(changes, (args) => args.scene);
38
+ const liveChanges = Stream.map(changes, (args) => args.live ?? true);
39
+ const startup = mountSceneHost({
252
40
  environment,
253
41
  element,
254
- canvas,
255
- overlays,
256
- handle,
257
- frames,
258
- gate: createFrameGate(frames),
259
- live: createLatch(true),
260
- };
261
- yield* acquirePointer(canvas, mounted);
262
- yield* acquireXrTarget(mounted);
263
- yield* Effect.sync(touchActionSync(canvas, handle.graph));
264
- yield* Effect.sync(reconciledDispatch(element, environment.store)(handle.startupOps));
265
- const forks = watchers(mounted, updates, viewStateChanges).map((watcher) =>
266
- Effect.forkScoped(watcher),
42
+ initial: scene,
43
+ initialLive: live ?? true,
44
+ updates,
45
+ liveChanges,
46
+ });
47
+ return startup.pipe(
48
+ Effect.map(() => SceneLifecycle.StartedScene()),
49
+ Effect.catchTags(recoverStartup),
267
50
  );
268
- yield* Effect.all(forks, { concurrency: 'unbounded' });
269
- return SceneLifecycle.StartedScene();
270
- }
271
-
272
- const failure = (reason: string) => SceneLifecycle.FailedScene({ reason });
273
-
274
- const recoverStart = {
275
- SchemaError: (error: Schema.SchemaError) =>
276
- Effect.succeed(failure(`malformed scene: ${error.message}`)),
277
- InvalidScene: ({ error }: { readonly error: { readonly _tag: string } }) =>
278
- Effect.succeed(failure(`invalid scene: ${error._tag}`)),
279
- RendererUnavailable: () => Effect.succeed(failure('renderer unavailable')),
280
- PhysicsUnavailable: () => Effect.succeed(failure('physics unavailable')),
281
51
  };
282
52
 
283
- type ExecuteInput = {
284
- readonly element: Element;
285
- readonly viewStateChanges: Stream.Stream<ViewState>;
286
- };
287
-
288
- const mountScene =
289
- (environment: Environment) =>
290
- ({ element, viewStateChanges }: ExecuteInput) => {
291
- const scene = Effect.gen(() => runScene(environment, element, viewStateChanges));
292
- return scene.pipe(Effect.catchTags(recoverStart));
293
- };
53
+ const mountArgs = { scene: SceneSchema.SceneDescription, live: Schema.optionalKey(Schema.Boolean) };
294
54
 
295
55
  /**
296
- * Defines the scene Mount for an environment. Call once at the application entry point
297
- * and pass the result to `view` as `mount`.
56
+ * Defines a scene mount with access to the component's environment. The provided
57
+ * Effect can read a shared service or acquire an environment scoped to the scene.
298
58
  */
299
- export const makeSceneMount = (environment: Environment) =>
300
- Mount.define('EffectThreeScene', {
301
- messages: [SceneLifecycle.StartedScene, SceneLifecycle.FailedScene],
302
- execute: mountScene(environment),
303
- });
59
+ export const defineScopedSceneMount = <R>(
60
+ // oxlint-disable-next-line linteffect/no-manual-effect-channels -- Retain the caller's environment requirements.
61
+ environment: Effect.Effect<Environment, never, R>,
62
+ ): SceneMount<R> =>
63
+ Mount.make('EffectThreeScene', { args: mountArgs }, (element, args, changes) =>
64
+ Effect.flatMap(environment, (resolved) => startScene(resolved, element, args, changes)),
65
+ );
304
66
 
305
- export type SceneMount = ReturnType<typeof makeSceneMount>;
67
+ /** Defines a scene mount for a shared environment owned by the caller. */
68
+ export const makeSceneMount = (environment: Environment): SceneMount => {
69
+ // oxlint-disable-next-line linteffect/no-effect-succeed-variable -- The caller has already acquired the shared environment.
70
+ const shared = Effect.succeed(environment);
71
+ return defineScopedSceneMount(shared);
72
+ };
@@ -1,71 +1,50 @@
1
- /**
2
- * Overlay HTML in the scene view: the application's content for each overlay, in an
3
- * element the scene moves to the overlay's point every frame it draws.
4
- *
5
- * @module
6
- */
1
+ import type { View } from '@effect/ui';
2
+ import type { Html } from '@effect/ui/html';
7
3
  import type { Overlay, SceneDescription } from '@opsydyn/effect-three';
8
- import type { Html, HtmlBuilder } from 'foldkit/html';
4
+ import { overlayData } from '@opsydyn/effect-three/browser';
9
5
 
10
- import { overlayData } from './dom/overlays.ts';
11
-
12
- /** HTML shown at a point in the scene, as Drei's `Html`: where, and what. */
13
- export type OverlayView = {
6
+ /** HTML shown at a point in the scene. */
7
+ export type OverlayView<Message> = {
14
8
  readonly overlay: Overlay;
15
- readonly content: Html;
16
- /**
17
- * `'auto'` (the default, as Drei): the HTML takes pointer events. `'none'`: they pass
18
- * through to the canvas, so the scene under a label stays pickable.
19
- */
9
+ readonly content: View.Input<Message, never>;
20
10
  readonly pointerEvents?: 'auto' | 'none';
21
11
  };
22
12
 
23
- /** The scene with the overlays' points added, so the scene places them. */
24
- export const withOverlays = (
13
+ export const withOverlays = <Message>(
25
14
  scene: SceneDescription,
26
- overlays: ReadonlyArray<OverlayView>,
15
+ overlays: ReadonlyArray<OverlayView<Message>>,
27
16
  ): SceneDescription =>
28
17
  overlays.length === 0
29
18
  ? scene
30
- : { ...scene, overlays: [...scene.overlays, ...overlays.map((view) => view.overlay)] };
31
-
32
- // The layer covers the canvas, lets events through, and keeps Drei's large z-indexes
33
- // inside the scene element rather than competing with the page.
34
- const layerStyle = {
35
- position: 'absolute',
36
- inset: '0',
37
- overflow: 'hidden',
38
- pointerEvents: 'none',
39
- isolation: 'isolate',
40
- };
19
+ : { ...scene, overlays: [...scene.overlays, ...overlays.map(({ overlay }) => overlay)] };
41
20
 
42
- /** The overlay layer, or nothing when there are no overlays. */
43
21
  export const overlayLayer = <Message>(
44
- h: HtmlBuilder<Message>,
45
- overlays: ReadonlyArray<OverlayView>,
46
- ): ReadonlyArray<Html> =>
22
+ h: Html.H<Message>,
23
+ overlays: ReadonlyArray<OverlayView<Message>>,
24
+ ) =>
47
25
  overlays.length === 0
48
- ? []
49
- : [
50
- h.div(
51
- [h.Style(layerStyle)],
52
- overlays.map(({ overlay, content, pointerEvents = 'auto' }) =>
53
- h.keyed('div')(
54
- overlay.key,
55
- [
56
- h.DataAttribute(overlayData.key, overlay.key),
57
- // Hidden until the scene first places it.
58
- h.Style({
59
- position: 'absolute',
60
- top: '0',
61
- left: '0',
62
- transformOrigin: '0 0',
63
- visibility: 'hidden',
64
- pointerEvents,
65
- }),
66
- ],
67
- [content],
68
- ),
26
+ ? null
27
+ : h.div
28
+ .style({
29
+ position: 'absolute',
30
+ inset: '0',
31
+ overflow: 'hidden',
32
+ pointerEvents: 'none',
33
+ isolation: 'isolate',
34
+ })
35
+ .children(
36
+ ...overlays.map(({ overlay, content, pointerEvents = 'auto' }) =>
37
+ h.div
38
+ .key(overlay.key)
39
+ .attr(overlayData.attribute, overlay.key)
40
+ .style({
41
+ position: 'absolute',
42
+ top: '0',
43
+ left: '0',
44
+ transformOrigin: '0 0',
45
+ visibility: 'hidden',
46
+ pointerEvents,
47
+ })
48
+ .children(content),
69
49
  ),
70
- ),
71
- ];
50
+ );
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Pivot controls as a Foldkit submodel (roadmap, "Beyond parity" 5): the core's pure
2
+ * Pivot controls as an Effect UI submodel (roadmap, "Beyond parity" 5): the core's pure
3
3
  * `pivotNodes`, `pressPivot` and `dragPivot` (after Drei's `PivotControls`) with
4
4
  * Messages for the scene's pointer events and OutMessages for what the parent does with
5
5
  * them, as `TransformGizmo` is for three's controls. The parent owns where its objects
@@ -17,102 +17,16 @@
17
17
  *
18
18
  * @module
19
19
  */
20
- import {
21
- dragPivot,
22
- hoverPivot,
23
- type Pivot,
24
- pivot,
25
- type PivotHandle,
26
- pivotHandleOf,
27
- pivotNodes,
28
- type Placement,
29
- pressPivot,
30
- releasePivot,
31
- SceneSchema,
32
- type SceneNode,
33
- type Vec3,
34
- } from '@opsydyn/effect-three';
35
- import { Option, Schema } from 'effect';
36
- import { defineMessageUnion } from 'foldkit/message';
37
- import type { ReturnWithOutMessage } from 'foldkit/update';
38
20
 
39
- export const Model = Schema.Struct({
40
- /** The key its handles are drawn under: `${key}:arrow-x`, `${key}:slider-z` and so on. */
41
- key: Schema.String,
42
- pivot: SceneSchema.Pivot,
43
- /** The placement the drag last reported, so `Released` can report where it ended. */
44
- last: Schema.Option(SceneSchema.Placement),
45
- });
46
- export type Model = typeof Model.Type;
21
+ import { UiSchema } from '@effect/ui';
22
+ import { PivotControls as Controller } from '@opsydyn/effect-three/browser';
47
23
 
48
- const Handle = SceneSchema.PivotHandle;
24
+ export const { Model, init, handleOf, held, nodes } = Controller;
25
+ export type Model = Controller.Model;
49
26
 
50
- export const Message = defineMessageUnion({
51
- Pressed: { handle: Handle, point: SceneSchema.Vec3, placement: SceneSchema.Placement },
52
- Dragged: { origin: SceneSchema.Vec3, direction: SceneSchema.Vec3, snap: Schema.Boolean },
53
- Released: {},
54
- Hovered: { handle: Schema.NullOr(Handle) },
55
- });
27
+ export const Message = UiSchema.TaggedEnum(Controller.MessageFields);
56
28
  export type Message = typeof Message.Type;
57
-
58
- export const OutMessage = defineMessageUnion({
59
- /** The object's placement now, during a drag. */
60
- Moved: { placement: SceneSchema.Placement },
61
- /** The drag is over, the object here: a step for history. */
62
- Finished: { placement: SceneSchema.Placement },
63
- });
29
+ export const OutMessage = UiSchema.TaggedEnum(Controller.OutMessageFields);
64
30
  export type OutMessage = typeof OutMessage.Type;
65
31
 
66
- /** Pivot controls drawn under `key`, with Drei's defaults or the options given. */
67
- export const init = (key: string, options: Parameters<typeof pivot>[0] = {}): Model => ({
68
- key,
69
- pivot: pivot(options),
70
- last: Option.none(),
71
- });
72
-
73
- type Return = ReturnWithOutMessage<Model, Message, OutMessage>;
74
-
75
- const withPivot = (model: Model, next: Pivot): Model => ({ ...model, pivot: next });
76
-
77
- const finished = (model: Model): Return => ({
78
- model: { ...withPivot(model, releasePivot(model.pivot)), last: Option.none() },
79
- ...Option.match(model.last, {
80
- onNone: () => ({}),
81
- onSome: (placement) => ({ outMessage: OutMessage.Finished({ placement }) }),
82
- }),
83
- });
84
-
85
- const dragged = (model: Model, origin: Vec3, direction: Vec3, snap: boolean): Return => {
86
- const placement = Option.fromNullishOr(dragPivot(model.pivot, { origin, direction }, { snap }));
87
- return Option.match(placement, {
88
- onNone: () => ({ model }),
89
- onSome: (placement) => ({
90
- model: { ...model, last: Option.some(placement) },
91
- outMessage: OutMessage.Moved({ placement }),
92
- }),
93
- });
94
- };
95
-
96
- export const update = (model: Model, message: Message): Return =>
97
- Message.match<Return>(message, {
98
- Pressed: ({ handle, point, placement }) => ({
99
- model: {
100
- ...withPivot(model, pressPivot(model.pivot, placement, handle, point)),
101
- last: Option.none(),
102
- },
103
- }),
104
- Dragged: ({ origin, direction, snap }) => dragged(model, origin, direction, snap),
105
- Released: () => finished(model),
106
- Hovered: ({ handle }) => ({ model: withPivot(model, hoverPivot(model.pivot, handle)) }),
107
- });
108
-
109
- /** Whether a hit `key` is one of these controls' handles, and which. */
110
- export const handleOf = (model: Model, key: string): Option.Option<PivotHandle> =>
111
- Option.fromNullishOr(pivotHandleOf(model.key, key));
112
-
113
- /** Whether a handle is held: the parent leaves the camera alone while it is. */
114
- export const held = (model: Model): boolean => model.pivot.grab !== null;
115
-
116
- /** The handles for `placement`, for the parent's scene. */
117
- export const nodes = (model: Model, placement: Placement): SceneNode =>
118
- pivotNodes(model.key, model.pivot, placement);
32
+ export const update = Controller.update;