Test code for the button prototype PROTO Button test... comments
#VRML V2.0 utf8
WorldInfo {
info ["author: Zlatomir Zarkov"]
title "Button test"
}
PROTO Button [
field SFVec3f bPosition 0 0 0
field SFRotation bOrientation 0 1 0 0
eventOut SFBool buttonOn
]
{
Transform {
translation IS bPosition
rotation IS bOrientation
children [
DEF buttonTouch TouchSensor {}
Shape {
appearance Appearance {
material DEF buttonColor Material {
diffuseColor 0 .5 0
specularColor .5 .5 .5
}
}
geometry IndexedFaceSet {
coord Coordinate {
point [
-.1 0 0 -.08 -.02 .02 .14 -.02 .02 .16 0 0
-.1 0 0 -.1 -.14 0 -.08 -.12 .02 -.08 -.02 .02
-.08 -.12 .02 -.1 -.14 0 .16 -.14 0 .14 -.12 .02
.14 -.02 .02 .14 -.12 .02 .16 -.14 0 .16 0 0
-.08 -.02 .02 -.08 -.12 .02 .14 -.12 .02 .14 -.02 .02
]
}
coordIndex [
0 1 2 3 0 -1
4 5 6 7 4 -1
8 9 10 11 8 -1
12 13 14 15 12 -1
16 17 18 19 16 -1
]
}
}
]
}
DEF ButtonScript Script {
eventIn SFBool isOver
eventIn SFBool isActive
field SFBool itisOver FALSE
field SFBool alredyPresed FALSE
field SFColor oldColor 0 .5 0
eventOut SFBool buttonActive IS buttonOn
eventOut SFColor changeColor
url [ "javascript:
function isActive (value) {
if (value == FALSE && itisOver == TRUE){
if (alredyPresed == FALSE) {
alredyPresed = TRUE ;
oldColor [0] = .5 ;
oldColor [1] = 0 ;
changeColor = oldColor ;
buttonActive = TRUE ;
}
else {
alredyPresed = FALSE ;
oldColor [0] = 0 ;
oldColor [1] = .5 ;
changeColor = oldColor ;
buttonActive = FALSE ;
}
}
}
function isOver (value) {
if (value == TRUE)
itisOver = TRUE ;
else
itisOver = FALSE ;
}
"]
}
ROUTE buttonTouch.isOver TO ButtonScript.isOver
ROUTE buttonTouch.isActive TO ButtonScript.isActive
ROUTE ButtonScript.changeColor TO buttonColor.diffuseColor
}
Viewpoint {
position .2 .3 1.5
}
DEF button_1 Button {
bOrientation 1 0 0 -.5
}
DEF button_2 Button {
bPosition .3 0 0
bOrientation 1 0 0 -.5
}
DEF myCube Transform {
translation .17 .5 -.5
children [
Shape {
appearance Appearance {
material DEF cubeColor Material {diffuseColor 0 0 1}
}
geometry Box {size .4 .4 .4}
}
]
}
DEF myCubeTimer TimeSensor {
cycleInterval 5
enabled FALSE
loop TRUE
}
DEF myCubeRot OrientationInterpolator {
key [0 .5 1]
keyValue [0 1 0 0 0 1 0 3.14 0 1 0 6.28]
}
DEF colorTimer TimeSensor {
cycleInterval 10
enabled FALSE
loop TRUE
}
DEF cubeMaterial ColorInterpolator {
key [0 .125 .25 .375 .4 .525 .65 .775 .9 1]
keyValue [1 0 0 1 1 0 1 1 1 0 1 1 0 0 1 1 0 1 0 1 0 1 0 0]
}
DEF ButtonTest Script {
eventIn SFBool go
eventIn SFBool getColor
directOutput TRUE
field SFNode rotationTimer USE myCubeTimer
field SFNode colTimer USE colorTimer
url ["javascript:
function go (value, timestamp) {
if (value == TRUE) {
rotationTimer.set_enabled = TRUE ;
rotationTimer.set_startTime = rotationTimer.startTime + timestamp - rotationTimer.stopTime ;
rotationTimer.set_stopTime = 0 ;
}
else {
rotationTimer.set_enabled = FALSE ;
rotationTimer.set_stopTime = timestamp ;
}
}
function getColor (value, timestamp) {
if (value == TRUE) {
colTimer.set_enabled = TRUE ;
colTimer.set_startTime = colTimer.startTime + timestamp - colTimer.stopTime ;
colTimer.set_stopTime = 0 ;
}
else {
colTimer.set_enabled = FALSE ;
colTimer.set_stopTime = timestamp ;
}
}
"]
}
ROUTE button_1.buttonOn TO ButtonTest.go
ROUTE myCubeTimer.fraction_changed TO myCubeRot.set_fraction
ROUTE myCubeRot.value_changed TO myCube.set_rotation
ROUTE button_2.buttonOn TO ButtonTest.getColor
ROUTE colorTimer.fraction_changed TO cubeMaterial.set_fraction
ROUTE cubeMaterial.value_changed TO cubeColor.set_diffuseColor