Skip to content

Commit

Permalink
Added renderAardvarkRoot function
Browse files Browse the repository at this point in the history
This function makes it easier to deal with browser vs. gadget modes and handles waiting for the gadget to connect before creating anything.
  • Loading branch information
JoeLudwig committed Dec 9, 2020
1 parent 1be1694 commit 7ce30df
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
52 changes: 52 additions & 0 deletions packages/aardvark-react/src/aardvark_render.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Av } from '@aardvarkxr/aardvark-shared';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { AvGadget } from './aardvark_gadget';
import { DefaultLanding } from './default_landing_page';


/** Uses ReactDOM to render the appropriate main component. If The gadget is running in Aardvark,
* this function will wait for it to connect to the local Aardvark server and then render gadgetMain.
* If the gadget is running in another browser this function will render browserMain.
*
* @param element the element to render on or the ID of an element
* @param gadgetMain The React component to render when running in Aardvark.
* @param browserMain The React component to render when running in another browser. If this is undefined
* an instance of DefaultLanding will be rendered.
*/
export async function renderAardvarkRoot( element: HTMLElement | string,
gadgetMain: JSX.Element | ( () => JSX.Element ), browserMain?: JSX.Element | ( () => JSX.Element ) )
{
if( typeof element == "string" )
{
element = document.getElementById( element );
}

let main: JSX.Element;
if( !Av() )
{
if( !browserMain )
{
browserMain = <DefaultLanding/>;
}
else if( typeof browserMain == "function" )
{
browserMain = browserMain();
}

main = browserMain;
}
else
{
await AvGadget.instance().waitForConnect();

if( typeof gadgetMain == "function" )
{
gadgetMain = gadgetMain();
}
main = gadgetMain;
}

ReactDOM.render( main, element );
}

1 change: 1 addition & 0 deletions packages/aardvark-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './aardvark_panel';
export * from './aardvark_panelanchor';
export * from './aardvark_parent_transform';
export * from './aardvark_primitive';
export * from './aardvark_render';
export * from './aardvark_standard_grabbable';
export * from './aardvark_transform';
export * from './aardvark_utils';
Expand Down
5 changes: 4 additions & 1 deletion src/avrenderer/vrmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ void CVRManager::init()
CVRManager::~CVRManager()
{
destroyVargglesOverlay();
vr::VR_Shutdown();
if ( vr::VRSystem() )
{
vr::VR_Shutdown();
}
}


Expand Down
6 changes: 3 additions & 3 deletions websrc/test_panel/src/test_panel_main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AvGadget, AvMessagebox, AvPanel, AvStandardGrabbable, AvTransform, DefaultLanding, GrabbableStyle } from '@aardvarkxr/aardvark-react';
import { AvGadget, AvMessagebox, AvPanel, AvStandardGrabbable, AvTransform, DefaultLanding, GrabbableStyle, renderAardvarkRoot } from '@aardvarkxr/aardvark-react';
import { Av, EAction, EHand, g_builtinModelBox, InitialInterfaceLock } from '@aardvarkxr/aardvark-shared';
import bind from 'bind-decorator';
import * as React from 'react';
Expand Down Expand Up @@ -236,5 +236,5 @@ class TestPanel extends React.Component< {}, TestPanelState >

}

let main = Av() ? <TestPanel/> : <DefaultLanding/>;
ReactDOM.render( main, document.getElementById( "root" ) );

renderAardvarkRoot( "root", <TestPanel/> );
5 changes: 2 additions & 3 deletions websrc/whiteboard/src/whiteboard_main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ShowGrabbableChildren, NetworkedItemComponent, ActiveInterface, AvComposedEntity, AvGadget, AvInterfaceEntity, AvLine, AvModel, AvPrimitive, AvStandardGrabbable, AvTransform, MoveableComponent, PrimitiveType, PrimitiveYOrigin, PrimitiveZOrigin, DefaultLanding, RemoteItemComponent, MoveableComponentState, k_remoteGrabbableInterface, GrabbableStyle } from '@aardvarkxr/aardvark-react';
import { ShowGrabbableChildren, NetworkedItemComponent, ActiveInterface, AvComposedEntity, AvGadget, AvInterfaceEntity, AvLine, AvModel, AvPrimitive, AvStandardGrabbable, AvTransform, MoveableComponent, PrimitiveType, PrimitiveYOrigin, PrimitiveZOrigin, DefaultLanding, RemoteItemComponent, MoveableComponentState, k_remoteGrabbableInterface, GrabbableStyle, renderAardvarkRoot } from '@aardvarkxr/aardvark-react';
import { Av, AvNodeTransform, AvVector, AvVolume, endpointAddrToString, EVolumeType, g_builtinModelBox, InitialInterfaceLock, infiniteVolume } from '@aardvarkxr/aardvark-shared';
import { vec2 } from '@tlaukkan/tsm';
import bind from 'bind-decorator';
Expand Down Expand Up @@ -516,5 +516,4 @@ class Whiteboard extends React.Component< {}, WhiteboardState >
}


let main = Av() ? <Whiteboard/> : <DefaultLanding/>;
ReactDOM.render( main, document.getElementById( "root" ) );
renderAardvarkRoot( document.getElementById( "root" ), () => <Whiteboard/>, () => <DefaultLanding/> );

0 comments on commit 7ce30df

Please sign in to comment.