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

package rec.simulate.widgets.wm;

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

public class WMSimRotationalDamper extends WSimRotationalDamper
{
    public WMSimRotationalDamper(GPoint center)
    {
	super(center);
    }

    public String load(WMSimulatorRegistryBase reg)
    {
	GPoint newEnd1 = reg.convert( getAdjustedCenterPoint() );
	GPoint newEnd2 = new GPoint( newEnd1 );
	String output;
	
	Console.writeln( "loading rotational damper" );
	
	output = "Dim " + m_Name + " as WMConstraint\n";
	output += "Set " + m_Name + " = doc.NewConstraint(\"Rdamper\")\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() );
	    newEnd1.x -= newBody1.x;
	    newEnd1.y -= newBody1.y;
	    Console.writeln( "attatching " + m_Name + " to " + m_body1.getName() );
	}
	if ( m_body2 != null ) {
	    GPoint newBody2 = reg.convert( m_body2.getLocation().getCenter() );
	    newEnd2.x -= newBody2.x;
	    newEnd2.y -= newBody2.y;
	    Console.writeln( "attatching " + m_Name + " to " + m_body2.getName() );
	}
	// Split this joint so that it can be moved around
	//output += "doc.SelectAll False\n";
	//output += "doc.Select " + m_Name + "\n";
	output += "doc.Split\n";
	output += "Set " + m_Name + ".Point(1).PX.Value = " + newEnd1.x + "\n";
	output += "Set " + m_Name + ".Point(1).PY.Value = " + newEnd1.y + "\n";
	output += "Set " + m_Name + ".Point(2).PX.Value = " + newEnd2.x + "\n";
	output += "Set " + m_Name + ".Point(2).PY.Value = " + newEnd2.y + "\n";
	
	if  ( m_body1 != null ) {
	    output +=  "Set " + m_Name + ".Point(1).Body = " + 
		m_body1.getName() + "\n";
	}
	if ( m_body2 != null ) {
	    output += "Set " + m_Name + ".Point(2).Body = " + m_body2.getName() + 
		"\n";
	}
	output += "doc.Join\n";
	
	return output;
    }
	
    }
