/*
 * @(#)object2d.java        1.0 2002/08/01
 *
 * Copyright 2001-2006 by Danger Research, Inc.
 * 3101 Park Blvd, Palo Alto, CA  94301
 * All rights reserved.
 */
package com.danger.test3d;

import danger.ui.Pen;
import danger.util.*;


class object2d
{
	//~ Instance fields --------------------------------------------------------

	private int				  mNumVertexes;
	private point2d[]		  mObjVertex;
	private point2d[]		  mTransformedVertex;
	private transformMatrix2d mTMatrix;
	private point2d			  mTempPoint;
	private int				  mLastScale;
	private int				  mLastRotate;
	private int				  mLastTranslateX;
	private int				  mLastTranslateY;

	//~ Constructors -----------------------------------------------------------

	/**
	 * Creates a new object2d object.
	 *
	 * @param numVertexes DOCUMENT ME!
	 */
	public object2d(int numVertexes)
	{
		mNumVertexes		   = numVertexes;
		mObjVertex			   = new point2d[numVertexes];
		mTransformedVertex     = new point2d[numVertexes];

		for (int i = 0; i < mNumVertexes; i++)
		{
			mTransformedVertex[i] = new point2d();
		}

		mTMatrix	   = new transformMatrix2d();
		mTempPoint     = new point2d();

		mLastScale		    = Fixed.fromInt(1);
		mLastRotate		    = 0;
		mLastTranslateX     = 0;
		mLastTranslateY     = 0;
	}

	//~ Methods ----------------------------------------------------------------

	/**
	 * DOCUMENT ME!
	 *
	 * @param vertexNum DOCUMENT ME!
	 * @param vertex DOCUMENT ME!
	 */
	public void SetVertex(int vertexNum, point2d vertex)
	{
		mObjVertex[vertexNum] = vertex;
	}

	/**
	 * DOCUMENT ME!
	 */
	public void StartTransformation()
	{
		for (int i = 0; i < mNumVertexes; i++)
		{
			mTransformedVertex[i].x     = mObjVertex[i].x;
			mTransformedVertex[i].y     = mObjVertex[i].y;
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public void EndTransformation()
	{
		// currently does nothing
		if (false)
		{
			DEBUG.p("EndTransformation: ");

			for (int i = 0; i < mNumVertexes; i++)
			{
				DEBUG.p(" " + i + " " + Fixed.toInt(mTransformedVertex[i].x) +
					"." + Fixed.getFraction(mTransformedVertex[i].x) + " " +
					Fixed.toInt(mTransformedVertex[i].y) + "." +
					Fixed.getFraction(mTransformedVertex[i].y));
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param angle DOCUMENT ME!
	 */
	public void Rotate(int angle)
	{
		mTMatrix.Clear();
		mTMatrix.SetRotation(angle);

		// do the matrix multiply
		for (int i = 0; i < mNumVertexes; i++)
		{
			mTMatrix.MultiplyBy(mTransformedVertex[i], mTempPoint);

			if (false)
			{
				DEBUG.p("Rotate " + i + " " + Fixed.toInt(mTempPoint.x) + "." +
					Fixed.getFraction(mTempPoint.x) + " " +
					Fixed.toInt(mTempPoint.y) + "." +
					Fixed.getFraction(mTempPoint.y));
			}

			mTransformedVertex[i].x     = mTempPoint.x;
			mTransformedVertex[i].y     = mTempPoint.y;
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param scale DOCUMENT ME!
	 */
	public void Scale(int scale)
	{
		for (int i = 0; i < mNumVertexes; i++)
		{
			mTransformedVertex[i].x     = Fixed.multiply(mTransformedVertex[i].x,
					scale);
			mTransformedVertex[i].y     = Fixed.multiply(mTransformedVertex[i].y,
					scale);
		}

		mLastScale = scale;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param scale DOCUMENT ME!
	 */
	public void IncScale(int scale)
	{
		Scale(mLastScale + scale);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param x DOCUMENT ME!
	 * @param y DOCUMENT ME!
	 */
	public void Translate(int x, int y)
	{
		for (int i = 0; i < mNumVertexes; i++)
		{
			mTransformedVertex[i].x     = Fixed.add(mTransformedVertex[i].x, x);
			mTransformedVertex[i].y     = Fixed.add(mTransformedVertex[i].y, y);
		}

		mLastTranslateX     = x;
		mLastTranslateY     = y;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param x DOCUMENT ME!
	 * @param y DOCUMENT ME!
	 */
	public void IncTranslate(int x, int y)
	{
		Translate(mLastTranslateX + x, mLastTranslateY + y);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param inPen DOCUMENT ME!
	 */
	public void Draw(Pen inPen)
	{
		for (int i = 1; i < mNumVertexes; i++)
		{
			inPen.drawLine(Fixed.toInt(mTransformedVertex[i - 1].x),
				Fixed.toInt(mTransformedVertex[i - 1].y),
				Fixed.toInt(mTransformedVertex[i].x),
				Fixed.toInt(mTransformedVertex[i].y));
		}

		inPen.drawLine(Fixed.toInt(mTransformedVertex[mNumVertexes - 1].x),
			Fixed.toInt(mTransformedVertex[mNumVertexes - 1].y),
			Fixed.toInt(mTransformedVertex[0].x),
			Fixed.toInt(mTransformedVertex[0].y));
	}
}
