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

// -*-Mode: JDE-*- 
// $Id: PointingRecognizer.java,v 1.4 2003/03/06 00:40:49 moltmans Exp $
// PointingRecognizer.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Fri Jun 09 15:55:37 2000> 
// Time-stamp: <2001-04-03 19:28:05 moltmans> 
package rec.annotate.recognizers;

import java.util.*;
import rec.sys.*;
import rec.core.widgets.*;
import rec.annotate.widgets.*;
import rec.geo.GPoint;
import rec.simulate.widgets.WSimBody;

/**
 * This class recognizes pointing gestures (i.e. clicks).
 **/
public class PointingRecognizer extends Recognizer
{

  public boolean recognize( Surface s, Widget w )
  {
    WClick click = (WClick)w;
    final GPoint point = click.getLocation().getCenter();
    GeometryCalculator gc = new GeometryCalculator(s);

    // Find the closest body that contains the point
    // allowing for a bit of slack as well
    GeoFunction gf = new GeoFunction() {
	public boolean check(VisibleWidget vw) 
	{
	  return (vw instanceof WSimBody) &&
	    ((WSimBody)vw).contains(point, 30);
	}
      };
    WSimBody body =
      (WSimBody)gc.findClosestSurfaceWidgetWithCheck(point, gf);
						   
    WPoint wpoint = new WPoint( s, point, body );
    
    s.replaceWidget( w, wpoint );
    return true;
  }

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

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

