// (c) MIT 2003.  All rights reserved.

////////////////////////////////////////////////////////////////////////////////
//
// FILE:        MoveTargetRecognizer.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	10-XX-98
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Drawing recognition
//
// HISTORY:     10-XX-98 archon
//              Adapted from ModifyModeRecognizer
//
//              11-15-98 archon
//              Now aware of new rec.geo package
//
///////////////////////////////////////////////////////////////////////////////

package rec.core.recognizers;

import java.util.*;
import rec.util.*;
import rec.geo.*;
import rec.sys.*;
import rec.core.widgets.*;

public class MoveTargetRecognizer extends TargetRecognizer
{
  public MoveTargetRecognizer()
  {
    super();
  }

  ////////////////////////////////////////////////////////////////////////////

  public boolean recognize( Surface s, Widget w )
  {
    rec.sys.Squiggle st = (rec.sys.Squiggle)w;

    Console.writeln( "-*- Checking for a move -*- " );

    // it doesn't have to be a line, just as long as it's not a loop
    if( !LooseCircleRecognizer.isCircle( st ) && ( st.getLength() > 5 ) ) {
      
      WSelection sel = (WSelection)getTarget();
      
      // only move from within bound lines
      VisibleWidget tar = sel.getWidget();
      if ( ! tar.getBounds().contains( st.getFirstPoint().x,
						   st.getFirstPoint().y ) )
	return false;
      
      GPoint prevLocation = tar.getLocation().getCenter();
      GPoint p = st.getLastPoint();
      GPoint h = st.getFirstPoint();
      
      // how did the stroke act offset so the wid moves relatively
      GPoint dest = new GPoint( p.x + prevLocation.x - h.x,
				p.y + prevLocation.y - h.y );
      
      s.replaceWidget( w, new WMove( tar, dest ) );
      
      return( true );
    }
    
    return( false );
  }

  public Iterator getInputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.sys.Squiggle" );

    return( v.iterator() );
  }


  public Iterator getOutputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.core.widgets.WMove" );

    return v.iterator();
  }
}

////////////////////////////////////////////////////////////////////////////////
