<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.3//EN" "https://www.web3d.org/specifications/x3d-3.3.dtd">
<X3D profile='Immersive' version='3.3' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='https://www.web3d.org/specifications/x3d-3.3.xsd'>
  <head>
    <meta content='ScriptComplexStateEvents.x3d' name='title'/>
    <meta content='A lamp with 4 intensities (off low medium high) and a push-button switcher, all controlled by a Script node.' name='description'/>
    <meta content='Leonard Daly and Don Brutzman' name='creator'/>
    <meta content='10 June 2006' name='created'/>
    <meta content='20 October 2019' name='modified'/>
    <meta content='https://X3dGraphics.com' name='reference'/>
    <meta content='https://www.web3d.org/x3d/content/examples/X3dResources.html' name='reference'/>
    <meta content='Copyright 2006, Daly Realism and Don Brutzman' name='rights'/>
    <meta content='X3D book, X3D graphics, X3D-Edit, http://www.x3dGraphics.com' name='subject'/>
    <meta content='https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter09EventUtilitiesScripting/ScriptComplexStateEvents.x3d' name='identifier'/>
    <meta content='X3D-Edit 3.3, https://www.web3d.org/x3d/tools/X3D-Edit' name='generator'/>
    <meta content='../license.html' name='license'/>
  </head>
  <Scene>
    <WorldInfo title='ScriptComplexStateEvents.x3d'/>
    <Background skyColor='1 1 1'/>
    <Viewpoint description='Control 3-way lamp' orientation='-0.993 0.101 -0.063 1.06' position='0.06 3.63 2.29'/>
    <Transform DEF='PushBox' translation='-2 0 0'>
      <Transform DEF='ControlBox'>
        <Shape>
          <Appearance>
            <Material diffuseColor='0 .8 0'/>
          </Appearance>
          <Box size='1 .5 1'/>
        </Shape>
      </Transform>
      <Transform DEF='ControlButton' translation='0 .25 0'>
        <TouchSensor DEF='ButtonTouch' description='Push button to change state'/>
        <Shape>
          <Appearance>
            <Material diffuseColor='1 0 0'/>
          </Appearance>
          <Cylinder DEF='Button' bottom='false' height='.5' radius='.25'/>
        </Shape>
        <PositionInterpolator DEF='ButtonMover' key='0 .5 1' keyValue='0 .25 0 0 .05 0 0 .25 0'/>
        <TimeSensor DEF='ButtonTimer' cycleInterval='.3' stopTime='1'/>
        <ROUTE fromField='touchTime' fromNode='ButtonTouch' toField='startTime' toNode='ButtonTimer'/>
        <ROUTE fromField='fraction_changed' fromNode='ButtonTimer' toField='set_fraction' toNode='ButtonMover'/>
        <ROUTE fromField='value_changed' fromNode='ButtonMover' toField='translation' toNode='ControlButton'/>
      </Transform>
    </Transform>
    <Transform DEF='Lamp' translation='1 0 0'>
      <Transform DEF='Base'>
        <Shape>
          <Appearance>
            <Material/>
          </Appearance>
          <Cylinder height='.5' radius='.25'/>
        </Shape>
      </Transform>
      <Transform DEF='Bulb' translation='0 .5 0'>
        <Shape>
          <Appearance>
            <!-- be sure to match initial color of script so that toggling is smooth and predictable -->
            <Material DEF='LightBulbMaterial' diffuseColor='0.2 0.2 0.2'/>
          </Appearance>
          <Sphere radius='.5'/>
        </Shape>
      </Transform>
    </Transform>
    <Script DEF='ControlScript' url='"ScriptComplexStateEvents.js" "https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter09EventUtilitiesScripting/ScriptComplexStateEvents.js"'>
      <field accessType='inputOnly' name='buttonMotionDone' type='SFBool'/>
      <field accessType='inputOutput' appinfo='the buttonPushCount value toggles 0..1..2..3 to represent four states, off/low/medium/high' name='buttonPushCount' type='SFInt32' value='0'/>
      <field accessType='outputOnly' appinfo='color of lightBulb indicates off/low/medium/high' name='lightBulbColor' type='SFColor'/>
    </Script>
    <ROUTE fromField='isActive' fromNode='ButtonTimer' toField='buttonMotionDone' toNode='ControlScript'/>
    <ROUTE fromField='lightBulbColor' fromNode='ControlScript' toField='diffuseColor' toNode='LightBulbMaterial'/>
    <Transform translation='-1 -2 0'>
      <Billboard axisOfRotation='0 0 0'>
        <Switch DEF='TextSwitch' whichChoice='0'>
          <!-- States: 0=Off, 1=Low, 2=Medium, 3=High -->
          <Shape>
            <Text string='"Off"'>
              <FontStyle DEF='TextStyle' family='"SANS"' justify='"BEGIN" "MIDDLE"' style='BOLD'/>
            </Text>
            <Appearance DEF='OffAppearance'>
              <Material diffuseColor='0.6 0.6 0.6'/>
            </Appearance>
          </Shape>
          <Shape>
            <Text string='"Low"'>
              <FontStyle USE='TextStyle'/>
            </Text>
            <Appearance DEF='LowAppearance'>
              <Material diffuseColor='1 0.6 0.6'/>
            </Appearance>
          </Shape>
          <Shape>
            <Text string='"Medium"'>
              <FontStyle USE='TextStyle'/>
            </Text>
            <Appearance DEF='MediumAppearance'>
              <Material diffuseColor='0.6 1 0.6'/>
            </Appearance>
          </Shape>
          <Shape>
            <Text string='"High"'>
              <FontStyle USE='TextStyle'/>
            </Text>
            <Appearance DEF='HighAppearance'>
              <Material diffuseColor='0.6 0.6 1'/>
            </Appearance>
          </Shape>
        </Switch>
      </Billboard>
    </Transform>
    <ROUTE fromField='buttonPushCount' fromNode='ControlScript' toField='whichChoice' toNode='TextSwitch'/>
  </Scene>
</X3D>