<?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='SingleTypeConversionPrototype.x3d' name='title'/>
    <meta content='Converts from a single typed value to various other types of values.' name='description'/>
    <meta content='Don Brutzman, Mike Hunsberger, Jane Wu' name='creator'/>
    <meta content='15 June 2001' name='created'/>
    <meta content='15 October 2023' name='modified'/>
    <meta content='type conversion' name='subject'/>
    <meta content='http://www.cs.brown.edu/~gss/VRML98/paper.rev.html' name='reference'/>
    <meta content='http://www.cs.brown.edu/~gss/vrml/hprotos.wrl' name='reference'/>
    <meta content='http://www.cs.brown.edu/~gss/vrml/hotpot.zip' name='reference'/>
    <meta content='https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/SingleTypeConversionPrototype.x3d' name='identifier'/>
    <meta content='X3D-Edit 4.0, https://www.web3d.org/x3d/tools/X3D-Edit' name='generator'/>
    <meta content='../../license.html' name='license'/>
  </head>
  <Scene>
    <WorldInfo title='SingleTypeConversionPrototype.x3d'/>
    <ProtoDeclare appinfo='SingleTypeConversion converts from a single typed value to various other types of values' name='SingleTypeConversion'>
      <ProtoInterface>
        <field accessType='initializeOnly' appinfo='decimalPlaces is the number of significant digits after the decimal point, use -1 to indicate no round off' name='decimalPlaces' type='SFInt32' value='-1'/>
        <field accessType='inputOnly' name='setDecimalPlaces' type='SFInt32'/>
        <field accessType='inputOnly' name='BooleanValue' type='SFBool'/>
        <field accessType='inputOnly' name='FloatValue' type='SFFloat'/>
        <field accessType='inputOnly' name='IntegerValue' type='SFInt32'/>
        <field accessType='inputOnly' name='TimeValue' type='SFTime'/>
        <field accessType='outputOnly' name='BooleanResult' type='SFBool'/>
        <field accessType='outputOnly' name='FloatResult' type='SFFloat'/>
        <field accessType='outputOnly' name='IntegerResult' type='SFInt32'/>
        <field accessType='outputOnly' name='TimeResult' type='SFTime'/>
        <field accessType='outputOnly' name='StringResult' type='SFString'/>
        <field accessType='outputOnly' name='StringsResult' type='MFString'/>
      </ProtoInterface>
      <ProtoBody>
        <Script DEF='ConversionScript'>
          <field accessType='initializeOnly' name='decimalPlaces' type='SFInt32'/>
          <field accessType='inputOnly' name='setDecimalPlaces' type='SFInt32'/>
          <field accessType='inputOnly' name='SFBoolValue' type='SFBool'/>
          <field accessType='inputOnly' name='SFFloatValue' type='SFFloat'/>
          <field accessType='inputOnly' name='SFInt32Value' type='SFInt32'/>
          <field accessType='inputOnly' name='SFTimeValue' type='SFTime'/>
          <field accessType='outputOnly' name='SFBoolResult' type='SFBool'/>
          <field accessType='outputOnly' name='SFInt32Result' type='SFInt32'/>
          <field accessType='outputOnly' name='SFFloatResult' type='SFFloat'/>
          <field accessType='outputOnly' name='SFTimeResult' type='SFTime'/>
          <field accessType='outputOnly' name='SFStringResult' type='SFString'/>
          <field accessType='outputOnly' name='MFStringResult' type='MFString'/>
          <field accessType='initializeOnly' appinfo='local temporary variable' name='newIntegerValue' type='SFInt32' value='0'/>
          <field accessType='initializeOnly' appinfo='local temporary variable' name='newFloatValue' type='SFFloat' value='0.0'/>
          <field accessType='initializeOnly' appinfo='local temporary variable' name='newTimeValue' type='SFFloat' value='0.0'/>
          <field accessType='initializeOnly' appinfo='local variable defined as field for persistence' name='roundOffFactor' type='SFFloat' value='1'/>
          <IS>
            <connect nodeField='decimalPlaces' protoField='decimalPlaces'/>
            <connect nodeField='setDecimalPlaces' protoField='setDecimalPlaces'/>
            <connect nodeField='SFBoolValue' protoField='BooleanValue'/>
            <connect nodeField='SFFloatValue' protoField='FloatValue'/>
            <connect nodeField='SFInt32Value' protoField='IntegerValue'/>
            <connect nodeField='SFTimeValue' protoField='TimeValue'/>
            <connect nodeField='SFBoolResult' protoField='BooleanResult'/>
            <connect nodeField='SFInt32Result' protoField='IntegerResult'/>
            <connect nodeField='SFFloatResult' protoField='FloatResult'/>
            <connect nodeField='SFTimeResult' protoField='TimeResult'/>
            <connect nodeField='SFStringResult' protoField='StringResult'/>
            <connect nodeField='MFStringResult' protoField='StringsResult'/>
          </IS>
          <![CDATA[
ecmascript:

function initialize()
{
	if (decimalPlaces <= -1)
	{
		roundOffFactor = 1;
	}
	else
	{
		roundOffFactor = Math.pow(10, decimalPlaces);
	}
//	Browser.println ('[SingleTypeConversion initialize()] decimalPlaces=' + decimalPlaces + ', roundOffFactor=' + roundOffFactor);
}
function setDecimalPlaces (value, timeStamp)
{
	decimalPlaces = value;
	initialize ();
}
function SFBoolValue(value, timeStamp)
{
	if (value)
	{
		SFBoolResult   = value;
		SFInt32Result  = 1;
		SFFloatResult  = 1.0;
		SFTimeResult   = timeStamp;
		SFStringResult = '1';
		MFStringResult = new MFString ('1');
	}
	else
	{
		SFBoolResult   = value;
		SFInt32Result  = 0;
		SFFloatResult  = 0.0;
		SFTimeResult   = -1;
		SFStringResult = '0';
		MFStringResult = new MFString ('0');
	}
}
function SFInt32Value(value, timeStamp)
{
	if (value == 0)
		SFBoolResult = false;
	else
		SFBoolResult = true;

	newIntegerValue = value;
        SFInt32Result   = newIntegerValue;
	SFFloatResult   = newIntegerValue * 1.0;
	SFTimeResult    = newIntegerValue * 1.0;
	SFStringResult  = newIntegerValue.toString(); // per EcmaScript specification
        if (SFStringResult == null)
        {
            SFStringResult = newIntegerValue; // try direct approach
        }
	MFStringResult = new MFString(SFStringResult);
}
function SFFloatValue(value, timeStamp)
{
	if (value == 0)
		SFBoolResult = false;
	else
		SFBoolResult = true;

        if (decimalPlaces >= 0)
	     newFloatValue = Math.round(value*roundOffFactor) / roundOffFactor;
	else newFloatValue = value;

	SFInt32Result  = Math.round(newFloatValue);
	SFFloatResult  = newFloatValue;
	SFTimeResult   = newFloatValue;
	SFStringResult = newFloatValue.toString(); // per EcmaScript specification
        if (SFStringResult == null)
        {
          SFStringResult = newFloatValue; // try direct approach
        }
	MFStringResult = new MFString(SFStringResult);
//      Browser.println ('value=' + value + ', SFFloatResult=' + SFFloatResult);
}
function SFTimeValue(value, timeStamp)
{
	if (value == 0.0)
		SFBoolResult = false;
	else
		SFBoolResult = true;

	if (decimalPlaces >= 0)
		newTimeValue = Math.round(value*roundOffFactor) / roundOffFactor;
	else	newTimeValue = value;

	SFInt32Result  = Math.round(newTimeValue);
	SFFloatResult  = newTimeValue;
	SFTimeResult   = newTimeValue;
	SFStringResult = newTimeValue.toString(); // per EcmaScript specification
        if (SFStringResult == null)
        {
            SFStringResult = newTimeValue; // try direct approach
        }
	MFStringResult = new MFString(SFStringResult);
}
]]>
        </Script>
      </ProtoBody>
    </ProtoDeclare>
    <!-- ==================== -->
    <Anchor description='SingleTypeConversion Example' url='"SingleTypeConversionExample.x3d" "../../../Savage/Tools/Authoring/SingleTypeConversionExample.x3d" "https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/SingleTypeConversionExample.x3d" "SingleTypeConversionExample.wrl" "../../../Savage/Tools/Authoring/SingleTypeConversionExample.wrl" "https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/SingleTypeConversionExample.wrl"'>
      <Shape>
        <Appearance>
          <Material diffuseColor='0 1 1' emissiveColor='0 1 1'/>
        </Appearance>
        <Text string='"SingleTypeConversionPrototype" "is a Prototype definition file." "" "To see an example scene" "select this text and view" "SingleTypeConversionExample"'>
          <FontStyle justify='"MIDDLE" "MIDDLE"' size='0.8'/>
        </Text>
      </Shape>
    </Anchor>
  </Scene>
</X3D>