Up until now, the applications were not interactive. In this tutorial we'll use mouse input to make the example from the 3D basics tutorial more dynamic. Here's the program we created.
<Application>
<Scene3D id="scene">
<Sprite3D rotationX="10" rotationY="20">
<Shape>
<SolidFill color="0xCCCCCC">
<Rectangle width="160" height="150" />
</SolidFill>
</Shape>
<Sprite x="20" y="20">
<Sprite>
<Shape>
<SolidFill color="0x222222">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 1</TextField>
</Sprite>
<Sprite y="40">
<Shape>
<SolidFill color="0x222222">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 2</TextField>
</Sprite>
<Sprite y="80">
<Shape>
<SolidFill color="0x222222">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 3</TextField>
</Sprite>
</Sprite>
</Sprite3D>
</Scene3D>
<Camera3D id="camera" z="{-this.perfectDistance}" />
<Viewport3D viewportWidth="{root.applicationWidth}" viewportHeight="{root.applicationHeight}" scene="{root.getElementById('scene')}" camera="{root.getElementById('camera')}" />
</Application>
All interactive objects have a hovered property which evaluates to true if the mouse is over the object, or false otherwise. The Sprite element happens to be an interactive object. In this example, we'll use bindings to change the color of the buttons when the mouse is over them. We'll also use the conditional operator, which has the form A ? B : C, and evaluates to B if A is true, or C otherwise.
Notice how the color attributes are changed.
<SolidFill color="{parent.parent.hovered ? 0xAA0000 : 0x222222}">
<Application>
<Scene3D id="scene">
<Sprite3D rotationX="10" rotationY="20">
<Shape>
<SolidFill color="0xCCCCCC">
<Rectangle width="160" height="150" />
</SolidFill>
</Shape>
<Sprite x="20" y="20">
<Sprite>
<Shape>
<SolidFill color="{parent.parent.hovered ? 0xAA0000 : 0x222222}">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 1</TextField>
</Sprite>
<Sprite y="40">
<Shape>
<SolidFill color="{parent.parent.hovered ? 0xAA0000 : 0x222222}">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 2</TextField>
</Sprite>
<Sprite y="80">
<Shape>
<SolidFill color="{parent.parent.hovered ? 0xAA0000 : 0x222222}">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 3</TextField>
</Sprite>
</Sprite>
</Sprite3D>
</Scene3D>
<Camera3D id="camera" z="{-this.perfectDistance}" />
<Viewport3D viewportWidth="{root.applicationWidth}" viewportHeight="{root.applicationHeight}" scene="{root.getElementById('scene')}" camera="{root.getElementById('camera')}" />
</Application>
Next we'll create a rectangle to paint a background for the application, then use the click event to change the background color when clicking a button. To add an event, you use the EventListener element. It requires two parameters, which refer to the element that triggers the event, and the event type.
<EventListener target="{parent}" type="click">
root.getElementById('background').color = 0x880000;
</EventListener>
Within the EventListener element, you can have as many instructions as you want, separated by semi-colons.
<Application>
<Shape>
<SolidFill id="background" color="0x333333">
<Rectangle width="{root.applicationWidth}" height="{root.applicationHeight}" />
</SolidFill>
</Shape>
<Scene3D id="scene">
<Sprite3D rotationX="10" rotationY="20">
<Shape>
<SolidFill color="0xCCCCCC">
<Rectangle width="160" height="150" />
</SolidFill>
</Shape>
<Sprite x="20" y="20">
<Sprite>
<EventListener target="{parent}" type="click">
root.getElementById('background').color = 0x880000;
</EventListener>
<Shape>
<SolidFill color="{parent.parent.hovered ? 0xAA0000 : 0x222222}">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 1</TextField>
</Sprite>
<Sprite y="40">
<EventListener target="{parent}" type="click">
root.getElementById('background').color = 0x008800;
</EventListener>
<Shape>
<SolidFill color="{parent.parent.hovered ? 0xAA0000 : 0x222222}">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 2</TextField>
</Sprite>
<Sprite y="80">
<EventListener target="{parent}" type="click">
root.getElementById('background').color = 0x000088;
</EventListener>
<Shape>
<SolidFill color="{parent.parent.hovered ? 0xAA0000 : 0x222222}">
<RoundedRectangle width="120" height="30" ellipseWidth="10" ellipseHeight="10" />
</SolidFill>
</Shape>
<TextField x="5" y="3" width="{this.textWidth}" height="{this.textHeight}" color="0xFFFFFF" size="16">Button 3</TextField>
</Sprite>
</Sprite>
</Sprite3D>
</Scene3D>
<Camera3D id="camera" z="{-this.perfectDistance}" />
<Viewport3D viewportWidth="{root.applicationWidth}" viewportHeight="{root.applicationHeight}" scene="{root.getElementById('scene')}" camera="{root.getElementById('camera')}" />
</Application>
This tutorial was probably harder to follow than the previous ones. However, once you understand the concepts, you'll be able to create interactive applications by mixing events and bindings.
© 2008 by — Valid XHTML