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

// -*-Mode: Java-*- 
// $Id: MovementGestureRecognizer.java,v 1.2 2003/03/06 00:40:43 moltmans Exp $
// MovementGestureRecognizer.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Tue Feb 15 21:16:43 2000> 
<<<<<<< MovementGestureRecognizer.java
// Time-stamp: <2000-04-18 14:56:46 moltmans> 
=======
// Time-stamp: <2000-05-11 20:51:05 moltmans> 
>>>>>>> 1.4
package rec.annotate.recognizers;

import java.util.*;
<<<<<<< MovementGestureRecognizer.java
import rec.util.*;
import rec.geo.*;
=======
>>>>>>> 1.4
import rec.sys.*;
import rec.core.widgets.*;
import rec.annotate.widgets.*;
//  import rec.simulate.widgets.*;
//  import rec.annotate.*;

/**
 * This class recognizes movement gestures (i.e. arrows).
 **/
public class MovementGestureRecognizer extends Recognizer
{
  public boolean recognize( Surface s, Widget w )
  {
    WSquiggleArrow arrow = (WSquiggleArrow)w;
    WMotionArrow warrow = new WMotionArrow( s, arrow );
    
<<<<<<< MovementGestureRecognizer.java
    if( found == null )
      return false;

    // check if this is a propels gesture
    // i.e. one body pushes another body
    WSimObject movee = pathIntersects( s, found, arrow );

    // the found widget is moving
    MovementAnnotation movement1 = new MovementAnnotation( s, found, arrow );
    WMovementAnnotation annotation1 =
      new WMovementAnnotation( movement1, s.getAnnotationNotebook() );

    // now check if it propels anything else
    if( movee != null ) {
      MovementAnnotation movement2 =
	new MovementAnnotation( s, movee, arrow );
      WMovementAnnotation annotation2 =
	new WMovementAnnotation( movement2, s.getAnnotationNotebook() );

      movement2.setCause( movement1 );
      movement1.setEffect( movement2 );
      
      HashSet set = new HashSet();
      set.add( annotation1 );
      set.add( annotation2 );
      s.replaceWidget( w, set, true );
    }
    else {
      s.replaceWidget( w, annotation1 );
    }
    return true;
=======
    s.replaceWidget( w, warrow );
    return true;
>>>>>>> 1.4
  }

  private WSimObject pathIntersects( Surface s,
				     WSimObject mover,
				     WSquiggleArrow arrow ) 
  {
    Iterator it = s.getSurfaceWidgets().iterator();
    WSimBody found = null;

    // check for simbody intersected by arrow
    while ( it.hasNext() ) {
      VisibleWidget body = (VisibleWidget)it.next();
      if( (body instanceof WSimBody) &&
	  (body != mover) &&
	  arrow.intersects( body ) )
      {
	if( found != null ) {
	  DebugWindow.addLine( "Problems",
			       "Path could be pushing several objects" );
	}
	found = (WSimBody)body;
      }
    }

    return found;
  }
  

  public Iterator getInputTypes()
  {
    Vector v = new Vector();
    v.addElement( "rec.core.widgets.WSquiggleArrow" );
    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();
    v.addElement( "rec.annotate.widgets.WMotionArrow" );
    return( v.iterator() );
  }
  
}
