####################################################################################################
#
# Invoking X3D model self-test:
#
#   $ python GridYZ_20x20Movable.py
#
# Python package x3d.py package is available on PyPI for import.
#   This approach simplifies Python X3D deployment and use.
#   https://pypi.org/project/x3d
#
# Installation:
#       pip install x3d
# or
#       python -m pip install x3d
#
# Developer options for loading x3d package in other Python programs:
#
#    from x3d import *  # preferred approach, terser source that avoids x3d.* class prefixes
#
# or
#    import x3d         # traditional way to subclass x3d package, all classes require x3d.* prefix,
#                       # but python source is very verbose, for example x3d.Material x3d.Shape etc.
#                       # X3dToPython.xslt stylesheet insertPackagePrefix=true supports this option.
#
# Project home page:    # X3D Python Scene Access Interface Library (X3DPSAIL)
#                       # https://www.web3d.org/x3d/stylesheets/python/python.html
# Conversion generator: # https://www.web3d.org/x3d/stylesheets/X3dToPython.xslt
#
####################################################################################################

from x3d import *

newModel=X3D(profile='Immersive',version='3.0',
  head=head(
    children=[
    meta(content='GridYZ_20x20Movable.x3d',name='title'),
    meta(content='Don Brutzman',name='creator'),
    meta(content='8 November 2000',name='created'),
    meta(content='28 November 2019',name='modified'),
    meta(content='Line grid authoring tool for precise measurement in 3D space: drag plane to move along X axis, click numbers to hide grid. Oriented along YZ plane, size 20m by 20m, default block size 1m by 1m.',name='description'),
    meta(content='https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/GridsExample.x3d',name='reference'),
    meta(content='https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/GridYZ_20x20Movable.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')]),
  Scene=Scene(
    children=[
    WorldInfo(title='GridYZ_20x20Movable.x3d'),
    Viewpoint(description='YZ grid (15 0 30 relative)',orientation=(0,1,0,0.465),position=(15,0,30)),
    Viewpoint(description='YZ grid (30 0 0 relative)',orientation=(0,1,0,1.57079),position=(30,0,0)),
    Viewpoint(description='YZ grid (25 0 -25 relative)',orientation=(0,1,0,2.37),position=(25,0,-25)),
    Transform(DEF='GridLocation',
      children=[
      Group(
        children=[
        TouchSensor(DEF='TextLabelTouchSensor',description='select and hold to hide grid'),
        Transform(translation=(0,-0.5,0),
          children=[
          Billboard(
            children=[
            Shape(
              geometry=Text(DEF='CenterText',string=["origin"],
                fontStyle=FontStyle(DEF='LABEL_FONT',family=["SANS"],justify=["MIDDLE","MIDDLE"],size=.4)),
              appearance=Appearance(DEF='DefaultAppearance',
                material=Material(),))])]),
        Transform(translation=(0,10.5,-10),
          children=[
          Billboard(
            children=[
            Shape(
              geometry=Text(DEF='UpperRightText',string=["0 10 -10"],
                fontStyle=FontStyle(USE='LABEL_FONT')),
              appearance=Appearance(USE='DefaultAppearance'))])]),
        Transform(translation=(0,10.5,10),
          children=[
          Billboard(
            children=[
            Shape(
              geometry=Text(DEF='UpperLeftText',string=["0 10 10"],
                fontStyle=FontStyle(USE='LABEL_FONT')),
              appearance=Appearance(USE='DefaultAppearance'))])]),
        Transform(translation=(0,-10.5,10),
          children=[
          Billboard(
            children=[
            Shape(
              geometry=Text(DEF='LowerLeftText',string=["0 -10 10"],
                fontStyle=FontStyle(USE='LABEL_FONT')),
              appearance=Appearance(USE='DefaultAppearance'))])]),
        Transform(translation=(0,-10.5,-10),
          children=[
          Billboard(
            children=[
            Shape(
              geometry=Text(DEF='LowerRightText',string=["0 -10 -10"],
                fontStyle=FontStyle(USE='LABEL_FONT')),
              appearance=Appearance(USE='DefaultAppearance'))])])]),
      Switch(DEF='GridOnOffSwitch',whichChoice=0,
        children=[
        Transform(rotation=(0,0,1,-1.57079),
          children=[
          PlaneSensor(DEF='GridSensor',description='click and drag to move grid'),
          Script(DEF='LineSensorAxisX',
            field=[
            field(accessType='inputOnly',name='set_translation',type='SFVec3f'),
            field(accessType='outputOnly',name='translation_changed',type='SFVec3f')],

          sourceCode="""
ecmascript:

// local-coordinates vertical Y is world-coordinates axis X
function set_translation (location, timeStamp)  {
	translation_changed [0] = location.y;
	translation_changed [1] = 0;
	translation_changed [2] = 0;
//	Browser.println ('location=' + location + ', translation_changed=' + translation_changed); 
}
"""),
          Shape(DEF='LinesAlignedAlongZ',
            geometry=IndexedLineSet(colorIndex=[1,0,0,0,0,2,0,0,0,0,1,0,0,0,0,2,0,0,0,0,1],colorPerVertex=False,coordIndex=[1,22,-1,2,23,-1,3,24,-1,4,25,-1,5,26,-1,6,27,-1,7,28,-1,8,29,-1,9,30,-1,10,31,-1,11,32,-1,12,33,-1,13,34,-1,14,35,-1,15,36,-1,16,37,-1,17,38,-1,18,39,-1,19,40,-1,20,41,-1,21,42,-1],
              coord=Coordinate(DEF='EndPoints',point=[(0,0,0),(-10,0,10),(-9,0,10),(-8,0,10),(-7,0,10),(-6,0,10),(-5,0,10),(-4,0,10),(-3,0,10),(-2,0,10),(-1,0,10),(0,0,10),(1,0,10),(2,0,10),(3,0,10),(4,0,10),(5,0,10),(6,0,10),(7,0,10),(8,0,10),(9,0,10),(10,0,10),(-10,0,-10),(-9,0,-10),(-8,0,-10),(-7,0,-10),(-6,0,-10),(-5,0,-10),(-4,0,-10),(-3,0,-10),(-2,0,-10),(-1,0,-10),(0,0,-10),(1,0,-10),(2,0,-10),(3,0,-10),(4,0,-10),(5,0,-10),(6,0,-10),(7,0,-10),(8,0,-10),(9,0,-10),(10,0,-10)]),
              color=Color(color=[(0.4,0.4,0.4),(0.8,0.2,0),(0.4,0.1,0.05)]))),
          Transform(DEF='LinesAlignedAlongX',rotation=(0,1,0,1.57079),
            children=[
            Shape(USE='LinesAlignedAlongZ')]),
          Transform(rotation=(0,0,1,1.57079),translation=(-10.5,0,0),
            children=[
            Billboard(
              children=[
              Shape(
                geometry=Text(string=["Y"],
                  fontStyle=FontStyle(USE='LABEL_FONT')),
                appearance=Appearance(DEF='LABEL_APPEARANCE',
                  material=Material(diffuseColor=(1,1,.3),emissiveColor=(.33,.33,.1))))])]),
          Transform(rotation=(0,0,1,1.57079),translation=(0,0,10.5),
            children=[
            Billboard(
              children=[
              Shape(
                geometry=Text(string=["Z"],
                  fontStyle=FontStyle(USE='LABEL_FONT')),
                appearance=Appearance(USE='LABEL_APPEARANCE'))])])])])]),
    Script(DEF='CenterTextScript',
      field=[
      field(accessType='inputOnly',name='set_translation',type='SFVec3f'),
      field(accessType='outputOnly',name='value',type='MFString')],

    sourceCode="""
ecmascript:

// eventOut 'value' is an MFString array to match type of destination Text node string field

function initialize ( ) { 
//	Browser.println ('Grid script initialized, print function works');
//	trace ('Grid script initialized, trace function works');
}

function set_translation (location, timeStamp)  {
	numberOfPlaces = 1000;
	value[0] = (Math.round (location.x * numberOfPlaces) / numberOfPlaces) + ' '
                 + (Math.round (location.y * numberOfPlaces) / numberOfPlaces) + ' '
                 + (Math.round (location.z * numberOfPlaces) / numberOfPlaces); 
//	Browser.println ('location=' + location); 
}
"""),
    Script(DEF='UpperRightTextScript',
      field=[
      field(accessType='inputOnly',name='set_translation',type='SFVec3f'),
      field(accessType='outputOnly',name='value',type='MFString')],

    sourceCode="""
ecmascript:

function set_translation (location, timeStamp)  {
	numberOfPlaces = 1000;
	value[0] = (Math.round (location.x * numberOfPlaces) / numberOfPlaces)
                 + ' ' + 10 + ' ' +  -10; 
}
"""),
    Script(DEF='UpperLeftTextScript',
      field=[
      field(accessType='inputOnly',name='set_translation',type='SFVec3f'),
      field(accessType='outputOnly',name='value',type='MFString')],

    sourceCode="""
ecmascript:

function set_translation (location, timeStamp)  {
	numberOfPlaces = 1000;
	value[0] =  (Math.round (location.x * numberOfPlaces) / numberOfPlaces)
                 + ' ' + 10 + ' ' + 10; 
}
"""),
    Script(DEF='LowerLeftTextScript',
      field=[
      field(accessType='inputOnly',name='set_translation',type='SFVec3f'),
      field(accessType='outputOnly',name='value',type='MFString')],

    sourceCode="""
ecmascript:

function set_translation (location, timeStamp)  {
	numberOfPlaces = 1000;
	value[0] = (Math.round (location.x * numberOfPlaces) / numberOfPlaces)
                 + ' ' + -10 + ' ' + 10; 
}
"""),
    Script(DEF='LowerRightTextScript',
      field=[
      field(accessType='inputOnly',name='set_translation',type='SFVec3f'),
      field(accessType='outputOnly',name='value',type='MFString')],

    sourceCode="""
ecmascript:

function set_translation (location, timeStamp)  {
	numberOfPlaces = 1000;
	value[0] = (Math.round (location.x * numberOfPlaces) / numberOfPlaces)
                 + ' ' + -10 + ' ' +  -10;
}
"""),
    Script(DEF='SwitchTextOnOffScript',
      field=[
      field(accessType='inputOnly',name='isTouched',type='SFBool'),
      field(accessType='outputOnly',name='touchChoice',type='SFInt32')],

    sourceCode="""
ecmascript:

function isTouched (value, timeStamp)  {
	if (value == true)
		touchChoice = -1;
	else	touchChoice =  0; 
}
"""),
    ROUTE(fromField='translation_changed',fromNode='GridSensor',toField='set_translation',toNode='LineSensorAxisX'),
    Comment(' Send changed position to text-construction scripts '),
    ROUTE(fromField='translation_changed',fromNode='LineSensorAxisX',toField='set_translation',toNode='GridLocation'),
    ROUTE(fromField='translation_changed',fromNode='LineSensorAxisX',toField='set_translation',toNode='CenterTextScript'),
    ROUTE(fromField='translation_changed',fromNode='LineSensorAxisX',toField='set_translation',toNode='UpperRightTextScript'),
    ROUTE(fromField='translation_changed',fromNode='LineSensorAxisX',toField='set_translation',toNode='UpperLeftTextScript'),
    ROUTE(fromField='translation_changed',fromNode='LineSensorAxisX',toField='set_translation',toNode='LowerLeftTextScript'),
    ROUTE(fromField='translation_changed',fromNode='LineSensorAxisX',toField='set_translation',toNode='LowerRightTextScript'),
    Comment(' Send text showing changed position to output Text nodes '),
    ROUTE(fromField='value',fromNode='CenterTextScript',toField='string',toNode='CenterText'),
    ROUTE(fromField='value',fromNode='UpperRightTextScript',toField='string',toNode='UpperRightText'),
    ROUTE(fromField='value',fromNode='UpperLeftTextScript',toField='string',toNode='UpperLeftText'),
    ROUTE(fromField='value',fromNode='LowerLeftTextScript',toField='string',toNode='LowerLeftText'),
    ROUTE(fromField='value',fromNode='LowerRightTextScript',toField='string',toNode='LowerRightText'),
    Comment(' Enable/disable grid visibility based on user touching coordinate labels '),
    ROUTE(fromField='isActive',fromNode='TextLabelTouchSensor',toField='isTouched',toNode='SwitchTextOnOffScript'),
    ROUTE(fromField='touchChoice',fromNode='SwitchTextOnOffScript',toField='whichChoice',toNode='GridOnOffSwitch')])
)

