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

package rec.simulate.widgets;

// -*-Mode: Java-*- 
// WSimSpring.java -- 
// Author: Christine Alvarado <calvarad@ai.mit.edu> 
// Maintainer: Christine Alvarado <calvarad@ai.mit.edu> 
// Version: 
// Created: <Sat Apr 17 18:40:32 1999> 
// Time-stamp: <> 
// Keywords: 

import rec.geo.*;
import rec.sys.*;
import rec.core.widgets.*;
import rec.meche.WMForce;
import rec.util.*;
import java.util.*;

public abstract class WSimForce extends WSimConstraint
{

  public WSimForce( WMForce f )
  {
    setDefaultColor( "force", "sim_constraint" );
    setEnglishName( "Force" );
    m_Components = copyComponents( f.getComponents() );
    m_Size = f.size();
    //m_Center = f.getLocation().getCenter();
    m_Angle = f.getAngle();
    m_Name = "Force" + getId();
    m_body1 = null;
  }
  
  public GPoint getHeadPoint()
  {
    return( ((GSegment)m_Components[0]).getHeadPoint() );
  }

  public boolean attach( WSimBody b )
  {
    // check to see if we can attach
    if ( ( m_body1 != null ) ) {
      Console.writeln( "Force already attached to a body.  Cannot attach to body " 
		       + b.getName() );
      return false;
    }
    // check to see if one of the ends is touching or inside the body in 
    // question.
    // the offset is set to 0, meaning the end must really be inside the body
    if ( m_body1 == null ) {
      if ( b.includes( getHeadPoint(), 20.0 ) ) {
	m_body1 = b;
	return true;
      }
    }
    
    return false;
  }
    
  public void detach()
  {
    m_body1 = null;
  }
  
  public int numAttached()
  {
    if ( m_body1 == null ) {
      return 0;
    }
    else {
      return 1;
    }
  }

  public GPoint getAttachmentPoint( WSimBody body ) 
  {
    if( body.equals( m_body1 ) )
      return getHeadPoint();
    else
      return null;
  }

  public List getBodies() 
  {
    List list = new ArrayList( numAttached() );
    if( m_body1 != null )
      list.add( m_body1 );
    return list;
  }

  public void forceIsGravity( boolean g )
  {
    isGravity = g;
  }
    
  //  protected GPoint m_end1;
  protected WSimBody m_body1;
  protected boolean isGravity;  

}
