from H3DInterface import *

class PrintFloat( AutoUpdate (TypedField( SFFloat, SFFloat ) ) ):
  def update( self, event ):
    print event.getValue()
    return event.getValue()
printFloat = PrintFloat()

class PrintBool( AutoUpdate (TypedField( SFBool, SFBool ) ) ):
  def __init__(self, prefix):
	AutoUpdate (TypedField( SFBool, SFBool ) ).__init__(self)
	self.prefix = prefix
	
  def update( self, event ):
    print self.prefix, event.getValue()
    return event.getValue()
printBool = PrintBool('')
printBool1 = PrintBool('1: ')
printBool2 = PrintBool('2: ')
printBool3 = PrintBool('3: ')
printBool4 = PrintBool('4: ')
printBool5 = PrintBool('5: ')
printBool6 = PrintBool('6: ')

class FloatSink( AutoUpdate (TypedField( SFFloat, SFFloat ) ) ):
  def update( self, event ):
    return event.getValue()
floatSink = FloatSink()

class PrintInt( AutoUpdate (TypedField( SFInt32, SFInt32 ) ) ):
  def update( self, event ):
    print event.getValue()
    return event.getValue()
printInt = PrintInt()
printInt1 = PrintInt()
printInt2 = PrintInt()
printInt3 = PrintInt()
printInt4 = PrintInt()

class PrintMFInt32( AutoUpdate (TypedField( MFInt32, MFInt32 ) ) ):
  def update( self, event ):
    print event.getValue()
    return event.getValue()
printMFInt32 = PrintMFInt32()

class PrintMFBool( AutoUpdate (TypedField( MFBool, MFBool ) ) ):
  def update( self, event ):
    print event.getValue()
    return event.getValue()
printMFBool = PrintMFBool()

class PrintMFFloat( AutoUpdate (TypedField( MFFloat, MFFloat ) ) ):
  def update( self, event ):
    print event.getValue()
    return event.getValue()
printMFFloat = PrintMFFloat()

class PrintMFVec3f( AutoUpdate (TypedField( MFVec3f, MFVec3f ) ) ):
  def update( self, event ):
    print event.getValue()
    return event.getValue()
printMFVec3f = PrintMFVec3f()

class PrintMRI( AutoUpdate (TypedField( SFInt32, SFInt32 ) ) ):
  def update( self, event ):
    print "MRI ", event.getValue()
    return event.getValue()
printMRI = PrintMRI()

class PrintVM( AutoUpdate (TypedField( SFInt32, SFInt32 ) ) ):
  def update( self, event ):
    print "VM ", event.getValue()
    return event.getValue()
printVM = PrintVM()

class PrintTime( AutoUpdate (TypedField( SFTime, SFTime ) ) ):
  def update( self, event ):
    print event.getValue()
    return event.getValue()
printTime = PrintTime()
printTime1 = PrintTime()
printTime2 = PrintTime()
printTime3 = PrintTime()
printTime4 = PrintTime()

class PrintVec( AutoUpdate (TypedField( SFVec3f, SFVec3f ) ) ):
  def __init__( self, m ):
    AutoUpdate (TypedField( SFVec3f, SFVec3f ) ).__init__( self )
    self.msg = m
  def update( self, event ):
    print self.msg, event.getValue()
    return event.getValue()
printVec = PrintVec("")
printVec1 = PrintVec("1: ")
printVec2 = PrintVec("2: ")
printVec3 = PrintVec("3: ")
printVec4 = PrintVec("4: ")
printVec5 = PrintVec("5: ")

class PrintVec3f( AutoUpdate (TypedField( SFVec3f, SFVec3f ) ) ):
  def __init__( self, m ):
    AutoUpdate (TypedField( SFVec3f, SFVec3f ) ).__init__( self )
    self.msg = m
  def update( self, event ):
    print self.msg, event.getValue()
    return event.getValue()
printVec3f = PrintVec3f("")
printVec3f1 = PrintVec3f("1: ")
printVec3f2 = PrintVec3f("2: ")
printVec3f3 = PrintVec3f("3: ")
printVec3f4 = PrintVec3f("4: ")

class PrintVec2f( AutoUpdate (TypedField( SFVec2f, SFVec2f ) ) ):
  def __init__( self, m ):
    AutoUpdate (TypedField( SFVec2f, SFVec2f ) ).__init__( self )
    self.msg = m
  def update( self, event ):
    print self.msg, event.getValue()
    return event.getValue()
printVec2f = PrintVec2f("")
printVec2f1 = PrintVec2f("1: ")
printVec2f2 = PrintVec2f("2: ")
printVec2f3 = PrintVec2f("3: ")
printVec2f4 = PrintVec2f("4: ")

class PrintRot( AutoUpdate (TypedField( SFRotation, SFRotation ) ) ):
  def update( self, event ):
#    t0 = time.get()
    print event.getValue()
    return event.getValue()
printRot = PrintRot()

class PrintMatrix( AutoUpdate (TypedField( SFMatrix4f, SFMatrix4f ) ) ):
  def update( self, event ):
#    t0 = time.get()
    m = event.getValue()
    print m
    #print m[0][0], " ", m[0][1], " " , m[0][2], " " , m[0][3]
    #print m[1][0], " ", m[1][1], " " , m[1][2], " " , m[1][3]
    #print m[2][0], " ", m[2][1], " " , m[2][2], " " , m[2][3]
    #print m[3][0], " ", m[3][1], " " , m[3][2], " " , m[3][3]
    return event.getValue()
printMatrix = PrintMatrix()

class PrintRate( AutoUpdate (TypedField( SFInt32, SFVec3f ) ) ):
  def __init__( self ):
    AutoUpdate (TypedField( SFInt32, SFVec3f ) ).__init__( self )
    self.count = 0
    self.last_time = 0.0
  def update( self, event ):
    t0 = time.get()
    diff = t0 - self.last_time
    if (diff > 1.0) :
      print "Rate = ", self.count, " hz"
      self.count = 0
      self.last_time = t0
    else:
      self.count = self.count + 1
    return 0
printRate = PrintRate()

class PrintString( AutoUpdate (TypedField( SFString, SFString ) ) ):
  def update( self, event ):
    print event.getValue()
#    print "Hello"
    return event.getValue()
printString1 = PrintString()
printString2 = PrintString()
printString3 = PrintString()
printString4 = PrintString()
printString5 = PrintString()

class Toggle( AutoUpdate (TypedField( SFBool, SFBool ) ) ):
  def __init__( self ):
    AutoUpdate (TypedField( SFBool, SFBool ) ).__init__( self )
    self.val = 0
  def update( self, event ):
    if (event.getValue()):
      if (self.val):
        self.val = 0
      else:
        self.val = 1
    return self.val
toggle = Toggle()

class Swing( AutoUpdate (TypedField( SFTime, SFBool ) ) ):
  def __init__( self ):
    AutoUpdate (TypedField( SFTime, SFBool ) ).__init__( self )
    self.setValue(9999999.0)
  def update( self, event ):
    if (event.getValue()):
        return 0.0
    else:
        return 9999999.0
swing = Swing()

