package Icarus.Graphics.Animation.GDI;

import Icarus.Graphics.Animation.*;

import System.Drawing.*;

/**
 * This is a frame that can be rendered on a GDI control, such as a button, or something like that.
 * 
 * @author $Author: asskoala $
 * @version $Revision: 1.1 $ $Date: 2005/12/11 21:07:21 $
 */
public class GDIFrame extends AbstractFrame
{
	/**
	 * The frame image...
	 */
	private Bitmap m_FrameImage;

	/**
	 * Doesn't do a whole lot...
	 */
	protected boolean initializeFromBitmap(Bitmap image) 
	{
		m_FrameImage = image;

		return (m_FrameImage != null);
	}

	/**
	 * Returns a bitmap showing the contents of the frame. This is for saving a frame to a file.
	 */
	public Bitmap getFrameImage() 
	{
		return m_FrameImage;
	}

	/**
	 * Returns the size of the renderable.
	 */
	public Size GetSize()
	{
		return m_FrameImage.get_Size();
	}

	/**
	 * Returns the width of the frame image.
	 */
	public int getImageWidth() 
	{
		return m_FrameImage.get_Width();
	}

	/**
	 * Returns the height of the frame image.
	 */
	public int getImageHeight() 
	{
		return m_FrameImage.get_Height();
	}

	/**
	 * Will render the frame in the specified context, at the specified coordinates. Note that it is not necessarily the top-left corner of the frame that will be rendered at those coordinates, but rather the anchor point.
	 */
	public void Render(Rectangle coordinates, IRenderParameters params)
	{
		GDIRenderParameters p = (GDIRenderParameters) params;

		int x = coordinates.get_X() - this.getAnchorPoint().get_X();
		int y = coordinates.get_Y() - this.getAnchorPoint().get_Y();

		if (coordinates.get_Size() == Size.Empty)
		{
			p.getGraphics().DrawImageUnscaled(m_FrameImage, x, y);
		} 
		else
		{
			int width = Math.min(m_FrameImage.get_Width(), coordinates.get_Width());
			int height = Math.min(m_FrameImage.get_Height(), coordinates.get_Height());
			Rectangle src = new Rectangle(0, 0, width, height);

			p.getGraphics().DrawImage(m_FrameImage, x, y, src, GraphicsUnit.Pixel);
		}
	}
}
