/*
 * @(#)Main.java	1.0 2006/01/18
 *
 * Copyright 2006 by Danger, Inc.
 * 3101 Park Blvd, Palo Alto, CA  94306
 * All rights reserved.
 */
package com.danger.testexture;

import danger.app.*;
import danger.system.*;
import danger.ui.*;
import danger.util.*;
import java.util.Random;
import danger.util.Fixed;


/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
public class Main extends Application
{
	//~ Static fields/initializers ---------------------------------------------

	public static Main sInstance;

	//~ Instance fields --------------------------------------------------------

	TextureWindow mWindow;
	Timer		  mTimer;

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

	//*	--------------------	Main
	public Main()
	{
		super();

		sInstance     = this;

		mWindow = new TextureWindow();
		mWindow.setTitle("Texture");
	}

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

	/**
	 * DOCUMENT ME!
	 */
	public void resume()
	{
		mWindow.show();

		if (mTimer == null)
		{
			mTimer = new Timer(50, true, mWindow);
		}

		mTimer.start();
	}

	/**
	 * DOCUMENT ME!
	 */
	public void suspend()
	{
		mTimer.stop();
		mWindow.hide();
	}
}


class TextureWindow extends ScreenWindow
{
	//~ Instance fields --------------------------------------------------------

	Bitmap mBitmap;
	Random mRandom;

	int[]	x = new int[3];
	int[]	y = new int[3];
	int[]	u = new int[3];
	int[]	v = new int[3];
	int 	mAngle;
	int		mScale;
	//~ Constructors -----------------------------------------------------------


	/**
	 * Creates a new TextureWindow object.
	 */
	public TextureWindow()
	{
		super( true);		// fullscreen

		mRandom		= new Random();
		mScale		= 100;
		mAngle		= 0;
		mBitmap = Main.sInstance.getBitmap(Resources.ID_TEXTURE);
	}

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

	void updatePoints()
	{
		int w, h;

		w = mBitmap.getWidth();
		h = mBitmap.getHeight();

		u[0] = (w / 2);
		v[0] = 0;
		u[1] = w;
		v[1] = h;
		u[2] = 0;
		v[2] = h;

		// caution! w, h are now set to screen dimensions!!

/*
		x[0] = 64;
		y[0] = 88;
		x[1] = 91;
		y[1] = 120;
		x[2] = 102;
		y[2] = 118;

		x[0] = 64;
		y[0] = 88;
		x[1] = 102;
		y[1] = 118;
		x[2] = 91;
		y[2] = 120;
*/
		w = getWidth();
		h = getHeight();

		x[0] = (w / 2);
		y[0] = -40;		// test off top of screen clipping
		x[1] = w;
		y[1] = h;
		x[2] = 0;
		y[2] = h;


		/*
		x' -= cx
		y' -= cy
		x' = (Cos(Theta) * x - Sin(Theta) * y)
		y' = (Sin(Theta) * x + Cos(Theta) * y)
		x' += cx
		y' += cy
		*/
		int xn, yn, ox, oy;
		int cx = w / 2;		// center
		int cy = h / 2;
		for (int count = 0; count < x.length; count++)
		{
			ox = x[count] - cx;
			oy = y[count] - cy;

			ox = (ox * mScale) / 100;
			oy = (oy * mScale) / 100;

			xn = Fixed.multiply(Fixed.cos(mAngle), ox) - Fixed.multiply(Fixed.sin(mAngle), oy);
			yn = Fixed.multiply(Fixed.sin(mAngle), ox) + Fixed.multiply(Fixed.cos(mAngle), oy);

			x[count] = xn + cx;
			y[count] = yn + cy;
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param inPen DOCUMENT ME!
	 */
	public void paint(Pen inPen)
	{
		clear(inPen);
		inPen.fillTexturedPoly(mBitmap, x, y, u, v);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param e DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean receiveEvent(Event e)
	{
		boolean handled = false;

		switch (e.type)
		{
			case Event.EVENT_TIMER:
				invalidate();
				mAngle++;
				
				if (mAngle > 360)
				{
					mAngle = 0;
				}
				updatePoints();
				return true;
		}

		return super.receiveEvent(e);
	}

	public boolean eventWidgetUp(int inWhichWidget, Event event)
	{
		if (inWhichWidget == Event.DEVICE_WHEEL)
		{
			mAngle -= 5;
			if (mAngle < 0)
			{
				mAngle = 360-5;
			}
			return true;
		}
		return super.eventWidgetUp(inWhichWidget, event);
	}
	public boolean eventWidgetDown(int inWhichWidget, Event event)
	{
		switch (inWhichWidget)
		{
			case Event.DEVICE_ARROW_LEFT:
				mScale -= 9;
				if (mScale < 1)
				{
					mScale = 1;
				}
				return true;
			case Event.DEVICE_ARROW_RIGHT:
				mScale += 9;
				if (mScale > 2000)
				{
					mScale = 2000;
				}
				return true;
			case Event.DEVICE_WHEEL:
				mAngle = (mAngle + 5) % 360;
				return true;
		}
		return super.eventWidgetDown(inWhichWidget, event);
	}

	public boolean eventKeyDown(char inKey, Event event)
	{
		return super.eventKeyDown(inKey, event);
	}
}

// EOF
