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

// -*-Mode: Java-*- 
// WMSquare.java -- 
// Author: Christine Alvarado <calvarad@ai.mit.edu> 
// Maintainer: Christine Alvarado <calvarad@ai.mit.edu> 
// Version: 
// Created: <Wed Mar 10 19:09:22 1999> 
// Time-stamp: <2001-09-10 18:28:11 moltmans> 
// Keywords: 
package rec.simulate.widgets.wm;

import rec.util.*;
import java.io.*;
import rec.geo.*;
import rec.core.widgets.*;
import java.math.*;
import rec.simulate.*;
import rec.simulate.widgets.*;

// class used when an interface to Working Model is desired.
// this is a rectangle, but for working model we must convert it to 
// a polygon if it is not stright up and down.  For now assume all 
// rectangles that we get at WM rectangles and deal with the rest later.
public class WMSimDamper extends WSimDamper
{

  public WMSimDamper( GSegment base )
  {
    super( base );
  }

  // the key to this class is the load funciton, which "loads"
  // the object into the simulator.  that is, prints it to the file.
  // it takes a WMSimulator so that it knows the conversion factors
  // that it should use to convert dimensions.
  // it needs another parameter to resolve name conflicts.  The system
  // is seriously limited at the moment.
  public String load( WMSimulatorRegistryBase reg ) 
  {
    GPoint newEnd1 = reg.convert( ((GSegment)m_Components[0]).getHeadPoint() );
    GPoint newEnd2 = reg.convert( ((GSegment)m_Components[0]).getTailPoint() );
    String output;

    Console.writeln( "loading damper" );
    
    output = "Dim " + m_Name + " as WMConstraint\n";
   
    // attach the endpoints to bodies, if they are attached to bodies
    // we assume at this point that the bodies have already been loaded
    // otherwise this does not work.
    if ( m_body1 != null ) {
      GPoint newBody1 = reg.convert( m_body1.getLocation().getCenter() );
      Console.writeln( "Body1 is at " + newBody1 );
      newEnd1.x -= newBody1.x;
      newEnd1.y -= newBody1.y;
    }
    if ( m_body2 != null ) {
      GPoint newBody2 = reg.convert( m_body2.getLocation().getCenter() );
      newEnd2.x -= newBody2.x;
      newEnd2.y -= newBody2.y;
    }

    output +=  "t1.X = " + newEnd1.x + " : t1.Y = " + newEnd1.y + "\n";
    output +=  "t2.X = " + newEnd2.x + " : t2.Y = " + newEnd2.y + "\n";
    output +=  "Set " + m_Name + " = Draw_Damper( t1, t2 )\n" ;
    
    if ( m_body1 != null ) {
      output +=  "Set " + m_Name + ".Point(1).Body = " + 
	m_body1.getName() + "\n";
      Console.writeln( "attatching " + m_Name + " to " + m_body1.getName() );
    }
    if ( m_body2 != null ) {
      output += "Set " + m_Name + ".Point(2).Body = " + m_body2.getName() + 
	"\n";
      Console.writeln( "attatching " + m_Name + " to " + m_body2.getName() );
    }

    return output;

  }

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

}
