/** 
 * \file Menu.cpp
 * \brief CMenu member class functions implementation.
 * 
 * Main menu welcomes you to the game.
 * Contains buttons: Start Game, Options, Credits, Exit.
 * It also introduces animated 3D model hanging by the menu, reacting on user selection.
 *
 * \author Petar Bajic, MPE (C) All Rights Reserved, Homepage: www.mystic-peanut.com
 * \date January, 16 2007.
 */

#include "Menu.h"
#include "Language.h"
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 )

/**
 * CMenu standard constructor. 
 *
 * All menu varibles are initialized to 0 or alike. 
 * To see some real initialization check out CMenu::Init() function.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
CMenu::CMenu()
{
	m_menuPosX = 0;
	m_menuPosY = 0;
	m_texMenu = NULL;
	m_pFontHuge = NULL;
	m_pFontBig = NULL;
	m_pFont = NULL;
	m_sprite = NULL;
	m_startButton = NULL;
	m_optionsButton = NULL;
	m_creditsButton = NULL;
	m_exitButton = NULL;
	m_backButton = NULL;
	m_controlButton = NULL;
	m_difficultyButton = NULL;
	m_detailsButton = NULL;
	m_ModelTerrain = NULL;
	m_pTurtle = NULL;
	m_menuTheme = NULL;
	m_buttonTrans=D3DXVECTOR2(2.0f,0.0f);
	m_titleTrans=D3DXVECTOR2(0.0f,0.0f);
	m_footerTrans=D3DXVECTOR2(0.0f,0.0f);
	m_playCredits = false;
	m_difficult = false;
	m_detailed = false;
	m_showControls = false;
	m_InOptions = false;
	m_CreditsTimer = 0.0f;
	m_Language = NULL;
}

/**
 * CLevel destructor. Deletes all that has been alocated in Menu class. 
 * If you get memleak error after exiting program - here is the problem.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
CMenu::~CMenu()
{
	SAFE_RELEASE(m_sprite);
	SAFE_RELEASE(m_pFont);
	SAFE_RELEASE(m_pFontBig);
	SAFE_RELEASE(m_pFontHuge);
	SAFE_RELEASE(m_texMenu);
	delete m_ModelTerrain;
	delete m_menuTheme;
}

/**
 * Init function initializes all menu components. Music is loaded, buttons are created, everything is allocated here.
 * Some models are allocated before CMenu::Init (like turtle) because they are needed elsewhere (in movie, level, etc.)
 * These models are sent to Init as pointers (pTurtle), so CMenu can use them instead of doing new allocation.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
bool CMenu::Init(HWND hWnd, LPDIRECT3DDEVICE9 device, int screenWidth, int screenHeight, IDirectSound8* pDS, float dificulty, bool details, bool fullscreen, CTurtle* pTurtle, CLanguage* language)
{
	m_Language = language;

	LPDIRECT3DTEXTURE9 butTexOver, butTex;
	m_startButton = new C2DButton();
	m_optionsButton = new C2DButton();
	m_creditsButton = new C2DButton();
	m_exitButton = new C2DButton();
	m_backButton = new C2DButton();
	m_controlButton = new C2DButton();
	m_difficultyButton = new C2DButton();
	m_detailsButton = new C2DButton();

	if (dificulty == 3.0f)
		m_difficult = false;
	else
		m_difficult = true;
	m_detailed = details;

	// Here we decorate the main menu. Position of buttons, captions, textures, music...

	m_menuPosX = screenWidth/2 - 260;  //little bit left to the center
	m_menuPosY = screenHeight/2 - 30; //little bit above center

	//Title picture position
	m_titleTrans.x = (float) screenWidth/2 - 320;
	m_titleTrans.y = (float) screenHeight/2 - 210;

	//footer text position
	m_footerTrans.x = (float) screenWidth/2 - 280;
	m_footerTrans.y = (float) screenHeight/2 + 210;

	//play music
	m_menuTheme = new CMedia();
	if (!(m_menuTheme->Init(hWnd, L"Media/Music/menutheme.mp3")))
	{
		MessageBox(hWnd,L"Could not load Media/Music/menutheme.mp3 file",L"Error",MB_OK);
		return false;
	}

	WCHAR* focusSoundFile = L"Media/Sounds/buttover.wav";
	WCHAR* clickSoundFile = L"Media/Sounds/click.wav";

	WCHAR* startCaption = m_Language->GetText(MPE_TEXT_MENU_START);
	RECT startRect;
	startRect.top = m_menuPosY;
	startRect.left = m_menuPosX;
	startRect.bottom = startRect.top + 30;
	startRect.right = startRect.left + 65;
	RECT startTextRect; //rectangle for text within a sprite
	startTextRect.top = 8; //top is calculated from the top of the sprite
	startTextRect.bottom = 38;
	startTextRect.left = 0;
	startTextRect.right = 62;

	WCHAR* optionsCaption = m_Language->GetText(MPE_TEXT_MENU_OPTIONS);
	RECT optionsRect;
	optionsRect.top = m_menuPosY + 50;
	optionsRect.left = m_menuPosX;
	optionsRect.bottom = optionsRect.top + 30;
	optionsRect.right = optionsRect.left + 65;
	RECT optTextRect;
	optTextRect.top = 8;
	optTextRect.bottom = 38;
	optTextRect.left = 0;
	optTextRect.right = 62;

	WCHAR* creditsCaption = m_Language->GetText(MPE_TEXT_MENU_CREDITS);
	RECT creditsRect;
	creditsRect.top = m_menuPosY + 100;
	creditsRect.left = m_menuPosX;
	creditsRect.bottom = creditsRect.top + 30;
	creditsRect.right = creditsRect.left + 65;
	RECT creditsTextRect;
	creditsTextRect.top = 8;
	creditsTextRect.bottom = 38;
	creditsTextRect.left = 0;
	creditsTextRect.right = 62;

	WCHAR* exitCaption = m_Language->GetText(MPE_TEXT_MENU_EXIT);
	RECT exitRect;
	exitRect.top = m_menuPosY + 150;
	exitRect.left = m_menuPosX;
	exitRect.bottom = exitRect.top + 30;
	exitRect.right = exitRect.left + 65;
	RECT exitTextRect;
	exitTextRect.top = 8;
	exitTextRect.bottom = 38;
	exitTextRect.left = 0;
	exitTextRect.right = 62;

	WCHAR* backCaption = m_Language->GetText(MPE_TEXT_MENU_BACK);
	RECT backRect;
	backRect.top = m_menuPosY + 200;
	backRect.left = m_menuPosX;
	backRect.bottom = backRect.top + 30;
	backRect.right = backRect.left + 65;
	RECT backTextRect;
	backTextRect.top = 8;
	backTextRect.bottom = 38;
	backTextRect.left = 0;
	backTextRect.right = 62;

	WCHAR* controlCaption = m_Language->GetText(MPE_TEXT_MENU_CONTROLS); 
	WCHAR* difficultyCaption = m_Language->GetText(MPE_TEXT_MENU_DIFFICULTY);
	WCHAR* detailsCaption = m_Language->GetText(MPE_TEXT_MENU_DETAILS);

	//Load Static Models for 3D menu environment
	WCHAR strSearchDir[MAX_PATH]= L"Media//Models//";
	WCHAR strFullPath[MAX_PATH];
	StringCchCopy(strFullPath,MAX_PATH,strSearchDir);
	StringCchCat( strFullPath, MAX_PATH, L"terrain1.x");
	m_ModelTerrain = new CModelStatic();
	if (!(m_ModelTerrain->Load(device, strSearchDir, strFullPath)))
	{
		MessageBox(hWnd,L"Could not load Media/Models/terrain1.x file",L"Error",MB_OK);
		return false;
	}

	//Load Aniamted MOdels for menu 3D scene
	// Create our entity class
	m_pTurtle = pTurtle;

	if (FAILED(D3DXCreateTextureFromFile(device,L"Media/Icons/button.png", &butTex)))
	{
		MessageBox(hWnd,L"Could not load Media/Icons/button.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(device,L"Media/Icons/buttover.png", &butTexOver )))
	{
		MessageBox(hWnd,L"Could not load Media/Icons/buttover.png file",L"Error",MB_OK);
		return false;
	}
	/*if (FAILED(D3DXCreateTextureFromFile(device,L"Media/Pics/menuTitle.png", &m_titleTexture )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/menuTitle.png file",L"Error",MB_OK);
		return false;
	}*/

	if (SUCCEEDED(D3DXCreateFont(device,14, 0, 0, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial",&m_pFont)))
	if (SUCCEEDED(D3DXCreateFont(device,18, 0, 0, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial",&m_pFontBig)))
	if (SUCCEEDED(D3DXCreateFont(device,45, 0, 0, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial",&m_pFontHuge)))
	if (SUCCEEDED(D3DXCreateSprite(device,&m_sprite)))
	if(m_startButton->Init(hWnd, startRect, true, true, startCaption, startTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_optionsButton->Init(hWnd, optionsRect, true, true, optionsCaption, optTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_creditsButton->Init(hWnd, creditsRect, true, true, creditsCaption, creditsTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_exitButton->Init(hWnd, exitRect, true, true, exitCaption, exitTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_backButton->Init(hWnd, backRect, false, true, backCaption, backTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_controlButton->Init(hWnd, startRect, false, true, controlCaption, startTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_difficultyButton->Init(hWnd, optionsRect, false, true, difficultyCaption, optTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_detailsButton->Init(hWnd, creditsRect, false, true, detailsCaption, creditsTextRect, butTex, butTexOver, 
		pDS, clickSoundFile, focusSoundFile, MPE_BUTTON_TYPE_2DSWITCH))
	if(m_menuTheme->Play())
			return true;

	return false;
}

/**
 * \brief This function restarts displaying of Menu
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMenu::Restart()
{
	m_menuTheme->Play();
}
/**
 * \brief This function catches events for menu.
 * Menu could react on arrow keys, mouse move, etc...
 * \param code CMenu notifies CGame about event type with this parameter.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
bool CMenu::OnEvent(LPDIRECT3DDEVICE9 pd3dDevice, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, int* code)
{
	// Current mouse position
	int iMouseX = (int)LOWORD(lParam);
	int iMouseY = (int)HIWORD(lParam);
	POINT pnt;
	pnt.x = iMouseX;
	pnt.y = iMouseY;

    switch( msg )
    {
		case WM_KEYDOWN:
		{
			if(wParam == VK_RETURN)
			{
				if(m_startButton->GetFocus())
				{
					*code = MENU_OPTION_RETURN_NEWGAME;
					m_startButton->OnButtonPress();
					m_menuTheme->Stop();
					m_startButton->SetFocus(false); //defocus
					return true;
				}
				if(m_optionsButton->GetFocus())
				{
					m_optionsButton->OnButtonPress();
					m_optionsButton->SetFocus(false); //defocus before making it invisible cause else wont work
					m_startButton->SetVisible(false);
					m_optionsButton->SetVisible(false); //making it invisible here
					m_creditsButton->SetVisible(false);
					m_exitButton->SetVisible(false);
					m_controlButton->SetVisible(true);
					m_controlButton->SetFocus(true);
					m_difficultyButton->SetVisible(true);
					m_detailsButton->SetVisible(true);
					m_backButton->SetVisible(true);
					m_pTurtle->m_Model->NextAnimation();
					m_InOptions = true;
					*code = MENU_OPTION_RETURN_OPTIONS;
					return true;
				}
				if(m_creditsButton->GetFocus())
				{
					m_creditsButton->OnButtonPress();
					m_creditsButton->SetFocus(false); //defocus
					m_startButton->SetVisible(false);
					m_optionsButton->SetVisible(false); //making it invisible here
					m_creditsButton->SetVisible(false);
					m_exitButton->SetVisible(false);
					m_backButton->SetVisible(true);
					m_backButton->SetFocus(true);
					m_pTurtle->m_Model->NextAnimation();
					m_CreditsTimer = 0.0f;
					m_playCredits = true;
					return true;
				}
				if(m_exitButton->GetFocus())
				{
					m_exitButton->OnButtonPress();
					*code = MENU_OPTION_RETURN_EXIT;
					return true;
				}
				if(m_controlButton->GetFocus())
				{
					m_controlButton->OnButtonPress();
					m_controlButton->SetFocus(false);
					m_controlButton->SetVisible(false);
					m_difficultyButton->SetVisible(false);
					m_detailsButton->SetVisible(false);
					m_showControls = true;
					return true;
				}
				if(m_difficultyButton->GetFocus())
				{
					m_difficultyButton->OnButtonPress();
					if (m_difficult)
					{
						*code = MENU_OPTION_RETURN_DIFFICULTY_EASY; 
						m_difficult = !m_difficult; //click changed it to easy
					}
					else
					{
						*code = MENU_OPTION_RETURN_DIFFICULTY_HARD;
						m_difficult = !m_difficult;
					}
					return true;
				}
				if(m_detailsButton->GetFocus())
				{
					m_detailsButton->OnButtonPress();
					if (m_detailed)
					{
						*code = MENU_OPTION_RETURN_DETAILS_OFF;
						m_detailed = !m_detailed; //click changed it to off
					}
					else
					{
						*code = MENU_OPTION_RETURN_DETAILS_ON;
						m_detailed = !m_detailed;
					}
					return true;
				}
				if(m_backButton->GetFocus())
				{
					if (m_showControls)
					{
						m_controlButton->SetVisible(true);
						m_controlButton->SetFocus(true);
						m_difficultyButton->SetVisible(true);
						m_detailsButton->SetVisible(true);
						m_InOptions = true;
						m_showControls = false;
					}
					else
					{
						m_backButton->OnButtonPress();
						m_startButton->SetVisible(true);
						m_optionsButton->SetVisible(true);
						m_creditsButton->SetVisible(true);
						if(m_playCredits)
							m_creditsButton->SetFocus(true);
						else
							m_optionsButton->SetFocus(true);
						m_InOptions = false;
						m_exitButton->SetVisible(true);
						m_backButton->SetFocus(false);
						m_controlButton->SetFocus(false);
						m_difficultyButton->SetFocus(false);
						m_detailsButton->SetFocus(false);
						m_backButton->SetVisible(false);
						m_controlButton->SetVisible(false);
						m_difficultyButton->SetVisible(false);
						m_detailsButton->SetVisible(false);
						m_playCredits = false;
					}
					return true;
				}
			}
			if(wParam == VK_DOWN)
			{
				if(m_startButton->GetFocus())
				{
					m_startButton->SetFocus(false);
					m_optionsButton->SetFocus(true);
					return true;
				}
				if(m_optionsButton->GetFocus())
				{
					m_optionsButton->SetFocus(false);
					m_creditsButton->SetFocus(true);
					return true;
				}
				if(m_creditsButton->GetFocus())
				{
					m_creditsButton->SetFocus(false);
					m_exitButton->SetFocus(true);
					return true;
				}
				if(m_exitButton->GetFocus())
				{
					m_exitButton->SetFocus(false);
					m_startButton->SetFocus(true);
					return true;
				}
				if(m_controlButton->GetFocus())
				{
					m_controlButton->SetFocus(false);
					m_difficultyButton->SetFocus(true);
					return true;
				}
				if(m_difficultyButton->GetFocus())
				{
					m_difficultyButton->SetFocus(false);
					m_detailsButton->SetFocus(true);
					return true;
				}
				if(m_detailsButton->GetFocus())
				{
					m_detailsButton->SetFocus(false);
					m_backButton->SetFocus(true);
					return true;
				}
				if(m_backButton->GetFocus())
				{
					if(m_InOptions)
					{
						m_backButton->SetFocus(false);
						m_controlButton->SetFocus(true);
					}
				}

				m_startButton->SetFocus(true);
			}
			if(wParam == VK_UP)
			{
				if(m_startButton->GetFocus())
				{
					m_startButton->SetFocus(false);
					m_exitButton->SetFocus(true);
					return true;
					
				}
				if(m_optionsButton->GetFocus())
				{
					m_optionsButton->SetFocus(false);
					m_startButton->SetFocus(true);
					return true;
					
				}
				if(m_creditsButton->GetFocus())
				{
					m_creditsButton->SetFocus(false);
					m_optionsButton->SetFocus(true);
					return true;
				}
				if(m_exitButton->GetFocus())
				{
					m_exitButton->SetFocus(false);
					m_creditsButton->SetFocus(true);
					return true;
				}
				if(m_controlButton->GetFocus())
				{
					m_controlButton->SetFocus(false);
					m_backButton->SetFocus(true);
					return true;
				}
				if(m_difficultyButton->GetFocus())
				{
					m_difficultyButton->SetFocus(false);
					m_controlButton->SetFocus(true);
					return true;
				}
				if(m_detailsButton->GetFocus())
				{
					m_detailsButton->SetFocus(false);
					m_difficultyButton->SetFocus(true);
					return true;
				}
				if(m_backButton->GetFocus())
				{
					if(m_InOptions)
					{
						m_backButton->SetFocus(false);
						m_detailsButton->SetFocus(true);
					}
				}
				m_exitButton->SetFocus(true);
			}
		}
		break;
		//Left Mouse button pressed, if one of the buttons is in focus - handle event
		case WM_LBUTTONDOWN:
		{
				if(m_startButton->GetFocus())
				{
					*code = MENU_OPTION_RETURN_NEWGAME;
					m_startButton->OnButtonPress();
					m_menuTheme->Stop();
					m_startButton->SetFocus(false); //defocus
					return true;
				}
				if(m_optionsButton->GetFocus())
				{
					m_optionsButton->OnButtonPress();
					m_optionsButton->SetFocus(false); //defocus before making it invisible cause else wont work
					m_startButton->SetVisible(false);
					m_optionsButton->SetVisible(false); //making it invisible here
					m_creditsButton->SetVisible(false);
					m_exitButton->SetVisible(false);
					m_controlButton->SetVisible(true);
					m_difficultyButton->SetVisible(true);
					m_detailsButton->SetVisible(true);
					m_backButton->SetVisible(true);
					m_pTurtle->m_Model->NextAnimation();
					*code = MENU_OPTION_RETURN_OPTIONS;
					return true;
				}
				if(m_creditsButton->GetFocus())
				{
					m_creditsButton->OnButtonPress();
					m_creditsButton->SetFocus(false); //defocus
					m_startButton->SetVisible(false);
					m_optionsButton->SetVisible(false); //making it invisible here
					m_creditsButton->SetVisible(false);
					m_exitButton->SetVisible(false);
					m_backButton->SetVisible(true);
					m_pTurtle->m_Model->NextAnimation();
					m_CreditsTimer = 0.0f;
					m_playCredits = true;
					return true;
				}
				if(m_exitButton->GetFocus())
				{
					m_exitButton->OnButtonPress();
					*code = MENU_OPTION_RETURN_EXIT;
					return true;
				}
				if(m_controlButton->GetFocus())
				{
					m_controlButton->OnButtonPress();
					m_controlButton->SetFocus(false);
					m_controlButton->SetVisible(false);
					m_difficultyButton->SetVisible(false);
					m_detailsButton->SetVisible(false);
					m_showControls = true;
					return true;
				}
				if(m_difficultyButton->GetFocus())
				{
					m_difficultyButton->OnButtonPress();
					if (m_difficult)
					{
						*code = MENU_OPTION_RETURN_DIFFICULTY_EASY; 
						m_difficult = !m_difficult; //click changed it to easy
					}
					else
					{
						*code = MENU_OPTION_RETURN_DIFFICULTY_HARD;
						m_difficult = !m_difficult;
					}
					return true;
				}
				if(m_detailsButton->GetFocus())
				{
					m_detailsButton->OnButtonPress();
					if (m_detailed)
					{
						*code = MENU_OPTION_RETURN_DETAILS_OFF;
						m_detailed = !m_detailed; //click changed it to off
					}
					else
					{
						*code = MENU_OPTION_RETURN_DETAILS_ON;
						m_detailed = !m_detailed;
					}
					return true;
				}
				if(m_backButton->GetFocus())
				{
					if (m_showControls)
					{
						m_controlButton->SetVisible(true);
						m_difficultyButton->SetVisible(true);
						m_detailsButton->SetVisible(true);
						m_showControls = false;
					}
					else
					{
						m_backButton->OnButtonPress();
						m_startButton->SetVisible(true);
						m_optionsButton->SetVisible(true);
						m_creditsButton->SetVisible(true);
						m_exitButton->SetVisible(true);
						m_backButton->SetFocus(false);
						m_controlButton->SetFocus(false);
						m_difficultyButton->SetFocus(false);
						m_detailsButton->SetFocus(false);
						m_backButton->SetVisible(false);
						m_controlButton->SetVisible(false);
						m_difficultyButton->SetVisible(false);
						m_detailsButton->SetVisible(false);
						m_playCredits = false;
					}
					return true;
				}
		}
		break;
		case WM_MOUSEMOVE:
			{
				if(m_startButton->ContainsPoint(pnt))
					m_startButton->SetFocus(true);
				else
					m_startButton->SetFocus(false);
				if(m_optionsButton->ContainsPoint(pnt))
					m_optionsButton->SetFocus(true);
				else
					m_optionsButton->SetFocus(false);
				if(m_creditsButton->ContainsPoint(pnt))
					m_creditsButton->SetFocus(true);
				else
					m_creditsButton->SetFocus(false);
				if(m_exitButton->ContainsPoint(pnt))
					m_exitButton->SetFocus(true);
				else
					m_exitButton->SetFocus(false);				
				if(m_backButton->ContainsPoint(pnt))
					m_backButton->SetFocus(true);
				else
					m_backButton->SetFocus(false);				
				if(m_controlButton->ContainsPoint(pnt))
					m_controlButton->SetFocus(true);
				else
					m_controlButton->SetFocus(false);				
				if(m_difficultyButton->ContainsPoint(pnt))
					m_difficultyButton->SetFocus(true);
				else
					m_difficultyButton->SetFocus(false);	
				if(m_detailsButton->ContainsPoint(pnt))
					m_detailsButton->SetFocus(true);
				else
					m_detailsButton->SetFocus(false);
			}
	}

	return false;
}

/**
 * \brief This function is called when 3D device is lost.
 * Its important to call this function BEFORE device is reset, like when switching to/from fullscreen mode.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMenu::OnLostDevice()
{
	m_sprite->OnLostDevice();
	m_pFont->OnLostDevice();
	m_pFontBig->OnLostDevice();
	m_pFontHuge->OnLostDevice();
}

/**
 * \brief This function is called when 3D device is reset.
 * Its important to call this function AFTER device is reset, like when switching to/from fullscreen mode.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMenu::OnResetDevice(int screenWidth, int screenHeight)
{
	m_sprite->OnResetDevice();
	m_pFont->OnResetDevice();
	m_pFontBig->OnResetDevice();
	m_pFontHuge->OnResetDevice();
	
	//We can reorder menu depending on the new window width and height
	m_menuPosX = screenWidth/2 - 260;  //little bit left to the center
	m_menuPosY = screenHeight/2 - 30; //vertical center

	//Title picture position
	m_titleTrans.x = (float) screenWidth/2 - 320;
	m_titleTrans.y = (float) screenHeight/2 - 250;

	//footer text position
	m_footerTrans.x = (float) screenWidth/2 - 280;
	m_footerTrans.y = (float) screenHeight/2 + 210;

	RECT startRect;
	startRect.top = m_menuPosY;
	startRect.left = m_menuPosX;
	startRect.bottom = m_menuPosY + 30;
	startRect.right = m_menuPosX + 65;
	m_startButton->SetPosition(startRect);
	m_controlButton->SetPosition(startRect);
	RECT optionsRect;
	optionsRect.top = m_menuPosY + 50;
	optionsRect.left = m_menuPosX;
	optionsRect.bottom = optionsRect.top + 30;
	optionsRect.right = optionsRect.left + 65;
	m_optionsButton->SetPosition(optionsRect);
	m_difficultyButton->SetPosition(optionsRect);
	RECT creditsRect;
	creditsRect.top = m_menuPosY + 100;
	creditsRect.left = m_menuPosX;
	creditsRect.bottom = creditsRect.top + 30;
	creditsRect.right = creditsRect.left + 65;
	m_creditsButton->SetPosition(creditsRect);
	m_detailsButton->SetPosition(creditsRect);
	RECT exitRect;
	exitRect.top = m_menuPosY + 150;
	exitRect.left = m_menuPosX;
	exitRect.bottom = exitRect.top + 30;
	exitRect.right = exitRect.left + 65;
	m_exitButton->SetPosition(exitRect);
	RECT backRect;
	backRect.top = m_menuPosY + 200;
	backRect.left = m_menuPosX;
	backRect.bottom = backRect.top + 30;
	backRect.right = backRect.left + 65;
	m_backButton->SetPosition(backRect);
}

/**
 * \brief Renders the main menu screeen of the game. 
 * There can be title, buttons, 3D buttons, images and 3D objects as well.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMenu::OnRender(LPDIRECT3DDEVICE9 pd3dDevice)
{
	D3DXMATRIX mat;

	//render 3D terrain
	m_ModelTerrain->OnRender(pd3dDevice);

	//render animated 3D models
	m_pTurtle->OnRender(pd3dDevice);

	D3DXVECTOR2 spriteCentre=D3DXVECTOR2(0.0f,0.0f);
	D3DXVECTOR2 scaling=D3DXVECTOR2(1.0f,1.0f);
	float rotation=0; //timeGetTime()/500.0f;

	pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
	m_sprite->Begin(D3DXSPRITE_ALPHABLEND);

	RECT rect;
	rect.top = 8;
	rect.bottom = 168;
	rect.left = 0;
	rect.right = 640;
	scaling.x = 1.0f;
	scaling.y = 1.0f;

	//Render Game Title
	//my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &m_titleTrans, m_titleTexture, m_sprite, true, NULL, NULL, NULL, NULL);
	//m_titleTrans.x += 60;
	my_DrawOutlineText(&mat,&scaling,&spriteCentre,rotation,&m_titleTrans,m_sprite, m_pFontHuge, m_Language->GetText(MPE_TEXT_MENU_TITLE1), &rect, 0xFF008800, 0xFF000000);
	//my_DrawText	  (&mat,&scaling,&spriteCentre,rotation,&m_titleTrans,m_sprite, m_pFontHuge,m_Language->GetText(MPE_TEXT_MOVIE_TEXT1)L"TUKI THE TURTLE SHEPHERD", &rect, 0xFF005800, 0xFF000000);
	//m_titleTrans.x -= 60;
	m_titleTrans.y += 45;
	my_DrawOutlineText(&mat,&scaling,&spriteCentre,rotation,&m_titleTrans,m_sprite, m_pFontHuge, m_Language->GetText(MPE_TEXT_MENU_TITLE2), &rect, 0xFF009900, 0xFF000000);
	//my_DrawText	  (&mat,&scaling,&spriteCentre,rotation,&m_titleTrans,m_sprite, m_pFontHuge, L"Turtle Tuki Adventures: Vol.1", &rect, 0xFF007800);
	m_titleTrans.y -= 45;

	//Render text on the bottom of the menu screen
	rect.top = 8;
	rect.bottom = 168;
	rect.left = 0;
	rect.right = 462;
	scaling.x = 1.0f;
	scaling.y = 1.0f;
	my_DrawText(&mat,&scaling,&spriteCentre,rotation,&m_footerTrans,m_sprite,m_pFont,L"MPE (c) 2007 www.mystic-peanut.com, freeware, opensource, version 1.0v", &rect, 0xFF008800);

	scaling.x = .5f;
	scaling.y = .5f;

	//Render Menu Buttons last to be on top of all
	m_startButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);
	m_optionsButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);
	m_creditsButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);
	m_exitButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);
	m_backButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);
	m_controlButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);
	m_difficultyButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);
	m_detailsButton->OnRender(pd3dDevice, m_sprite, m_pFontBig, &scaling, &m_buttonTrans, &spriteCentre, rotation);

	scaling.x = 1.0f;
	scaling.y = 1.0f;
	
	//Play Credits
	if (m_playCredits)
	{
		D3DXVECTOR2 trans=D3DXVECTOR2((float)(m_menuPosX - 70),(float)(m_menuPosY + 150));
		trans.y -= m_CreditsTimer;
		RECT rect;
		rect.top = 0;
		rect.bottom = 50;
		rect.left = 0;
		rect.right = 350;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"programming:", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Petar Bajic", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Danijel Pribic", &rect, 0xFF880000);
		trans.y +=30;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"modeling:", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Darko Mitev", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Daniel Moro", &rect, 0xFF880000);
		trans.y +=30;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"music&sound:", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Cameron Sears http://www.cameronsears.tk", &rect, 0xFF880000);
		trans.y +=30;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"turtle+dwarf modeling: ", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Psionic3D www.psionic3D.com", &rect, 0xFF880000);
		trans.y +=30;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"tractor modeling: ", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Marko M. Markovic", &rect, 0xFF880000);
		trans.y +=30;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"special thanks", &rect, 0xFF880000);
		trans.y +=20;
		if((trans.y > m_menuPosY - 20)&&(trans.y < m_menuPosY + 170)) my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, L"Keith Ditchburn", &rect, 0xFF880000);
	}

	//Show controls
	if(m_showControls)
	{
		D3DXVECTOR2 trans=D3DXVECTOR2((float)(m_menuPosX - 130),(float)(m_menuPosY));
		my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_CONTROL_SHOOT), &rect, 0xFF880000);
		trans.y +=20;
		my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_CONTROL_PAUSE), &rect, 0xFF880000);
		trans.y +=20;
		my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_CONTROL_MOVE), &rect, 0xFF880000);
		trans.y +=20;
		my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_CONTROL_RUN), &rect, 0xFF880000);
	}

	//Show dificulty
	if(m_difficultyButton->m_bVisible)
	{
		D3DXVECTOR2 trans=D3DXVECTOR2((float)(m_menuPosX - 130),(float)(m_menuPosY + 50));
		if(m_difficult)
		{
			my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_DIFFICULTY_HARD), &rect, 0xFF880000);
		}
		else
		{
			my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_DIFFICULTY_EASY), &rect, 0xFF880000);
		}
	}

	//Show details
	if(m_detailsButton->m_bVisible)
	{
		D3DXVECTOR2 trans=D3DXVECTOR2((float)(m_menuPosX - 130),(float)(m_menuPosY + 100));
		if(m_detailed)
		{
			my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_DETAILS_ON), &rect, 0xFF880000);
		}
		else
		{
			my_DrawText(&mat,&scaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, m_Language->GetText(MPE_TEXT_MENU_DETAILS_OFF), &rect, 0xFF880000);
		}
	}

	m_sprite->End();
	pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
}

/**
 * \brief OnFrameMove is required for menu only if there is some sort of animation involved:
 * button onmouseover transition can be smooth or 3D objects could be animated, also
 * credits text can move up in lines like in movies.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMenu::OnFrameMove(LPDIRECT3DDEVICE9 pd3dDevice, double fTime, float fElapsedTime)
{
    D3DXMATRIXA16 matView;
	D3DXVECTOR3 eye = D3DXVECTOR3( -16.0f , 17.0f, 0.0f );
	D3DXVECTOR3 lookAt = D3DXVECTOR3( 0.0f, 2.0f, +4.0f );
	D3DXVECTOR3 upVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );

	m_CreditsTimer += 30*fElapsedTime;

	if (fTime < 8)
	{
		eye.y -= (float)fTime;
		eye.z += (float)fTime/4;
	}
	else
	{
		eye.y -=8;
		eye.z += 2;
	}
	//Position view matrix (camera)
    D3DXMatrixLookAtLH( &matView, &eye, &lookAt, &upVec );
    pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

	//Positioning menu 3D scenography
	D3DXVECTOR3 pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	float rot = 3.5f;
	m_ModelTerrain->OnFrameMove(pd3dDevice, rot, pos);

	D3DXMATRIXA16 matWorld, matTrans, matRotY;
	D3DXMatrixIdentity(&matWorld);
	D3DXMatrixIdentity(&matView);
	D3DXMatrixRotationY(&matRotY, 0.0f);
	D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, 0.0f);
	D3DXMatrixMultiply(&matWorld, &matWorld, &matRotY);
	D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans);
	pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
	m_pTurtle->OnFrameMove(pd3dDevice, fElapsedTime, &matWorld);

	m_startButton->OnFrameMove(pd3dDevice, fElapsedTime);
	m_optionsButton->OnFrameMove(pd3dDevice, fElapsedTime);
	m_creditsButton->OnFrameMove(pd3dDevice, fElapsedTime);
	m_exitButton->OnFrameMove(pd3dDevice, fElapsedTime);
	m_backButton->OnFrameMove(pd3dDevice, fElapsedTime);
	m_controlButton->OnFrameMove(pd3dDevice, fElapsedTime);
	m_difficultyButton->OnFrameMove(pd3dDevice, fElapsedTime);
	m_detailsButton->OnFrameMove(pd3dDevice, fElapsedTime);
}
