Creating an interactor

How you create your own interactor depends on the type of interaction that you want. Specifically, this is where you dictate what happens between the MouseDown and MouseUp events. In the case of the TPolygonCreationInteractor, you track points where a user clicks, and draw a line between these points. (The user either double clicks or clicks near a previously recorded point to signal that the polygon is complete.) As such, the interactor inherits from GrafEdit's TCanvasPolylineMulticlickInteractor, which keeps track of these points. The relevant code in this case is a single function that you override:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      void
      TCanvasPolygonCreationInteractor::EndInteraction ()
      {
          #ifdef DEBUG
              ::qprintf ("TCanvasPolygonCreationInteractor::EndInteraction\n");
          #endif
                      
          if ( fTarget && GetNumberOfPoints() > 2 ) {
          
              //
              // Create and do a new polygon component command
              //
              
              if ( GetNumberOfPoints() > 3 ) {
              
                  //
                  // Check if user closed the loop
                  //
          
                  TGPoint distanceVector = GetLastPoint() - GetFirstPoint();
                  if ( distanceVector.VectorLength() < 4 )
                      RemoveLastPoint ();
      
                  }
              
              TGPolygon p (GetPoints());
              TCanvasPolygon* thePolygon = new TCanvasPolygon (p);
              
              TAdoptCanvasGraphicCmd *command = new TAdoptCanvasGraphicCmd (thePolygon);
              AdoptAndDo (new TToolCommandBindingTo<MCanvasSelection> (command, fTarget.OrphanObject()));
              
              }
      }
EndInteraction is an overridden function from the TCanvasInteractor class. It determines when to stop the interaction process and create the specified canvas graphic. To do this, instantiate a new instance of your graphic (a TCanvasPolygon) and use the TAdoptCanvasGraphicCmd to adopt this graphic into your canvas view.


[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