Guest User

Untitled

a guest
Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #include "MainMenu.h"
  2. #include "Globals.h"
  3.  
  4. namespace Game
  5. {
  6. MainMenu::MainMenu()
  7. : tLMB( false ),
  8. State( eMenu_Main ),
  9. gState( eGame_MainMenu )
  10. {
  11. Bg = sf::Shape::Rectangle( 0.f, 0.f, ( float )MAX_X, ( float )MAX_Y, sf::Color( 0, 0, 0, sf::Uint8( 200 ) ) );
  12.  
  13. sf::Text *lP = new sf::Text( "Play", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
  14. lP->SetOrigin( ( float )( lP->GetRect().Width / 2.f ), ( float )( lP->GetRect().Height / 2.f ) );
  15. MenuButton *bP = new MenuButton;
  16. bP->Action = eMenu_SinglePlay;
  17. bP->Label = lP;
  18. bP->Show = eGame_MainMenu;
  19. Buttons.push_back( bP );
  20.  
  21. sf::Text *lR = new sf::Text( "Resume", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
  22. lR->SetOrigin( ( float )( lR->GetRect().Width / 2.f ), ( float )( lR->GetRect().Height / 2.f ) );
  23. MenuButton *bR = new MenuButton;
  24. bR->Action = eMenu_Resume;
  25. bR->Label = lR;
  26. bR->Show = eGame_Paused;
  27. Buttons.push_back( bR );
  28.  
  29. sf::Text *lEM = new sf::Text( "Exit to main menu", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
  30. lEM->SetOrigin( ( float )( lEM->GetRect().Width / 2.f ), ( float )( lEM->GetRect().Height / 2.f ) );
  31. MenuButton *bEM = new MenuButton;
  32. bEM->Action = eMenu_Main;
  33. bEM->Label = lEM;
  34. bEM->Show = eGame_Paused;
  35. Buttons.push_back( bEM );
  36.  
  37. sf::Text *lE = new sf::Text( "Exit", sf::Font::GetDefaultFont(), MENU_FONT_SIZE );
  38. lE->SetOrigin( ( float )( lE->GetRect().Width / 2.f ), ( float )( lE->GetRect().Height / 2.f ) );
  39. MenuButton *bE = new MenuButton;
  40. bE->Action = eMenu_Exit;
  41. bE->Label = lE;
  42. bE->Show = eGame_MainMenu | eGame_Paused;
  43. Buttons.push_back( bE );
  44. }
  45.  
  46. MainMenu::~MainMenu()
  47. {
  48. for(ButtonList::iterator iter = Buttons.begin(); iter != Buttons.end(); iter++)
  49. {
  50. MenuButton *mBut = *iter;
  51. delete mBut->Label;
  52. delete mBut;
  53. }
  54. Buttons.clear();
  55. }
  56.  
  57. void MainMenu::Draw( sf::RenderTarget &rt )
  58. {
  59. rt.Draw( Bg );
  60.  
  61. for( ButtonList::iterator iter = Buttons.begin(); iter != Buttons.end(); iter++ )
  62. if( ( *iter )->Show & gState )
  63. rt.Draw( *( ( *iter )->Label ) );
  64. }
  65.  
  66. void MainMenu::Update( float ft, const sf::Input &input, sf::RenderWindow &rw, sf::Vector2f &off )
  67. {
  68. if( Bg.GetColor().a < 200 )
  69. Bg.SetColor( sf::Color( 0, 0, 0, sf::Uint8( Bg.GetColor().a + 10 ) ) );
  70.  
  71. Bg.SetPosition( off );
  72.  
  73. int i = 0;
  74. int X = MAX_X / 2;
  75. int Y = MAX_Y / 2;
  76.  
  77. for( ButtonList::iterator iter = Buttons.begin(); iter != Buttons.end(); iter++ )
  78. {
  79. if( !( ( *iter )->Show & gState ) )
  80. continue;
  81.  
  82. i++;
  83. bool Active = false;
  84. int t = 50 * Buttons.size();
  85. int ys = ( int )( ( Y - t / 2.f ) + i * 50.f );
  86. int xs = X;
  87. MenuButton *mBut = *iter;
  88.  
  89. if( input.GetMouseX() > X - 125 && input.GetMouseX() < X + 125 )
  90. {
  91. if( input.GetMouseY() > ys - 20 && input.GetMouseY() < ys + 20 )
  92. {
  93. mBut->Label->SetCharacterSize( MENU_FONT_SIZE + 6 );
  94. mBut->Label->SetOrigin( ( float )( mBut->Label->GetRect().Width / 2.f ), ( float )( mBut->Label->GetRect().Height / 2.f ) );
  95. Active = true;
  96. }
  97. }
  98.  
  99. if(!Active)
  100. {
  101. mBut->Label->SetCharacterSize(MENU_FONT_SIZE);
  102. mBut->Label->SetOrigin( ( float )( mBut->Label->GetRect().Width / 2.f ), ( float )( mBut->Label->GetRect().Height / 2.f ) );
  103. }
  104.  
  105. mBut->Label->SetPosition( ( float )xs + off.x, ( float )ys + off.y );
  106.  
  107. if( Active && input.IsMouseButtonDown( sf::Mouse::Left ) && !tLMB )
  108. {
  109. switch( mBut->Action )
  110. {
  111. case eMenu_Exit:
  112. eMFunc_Exit( rw );
  113. break;
  114. case eMenu_SinglePlay:
  115. eMFunc_SinglePlay();
  116. break;
  117. case eMenu_Resume:
  118. eMFunc_Resume();
  119. break;
  120. case eMenu_Main:
  121. eMFunc_Main();
  122. break;
  123. default:
  124. eMFunc_DummyFunc();
  125. }
  126.  
  127. tLMB = true;
  128. }
  129.  
  130. }
  131.  
  132. if( tLMB && !input.IsMouseButtonDown( sf::Mouse::Left ) )
  133. tLMB = false;
  134. }
  135.  
  136. MenuState MainMenu::GetState()
  137. {
  138. return State;
  139. }
  140.  
  141. void MainMenu::eMFunc_DummyFunc()
  142. {
  143.  
  144. }
  145.  
  146. void MainMenu::eMFunc_Main()
  147. {
  148. gState = eGame_MainMenu;
  149. State = eMenu_Main;
  150. Act = eAction_ResetSnake;
  151. }
  152.  
  153. void MainMenu::eMFunc_Resume()
  154. {
  155. State = eMenu_SinglePlay;
  156. gState = eGame_Playing;
  157. }
  158.  
  159. void MainMenu::eMFunc_Exit( sf::RenderWindow &rw )
  160. {
  161. rw.Close();
  162. }
  163.  
  164. void MainMenu::eMFunc_SinglePlay()
  165. {
  166. State = eMenu_SinglePlay;
  167. gState = eGame_Playing;
  168. }
  169.  
  170. void MainMenu::SetState( MenuState mState )
  171. {
  172. if( mState == eMenu_Main )
  173. {
  174. Bg.SetColor( sf::Color( 0, 0, 0, 0 ) );
  175. gState = eGame_Paused;
  176. }
  177. if( mState == eMenu_SinglePlay )
  178. gState = eGame_Playing;
  179.  
  180. State = mState;
  181. }
  182.  
  183. Action MainMenu::RequestAction()
  184. {
  185. if( Act == eAction_None )
  186. return Act;
  187.  
  188. Action tAct = Act;
  189. Act = eAction_None;
  190. return tAct;
  191. }
  192. }
Add Comment
Please, Sign In to add comment