### X3D model conversion complete ###

####################################################################################################
# Self-test diagnostics
####################################################################################################

print('Self-test diagnostics for GridYZ_20x20Movable.py:')
if        metaDiagnostics(newModel): # built-in utility method in X3D class
    print(metaDiagnostics(newModel)) # display meta info, hint, warning, error, TODO values in this model
# print('check newModel.XML() serialization...')
newModelXML= newModel.XML() # test export method XML() for exceptions during export
newModel.XMLvalidate()
# print(newModelXML) # diagnostic

try:
#   print('check newModel.VRML() serialization...')
    newModelVRML=newModel.VRML() # test export method VRML() for exceptions during export
    # print(prependLineNumbers(newModelVRML)) # debug
    print("Python-to-VRML export of VRML output successful", flush=True)
except Exception as err: # usually BaseException
    # https://stackoverflow.com/questions/18176602/how-to-get-the-name-of-an-exception-that-was-caught-in-python
    print("*** Python-to-VRML export of VRML output failed:", type(err).__name__, err)
    if newModelVRML: # may have failed to generate
        print(prependLineNumbers(newModelVRML, err.lineno))

try:
#   print('check newModel.JSON() serialization...')
    newModelJSON=newModel.JSON() # test export method JSON() for exceptions during export
#   print(prependLineNumbers(newModelJSON)) # debug
    print("Python-to-JSON export of JSON output successful (under development)")
except Exception as err: # usually SyntaxError
    print("*** Python-to-JSON export of JSON output failed:", type(err).__name__, err)
    if newModelJSON: # may have failed to generate
        print(prependLineNumbers(newModelJSON,err.lineno))

print("python GridYZ_20x20Movable.py load and self-test diagnostics complete.")
