/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     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: sgx/src/sgx/gui/standard_skin.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/graphics/graphics.hpp"
#include "sgx/gui/gui.hpp"

//
// Skin overloads
//
sgxGUISkinStandard::sgxGUISkinStandard()  {
}

sgxGUISkinStandard::sgxGUISkinStandard(const sgxGUISkinSettings &settings) :
	CSGXGUISkin(settings) {
	m_pTexture = CSGXTextureManager::get()->loadTexture("sgx/interface");
}

sgxGUISkinnedImageButton *
sgxGUISkinStandard::getSkinnedImageButton() const {
	sgxGUISkinnedStdImageButton *ptr = new sgxGUISkinnedStdImageButton();
	ptr->m_pTexture = m_pTexture;
	return ptr;
}

sgxGUISkinnedButton *
sgxGUISkinStandard::getSkinnedButton() const {
	sgxGUISkinnedStdButton *ptr = new sgxGUISkinnedStdButton();
	ptr->m_pTexture = m_pTexture;
	return ptr;
}

sgxGUISkinnedCheckBox *
sgxGUISkinStandard::getSkinnedCheckBox() const {
	sgxGUISkinnedStdCheckBox *ptr = new sgxGUISkinnedStdCheckBox();
	ptr->m_pTexture = m_pTexture;
	return ptr;
}

sgxGUISkinnedRadioButton *
sgxGUISkinStandard::getSkinnedRadioButton() const {
	sgxGUISkinnedStdRadioButton *ptr = new sgxGUISkinnedStdRadioButton();
	ptr->m_pTexture = m_pTexture;
	return ptr;
}

sgxGUISkinnedHotSpot *
sgxGUISkinStandard::getSkinnedHotSpot() const {
	return new sgxGUISkinnedHotSpot();
}

//
// Individual widgets
//

void 
sgxGUISkinnedStdImageButton::draw(CSGXDrawSurface *pSurface) const {

	const CSGXGUISkin *pSkin = CSGXGUI::get()->getCurrentSkin();
	tBOOL useHoverGraphics = (!selected && pSkin->m_BaseSettings.m_bSupportHover && cursorOver);

	tUINT32 region = 0, regionHover = 18;
	switch(m_Type) {
		case eLeft:
			region = FACE_ARROW_LEFT;
			regionHover = FACE_ARROW_LEFT_HOVER;
			break;
		case eRight:
			region = FACE_ARROW_RIGHT;
			regionHover = FACE_ARROW_RIGHT_HOVER;
			break;
		case eUp:
			region = FACE_ARROW_UP;
			regionHover = FACE_ARROW_UP_HOVER;
			break;
		case eDown:
			region = FACE_ARROW_DOWN;
			regionHover = FACE_ARROW_DOWN_HOVER;
			break;
		case eCustom:
			region = m_iRegion;
			pSurface->setFillTexture(m_pTexture, selected?region:(useHoverGraphics?region+2:region+1));
			pSurface->setFillColor(enabled?sgxColorRGB::White:sgxColorRGB::DkGrey);	
			pSurface->fillPoint(topleft, CSGXDrawSurface::eFromTopLeft);
			return;	// do we don't redraw

		default:
			sgxTrace("Unknown image button image: %d", m_Type);
	}

	pSurface->setFillTexture(m_pTexture, selected?region:(useHoverGraphics?regionHover:region+1));
	pSurface->setFillColor(enabled?sgxColorRGB::White:sgxColorRGB::DkGrey);	
	pSurface->fillPoint(topleft, CSGXDrawSurface::eFromTopLeft);
}

void 
sgxGUISkinnedStdButton::draw(CSGXDrawSurface *pSurface) const {

	pSurface->setFillTexture(m_pTexture);
	pSurface->setFillColor(enabled?sgxColorRGB::White:sgxColorRGB::DkGrey);	

	const CSGXGUISkin *pSkin = CSGXGUI::get()->getCurrentSkin();
	tBOOL useHoverGraphics = (!selected && pSkin->m_BaseSettings.m_bSupportHover && cursorOver);
	if (pSkin->m_BaseSettings.m_bUseTiledButtons) {
		sgxPoint2i pos(topleft);
		tINT32 currentRegion = selected?FACE_BUTTON_LEFT_IN:FACE_BUTTON_LEFT_OUT;
		tINT32 rightWidth = m_pTexture->getRegionWidth(FACE_BUTTON_RIGHT);

		currentRegion = useHoverGraphics?FACE_BUTTON_HOVER_LEFT:currentRegion;
		
		// LHS
		pSurface->setFillTextureRegion(currentRegion);
		pSurface->fillPoint(pos, CSGXDrawSurface::eFromTopLeft);
		pos.x += m_pTexture->getRegionWidth(currentRegion);

		// Middle bit
		currentRegion = selected?FACE_BUTTON_MIDDLE_IN:FACE_BUTTON_MIDDLE_OUT;
		currentRegion = useHoverGraphics?FACE_BUTTON_HOVER_MIDDLE:currentRegion;
		pSurface->setFillTextureRegion(currentRegion);
		while(pos.x+rightWidth < bottomright.x) {
			pSurface->fillPoint(pos, CSGXDrawSurface::eFromTopLeft);
			pos.x += m_pTexture->getRegionWidth(currentRegion);;
		}
		
		// RHS
		currentRegion = selected?FACE_BUTTON_RIGHT_IN:FACE_BUTTON_RIGHT_OUT;
		currentRegion = useHoverGraphics?FACE_BUTTON_HOVER_RIGHT:currentRegion;
		pSurface->setFillTextureRegion(currentRegion);
		pSurface->fillPoint(pos, CSGXDrawSurface::eFromTopLeft);
			
	} else {
		pSurface->setFillTextureRegion(selected?FACE_BUTTON_LEFT_IN:FACE_BUTTON_LEFT_OUT);
		pSurface->fillBox(topleft, bottomright);
	}

	CSGXFontFormatting fmt(CSGXFontFormatting::fmtCentreJustify, CSGXFontFormatting::fmtMiddleAlignment);
	sgxPoint2i position(topleft);
	position += bottomright;
	position /= 2;
	position.x += (selected?1:0);
	position.y += (selected?1:0);
#if SGX_PLATFORM_NDS	
	pSurface->setFontColor(sgxColorRGBA::White);
#else
	pSurface->setFontColor(sgxColorRGBA::Black);
#endif
	pSurface->drawText(text.c_str(), position.x, position.y, fmt);
}

void 
sgxGUISkinnedStdCheckBox::draw(CSGXDrawSurface *pSurface) const {

	pSurface->setFillTexture(m_pTexture);
	pSurface->setFillTextureRegion(selected?FACE_CHECK_BOX_IN:FACE_CHECK_BOX_OUT);

	CSGXGraphicsEngine::get()->setColor(enabled?sgxColorRGB::White:sgxColorRGB::DkGrey);	

	pSurface->fillBox(topleft, bottomright);
}

void 
sgxGUISkinnedStdRadioButton::draw(CSGXDrawSurface *pSurface) const {

	pSurface->setFillTexture(m_pTexture);
	pSurface->setFillTextureRegion(selected?FACE_RADIO_BUTTON_IN:FACE_RADIO_BUTTON_OUT);

	CSGXGraphicsEngine::get()->setColor(enabled?sgxColorRGB::White:sgxColorRGB::DkGrey);	

	pSurface->fillBox(topleft, bottomright);
}

