/***
 * 
 * Copyright (C) 2008 Alessandro La Rosa
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: alessandro.larosa@gmail.com
 *
 * Author: Alessandro La Rosa
 */

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ExplodingImage
{
	public static final int DEFAULT_LEVEL = 7;
	public int level = 7;
	
	public int vPieces = 4;
	public int hPieces = 8;
	
	Image image = null;
	int imageWidth = 0;
	int imageHeight = 0;
	long duration = 0;
	long startTime = 0;
	
	public boolean exploding = false;
	public boolean ended = false;
	
	public ExplodingImage(Image image, int level, int hPieces, int vPieces)
	{
		this.level = level;
		this.hPieces = hPieces;
		this.vPieces = vPieces;
		this.image = image;
		this.imageWidth = image.getWidth();
		this.imageHeight = image.getHeight();
	}
	public void explode(long duration)
	{
		this.duration = duration;
		this.startTime = System.currentTimeMillis();
		
		ended = false;
		exploding = true;
	}
	public void paint(Graphics g, int x, int y, int constraints)
	{
		if(ended)
		{
			return;
		}
		
		int cx, cy, cw, ch;
		
		if(!exploding)
		{
			g.drawImage(image, x, y, constraints);
		}
		else
		{
			cx = g.getClipX();
			cy = g.getClipY();
			cw = g.getClipWidth();
			ch = g.getClipHeight();
			
			int translateX = (constraints & Graphics.RIGHT) > 0 ? x - imageWidth :
				((constraints & Graphics.HCENTER) > 0 ? x - imageWidth / 2 :
					x);
			int translateY = (constraints & Graphics.BOTTOM) > 0 ? y - imageHeight :
				((constraints & Graphics.VCENTER) > 0 ? y - imageHeight / 2 :
					y);
			
			g.translate(translateX, translateY);
			
			long timeDiff = System.currentTimeMillis() - startTime;
			
			if(timeDiff > duration)
			{
				timeDiff = duration;
				
				ended = true;
				
				exploding = false;
			}
			
			int perc = (int)(100 * timeDiff / duration);
			
			int pieceEndX = 0;
			int pieceEndY = 0;
			int pieceX = 0;
			int pieceY = 0;
			int pieceWidth = imageWidth / hPieces;
			int pieceHeight = imageHeight / vPieces;
			
			int centerX = (imageWidth - pieceWidth) / 2;
			int centerY = (imageHeight - pieceHeight) / 2;
			
			for(int i = 0; i < hPieces; i++)
			{
				for(int j = 0; j < vPieces; j++)
				{
					pieceEndX = i * pieceWidth + (i * pieceWidth - centerX) * level;
					pieceEndY = j * pieceHeight + (j * pieceHeight - centerY) * level;
					
					pieceX = i * pieceWidth + (pieceEndX - i * pieceWidth) * perc / 100;
					pieceY = j * pieceHeight + (pieceEndY - j * pieceHeight) * perc / 100;
					
					g.setClip(pieceX, pieceY, pieceWidth, pieceHeight);
					
					g.drawImage(image, pieceX - i * pieceWidth, pieceY - j * pieceHeight, Graphics.TOP | Graphics.LEFT);
				}
			}
			
			g.translate(- translateX, - translateY);
			
			g.setClip(cx, cy, cw, ch);
		}
	}
}
