Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include "MenuScreen.hpp"
  2. #include "../game.hpp"
  3. #include <SFML/Graphics.hpp>
  4. #include "../player/player.hpp"
  5. #include "../customObjects/menuButton.hpp"
  6.  
  7. void MenuScreen::run(sf::RenderWindow& window, GameState& state, Player& plr) {
  8.  
  9.     enum selection {
  10.         Play,
  11.         Exit,
  12.         Nothing
  13.     };
  14.     selection hoveredSelection = selection::Nothing;
  15.     selection enteredSelection = selection::Nothing;
  16.    
  17.     std::vector<sf::Drawable*> screenElements;
  18.     std::vector<menuButton> buttons;
  19.    
  20.     bool exiting = false;
  21.  
  22.     sf::Texture bg;
  23.     if (bg.loadFromFile("assets/mainMenu/menuBackground.png") != true) {
  24.         return;
  25.     }
  26.     sf::Sprite menuBg(bg);
  27.     screenElements.push_back(&menuBg);
  28.    
  29.     menuButton exitButton("Exit", sf::Vector2f(200, 100), sf::Vector2f(window.getSize().x / 2, (window.getSize().y / 5) * 4));
  30.     screenElements.push_back(&exitButton);
  31.     buttons.push_back(exitButton);
  32.    
  33.     menuButton playButton("Play", sf::Vector2f(200, 100), sf::Vector2f(window.getSize().x / 2, (window.getSize().y / 5) * 2));
  34.     screenElements.push_back(&playButton);
  35.     buttons.push_back(playButton);
  36.  
  37.     sf::CircleShape activeButton(10);
  38.     activeButton.setFillColor(sf::Color::Red);
  39.     activeButton.setOrigin(10 / 2, 10 / 2);
  40.     activeButton.setPosition(((exitButton.getPos().x) - (exitButton.getSize().x / 2) - 10), (exitButton.getPos().y) - exitButton.getSize().y / 2);
  41.     screenElements.push_back(&activeButton);
  42.    
  43.     sf::Event evnt;
  44.     while (!exiting) {
  45.         // Event Processor
  46.         while (window.pollEvent(evnt)) {
  47.             if (sf::Event::KeyPressed) {
  48.                 switch (evnt.key.code)
  49.                 {
  50.                 case sf::Keyboard::Up:
  51.                     if (hoveredSelection > 0) {
  52.                     }
  53.                 case sf::Keyboard::Down:
  54.                     if (hoveredSelection < 1) {
  55.  
  56.                     }
  57.                 }
  58.             } else if (sf::Event::MouseButtonPressed) {
  59.                 sf::Vector2i mousePos = sf::Mouse::getPosition(window);
  60.                 sf::Vector2f mousePosF(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
  61.                 for (auto& element : buttons) {
  62.                     if (element.getBounds().contains(mousePosF)) {
  63.                         if (element.getName() == "Play") {
  64.                             hoveredSelection = selection::Play;
  65.                         }
  66.                         else if (element.getName() == "Exit") {
  67.                             hoveredSelection = selection::Exit;
  68.                         }
  69.                     }
  70.                 }
  71.             } else if (sf::Event::MouseMoved) {
  72.                 sf::Vector2i mousePos = sf::Mouse::getPosition(window);
  73.                 sf::Vector2f mousePosF(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
  74.                 for (auto& element : buttons) {
  75.                     if (element.getBounds().contains(mousePosF)) {
  76.                         if (element.getName() == "Play") {
  77.                             hoveredSelection = selection::Play;
  78.                         } else if (element.getName() == "Exit") {
  79.                             hoveredSelection = selection::Exit;
  80.                         }
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         // Logic
  86.         if (enteredSelection != selection::Nothing)
  87.             switch (enteredSelection)
  88.             {
  89.             case selection::Play:
  90.                 state = GameState::Playing;
  91.                 exiting = true;
  92.             case selection::Exit:
  93.                 state = GameState::Exiting;
  94.                 exiting = true;
  95.             }
  96.         // Drawing
  97.         window.clear();
  98.         for (const auto& element : screenElements) {
  99.             window.draw(*element);
  100.         }
  101.         window.display();
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement