/** 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/flying_menu.cpp

*/

#include "sgx/core/core.hpp"
#include "utils/menusystem/menusystem.hpp"

#define ANIMATING_MENUS	1

CFlyingMenu::CFlyingMenuSettings::CFlyingMenuSettings() {
	m_fEntranceSpeed = 540.0f;
	m_fExitSpeed = 600.0f;
	m_bAnimating = TRUE;
}

CFlyingMenu::CFlyingMenu(const CFlyingMenuSettings &settings) :
m_Settings(settings) {
	m_ButtonWidth = CSGXGUI::get()->getCurrentSkin()->getDefaultButtonWidth();
	m_TargetX = (CSGXGraphicsEngine::get()->getScreenWidth() - m_ButtonWidth) / 2;
}

tBOOL CFlyingMenu::onGameMenuGetCaptionPosition(const CGameMenuHandler *p, sgxPoint2i &pos, const char *pCaption) const {
	SGX_UNUSED(p);
	SGX_UNUSED(pCaption);
	pos.x = (CSGXGraphicsEngine::get()->getScreenWidth()) / 2;
#if SGX_PLATFORM_NDS
	pos.y = 5;
#else
	pos.y = 35;
#endif
	return TRUE;
}

tBOOL CFlyingMenu::onGameMenuGetWidgetPosition(const CGameMenuHandler *p, sgxPoint2i &pos, const tUINT32 idx, const sgxWidget *pWidget) const {
	SGX_UNUSED(p);
	pos.x = (CSGXGraphicsEngine::get()->getScreenWidth() - pWidget->getWidth()) / 2;
#if SGX_PLATFORM_NDS
	pos.y = 32 + 34*idx;
#else
	pos.y = 85 + (pWidget->getHeight()*idx*6)/5;
#endif
	return TRUE;
}

void CFlyingMenu::beginEntrance(CGameMenuHandler *p) {
	tINT32 idx = 0;
	sgxWidget *pButton;

	onGameMenuPrepareMenu(p);
	sgxPoint2i pos;

	tINT32 x1 = -m_ButtonWidth;
	tINT32 x2 = CSGXGraphicsEngine::get()->getScreenWidth();
	tUINT32 menuIndex = 0;

	while((pButton = p->getButtonWidget(idx)) != 0) {
		if (pButton->isVisible()) {
			onGameMenuGetWidgetPosition(p, pos, menuIndex, pButton);

			if (m_Settings.m_bAnimating) {
				pos.x = (idx&1)?x1:x2;
			}

			pButton->setPosition(pos.x, pos.y);
		    pButton->disable();
			++menuIndex;
			//y += m_Settings.m_ButtonSpacing;
		}
		++idx;
	}

}

tBOOL CFlyingMenu::updateEntrance(CGameMenuHandler *p, const tREAL32 telaps) {
	if (!m_Settings.m_bAnimating) {
		return TRUE;
	}
#if ANIMATING_MENUS	
	sgxPoint2i pt;

	tUINT32 buttonsReady = 0, buttonsValid = 0;
	tINT32 idx = 0;
	sgxWidget *pButton;
	while((pButton = p->getButtonWidget(idx)) != 0) {
		if (pButton->isVisible()) {
			++buttonsValid;
			pButton->getPosition(pt);
			if (idx&1) { // coming from left
				pt.x += (tINT32)(telaps * m_Settings.m_fEntranceSpeed);
				if (pt.x > m_TargetX) {
					pt.x = m_TargetX;
					++buttonsReady;
					pButton->enable();
				} 
			} else { // coming from right
				pt.x -= (tINT32)(telaps * m_Settings.m_fEntranceSpeed);
				if (pt.x < m_TargetX) {
					pt.x = m_TargetX;
					++buttonsReady;
					pButton->enable();
				} 
			}
			pButton->setPosition(pt);
		}
		++idx;
	}

	if (buttonsReady == buttonsValid) {
		//CSGXAudioEngine::get()->playSound(CGameAssets::m_pSoundSting);
		return TRUE;
	}
	return FALSE;
#else
	return TRUE;
#endif		
}

void CFlyingMenu::beginExit(CGameMenuHandler *p) {
#if ANIMATING_MENUS	
	if (m_Settings.m_bAnimating) {
		sgxWidget *pButton;
		tINT32 idx = 0;
		while((pButton = p->getButtonWidget(idx)) != 0) {
			pButton->disable();
			++idx;
		}
	}
#endif	
}

tBOOL CFlyingMenu::updateExit(CGameMenuHandler *p, const tREAL32 telaps) {
	if (!m_Settings.m_bAnimating) {
		return TRUE;
	}
#if ANIMATING_MENUS	
	sgxPoint2i pt;

	tUINT32 buttonsReady = 0, buttonsValid = 0;
	tINT32 idx = 0;
	tINT32 sw = CSGXGraphicsEngine::get()->getScreenWidth();
	sgxWidget *pButton;
	while((pButton = p->getButtonWidget(idx)) != 0) {
		if (pButton->isVisible()) {
			++buttonsValid;
			pButton->getPosition(pt);
			if (idx&1) { // exit to right
				pt.x += (tINT32)(telaps * m_Settings.m_fExitSpeed);
				if (pt.x > sw) {
					pt.x = sw;
					++buttonsReady;
				} 
			} else { // exit to left
				pt.x -= (tINT32)(telaps * m_Settings.m_fExitSpeed);
				if (pt.x < -m_ButtonWidth) {
					pt.x = -m_ButtonWidth;
					++buttonsReady;
				} 
			}
			pButton->setPosition(pt);
		}
		++idx;
	}

	return buttonsReady == buttonsValid;
#else
	return TRUE;
#endif		
}
