/
aardvark_gadget_seed.tsx
497 lines (422 loc) · 12.9 KB
/
aardvark_gadget_seed.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import { AardvarkManifest, AvNodeTransform, AvVolume, EndpointAddr, endpointAddrToString, EVolumeType, g_builtinModelError, InitialInterfaceLock, quatFromAxisAngleDegrees, ResourceRotation } from '@aardvarkxr/aardvark-shared';
import { vec3 } from '@tlaukkan/tsm';
import bind from 'bind-decorator';
import isUrl from 'is-url';
import * as React from 'react';
import { AvBaseNodeProps } from './aardvark_base_node';
import { AvComposedEntity, EntityComponent } from './aardvark_composed_entity';
import { AvEntityChild } from './aardvark_entity_child';
import { AvGadget } from './aardvark_gadget';
import { ActiveInterface, InterfaceProp } from './aardvark_interface_entity';
import { AvModel } from './aardvark_model';
import { AvTransform } from './aardvark_transform';
import { ContainerRequest, ContainerRequestType, MoveableComponent, MoveableComponentState } from './component_moveable';
/** Represents the possible highlight states of an {@link AvGadgetSeed} */
export enum GadgetSeedHighlight
{
Idle,
GrabberNearby,
GadgetStarting,
Menu,
}
/** Props for {@link AvGadgetSeed} */
export interface AvGadgetSeedProps extends AvBaseNodeProps
{
/** The manifest object for this gadget. These can be loaded from
* gadget URLs with AvGadget.instance().loadManifest(...).
*/
manifest: AardvarkManifest;
/** The gadget URL that the manifest was loaded form. The gadget
* seed needs to this so it can load the gadget's icon, and also
* to start the gadget.
*/
gadgetUrl: string;
/** Size in meters of the gadget seed. This will control both
* the active area and the scale of the gadget's model, at least
* for gadget models that are centered around the origin.
*
* @default 0.1
*/
radius?: number;
/** Called when the seed is highlighted or unhighlighted. */
highlightCallback?: ( highlight: GadgetSeedHighlight ) => void;
/** Used instead of anything related to the manifest when drawing
* this gadget seed
*
* @default none
*/
customAppearance?: JSX.Element;
/** If provided, this custom volume will be used instead of the radius
* property.
*
* @default none
*/
customVolume?: AvVolume;
/** If provided, these initial interface locks will be passed along
* to the gadget when it's started.
*
* @default none
*/
interfaceLocks?: InitialInterfaceLock[];
}
enum GadgetSeedPhase
{
Idle,
GrabberNearby,
WaitingForGadgetStart,
WaitingForRegrab,
WaitingForRedropToFinish,
Menu,
}
interface AvGadgetSeedState
{
phase: GadgetSeedPhase;
}
const k_seedFromGadgetQuat = quatFromAxisAngleDegrees( vec3.right, -90 );
const k_seedFromGadget: AvNodeTransform = { rotation: { x: k_seedFromGadgetQuat.x, y: k_seedFromGadgetQuat.y, z: k_seedFromGadgetQuat.z, w: k_seedFromGadgetQuat.w } };
/** @hidden */
export class GadgetSeedContainerComponent implements EntityComponent
{
private contentsEpa: EndpointAddr;
private contentsRested = false;
private entityCallback: () => void = null;
private activeContainer: ActiveInterface = null;
private childAddedCallback: () => void;
private childRemovedCallback: () => void;
constructor( childAddedCallback: () => void, childRemovedCallback: () => void )
{
this.childAddedCallback = childAddedCallback;
this.childRemovedCallback = childRemovedCallback;
}
private updateListener()
{
this.entityCallback?.();
}
@bind
private onContainerStart( activeContainer: ActiveInterface )
{
this.contentsEpa = activeContainer.peer;
console.log( `Seed container onContainerStart with ${ endpointAddrToString( activeContainer.peer ) }` );
activeContainer.onEvent(
( event: any ) =>
{
this.contentsRested = event.state == "Resting";
this.updateListener();
}
)
activeContainer.onEnded(
() =>
{
console.log( `Seed container active container ended with `
+ `${ endpointAddrToString( activeContainer.peer ) }` );
this.contentsRested = false;
this.contentsEpa = null;
this.activeContainer = null;
this.updateListener();
this.childRemovedCallback?.();
} );
this.activeContainer = activeContainer;
this.childAddedCallback?.();
}
public get transmits(): InterfaceProp[]
{
return [];
}
public get receives(): InterfaceProp[]
{
return [ { iface: MoveableComponent.containerInterface, processor: this.onContainerStart } ];
}
public get parent(): EndpointAddr
{
return null;
}
public get wantsTransforms()
{
return false;
}
public get interfaceLocks(): InitialInterfaceLock[] { return []; }
public redrop( newContainer: EndpointAddr, moveableToReplace: EndpointAddr )
{
if( this.activeContainer )
{
let req: ContainerRequest =
{
type: ContainerRequestType.Redrop,
newContainer,
moveableToReplace,
oldMoveableFromNewMoveable: k_seedFromGadget,
};
this.activeContainer.sendEvent( req );
}
}
public onUpdate( callback: () => void ): void
{
this.entityCallback = callback;
}
public get child(): EndpointAddr
{
return this.activeContainer?.peer;
}
public render(): JSX.Element
{
if( this.contentsEpa && this.contentsRested )
{
return <AvTransform rotateX={ -90 } key="seed">
<AvEntityChild child={ this.contentsEpa } key={ endpointAddrToString( this.contentsEpa ) }/>
</AvTransform>
}
else
{
return null;
}
}
}
function findIconOfType( manifest: AardvarkManifest, mimeType: string )
{
if( !manifest.icons )
return null;
for( let icon of manifest.icons )
{
if( icon.type.toLowerCase() == mimeType.toLowerCase() )
{
return icon;
}
}
return null;
}
/** Finds a gadget manifest's glTF model URL if there is one. */
export function findGltfIconFullUrl( gadgetUrl: string, manifest: AardvarkManifest) : null | [ string, ResourceRotation]
{
let model = findIconOfType( manifest, "model/gltf-binary" );
if( model )
{
return [ isUrl( model.src ) ? model.src
: gadgetUrl + "/" + model.src, model.rotation ];
}
else
{
return null;
}
}
/** Renders Aardvark components for a gadget's glTF model. */
export function renderGadgetIcon( gadgetUrl: string, manifest: AardvarkManifest, radius: number )
{
let modelInfo = findGltfIconFullUrl( gadgetUrl, manifest );
if( modelInfo )
{
const [ modelUrl, rotation ] = modelInfo;
return <AvTransform rotateX={ rotation?.x } rotateY={ rotation?.y } rotateZ={ rotation?.z }>
<AvModel uri= { modelUrl } scaleToFit={ { x: radius, y: radius, z: radius } }/>
</AvTransform>;
}
else
{
return <AvModel uri= { g_builtinModelError } scaleToFit={ { x: radius, y: radius, z: radius } }/>;
}
}
/** A grabbable control that causes the grabber to grab a new
* instance of a gadget instead of the control itself.
*/
export class AvGadgetSeed extends React.Component< AvGadgetSeedProps, AvGadgetSeedState >
{
private moveableComponent = new MoveableComponent( this.onMoveableUpdate );
private containerComponent = new GadgetSeedContainerComponent( this.onNewChild, this.onLostChild );
private refContainer = React.createRef<AvComposedEntity>();
private refSeed = React.createRef<AvComposedEntity>();
constructor( props:any )
{
super( props );
this.state = { phase: GadgetSeedPhase.Idle };
}
private seedPhaseToHighlight( phase: GadgetSeedPhase )
{
switch( phase )
{
default:
case GadgetSeedPhase.Idle:
return GadgetSeedHighlight.Idle;
case GadgetSeedPhase.GrabberNearby:
return GadgetSeedHighlight.GrabberNearby;
case GadgetSeedPhase.WaitingForGadgetStart:
case GadgetSeedPhase.WaitingForRedropToFinish:
case GadgetSeedPhase.WaitingForRegrab:
return GadgetSeedHighlight.GadgetStarting;
case GadgetSeedPhase.Menu:
return GadgetSeedHighlight.Menu;
}
}
componentDidUpdate( prevProps: AvGadgetSeedProps, prevState: AvGadgetSeedState )
{
if( this.state.phase != prevState.phase )
{
console.log( `Seed transitioned to ${ GadgetSeedPhase[ this.state.phase ] }` );
}
let oldHighlight = this.seedPhaseToHighlight( prevState.phase );
let newHighlight = this.seedPhaseToHighlight( this.state.phase );
if( oldHighlight != newHighlight )
{
console.log( `Changing from ${ GadgetSeedHighlight[ oldHighlight ] } to ${ GadgetSeedHighlight[ newHighlight ] }` );
this.props.highlightCallback?.( newHighlight );
}
}
componentWillUnmount()
{
if( this.state.phase != GadgetSeedPhase.Idle )
{
this.props.highlightCallback?.( GadgetSeedHighlight.Idle );
}
}
@bind
private async onMoveableUpdate()
{
console.log( `onMoveableUpdate ${ GadgetSeedPhase[ this.state.phase ] } `
+`${ MoveableComponentState[ this.moveableComponent.state ]}` );
switch( this.state.phase )
{
case GadgetSeedPhase.Idle:
switch( this.moveableComponent.state )
{
case MoveableComponentState.GrabberNearby:
this.setState( { phase: GadgetSeedPhase.GrabberNearby } );
break;
case MoveableComponentState.Menu:
this.setState( { phase: GadgetSeedPhase.Menu } );
break;
}
break;
case GadgetSeedPhase.Menu:
case GadgetSeedPhase.GrabberNearby:
switch( this.moveableComponent.state )
{
case MoveableComponentState.Grabbed:
this.setState( { phase: GadgetSeedPhase.WaitingForGadgetStart } );
this.startGadget();
break;
case MoveableComponentState.Idle:
this.setState( { phase: GadgetSeedPhase.Idle } );
break;
case MoveableComponentState.GrabberNearby:
this.setState( { phase: GadgetSeedPhase.GrabberNearby } );
break;
case MoveableComponentState.InContainer:
// This means we missed the grab and were already dropped?
break;
case MoveableComponentState.Menu:
this.setState( { phase: GadgetSeedPhase.Menu } );
break;
}
break;
case GadgetSeedPhase.WaitingForGadgetStart:
// in this state we're mostly going to ignore moveable state changes.
// Our next internal state change will be driven by the gadget starting
break;
}
}
@bind
private onNewChild()
{
console.log( "onNewChild" );
switch ( this.moveableComponent.state )
{
case MoveableComponentState.Grabbed:
console.log( `regrabbing new gadget moveable ${ endpointAddrToString( this.containerComponent.child ) }` );
this.triggerRegrab();
break;
case MoveableComponentState.GrabberNearby:
case MoveableComponentState.InContainer:
console.log( `redropping new gadget moveable ${ endpointAddrToString( this.containerComponent.child ) }` );
this.containerComponent.redrop( this.moveableComponent.parent, this.refSeed.current.globalId );
this.setState( { phase: GadgetSeedPhase.WaitingForRedropToFinish } );
break;
case MoveableComponentState.Idle:
console.log( "How did we get all the way back to idle?" );
break;
}
}
@bind
private onLostChild()
{
console.log( "lost child in container" );
switch( this.state.phase )
{
case GadgetSeedPhase.WaitingForRedropToFinish:
case GadgetSeedPhase.WaitingForRegrab:
// we've been dropped
this.setState( { phase: GadgetSeedPhase.Idle } );
this.moveableComponent.reset();
break;
}
}
private async startGadget()
{
let locks: InitialInterfaceLock[] =
[
{
iface: MoveableComponent.containerInterface,
receiver: this.refContainer.current.globalId,
}
];
if( this.props.interfaceLocks )
{
locks = locks.concat( this.props.interfaceLocks );
}
let res = await AvGadget.instance().startGadget( this.props.gadgetUrl, locks );
if( !res.success )
{
this.setState( { phase: GadgetSeedPhase.Idle } );
throw new Error( "startGadget failed" );
}
}
private triggerRegrab()
{
this.moveableComponent.triggerRegrab( this.containerComponent.child, k_seedFromGadget );
this.setState( { phase: GadgetSeedPhase.WaitingForRegrab } );
}
@bind public onGadgetStarted( success: boolean, mainGrabbableId: string )
{
console.log( "main grabbable id was "+ mainGrabbableId );
}
public render()
{
let radius = this.props.radius ? this.props.radius : 0.1;
let scale:number;
switch( this.state.phase )
{
case GadgetSeedPhase.Idle:
scale = 1.0;
break;
default:
scale = 1.25;
break;
}
let volume: AvVolume;
if( this.state.phase == GadgetSeedPhase.WaitingForRedropToFinish )
{
// we want to not match against the container we're telling our child to redrop into
volume = { type: EVolumeType.Empty };
}
else if( this.props.customVolume )
{
volume = this.props.customVolume;
}
else
{
volume = { type: EVolumeType.Sphere, radius: radius };
}
let drawIcon = this.state.phase != GadgetSeedPhase.WaitingForRegrab
&& this.state.phase != GadgetSeedPhase.WaitingForRedropToFinish;
return (
<AvComposedEntity components={ [ this.moveableComponent ] } ref={ this.refSeed }
volume={ volume } >
{ drawIcon &&
<AvTransform uniformScale={ scale }>
{ this.props.customAppearance }
{ !this.props.customAppearance &&
renderGadgetIcon( this.props.gadgetUrl, this.props.manifest, radius ) }
</AvTransform>
}
<AvComposedEntity components={ [ this.containerComponent ] }
ref={ this.refContainer } volume={ { type: EVolumeType.Empty } } />
</AvComposedEntity> );
}
}