Designing a Behavior Object

A behavior object is an instance of a custom class that controls the display of the object(s) contained in a model object that supports animation. This behavior object determines what action to take in response to a timer event. When a timer event occurs, the window object calls the OnTimer method of each window observer (each behavior object) that implements it. The following figure shows the interaction between the window and a behavior object.

Figure 10-3: Interaction Between Window, Behavior Object, and Animation Model

animation_behavior.gif

In the example of a Cine, the behavior object's OnTimer method tells the model object which model or graphic to display the next time the scene is drawn. The behavior object completes its action by signaling the window object to draw the scene with the updated model. The system is quiescent until the next timer interval expires, at which point the process begins again. In widget applications, widget events and other application processing may occur during the quiet time.

The OnTimer method of the behavior object need not be complex. The following simple OnTimer method of the user-defined behavior object, MyAnimation, simply iterates through the frames in an image series. The OnTimer method parameter specifies the IDLitWindow object in which the timer event occurred.

PRO MyAnimation::OnTimer, oWin 
 
; Add one to the current frame number. 
self.currentFrame++ 
 
; Iterate through the image frames. Define the frame to display 
; by setting the ACTIVE_POSTION property on the model. 
IF self.currentFrame GE self.oAnimationModel->Count() THEN $ 
   self.currentFrame = 0 
self.oAnimationModel->SetProperty, $ 
   ACTIVE_POSITION=self.currentFrame 
    
; Draw the scene. 
oWin->Draw 
 
END 

Example Code
For the simple Cine animation example, see animation_image_doc.pro in the examples/doc/objects subdirectory of the IDL installation directory. Run the example procedure by entering animation_image_doc at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT animation_image_doc.pro. This example shows a simple animation in a window that continues until the window is closed.

More than one behavior object can be associated with a window, which lets you create synchronous animations. The window triggers all behaviors associated with it by calling all observers that are interested in OnTimer notifications. Also, a behavior can be programmed to perform any arbitrary operation. It is not limited to cycling through a series of images. For example, it could alter a transform in a model object to implement a time-based rotation.

Example Code
For a simple surface rotation animation example, see animation_surface_doc.pro in the examples/doc/objects subdirectory of the IDL installation directory. Run the example procedure by entering animation_surface_doc at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT animation_surface_doc.pro.