package Icarus.Graphics.Animation;

import System.Drawing.Rectangle;
import System.Drawing.Size;

/**
 * This thingey will render and grid of child renderables, where the count of the renderables depends on the GridSize property.
 */
public class TiledFrame extends AbstractRenderable
{
	/**
	 * The size of the tiled frame. Basically, this is the number of times the frame will be tiled.
	 */
	private Size mGridSize;

	/**
	 * This is the renderable that will be tiled.
	 */
	private IRenderable mChildRenderable;

	/** @property */
	public Size get_GridSize() { return mGridSize; }
	/** @property */
	public void set_GridSize(Size v) { mGridSize = v; }

	/** @property */
	public IRenderable get_ChildRenderable() { return mChildRenderable; }
	/** @property */
	public void set_ChildRenderable(IRenderable v) { mChildRenderable = v; }

	/**
	 * Returns the size of the renderable.
	 */
	public Size GetSize()
	{
		Size result = this.get_ChildRenderable().GetSize();
		result.set_Width(result.get_Width() * this.get_GridSize().get_Width());
		result.set_Height(result.get_Height() * this.get_GridSize().get_Height());

		return result;
	}

	/**
	 * Renders the thing. Note that the destination parameter can be used to specify clipping. The size of the rectangle can be empty, in which case no clipping will be applied.
	 */
	public void Render(Rectangle destination, IRenderParameters parameters) 
	{
		for (int r = 0; r < this.get_GridSize().get_Height(); r++)
		{
			int yoffset = this.get_ChildRenderable().GetSize().get_Height() * r;
			destination.set_Y(destination.get_Y() + yoffset);

			for (int c = 0; c < this.get_GridSize().get_Width(); c++)
			{
				int xoffset = this.get_ChildRenderable().GetSize().get_Width() * c;
				destination.set_X(destination.get_X() + xoffset);

				this.get_ChildRenderable().Render(destination, parameters);
				
				destination.set_X(destination.get_X() - xoffset);
			}
			
			destination.set_Y(destination.get_Y() - yoffset);
		}
	}
}
