<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "https://www.web3d.org/specifications/x3d-3.0.dtd">
<X3D profile='Immersive' version='3.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='https://www.web3d.org/specifications/x3d-3.0.xsd'>
  <head>
    <meta content='HeadsUpDisplayPrototype.x3d' name='title'/>
    <meta content='Generic Heads Up Display (HUD) prototype to keep children on screen.' name='description'/>
    <meta content='Don Brutzman' name='creator'/>
    <meta content='9 November 2003' name='created'/>
    <meta content='28 November 2019' name='modified'/>
    <meta content='HUD Heads Up Display' name='subject'/>
    <meta content='https://www.web3d.org/x3d/content/examples/Savage/Tools/HeadsUpDisplays/HeadsUpDisplayPrototype.x3d' name='identifier'/>
    <meta content='X3D-Edit 3.2, https://www.web3d.org/x3d/tools/X3D-Edit' name='generator'/>
    <meta content='../../license.html' name='license'/>
  </head>
  <Scene>
    <WorldInfo title='HeadsUpDisplayPrototype.x3d'/>
    <ProtoDeclare appinfo='HeadsUpDisplay positions child geometry in screen space, movable by the user' name='HeadsUpDisplay'>
      <ProtoInterface>
        <field accessType='inputOutput' appinfo='Displayed subscene positioned as a HUD.' name='children' type='MFNode'>
          <!-- default is null array of nodes -->
        </field>
        <field accessType='inputOutput' appinfo='Additional HUD geometry which can be touched and dragged for repositioning. If this geometry goes offscreen (perhaps due to screen resizing) then it snaps back to original position.' name='dragChildren' type='MFNode'>
          <!-- default is null array of nodes -->
        </field>
        <field accessType='initializeOnly' appinfo='Modified screen location and distance (for size).' name='locationOffset' type='SFVec3f' value='-2 -2 0'/>
        <field accessType='initializeOnly' appinfo='Enable/disable console output for troubleshooting.' name='traceEnabled' type='SFBool' value='false'/>
      </ProtoInterface>
      <ProtoBody>
        <Group>
          <ProximitySensor DEF='WhereSensor' size='1000000000 1000000000 1000000000'>
            <IS>
              <connect nodeField='center' protoField='locationOffset'/>
            </IS>
          </ProximitySensor>
          <Transform DEF='FixedLocation'>
            <Transform DEF='MovableLocation'>
              <Transform DEF='LocationOffset'>
                <IS>
                  <connect nodeField='translation' protoField='locationOffset'/>
                </IS>
                <Transform translation='0 0 -10'>
                  <Group>
                    <IS>
                      <connect nodeField='children' protoField='children'/>
                    </IS>
                  </Group>
                  <Group DEF='PlaneMovementSensorGroup'>
                    <Group DEF='DragGeometry'>
                      <IS>
                        <connect nodeField='children' protoField='dragChildren'/>
                      </IS>
                    </Group>
                    <PlaneSensor DEF='PlaneMovementSensor' description='click and drag to move interface'>
                      <IS>
                        <connect nodeField='offset' protoField='locationOffset'/>
                      </IS>
                    </PlaneSensor>
                    <VisibilitySensor DEF='MovementVisibilitySensor'/>
                    <Script DEF='VisibilityControlScript'>
                      <field accessType='initializeOnly' name='traceEnabled' type='SFBool'/>
                      <field accessType='initializeOnly' name='isVisible' type='SFBool' value='true'/>
                      <field accessType='initializeOnly' name='planeSensorTranslation' type='SFVec3f' value='0 0 0'/>
                      <field accessType='inputOnly' name='setIsVisible' type='SFBool'/>
                      <field accessType='inputOnly' name='setPlaneSensorIsActive' type='SFBool'/>
                      <field accessType='inputOnly' name='setPlaneSensorTranslation' type='SFVec3f'/>
                      <field accessType='outputOnly' name='translationChanged' type='SFVec3f'/>
                      <field accessType='outputOnly' name='translationOffsetChanged' type='SFVec3f'/>
                      <IS>
                        <connect nodeField='traceEnabled' protoField='traceEnabled'/>
                      </IS>
                      <![CDATA[
ecmascript:

function tracePrint (text)
{
	if (traceEnabled) Browser.println ('[HeadsUpDisplayPrototype VisibilityControlScript] ' + text);
}
function setIsVisible (value, timeStamp)
{
	isVisible = value;
	tracePrint('isVisible=' + value);
}
function setPlaneSensorIsActive (value, timeStamp)
{
	tracePrint('PlaneSensor isActive=' + value);

	if (value == false)
	{
		tracePrint('planeSensorTranslation=' + planeSensorTranslation);
		if (isVisible)
		{
			translationChanged = planeSensorTranslation;
		}
		else
		{
			// fell off screen, reset to center
			translationChanged = new SFVec3f(0, 0, 0);
			translationOffsetChanged  = new SFVec3f(0, 0, 0);
		}
	}
}
function setPlaneSensorTranslation (value, timeStamp)
{
	planeSensorTranslation = value;
	tracePrint('planeSensorTranslation=' + value);
}
]]>
                    </Script>
                    <ROUTE fromField='isActive' fromNode='PlaneMovementSensor' toField='setPlaneSensorIsActive' toNode='VisibilityControlScript'/>
                    <ROUTE fromField='translation_changed' fromNode='PlaneMovementSensor' toField='setPlaneSensorTranslation' toNode='VisibilityControlScript'/>
                    <ROUTE fromField='isActive' fromNode='MovementVisibilitySensor' toField='setIsVisible' toNode='VisibilityControlScript'/>
                  </Group>
                </Transform>
              </Transform>
              <ROUTE fromField='translation_changed' fromNode='PlaneMovementSensor' toField='set_translation' toNode='MovableLocation'/>
              <ROUTE fromField='translationChanged' fromNode='VisibilityControlScript' toField='set_translation' toNode='MovableLocation'/>
              <ROUTE fromField='translationOffsetChanged' fromNode='VisibilityControlScript' toField='set_offset' toNode='PlaneMovementSensor'/>
            </Transform>
          </Transform>
          <ROUTE fromField='position_changed' fromNode='WhereSensor' toField='set_translation' toNode='FixedLocation'/>
          <ROUTE fromField='orientation_changed' fromNode='WhereSensor' toField='set_rotation' toNode='FixedLocation'/>
        </Group>
      </ProtoBody>
    </ProtoDeclare>
    <!-- ==================== -->
    <Background groundColor='0.1 0.1 0.3' skyColor='0.5 0.5 0.1'/>
    <Anchor description='HeadsUpDisplayExample' parameter='"target=_blank"' url='"HeadsUpDisplayExample.x3d" "https://www.web3d.org/x3d/content/examples/Savage/Tools/HeadsUpDisplays/HeadsUpDisplayrExample.x3d" "HeadsUpDisplayExample.wrl" "https://www.web3d.org/x3d/content/examples/Savage/Tools/HeadsUpDisplays/HeadsUpDisplayExample.wrl"'>
      <Shape>
        <Appearance>
          <Material diffuseColor='0 1 1' emissiveColor='0 1 1'/>
        </Appearance>
        <Text string='"../../../X3dForWebAuthors/Chapter14Prototypes/HeadsUpDisplayPrototype.x3d" "is a Prototype definition file." "" "To see an example scene using this node" "click this text to view" "HeadsUpDisplayExample.x3d"'>
          <FontStyle justify='"MIDDLE" "MIDDLE"' size='0.8'/>
        </Text>
      </Shape>
    </Anchor>
  </Scene>
</X3D>