Developing interactors

TCanvasInteractor is the interface for all canvas interactors in GrafEdit. It derives from the Document framework classes TToolInteractor and MMouseEventHandler. TToolInteractor lets the canvas interactor be used by cursor tools and provides a way to do commands. MMouseEventHandler mixes in functions that define the actions to take when a mouse-up, mouse-down, or mouse-move event is detected.

Defining an interactor involves deciding the interaction style you need, the interaction feedback you want, the command to execute, and using the correct GrafEdit interaction classes for your purpose.

Interaction style

GrafEdit gives you the following choices in interaction styles. The interaction style classes derive from TCanvasInteractor.

If the interaction style you want involves a simple click, you can use TCanvasDragInteractor and not have mouse-click activities between the mouse-down and the mouse-up event.

Interaction feedback

GrafEdit gives you the following choices in interaction feedback style. The feedback-style classes derive from the interaction style classes described in the preceding section. The feedback-style classes with "Drag" in their names derive from TCanvasDragInteractor, and those with "Multiclick" in their names derive from TCanvasMulticlickInteractor.

If you want no feedback or a custom feedback style, derive directly from TCanvasDragInteractor or TCanvasMulticlickInteractor.

Action

When you know your interaction style and feedback, you know which class to derive from to implement interaction behavior for your program. To define when action occurs in your derived class, implement any or all of the pure virtual functions described here:

These functions have different meanings according to which interaction style you choose. If your program uses drag interaction, StartInteraction corresponds to the mouse down, ContinueInteraction corresponds to each mouse move while dragging, and EndInteraction corresponds to mouse up.

If your program uses multiclick interaction, StartInteraction corresponds to the first click, ContinueInteraction corresponds to each subsequent click, and EndInteraction corresponds to double-click.

Command creation and execution

Most interactors create and execute commands. TCanvasInteractor derives from the following functions from TToolInteractor for command creation and execution:

      virtual void AdoptAndDo( TToolCommandBinding* );
      virtual void DoBegin( TToolCommandBinding& );
      virtual void DoIncrement( TToolCommandBinding& );
      virtual void AdoptAndDoEnd( TToolCommandBinding* );
Your interactor derived class can create and execute a single command or an incremental command by calling the above functions as follows:

Single command: Call AdoptAndDo in EndInteraction.

Incremental command: Call DoBegin in StartInteraction; Call DoIncrement in ContinueInteraction; Call AdoptAndDoEnd in EndInteraction.

The TToolCommandBinding parameter binds the command to its target. It is constructed from the command and the target. This binding is always adopted in the last function call, AdoptAndDo for a single command or AdoptAndDoEnd for an incremental command. The interactor class forgets about the binding, command, and target after adopting them because storage responsibilities are transferred to the interactor's base class.

For example, TCanvasLineCreationInteractor, which is provided in GrafEdit, derives from TCanvasLineDragInteractor. TCanvasLineCreationInteractor needs two implementations:

The following code is a slightly simplified implementation for EndInteraction. In this case, the target (fTarget) is an MCanvasSelection from which the interactor was constructed.

      void
      TCanvasLineCreationInteractor::EndInteraction()
      {
          if( GetNumberOfPoints() > 1 ) {
              TGPoint firstPoint = GetFirstPoint();
              TGPoint lastPoint = GetLastPoint();
      
              if( fTarget && firstPoint != lastPoint ) {
                  TGLine line( firstPoint, lastPoint );
                  TCanvasLine* theLine = new TCanvasLine( 1ine );
      
                  TAdoptCanvasGraphicCmd* cmd = new TAdoptCanvasGraphicCmd( theLine );
                  AdoptAndDo( new TToolCommandBindingTo < MCanvasSelection > ( cmd,
                      fTarget() ) ) ;
              }
          }
      }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker