using System;
using System.Drawing;
using Icarus.Graphics.Animation;

namespace Icarus.Graphics.Animation.GDI
{

    /**
     * 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 
		: AbstractFrame
	{
		/**
		 * The frame image...
		 */
		private Bitmap m_FrameImage;

		/**
		 * Doesn't do a whole lot...
		 */
		public override bool 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 override Bitmap getFrameImage() 
		{
			return m_FrameImage;
		}

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

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

		/**
		 * Returns the height of the frame image.
		 */
		public int getImageHeight() 
		{
			return m_FrameImage.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 override void Render(Rectangle coordinates, IRenderParameters iparams)
		{
			GDIRenderParameters p = (GDIRenderParameters) iparams;

			int x = coordinates.X - this.getAnchorPoint().X;
			int y = coordinates.Y - this.getAnchorPoint().Y;

			if (coordinates.Size == Size.Empty)
			{
				p.getGraphics().DrawImageUnscaled(m_FrameImage, x, y);
			} 
			else
			{
				int width = Math.Min(m_FrameImage.Width, coordinates.Width);
				int height = Math.Min(m_FrameImage.Height, coordinates.Height);
				Rectangle src = new Rectangle(0, 0, width, height);

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