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

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

package rec.core.widgets;

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

public class WButton extends VisibleWidget
{
  public String name;
  boolean selected = false;
  GCircle button;

  public WButton( )
  {
    this( "button" );
  }

  public WButton( String name )
  {
    //    setEnglishName( "Button named: " + name );
    button = new GCircle( new GPoint( 0, 0 ), 25 );
    this.name = name;
  }


  public double size()
  {
    return( button.size() );
  }

  public void resize( double scaleFactor )
  {
    button.resize( scaleFactor );
  }

  /**
   * We don't really want to draw the squiggles
   * since there aren't any...
   **/
  public void drawSquiggles( Graphics g ) 
  {
    draw( g );
  }
    

  public void draw( Graphics g )
  {
    double radius = button.getRadius();
    GPoint center = button.getCenter();

    if ( selected )
      g.setColor( Color.blue );

    int cx = (int)(center.x - radius );
    int cy = (int)(center.y - radius );
    int wh =(int)( 2.0 * radius);

    g.draw3DRect( cx, cy, wh, wh, !selected );

    g.setColor( Color.blue );
    FontMetrics fm = g.getFontMetrics();


    String nm = name;
    if ( name == null )
      nm = "button";

    int wd = fm.stringWidth( nm ) / 2;
    
    g.drawString( nm, (int)center.x - wd, (int)center.y );
    
  }


  public boolean includes( GPoint gp, double offset )
  {
    return( button.includes( gp, offset ) );
  }


  public boolean contains( GPoint gp, double offset )
  {
    return button.contains( gp, offset );
  }

  public boolean contains( GPoint gp )
  {
    return contains( gp, 0 );
  }


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

  public void move( GPoint gp )
  {
    button.translate( getLocation().getCenter(), gp );
  }

  public void rotate( GPoint gp, double angle )
  {
    button.rotate( gp, angle );
  }

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

  public Location getLocation()
  {
    return( new Location( button.getBounds() ) );
  }

  public Rectangle getBounds()
  {
    return( button.getBounds() );
  }

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

  public void translate( int dx, int dy )
  {
    button.translate( dx, dy );
  }

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

  public Object clone()
  {
    WButton ret = new WButton( name );

    ret.button = new GCircle( button );

    return( ret );
  }



  /**
   * Do the hit stuff
   */
  public void hit( )
  {
    Console.writeln( "I, " + name + ", was clicked on!" );
  }

  public void hit( Surface s )
  {
    Console.writeln( "I, " + name + ", was clicked on!" );
  }

  public void undoHit()
  {
    Console.writeln( "I, " + name + ", am undoing my hit" );
  }

  public String describeHit()
  {
    return( "Execute button click function" );
  }

  public void select( boolean is_selected )
  {
    Console.writeln( "I am now selected="+is_selected );
    selected = is_selected;
  }

}
