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

// $Id: ButtonClickRecognizer.java,v 1.3 2003/03/06 00:40:48 moltmans Exp $
// Time-stamp: <2000-03-11 18:54:46 moltmans>
package rec.core.recognizers;



////////////////////////////////////////////////////////////////////////////
//
// FILE:	ButtonClickRecognizer.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	June, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Drawing recognition
//
// HISTORY:     September, 1998
//              MDM
//              modified to fit with new recognition model
//
/////////////////////////////////////////////////////////////////////////////


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

public class ButtonClickRecognizer extends FastRecognizer
{

  // current button being hit
  WButton button;

  public ButtonClickRecognizer()
  {
  }

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

  public int want( Surface s, rec.sys.Squiggle st, boolean ending )
  {
    button = findButton( s, st.getFirstPoint() );

    if ( button == null )
      return DONT_WANT;
    else
      {
	button.select( true );
	return WANT;
      }
  }

    
  WButton findButton( Surface s,  GPoint p )
  {
    GeometryCalculator calc = new GeometryCalculator( s );
    
    final GPoint gp = p;
    GeoFunction gf = new GeoFunction() {
      public boolean check( VisibleWidget w )
	{
	  if ( w instanceof WButton )
	    {
	      //Console.writeln( "we got " + w + " bounds = " + w.getBounds() );
	      if ( w.getBounds().contains( (int)gp.x, (int)gp.y ) )
		return true;
	    }

	  return false;
	}
    };

    WButton vw = (WButton)
      calc.findClosestWithCheck( gp, gf );

    return vw;
  }


  public void reset()
  {
    button = null;
  }

  public boolean recognize( Surface s, rec.sys.Squiggle st, boolean ending )
  {
    GPoint gp = st.getLastPoint();

    if ( button.contains( gp ) )
      {
	if ( ending )
	  {
	    button.select( false );
	    //button.hit( s );
	    // instead of hitting the button here, we create a Button Click
	    // Widget and add it to the surface as a child of the squiggle
	    WButtonClick bc = new WButtonClick( gp, button );
	    s.replaceWidget( st, bc );
	  }

	return true;
      }
    else
      {
	button.select( false );
	return false;
      }
  }

  public Iterator getOutputTypes()
  {
    return( (new Vector( 0 )).iterator() );
  }

}

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