/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2008.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: src/domains/graphics/surface/imagecanvas.cpp

*/

#include "sgx/core/core.hpp"
#include "domains/graphics/surface/surface.hpp"

// TODO: support an array of m_pImage (possibly through vector
// and addImageData(img, offsetX, offrsetY) methods so drawLine(e.g.)
// becomes:
// foreach(image)
// calc replacement x,y,x2,y2 coords based on image offset.
// crop each imagedata block according to that of the composite
// draw it
// uncrop
// rof
CImageCanvas::CImageCanvas(CImageData *pImage)
{
	m_pImage = pImage;
	m_DrawColor = sgxColorRGBA::Black;
	m_FillColor = sgxColorRGBA::White;
}

CImageCanvas::~CImageCanvas() 
{
}

void CImageCanvas::setDrawColor(sgxColorRGBA &col)
{
	m_DrawColor = col;
}


void CImageCanvas::setFillColor(sgxColorRGBA &col) 
{
	m_FillColor = col;
}


sgxColorRGBA	CImageCanvas::getDrawColor() const
{
	return m_DrawColor;
}


sgxColorRGBA	CImageCanvas::getFillColor() const
{
	return m_FillColor;
}



void CImageCanvas::setCrop()
{
// TODO m_pImage
}


void CImageCanvas::setCrop(sgxRect2i &cropRect)
{
	setCrop(cropRect.left, cropRect.top, cropRect.right, cropRect.bottom);
}

void CImageCanvas::setCrop(tINT32 x, tINT32 y, tINT32 x2, tINT32 y2)
{
	SGX_UNUSED(x);
	SGX_UNUSED(y);
	SGX_UNUSED(x2);
	SGX_UNUSED(y2);
}

void CImageCanvas::drawLine(tINT32 x, tINT32 y, tINT32 x2, tINT32 y2)
{
	SGX_UNUSED(x);
	SGX_UNUSED(y);
	SGX_UNUSED(x2);
	SGX_UNUSED(y2);
}


void CImageCanvas::drawRect(tINT32 x, tINT32 y, tINT32 x2, tINT32 y2)
{
	if (x2 > x) {
		for(tINT32 i=x;i<=x2;++i) {
			m_pImage->setPixelAt(m_DrawColor, i, y);
			m_pImage->setPixelAt(m_DrawColor, i, y2);
		}
	} else if (x > x2) {
		for(tINT32 i=x2;i>=x;--i) {
			m_pImage->setPixelAt(m_DrawColor, i, y);
			m_pImage->setPixelAt(m_DrawColor, i, y2);
		}
	}
	//
	if (y2 > y) {
		for(tINT32 i=y;i<=y2;++i) {
			m_pImage->setPixelAt(m_DrawColor, x, i);
			m_pImage->setPixelAt(m_DrawColor, x2, i);
		}
	} else if (y > y2) {
		for(tINT32 i=y2;i>=y;--i) {
			m_pImage->setPixelAt(m_DrawColor, x, i);
			m_pImage->setPixelAt(m_DrawColor, x2, i);
		}
	}
}


void CImageCanvas::drawCircle(tINT32 x, tINT32 y, tINT32 r)
{
	SGX_UNUSED(x);
	SGX_UNUSED(y);
	SGX_UNUSED(r);
}


void CImageCanvas::drawEllipse(tINT32 x, tINT32 y, tINT32 rx, tINT32 ry)
{
	SGX_UNUSED(x);
	SGX_UNUSED(y);
	SGX_UNUSED(rx);
	SGX_UNUSED(ry);
}



void CImageCanvas::fillRect(tINT32 x, tINT32 y, tINT32 x2, tINT32 y2)
{
	drawRect(x, y, x2, y2);
	
	tINT32 fromX = sgxMin(x, x2);
	tINT32 fromY = sgxMin(y, y2);
	tINT32 toX = sgxMax(x, x2);
	tINT32 toY = sgxMax(y, y2);

	if (toX-fromX < 2 || toY-fromY < 2) {
		return;
	}

	// Shrink to only fill the internals
	--toX; --toY;
	++fromX; ++fromY;

	for(tINT32 iy=fromY;iy<=toY;++iy) {
		for(tINT32 ix=fromX;ix<=toX;++ix) {
			m_pImage->setPixelAt(m_FillColor, ix, iy);
		}
	}

}


void CImageCanvas::fillCircle(tINT32 x, tINT32 y, tINT32 r)
{
	SGX_UNUSED(x);
	SGX_UNUSED(y);
	SGX_UNUSED(r);
}


void CImageCanvas::fillEllipse(tINT32 x, tINT32 y, tINT32 rx, tINT32 ry)
{
	SGX_UNUSED(x);
	SGX_UNUSED(y);
	SGX_UNUSED(rx);
	SGX_UNUSED(ry);
}

