unit Step4_1;
//******************************************************************/
//* 2.  Cursor  Placement           - DrawRectCallBack,   */
//*                                            DrawRectNULL        */
//******************************************************************/

interface

uses Windows, SysUtils, LtDefine, LibTool, LibDB, LDefin2D;

//-------------------------------------------------------------------------------
//  
//---
procedure DrawRectCallBack;
procedure DrawRectNULL;

function  CallBackP( _dynamic : Integer;      //   
                     phantom  : Pointer;      //    
                     info     : PRequestInfo; //        
                     var angl : Double;       //  
                     var y, x : Double;       //   
                     com      : Integer       //     
                   ): Integer; Pascal;

function  CallBackC( _dynamic : Integer;      //   
                     phantom  : Pointer;      //    
                     info     : PRequestInfo; //        
                     var y, x : Double;       //   
                     com      : Integer       //     
                   ): Integer; Pascal;
//-------------------------------------------------------------------------------
implementation

var
 _type, flag : Integer;

//-------------------------------------------------------------------------------
//   ,   Cursor
//---
function CallBackC( _dynamic : Integer;      //   
                    phantom  : Pointer;      //    
                    info     : PRequestInfo; //        
                    var y, x : Double;       //   
                    com      : Integer       //     
                   ): Integer;  Pascal;

var
  rub : PPhantom;                            //    
begin

  rub := PPhantom(phantom);

  case com  of
     1 : _type := com;                       //  1
     2 : _type := com;                       //  2
    -1 : begin                               //   
           MoveObj( rub^.type1.gr, x, y );   //  

           if ( Abs(rub^.type1.ang) > 0.001 ) then
             RotateObj( rub^.type1.gr, x, y, rub^.type1.ang ); //  

           StoreTmpGroup( rub^.type1.gr );   //     
           ClearGroup( rub^.type1.gr );      //  
         end;
  end;
  //            
  if  rub^.type1.gr > 0 then
    DeleteObj( rub^.type1.gr );   //   

  //   
  rub^.type1.gr := NewGroup( 1 ); //  

  if ((flag=1) And (com=1)) Or ((flag=2) And (com=2)) then
    _type := 3;

  //         
  case _type  of
    1: begin // 
         Circle( 0, 0, 20, 1 );
         info^.commands := '! !';
         flag := 1;
       end;
    2: begin // 
         LineSeg( -10, 0, 10,  0, 1 );
         LineSeg(  10, 0,  0, 20, 1 );
         LineSeg( -10, 0,  0, 20, 1 );
         info^.commands := '! !';
         flag := 2;
       end;
    3: begin // 
         LineSeg( -10,  0,  10,  0, 1 );
         LineSeg(  10,  0,  10, 20, 1 );
         LineSeg(  10, 20, -10, 20, 1 );
         LineSeg( -10, 20, -10,  0, 1 );
         info^.commands := '! !';
         flag := 0;
       end;
  end;

  EndGroup();  //  

  CallBackC := 1;
end;

//-------------------------------------------------------------------------------
//   ,   Placement
//---
function CallBackP( _dynamic : Integer;      //   
                    phantom  : Pointer;      //    
                    info     : PRequestInfo; //        
                    var angl : Double;       //  
                    var y, x : Double;       //   
                    com      : Integer       //     
                    ): Integer; Pascal;
var
  rub : PPhantom;

begin
  rub := PPhantom (phantom);                 //    
  case  com of
     1 : _type := com;
     2 : _type := com;
    -1 : begin //   
           MoveObj( rub^.type1.gr, x, y );  //  

          //    Cursor      
          if ( Abs(angl) > 0.001 ) then
            RotateObj( rub^.type1.gr, x, y, angl ); //  

          StoreTmpGroup( rub^.type1.gr );  //     
          ClearGroup( rub^.type1.gr );     //  
      end;
  end;

  //            
  if rub^.type1.gr > 0 then
    DeleteObj( rub^.type1.gr );   //   

  //   
  rub^.type1.gr := NewGroup( 1 ); //  
  if ((flag=1) And (com=1)) Or ((flag=2) And (com=2)) then
    _type := 3;

  //         
  case  _type  of
    1 : begin
          Circle( 0, 0, 20, 1 );
          info^.commands := '! ! ';
          flag := 1;
        end;
    2 : begin
          LineSeg( -10, 0, 10,  0, 1 );
          LineSeg(  10, 0,  0, 20, 1 );
          LineSeg( -10, 0,  0, 20, 1 );
          info^.commands := '! ! ';
          flag := 2;
        end;
    3 : begin
          LineSeg( -10,  0,  10,  0, 1 );
          LineSeg(  10,  0,  10, 20, 1 );
          LineSeg(  10, 20, -10, 20, 1 );
          LineSeg( -10, 20, -10,  0, 1 );
          info^.commands := '! ! ';
          flag := 0;
        end;
  end;

  EndGroup();

  CallBackP :=1;
