/** 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/utils/menusystem/game_titles_world.cpp

*/

#include "sgx/sgx.hpp"
#include "skeleton/input/input.hpp"
#include "utils/menusystem/game_titles_world.hpp"

CGameTitlesWorld::CGameTitlesWorld(CSGXGameScenario *pNextScenario) {
	m_fTimecum = 0;
	m_fAlpha = 0;
	m_iStateScreen = 0;
	m_bFadingIn = TRUE;
	m_bFadingOut = FALSE;
	m_pNextScenario = pNextScenario;

	CSGXGameEngine::get()->registerScenarioObject(this);
	CSGXGraphicsEngine::get()->registerScenarioObject(this);
}

void 		
CGameTitlesWorld::initialize() {
}

void 		
CGameTitlesWorld::addScreen(const sgxString &filename) {
	m_ScreenList.push_back(CSGXTextureManager::get()->loadTexture(filename));
}


CGameTitlesWorld::~CGameTitlesWorld() {

	for(sgxVector<CSGXTexture *>::iterator it = m_ScreenList.begin();it!=m_ScreenList.end();++it) {
		delete (*it);
	}
}

tREAL32		
CGameTitlesWorld::getMinimumVisibility(const tUINT32 screen) {
	SGX_UNUSED(screen);
	return 1.1f;
}

tREAL32		
CGameTitlesWorld::getMaximumVisibility(const tUINT32 screen) {
	SGX_UNUSED(screen);
	return 6;
}

tBOOL		
CGameTitlesWorld::isInteruptable(const tUINT32 screen) {
	SGX_UNUSED(screen);
	return TRUE;
}

void 		
CGameTitlesWorld::doNextLoad(const tUINT32 phase) {
	SGX_UNUSED(phase);
}


	
void CGameTitlesWorld::update(const tREAL32 telaps) {
	if (m_bFadingIn) {
		m_fAlpha += telaps * 1.5f;
		if (m_fAlpha > 1.0f) {
			m_fAlpha = 1;
			m_bFadingIn = FALSE;
			m_fTimecum = 0;
			// TODO: deduct loading from the total time for displaying this screen
			doNextLoad(m_iStateScreen);
		}
		return;
	}
	if (m_bFadingOut) {
		m_fAlpha -= telaps * 1.5f;
		if (m_fAlpha < 0) {
			m_fAlpha = 0;
			m_bFadingOut = FALSE;
			m_bFadingIn = TRUE;
			//
			if (++m_iStateScreen == m_ScreenList.size()) {
				CSGXScenarioManager::get()->cueNextScenario(m_pNextScenario);
			}
		}
		return;
	}
	
	m_fTimecum += telaps;
	if (m_fTimecum > getMaximumVisibility(m_iStateScreen)) {
		m_bFadingOut = TRUE;
	} else if (m_fTimecum > getMinimumVisibility(m_iStateScreen)) {
		if (skelMouseHandler::m_bAnyButtonPressed) {
			m_bFadingOut = TRUE;
		}	
	}
}

void 	 
CGameTitlesWorld::draw() const {
	CSGXDrawSurface *pSurface = CSGXDrawSurfaceManager::get()->getDisplaySurface();
	pSurface->setFillColor(sgxColorRGBA::White);
	pSurface->setFillTexture(m_ScreenList[m_iStateScreen], 0);
	pSurface->fillBox();
	pSurface->setFillColor(m_fAlpha,m_fAlpha,m_fAlpha,m_fAlpha);
	pSurface->setFillTextureRegion(1);
	pSurface->fillPoint(pSurface->getWidth()/2, pSurface->getHeight()/2);
}

