Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MainMenu.h"
- #include "Globals.h"
- namespace Game
- {
- MainMenu::MainMenu()
- : tLMB( false ),
- State( eMenu_Main ),
- gState( eGame_MainMenu )
- {
- Bg = sf::Shape::Rectangle( 0.f, 0.f, ( float )MAX_X, ( float )MAX_Y, sf::Color( 0, 0, 0, sf::Uint8( 200 ) ) );
- sf::Text *lP = new sf::Text( "Play", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
- lP->SetOrigin( ( float )( lP->GetRect().Width / 2.f ), ( float )( lP->GetRect().Height / 2.f ) );
- MenuButton *bP = new MenuButton;
- bP->Action = eMenu_SinglePlay;
- bP->Label = lP;
- bP->Show = eGame_MainMenu;
- Buttons.push_back( bP );
- sf::Text *lR = new sf::Text( "Resume", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
- lR->SetOrigin( ( float )( lR->GetRect().Width / 2.f ), ( float )( lR->GetRect().Height / 2.f ) );
- MenuButton *bR = new MenuButton;
- bR->Action = eMenu_Resume;
- bR->Label = lR;
- bR->Show = eGame_Paused;
- Buttons.push_back( bR );
- sf::Text *lEM = new sf::Text( "Exit to main menu", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
- lEM->SetOrigin( ( float )( lEM->GetRect().Width / 2.f ), ( float )( lEM->GetRect().Height / 2.f ) );
- MenuButton *bEM = new MenuButton;
- bEM->Action = eMenu_Main;
- bEM->Label = lEM;
- bEM->Show = eGame_Paused;
- Buttons.push_back( bEM );
- sf::Text *lE = new sf::Text( "Exit", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
- lE->SetOrigin( ( float )( lE->GetRect().Width / 2.f ), ( float )( lE->GetRect().Height / 2.f ) );
- MenuButton *bE = new MenuButton;
- bE->Action = eMenu_Exit;
- bE->Label = lE;
- bE->Show = eGame_MainMenu | eGame_Paused;
- Buttons.push_back( bE );
- }
- MainMenu::~MainMenu()
- {
- for(ButtonList::iterator iter = Buttons.begin(); iter != Buttons.end(); iter++)
- {
- MenuButton *mBut = *iter;
- delete mBut->Label;
- delete mBut;
- }
- Buttons.clear();
- }
- void MainMenu::Draw( sf::RenderTarget &rt )
- {
- rt.Draw( Bg );
- for( ButtonList::iterator iter = Buttons.begin(); iter != Buttons.end(); iter++ )
- if( ( *iter )->Show & gState )
- rt.Draw( *( ( *iter )->Label ) );
- }
- void MainMenu::Update( float ft, const sf::Input &input, sf::RenderWindow &rw, sf::Vector2f &off )
- {
- if( Bg.GetColor().a < 200 )
- Bg.SetColor( sf::Color( 0, 0, 0, sf::Uint8( Bg.GetColor().a + 10 ) ) );
- Bg.SetPosition( off );
- int i = 0;
- int X = MAX_X / 2;
- int Y = MAX_Y / 2;
- for( ButtonList::iterator iter = Buttons.begin(); iter != Buttons.end(); iter++ )
- {
- if( !( ( *iter )->Show & gState ) )
- continue;
- i++;
- bool Active = false;
- int t = 50 * Buttons.size();
- int ys = ( int )( ( Y - t / 2.f ) + i * 50.f );
- int xs = X;
- MenuButton *mBut = *iter;
- if( input.GetMouseX() > X - 125 && input.GetMouseX() < X + 125 )
- {
- if( input.GetMouseY() > ys - 20 && input.GetMouseY() < ys + 20 )
- {
- mBut->Label->SetCharacterSize( MENU_FONT_SIZE + 6 );
- mBut->Label->SetOrigin( ( float )( mBut->Label->GetRect().Width / 2.f ), ( float )( mBut->Label->GetRect().Height / 2.f ) );
- Active = true;
- }
- }
- if(!Active)
- {
- mBut->Label->SetCharacterSize(MENU_FONT_SIZE);
- mBut->Label->SetOrigin( ( float )( mBut->Label->GetRect().Width / 2.f ), ( float )( mBut->Label->GetRect().Height / 2.f ) );
- }
- mBut->Label->SetPosition( ( float )xs + off.x, ( float )ys + off.y );
- if( Active && input.IsMouseButtonDown( sf::Mouse::Left ) && !tLMB )
- {
- switch( mBut->Action )
- {
- case eMenu_Exit:
- eMFunc_Exit( rw );
- break;
- case eMenu_SinglePlay:
- eMFunc_SinglePlay();
- break;
- case eMenu_Resume:
- eMFunc_Resume();
- break;
- case eMenu_Main:
- eMFunc_Main();
- break;
- default:
- eMFunc_DummyFunc();
- }
- tLMB = true;
- }
- }
- if( tLMB && !input.IsMouseButtonDown( sf::Mouse::Left ) )
- tLMB = false;
- }
- MenuState MainMenu::GetState()
- {
- return State;
- }
- void MainMenu::eMFunc_DummyFunc()
- {
- }
- void MainMenu::eMFunc_Main()
- {
- gState = eGame_MainMenu;
- State = eMenu_Main;
- Act = eAction_ResetSnake;
- }
- void MainMenu::eMFunc_Resume()
- {
- State = eMenu_SinglePlay;
- gState = eGame_Playing;
- }
- void MainMenu::eMFunc_Exit( sf::RenderWindow &rw )
- {
- rw.Close();
- }
- void MainMenu::eMFunc_SinglePlay()
- {
- State = eMenu_SinglePlay;
- gState = eGame_Playing;
- }
- void MainMenu::SetState( MenuState mState )
- {
- if( mState == eMenu_Main )
- {
- Bg.SetColor( sf::Color( 0, 0, 0, 0 ) );
- gState = eGame_Paused;
- }
- if( mState == eMenu_SinglePlay )
- gState = eGame_Playing;
- State = mState;
- }
- Action MainMenu::RequestAction()
- {
- if( Act == eAction_None )
- return Act;
- Action tAct = Act;
- Act = eAction_None;
- return tAct;
- }
- }
Add Comment
Please, Sign In to add comment