end;

//-------------------------------------------------------------------------------
//    Cursor  Placemant c  
//---
procedure DrawRectCallBack;
var
  info : RequestInfo;               //  
  rub  : Phantom;                   // 
  x, y : Double;                    //   
begin
  _type := 1;

  FillChar( rub, sizeof(rub), 0 );
  rub.type1.scale := 1;             // 
  rub.phType      := 1;             //  

  FillChar( info, sizeof(info), 0 );

  rub.type1.gr := NewGroup( 1 );    //  
    Circle( 0, 0, 20, 1 );          // 
  EndGroup();

  info.commands := '! !';
  info.callBack := Addr(CallBackP); //      Placement
  Placement( Addr(info), x, y, rub.type1.ang, Addr(rub) );

  rub.type1.gr := NewGroup( 1 );    //  
    Circle( 0, 0, 20, 1 );          // 
  EndGroup();

  info.callBack := Addr(CallBackC); //      Cursor
  ksCursor( Addr(info), x, y, Addr(rub) );

end;


//-------------------------------------------------------------------------------
//    Cursor  Placemant   
//       ,     
//  Cursor  Plecemant  .      
//          .
//---
procedure DrawRectNULL;
var
  info : RequestInfo; //  
  rub  : Phantom;     // 
  x, y : double ;     //   
  j    : integer;

begin
  _type := 1;
  flag  := 1;
  j     := 1;
  FillChar( rub, sizeof(rub), 0 );
  rub.type1.scale := 1;  // 
  rub.phType      := 1;  //  

  FillChar( info, sizeof(info), 0 );
  info.commands := '   ';

  while ( j <> 0 ) do
  begin
    if ( rub.type1.gr > 0 ) then
      DeleteObj( rub.type1.gr  );  //   
    //   
    rub.type1.gr := NewGroup( 1 ); //  
    if( (flag=1) And (j=1)) Or ((flag=2) And (j=2) ) then
      _type := 3;

    case  _type  of
      1 : begin // 
           Circle( 0, 0, 20, 1 );
           info.commands := '! ! ';
           flag := 1;
          end;
      2 : begin  // 
            LineSeg( -10, 0, 10,  0, 1 );
            LineSeg(  10, 0,  0, 20, 1 );
            LineSeg( -10, 0,  0, 20, 1 );
            info.commands := '! ! ';
            flag := 2;
          end;
      3 : begin // 
            LineSeg( -10,  0,  10,  0, 1 );
            LineSeg(  10,  0,  10, 20, 1 );
            LineSeg(  10, 20, -10, 20, 1 );
            LineSeg( -10, 20, -10,  0, 1 );
            info.commands := '! ! ';
            flag := 0;
          end;
    end;

    EndGroup();

    //       
    j := Placement( Addr(info), x, y,
                    rub.type1.ang,
                    Addr(rub) );
//    j := Cursor(&info, &x, &y, &rub );  //    

    case  j of
      1 : _type := j;
      2 : _type := j;
     -1 : begin //   
            MoveObj( rub.type1.gr, x, y );                    //  

            if ( Abs(rub.type1.ang) > 0.001 ) then
              RotateObj( rub.type1.gr, x, y, rub.type1.ang ); //  

            StoreTmpGroup( rub.type1.gr );   //     
            ClearGroup( rub.type1.gr );      //  
      end;
    end;
  end;
end;

end.