<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://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='http://www.web3d.org/specifications/x3d-3.0.xsd'>
  <head>
    <meta content='BooleanSequencerPrototype.x3d' name='title'/>
    <meta content='BooleanSequencer is modeled after ScalarInterpolator and generates true or false values.' name='description'/>
    <meta content='Don Brutzman, Estuko Lippi, Jeff Weekley, Jane Wu' name='creator'/>
    <meta content='7 August 2001' name='created'/>
    <meta content='22 December 2011' name='modified'/>
    <meta content='http://www.web3d.org/technicalinfo/specifications/vrml97/part1/nodesRef.html#ScalarInterpolator' name='reference'/>
    <meta content='boolean sequencer' name='subject'/>
    <meta content='http://www.web3d.org/x3d/content/examples/Basic/development/BooleanSequencerPrototype.x3d' name='identifier'/>
    <meta content='X3D-Edit 3.2, https://savage.nps.edu/X3D-Edit' name='generator'/>
    <meta content='../license.html' name='license'/>
  </head>
  <Scene>
    <ProtoDeclare name='BooleanSequencer'>
      <ProtoInterface>
        <!-- Regular interpolator-style input -->
        <field accessType='inputOnly' appinfo='typical range [0..1]' name='set_fraction' type='SFFloat'/>
        <field accessType='inputOnly' name='set_key' type='MFFloat'/>
        <field accessType='inputOutput' appinfo='Array sequentially increasing typically [0..1]. Must have the same number of keys as keyValues.' name='key' type='MFFloat'>
          <!-- NULL initialization value -->
        </field>
        <field accessType='outputOnly' name='key_changed' type='MFFloat'/>
        <field accessType='inputOnly' name='set_keyValue' type='MFBool'/>
        <field accessType='inputOutput' appinfo='Array of true|false values. Must have the same number of keys as keyValues.' name='keyValue' type='MFBool'>
          <!-- NULL initialization value -->
        </field>
        <field accessType='outputOnly' name='keyValue_changed' type='MFBool'/>
        <!-- Regular interpolator-style output -->
        <field accessType='outputOnly' name='value_changed' type='SFBool'/>
        <!-- Utility methods -->
        <field accessType='inputOnly' name='previous' type='SFBool'/>
        <field accessType='inputOnly' name='next' type='SFBool'/>
      </ProtoInterface>
      <ProtoBody>
        <Group>
          <ScalarInterpolator DEF='KeyHolder'>
            <IS>
              <connect nodeField='key' protoField='key'/>
            </IS>
          </ScalarInterpolator>
          <Anchor DEF='KeyValueHolder'>
            <IS>
              <connect nodeField='description' protoField='keyValue'/>
            </IS>
          </Anchor>
          <Script DEF='SequencerScript' directOutput='true'>
            <!-- Regular interpolator-style input -->
            <field accessType='inputOnly' appinfo='typical range [0..1]' name='set_fraction' type='SFFloat'/>
            <field accessType='inputOnly' appinfo='Array sequentially increasing typically [0..1]. Must have the same number of keys as keyValues.' name='set_key' type='MFFloat'/>
            <field accessType='initializeOnly' name='keyHolderNode' type='SFNode'>
              <ScalarInterpolator USE='KeyHolder'/>
            </field>
            <field accessType='outputOnly' name='key_changed' type='MFFloat'/>
            <field accessType='inputOnly' appinfo='Array of true or false values. Must have the same number of keys as keyValues.' name='set_keyValue' type='MFBool'/>
            <field accessType='initializeOnly' name='keyValueHolderNode' type='SFNode'>
              <Anchor USE='KeyValueHolder'/>
            </field>
            <field accessType='outputOnly' name='keyValue_changed' type='MFBool'/>
            <!-- Regular interpolator-style output -->
            <field accessType='outputOnly' name='value_changed' type='SFBool'/>
            <!-- Utility methods -->
            <field accessType='inputOnly' name='previous' type='SFBool'/>
            <field accessType='inputOnly' name='next' type='SFBool'/>
            <!-- Script-specific interfaces, not needed for node definition -->
            <field accessType='initializeOnly' name='traceEnabled' type='SFBool' value='false'/>
            <field accessType='initializeOnly' name='keyValueArray' type='MFInt32'>
              <!-- NULL initialization value -->
            </field>
            <field accessType='initializeOnly' name='previousFraction' type='SFFloat' value='0.0'/>
            <field accessType='initializeOnly' name='nextIndex' type='SFInt32' value='0'/>
            <field accessType='outputOnly' name='valid' type='SFBool' value='true'/>
            <field accessType='initializeOnly' name='recheckValidity' type='SFBool' value='false'/>
            <field accessType='initializeOnly' appinfo='leftToRight' name='forward' type='SFBool' value='true'/>
            <field accessType='inputOnly' name='key' type='MFFloat'>
              <!-- NULL initialization value -->
            </field>
            <field accessType='inputOutput' name='keyValue' type='MFInt32'>
              <!-- NULL initialization value -->
            </field>
            <IS>
              <connect nodeField='set_fraction' protoField='set_fraction'/>
              <connect nodeField='set_key' protoField='set_key'/>
              <connect nodeField='key_changed' protoField='key_changed'/>
              <connect nodeField='set_keyValue' protoField='set_keyValue'/>
              <connect nodeField='keyValue_changed' protoField='keyValue_changed'/>
              <connect nodeField='value_changed' protoField='value_changed'/>
              <connect nodeField='previous' protoField='previous'/>
              <connect nodeField='next' protoField='next'/>
            </IS>
            <![CDATA[ecmascript:

var key, keyValue;

function initialize()
{
	key      = keyHolderNode.key;
	keyValue = keyValueHolderNode.description;
	tracePrint('key =' + key);
	tracePrint('keyValue =' + keyValue);
	keyValueToKeyValueArray ();
	tracePrint('keyValueArray =' + keyValueArray);

	forward = true;
	tracePrint('Initializing a new BooleanSequencer.  key.length=' + key.length + '; keyValueArray.length=' + keyValueArray.length);
	validityCheck();
}

function keyValueToKeyValueArray ()
{
	tracePrint('keyValueToKeyValueArray starting');
	index = 0;
	complete = false;
	nextString = keyValue.toLowerCase();
	tracePrint('initial nextString=' + nextString);
	tokenCount=0;
	while ((complete != true) && (nextString.length > 0))
	{
		tracePrint('nextString=' + nextString);
		while ((nextString.substring(0,1) == ' ') || (nextString.substring(0,1) == ','))
		       nextString = nextString.slice(1);
		tracePrint('deblanked nextString=' + nextString);
		if (nextString.length == 0)
		{
			tracePrint ('nextString.length == 0');
			complete = true;
		}
		if (nextString.length < 4)
		{
			alwaysPrint ('*** illegal keyValue input=' + nextString);
			valid = false;
			complete = true;
		}
		else if (nextString.substring(0,4) =='true')
		{
			keyValueArray[keyValueArray.length] = 1; // append
			newString = nextString.slice(4);
			nextString = newString;
			tokenCount++;
			tracePrint('found true, nextString=' + nextString + ', tokenCount=' + tokenCount);
		}
		else if (nextString.length < 5)
		{
			alwaysPrint ('*** illegal keyValue input=' + nextString);
			valid = false;
			complete = true;
		}
		else if (nextString.substring(0,5) =='false')
		{
			keyValueArray[keyValueArray.length] = 0; // append
			newString = nextString.slice(5);
			nextString = newString;
			tokenCount++;
			tracePrint('found false, nextString=' + nextString + ', tokenCount=' + tokenCount);
		}
		tracePrint('  intermediate keyValueArray=' + keyValueArray);
	}
	tracePrint('keyValueToKeyValueArray complete');
}

function set_fraction(value, timeStamp)
{
	if (recheckValidity) validityCheck();

	if (!valid) return; //BooleanSequencer ignored

	tracePrint('fraction =' + value);
	//Bounds checking
	if (value < 0)
	{
		alwaysPrint('*** warning: fraction is less than 0.  fraction reset to 0 ***');
		value = 0;
	}
	else if (value > 1)
	{
		alwaysPrint('*** warning: fraction is greater than 1.  fraction reset to 1 ***');
		value = 1;
	}

	//Check animation direction
	if (value < previousFraction && forward == true)
	{
		forward = false;
		nextIndex = nextIndex - 1;
		tracePrint('Animate backward');
	}
	else if (value > previousFraction && forward == false)
	{
		forward = true;
		//nextIndex = 0;
		tracePrint('Animate forward');
	}

	previousFraction = value;

	if (forward == true)
	{
		for (i = nextIndex; i < key.length; i++)
		{
			if (value < key[i])
				return;

			nextIndex = i + 1;
			tracePrint('nextIndex =' + nextIndex);
			if (nextIndex < key.length)
			{
				if (value <= key[nextIndex])
				{
					//Fire event
					if (keyValueArray[nextIndex-1] == 0)
						value_changed = false;
					else
						value_changed = true;
					tracePrint('value_changed eventOut is:' + value_changed);
				}
			}
			else if (nextIndex == key.length)
			{
				//Fire event
				if (keyValueArray[nextIndex-1] == 0)
					value_changed = false;
				else
					value_changed = true;
				tracePrint('value_changed eventOut is:' + value_changed);
			}
			else //nextIndex > key.length
			{
				//nextIndex = 0;
				break;
			}
		}
	}
	else //backward
	{
		for (i = nextIndex; i > 0; i--)
		{
			if (value >= key[i])
				return;

			nextIndex = i - 1;
			tracePrint('nextIndex =' + nextIndex);
			if (nextIndex >= 0)
			{
				if (value >= key[nextIndex])
				{
					//Fire event
					if (keyValueArray[nextIndex] == 0)
						value_changed = false;
					else
						value_changed = true;
					tracePrint('value_changed eventOut is:' + value_changed);
				}
			}
			else //nextIndex < 0
			{
				//nextIndex = key.length;
				break;
			}
		}
	}
}

function set_key(value, timeStamp)
{
	key = value;
	keyHolderNode.key  = key;
	recheckValidity = true;
}

function set_keyValue(value, timeStamp)
{
	keyValue = value;
	keyValueHolderNode.description = keyValue;
	recheckValidity = true;
	keyValueToKeyValueArray ();
	keyValue_changed = keyValue;
}

function validityCheck()
{
	//Check if key & keyValueArray array length matches
	if (key.length != keyValueArray.length)
	{
		alwaysPrint('*** error: key and keyValue arrays must be of the same length.  BooleanSequencer ignored ***');
		valid = false;
		return;
	}

	//Check if key array has values in the range of [0..1] in an increasing order
	if (key[0] < 0 || key[0] > 1)
	{
		alwaysPrint('*** error: key[0] value is NOT in the range of [0..1].  BooleanSequencer ignored ***');
		valid = false;
		return;
	}
	for (i = 1; i < key.length; i++)
	{
		if (key[i] < 0 || key[i] > 1)
		{
			alwaysPrint('*** error: key[' + i + '] value is NOT in the range of [0..1].  BooleanSequencer ignored ***');
			valid = false;
			return;
		}

		if (key[i] <= key [i-1])
		{
			alwaysPrint('*** error: values for key[] array must be listed in an increasing order.  BooleanSequencer ignored ***');
			valid = false;
			return;
		}
	}
	recheckValidity = false;
	key_changed = key;
	return;
}
function previous (SFBoolValue, timestamp)
{
	nextIndex = nextIndex - 1;
	if (nextIndex == 0) nextIndex = key.length - 1;
}
function next (SFBoolValue, timestamp)
{
	nextIndex = nextIndex + 1;
	if (nextIndex == key.length) nextIndex = 0;
}

function tracePrint(outputString)
{
	if (traceEnabled) Browser.print ('[ BooleanSequencer ]' + outputString);
}

function alwaysPrint(outputString)
{
	Browser.print ('[ BooleanSequencer ]' + outputString);
}]]>
          </Script>
        </Group>
      </ProtoBody>
    </ProtoDeclare>
    <!-- ===============Example============== -->
    <Anchor description='BooleanSequencerExample' parameter='"target=_blank"' url='"BooleanSequencerExample.x3d" "http://www.web3d.org/x3d/content/examples/Basic/development/BooleanSequencerExample.x3d" "BooleanSequencerExample.wrl" "http://www.web3d.org/x3d/content/examples/Basic/development/BooleanSequencerExample.wrl"'>
      <Shape>
        <Text string='"BooleanSequencerPrototype" "defines a prototype" "" "Click on this text to see" "BooleanSequencerExample" " scene"'>
          <FontStyle justify='"MIDDLE" "MIDDLE"'/>
        </Text>
        <Appearance>
          <Material diffuseColor='1 1 0.2'/>
        </Appearance>
      </Shape>
    </Anchor>
  </Scene>
</X3D>
