X3D introduced seven nodes that do many of the functions that required Script nodes in VRML. These nodes are particularly useful when a world needs to run in a profile that does not support Script nodes (e.g., Interactive or Interchange). The locking switch has been encapsulated in a PROTO so that it can easily be reused.
The event utility nodes handle events and convert them from one data type to another. There are currently nodes that handle boolean and time incoming events. Two of the events generate integer outgoing events.
The remaining discussion will focus on the three nodes that are used in this example. The nodes are BooleanFilter, BooleanToggle, and IntegerTrigger. The remaining nodes will be discussed in upcoming examples.
A locking switch is one that maintains its state until it is touched. It then maintains the new state until touched a second time. This type of switch is also known as a push-on/push-off switch. These switches are used throughout the physical world, typically to control electricity (wall switch, on/off switch, etc.). In X3D, the basic switch is controlled by a TouchSensor. The TouchSensor sends an event when the mouse button goes down, and a second event when the mouse button is released. It is not enough to convert that event to a control of the target. The target would turn off whenever the mouse button is released.
A means for preventing the second event from being distributed is required. The TouchSensor isActive event does have a different value for mouse-up (FALSE) and mouse-down (TRUE). The BooleanToggle node responds only to incoming TRUE events. When the node receives a TRUE event, it generates an outgoing event. Incoming FALSE events are swallowed.
The BooleanToggle node also maintains a state. That state is inverted and used as the value of the outgoing event. This is a very useful feature. It allows the TouchSensor generated event to flip between TRUE and FALSE on successive mouse-down events. The event cascade is now generating an event only when the mouse button goes down. The value of the event flips between TRUE and FALSE.
The BooleanFilter node generates a different event depending on the value of the incoming event. It acts as an event separator. There are now one of two event cascades. Each cascade is fed into an IntegerTrigger node.
The IntegerTrigger node generates an outgoing event whose value is an internally stored integer. Two IntegerTrigger nodes (one to each cascade) each with different values are connected up to the BooleanFilter node.
The output of the IntegerTrigger node is routed back to a Switch node that controls the geometry displayed in the center. The internal values for the IntegerTrigger nodes must be legal values for the Switch node. In this example, the values used are 0 and 1. Any set of values that correspond to appropriate children can be used.
The example is EventUtilities-LockingSwitch.x3dv (plain text listing). After defining the external prototype API (plain text format) and the environment, the example defines two cones. One on the left (blue) and one on the right (green). The green cone uses the Event Utilities prototype to control the geometry. The blue cone uses a Script node to control the geometry. There is no interaction between the external PROTO and the Script node. This is not a 3-way switch.
After the geometry and appearance of the TouchSensors are defined, the controlled geometry is defined in a Switch node. The initial state is a box. The alternate state is a sphere. Both are colored red.
The switch mechanisms are defined last. The first definition is for the prototype. It is wired to the green cone. The second definition is for the Script node and is wired to the blue cone.
In this example, the script code is simple; however, the Script node requires a Profile of Immersive or higher. In addition as the logic grows more complex so will the script. The BooleanSequencer and IntegerSequencer nodes provide support for move complex logic. Those nodes will be discussed in a future tip.