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

package rec.fsm;

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

public class WritingRecognizer extends Recognizer
{

  public WritingRecognizer()
  {
  }


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

    Rectangle b = st.getBounds();
    GPoint center = st.getLocation().getCenter();

    // check for size
    if ( ( b.width < 30 ) && ( b.height < 30 ) )
      {
	// see if we are near Edge midpoint
	GeoFunction gf = new GeoFunction()
	  {
	    public boolean check( VisibleWidget vw )
	      {
		if ( vw instanceof Edge )
		  return true;
		else
		  return false;
	      }
	  };
	GeometryCalculator gc = new GeometryCalculator( s );
	Edge ed = (Edge)gc.findClosestWithCheck( center, gf );

	if ( ( ed != null ) &&
	     ( Geometry.distance( center, ed.getLocation().getCenter() ) 
		 < 20 ) )
	  {
	    // we have writing (yeah, as _if_ this is working.)
	    if ( LineRecognizer.isLine( st ) )
	      ed.setName( "1" );
	    else
	      ed.setName( "0" );

	    s.removeWidget( st );
	    
	    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();

    return( v.iterator() );
  }
}

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