<?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 name='titlecontent='SingleTypeConversionPrototype.x3d'/>
<meta name='descriptioncontent='Converts from a single typed value to various other types of values.'/>
<meta name='creatorcontent='Don Brutzman, Mike Hunsberger, Jane Wu'/>
<meta name='createdcontent='15 June 2001'/>
<meta name='modifiedcontent='14 January 2014'/>
<meta name='subjectcontent='type conversion'/>
<meta name='referencecontent=' http://www.cs.brown.edu/~gss/VRML98/paper.rev.html '/>
<meta name='referencecontent=' http://www.cs.brown.edu/~gss/vrml/hprotos.wrl '/>
<meta name='referencecontent=' http://www.cs.brown.edu/~gss/vrml/hotpot.zip '/>
<meta name='identifiercontent=' https://savage.nps.edu/Savage/Tools/Authoring/SingleTypeConversionPrototype.x3d '/>
<meta name='generatorcontent='X3D-Edit 3.2, https://savage.nps.edu/X3D-Edit'/>
<meta name='licensecontent=' ../../license.html'/>
</head>
<!--

Index for ProtoDeclare definition : SingleTypeConversion

Index for DEF node : ConversionScript
-->
<Scene>
<ProtoDeclare name='SingleTypeConversionappinfo='SingleTypeConversion converts from a single typed value to various other types of values'>
<ProtoInterface>
<field name='decimalPlacestype='SFInt32value='-1accessType='initializeOnly'
 appinfo='decimalPlaces is the number of significant digits after the decimal point, use -1 to indicate no round off'/>

<field name='setDecimalPlacestype='SFInt32accessType='inputOnly'/>
<field name='BooleanValuetype='SFBoolaccessType='inputOnly'/>
<field name='FloatValuetype='SFFloataccessType='inputOnly'/>
<field name='IntegerValuetype='SFInt32accessType='inputOnly'/>
<field name='TimeValuetype='SFTimeaccessType='inputOnly'/>
<field name='BooleanResulttype='SFBoolaccessType='outputOnly'/>
<field name='FloatResulttype='SFFloataccessType='outputOnly'/>
<field name='IntegerResulttype='SFInt32accessType='outputOnly'/>
<field name='TimeResulttype='SFTimeaccessType='outputOnly'/>
<field name='StringResulttype='SFStringaccessType='outputOnly'/>
<field name='StringsResulttype='MFStringaccessType='outputOnly'/>
</ProtoInterface>
<ProtoBody>
<Script DEF='ConversionScript'>
<field name='decimalPlacestype='SFInt32accessType='initializeOnly'/>
<field name='setDecimalPlacestype='SFInt32accessType='inputOnly'/>
<field name='SFBoolValuetype='SFBoolaccessType='inputOnly'/>
<field name='SFFloatValuetype='SFFloataccessType='inputOnly'/>
<field name='SFInt32Valuetype='SFInt32accessType='inputOnly'/>
<field name='SFTimeValuetype='SFTimeaccessType='inputOnly'/>
<field name='SFBoolResulttype='SFBoolaccessType='outputOnly'/>
<field name='SFInt32Resulttype='SFInt32accessType='outputOnly'/>
<field name='SFFloatResulttype='SFFloataccessType='outputOnly'/>
<field name='SFTimeResulttype='SFTimeaccessType='outputOnly'/>
<field name='SFStringResulttype='SFStringaccessType='outputOnly'/>
<field name='MFStringResulttype='MFStringaccessType='outputOnly'/>
<field name='newIntegerValuetype='SFInt32value='0accessType='initializeOnly'
 appinfo='local temporary variable'/>

<field name='newFloatValuetype='SFFloatvalue='0.0accessType='initializeOnly'
 appinfo='local temporary variable'/>

<field name='newTimeValuetype='SFFloatvalue='0.0accessType='initializeOnly'
 appinfo='local temporary variable'/>

<field name='roundOffFactortype='SFFloatvalue='1accessType='initializeOnly'
 appinfo='local variable defined as field for persistence'/>

<IS>
<connect nodeField='decimalPlacesprotoField='decimalPlaces'/>
<connect nodeField='setDecimalPlacesprotoField='setDecimalPlaces'/>
<connect nodeField='SFBoolValueprotoField='BooleanValue'/>
<connect nodeField='SFFloatValueprotoField='FloatValue'/>
<connect nodeField='SFInt32ValueprotoField='IntegerValue'/>
<connect nodeField='SFTimeValueprotoField='TimeValue'/>
<connect nodeField='SFBoolResultprotoField='BooleanResult'/>
<connect nodeField='SFInt32ResultprotoField='IntegerResult'/>
<connect nodeField='SFFloatResultprotoField='FloatResult'/>
<connect nodeField='SFTimeResultprotoField='TimeResult'/>
<connect nodeField='SFStringResultprotoField='StringResult'/>
<connect nodeField='MFStringResultprotoField='StringsResult'/>
</IS>
<![CDATA[
          
ecmascript:

function initialize()
{
	if (decimalPlaces <= -1)
	{
		roundOffFactor = 1;
	}
	else
	{
		roundOffFactor = Math.pow(10, decimalPlaces);
	}
//	Browser.print ('[SingleTypeConversion initialize()] decimalPlaces=' + decimalPlaces + ', roundOffFactor=' + roundOffFactor + '\n');
}
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.print ('value=' + value + ', SFFloatResult=' + SFFloatResult + '\n');
}
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://savage.nps.edu/Savage/Tools/Authoring/SingleTypeConversionExample.x3d" "SingleTypeConversionExample.wrl" "../../../Savage/Tools/Authoring/SingleTypeConversionExample.wrl" "https://savage.nps.edu/Savage/Tools/Authoring/SingleTypeConversionExample.wrl" '>
<Shape>
<Appearance>
<Material diffuseColor='0 1 1emissiveColor='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>
<!--

Index for ProtoDeclare definition : SingleTypeConversion

Index for DEF node : ConversionScript
-->

<!-- Color key: <X3dNode DEF='idName' field='value'/> matches <XmlElement DEF='idName' attribute='value'/>
(Light blue background: behavior node) (Grey background: inserted documentation) (Magenta background: X3D Extensibility)
    <Prototype name='ProtoName'> <field name='fieldName'/> </Prototype> -->

<!-- Additional help information about X3D scenes: X3D Resources, X3D Scene Authoring Hints and X3D Tooltips -->