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.
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 your program uses multiclick interaction, StartInteraction corresponds to the first click, ContinueInteraction corresponds to each subsequent click, and EndInteraction corresponds to double-click.
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:
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. Command creation and execution
Most interactors create and execute commands. TCanvasInteractor derives from the following functions from TToolInteractor for command creation and execution:
Your interactor derived class can create and execute a single command or an incremental command by calling the above functions as follows: virtual void AdoptAndDo( TToolCommandBinding* );
virtual void DoBegin( TToolCommandBinding& );
virtual void DoIncrement( TToolCommandBinding& );
virtual void AdoptAndDoEnd( TToolCommandBinding* );
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() ) ) ; } } }