//////////////////////////////////////////////////////////////////////////////// 
// 
// step4_1.cpp -  Cursor  Placement
//
//    ( Placement -    )    .
//   (  )    commands  RequestInfo
//       .    
//        , 
//       .    
// callBack  NULL,       .
// 
//////////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h"

#ifndef _INC_MATH
#include <math.h> 
#endif 

#ifndef __LIBTOOL_H
#include <libtool.h>
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


int g_type = 1; //   -   


//-------------------------------------------------------------------------------
//         
// ---
void Update( reference & rGroup,  // 
             RequestInfoT & info ) //     
{
  //            
  if ( rGroup )
    DeleteObj( rGroup );
  
  //   , type -   ( 0 -  , 1 -  )
  rGroup = NewGroup( 1 );
  switch ( g_type )
  {
    case 0 :
      //  
      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 = _T("! !");
      break;

    case 1 :
      //  
      Circle( 0, 0, 20, 1 );
      //  
      info.commands = _T("! !");
      break;
 
    case 2 :
      //  
      LineSeg( -10, 0,   10,  0,  1 );
      LineSeg(  10, 0,   0,   20, 1 );
      LineSeg(  0,  20, -10,  0,  1 );
      //  
      info.commands = _T("! !");
      break;
  }
  EndGroup(); //    
}


//-------------------------------------------------------------------------------
//   ,   Cursor
// ---
int WINAPI CallBackC( int comm,             //  
                      double *x, double *y, //  
                      RequestInfoT *info,    //     
                      void *phantom,        //   
                      int/*dynamic*/ )
{
  Phantom *pPhantom = ( Phantom * )phantom;
 
  //  
  if ( comm == -1 ) //   
  {
    //    
    MoveObj( pPhantom->type1.gr, *x, *y );
    if ( fabs( pPhantom->type1.ang ) > 0.001 )
      RotateObj( pPhantom->type1.gr, *x, *y, pPhantom->type1.ang );
 
    //     
    StoreTmpGroup( pPhantom->type1.gr );
    //   
    ClearGroup( pPhantom->type1.gr );
  }
  else //   
  {
    if ( ( g_type == 1 && comm == 1 ) || ( g_type == 2 && comm == 2 ) )
      g_type = 0;
    else
      g_type = comm;
  }

  //         
  if ( info )
    Update( pPhantom->type1.gr, *info );

  return 1;
}


//------------------------------------------------------------------------------
//   ,   Placement
// ---
int WINAPI CallBackP( int comm,             //  
                      double *x, double *y, //  
                      double *ang,          //   
                      RequestInfoT *info,    //     
                      void *phantom,        //   
                      int )                 //    ( 1- , 0- )
{
  Phantom *pPhantom = ( Phantom * )phantom;
 
  //  
  if ( comm == -1 ) //   
  {
    //    
    //   Cursor      
    MoveObj( pPhantom->type1.gr, *x, *y );
    if ( fabs( *ang ) > 0.001 )
      RotateObj( pPhantom->type1.gr, *x, *y, *ang );
 
    //     
    StoreTmpGroup( pPhantom->type1.gr );
    //   
    ClearGroup( pPhantom->type1.gr );
  }
  else //   
  {
    if ( ( g_type == 1 && comm == 1 ) || ( g_type == 2 && comm == 2 ) )
      g_type = 0;
    else
      g_type = comm;
  }

  //         
  if ( info )
    Update( pPhantom->type1.gr, *info );

  return 1;
}


//-------------------------------------------------------------------------------
//    Cursor  Placemant c  
// ---
void DrawRectCallBack()
{
  Phantom phantom; //   
  
  phantom.phType      = 1; //   ( type1, type2, ... type7 )
  phantom.type1.xBase = 0; //    
  phantom.type1.yBase = 0;
  phantom.type1.scale = 1; // 
  phantom.type1.gr    = 0; //   
  phantom.type1.ang   = 0; //    
 
  //     
  RequestInfoT info;
  memset( &info, 0, sizeof( info ) );

  //    ,     
  Update( phantom.type1.gr, info );

  //  
  double x, y;

  //    Placemant
  //      Placement
  info.callBack = CallBackP;
  //  ,   
  PlacementExT( &info,              //       	
                &x, &y,             //   	
                &phantom.type1.ang, //  
                &phantom, NULL );         //     ,  	 

  /*
  //    Cursor
  //      Cursor
  info.callBack = CallBackC;
  //    
  CursorExT( &info,            //       	 
             &x, &y,           //   	
             &phantom, NULL ); //     ,  	 
  */
}


//-------------------------------------------------------------------------------
//    Cursor  Placemant   
//       ,     
//  Cursor  Plecemant  .      
//          .
// ---
void DrawRectNULL()
{
  Phantom phantom; //   
  
  phantom.phType      = 1; //   ( type1, type2, ... type7 )
  phantom.type1.xBase = 0; //    
  phantom.type1.yBase = 0;
  phantom.type1.scale = 1; // 
  phantom.type1.gr    = 0; //   
  phantom.type1.ang   = 0; //     
 
  //     
  RequestInfoT info;
  memset( &info, 0, sizeof( info ) );

  //  
  double x, y;

  int comm = 1; //  
  while ( comm )
  {
    //    ,     
    Update( phantom.type1.gr, info );

    //    Placemant
    //  ,   
    comm = PlacementExT( &info,              //       	
                         &x, &y,             //   	
                         &phantom.type1.ang, //  
                         &phantom, NULL );         //     ,  	     

    /*
    //    Cursor
    //    
    comm = CursorExT( &info,            //       	 
                      &x, &y,           //   	
                      &phantom, NULL ); //     ,  	 
    */

    //  
    if ( comm == -1 ) //   
    {
      //    
      MoveObj( phantom.type1.gr, x, y );
      if ( fabs( phantom.type1.ang ) > 0.001 )
        RotateObj( phantom.type1.gr, x, y, phantom.type1.ang );

      //     
      StoreTmpGroup( phantom.type1.gr );
      //   
      ClearGroup( phantom.type1.gr );
    }
    else //   
    {
      if ( ( g_type == 1 && comm == 1 ) || ( g_type == 2 && comm == 2 ) )
        g_type = 0;
      else
        g_type = comm;
    }
  }
}