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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	WMBallAndSocket.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	April 99
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:     Ball and socket widget
//
// HISTORY:     April 99  archon  created
//
////////////////////////////////////////////////////////////////////////////////

package rec.meche;

import rec.geo.*;

public class WMBallAndSocket extends WPart
{
  public WMBallAndSocket( WMBall ball, WMSocket socket )
  {
    super( Geometry.midpoint( ball.getJoint(), socket.getJoint() ),
	   Math.PI / 2.0, (ball.size() + socket.size()) / 2.0,
	   new CloneableArray( ball, socket ) );
  }

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

  public Object clone()
  {
    Cloneable[] parts = ((CloneableArray)getData()).getArray();

    return( new WMBallAndSocket( (WMBall)parts[0], (WMSocket)parts[1] ) );
  }

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

  protected GShape[] create( double size, Cloneable data )
  {
    Cloneable[] parts = ((CloneableArray)data).getArray();

    WMBall ball = (WMBall)parts[0];
    WMSocket socket = (WMSocket)parts[1];

    // Set the new center.

    GPoint ballJoint = ball.getJoint();
    GPoint socketJoint = socket.getJoint();
    GPoint cen = Geometry.midpoint( ballJoint, socketJoint );
    setCenter( cen );

    double ballXoff = cen.x - ballJoint.x;
    double ballYoff = cen.y - ballJoint.y;

    double socketXoff = cen.x - socketJoint.x;
    double socketYoff = cen.y - socketJoint.y;

    GShape[] ballShapes = ((WMBall)ball.clone()).getComponents();
    GShape[] socketShapes = ((WMSocket)socket.clone()).getComponents();

    GShape[] shapes = new GShape[ballShapes.length + socketShapes.length];

    for( int i = 0; i < ballShapes.length; ++i ) {
      GShape s = ballShapes[i];
      s.translate( ballXoff, ballYoff );
      shapes[i] = s;
    }

    for( int i = 0; i < socketShapes.length; ++i ) {
      GShape s = socketShapes[i];
      s.translate( socketXoff, socketYoff );
      shapes[ballShapes.length + i] = s;
    }

    return( shapes );
  }
}

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