| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "Menu.hpp" | ||
| 2 | #include "../core/Core.hpp" | ||
| 3 | |||
| 4 | ✗ | Menu::Menu(Core *core) : _core(core) { | |
| 5 | ✗ | _fullscreenButton = std::make_shared<Button>(sf::Vector2f(100, 100), | |
| 6 | ✗ | sf::Vector2f(100, 100), "Windowed", _core->getFont()); | |
| 7 | ✗ | _resolutionButtons.push_back(std::make_shared<Button>(sf::Vector2f(100, 150), | |
| 8 | ✗ | sf::Vector2f(100, 100), "1920x1080", _core->getFont())); | |
| 9 | ✗ | _resolutionButtons.push_back(std::make_shared<Button>(sf::Vector2f(300, 150), | |
| 10 | ✗ | sf::Vector2f(100, 100), "1280x720", _core->getFont())); | |
| 11 | ✗ | _quitButton = std::make_shared<Button>(sf::Vector2f(100, 500), sf::Vector2f(100, 100), "Quit", _core->getFont()); | |
| 12 | ✗ | _homeButton = std::make_shared<Button>(sf::Vector2f(100, 450), sf::Vector2f(100, 100), "Home", _core->getFont()); | |
| 13 | ✗ | _homeButton->setCallBack(std::bind(&Menu::getBackHome, this)); | |
| 14 | ✗ | _hzInput = std::make_shared<Input>(sf::Vector2f(100, 200), sf::Vector2f(100, 100), "60", _core->getFont(), "1234567890"); | |
| 15 | ✗ | _hzInput->setEnd("Hz"); | |
| 16 | ✗ | _hzInput->setInput("60"); | |
| 17 | ✗ | _funMode = std::make_shared<Button>(sf::Vector2f(100, 250), sf::Vector2f(100, 100), "Fun Mode Off", _core->getFont()); | |
| 18 | ✗ | } | |
| 19 | |||
| 20 | ✗ | bool Menu::update(sf::Event event, sf::RenderWindow &window) { | |
| 21 | ✗ | if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) | |
| 22 | ✗ | _core->_upperState = GameState::DEFAULT; | |
| 23 | ✗ | if (_fullscreenButton->update(event, window)) { | |
| 24 | ✗ | _core->switchFullscreen(); | |
| 25 | ✗ | _fullscreenButton->setText(_core->isFullscreen() ? "Fullscreen" : "Windowed"); | |
| 26 | ✗ | return true; | |
| 27 | } | ||
| 28 | ✗ | if (_resolutionButtons[0]->update(event, window)) { | |
| 29 | ✗ | _core->newResolution(sf::Vector2f(1920, 1080)); | |
| 30 | ✗ | return true; | |
| 31 | } | ||
| 32 | ✗ | if (_resolutionButtons[1]->update(event, window)) { | |
| 33 | ✗ | _core->newResolution(sf::Vector2f(1280, 720)); | |
| 34 | ✗ | return true; | |
| 35 | } | ||
| 36 | ✗ | if (_funMode->update(event, window)) { | |
| 37 | ✗ | if (_funMode->getText() == "Fun Mode Off") | |
| 38 | ✗ | _funMode->setText("Fun Mode On"); | |
| 39 | else | ||
| 40 | ✗ | _funMode->setText("Fun Mode Off"); | |
| 41 | ✗ | _core->_funMode = !_core->_funMode; | |
| 42 | } | ||
| 43 | ✗ | if (_quitButton->update(event, window)) | |
| 44 | ✗ | window.close(); | |
| 45 | ✗ | _homeButton->update(event, window); | |
| 46 | ✗ | if (_hzInput->isFocused() == false) { | |
| 47 | ✗ | if (_hzInput->getInput().size() == 0) | |
| 48 | ✗ | _hzInput->setInput("0"); | |
| 49 | ✗ | if (std::atoi(_hzInput->getInput().c_str()) != _core->_tickRate) | |
| 50 | ✗ | _hzInput->setInput(std::to_string(_core->_tickRate)); | |
| 51 | } | ||
| 52 | ✗ | if (_hzInput->update(event, window)) { | |
| 53 | int newTickRate = 2; | ||
| 54 | try { | ||
| 55 | ✗ | newTickRate = std::stoi(_hzInput->getInput()); | |
| 56 | ✗ | } catch (std::exception &e) { | |
| 57 | ✗ | } | |
| 58 | ✗ | if (newTickRate < 2) | |
| 59 | newTickRate = 2; | ||
| 60 | ✗ | if (newTickRate > 1000) | |
| 61 | newTickRate = 1000; | ||
| 62 | ✗ | _core->_tickRate = newTickRate; | |
| 63 | ✗ | _core->_data.requestNewTickRate(newTickRate, _core->_server); | |
| 64 | ✗ | _hzInput->setInput(std::to_string(newTickRate)); | |
| 65 | ✗ | return true; | |
| 66 | } | ||
| 67 | return true; | ||
| 68 | } | ||
| 69 | |||
| 70 | ✗ | void Menu::draw(sf::RenderWindow &window) { | |
| 71 | ✗ | _fullscreenButton->draw(window); | |
| 72 | ✗ | for (auto &button : _resolutionButtons) | |
| 73 | ✗ | button->draw(window); | |
| 74 | ✗ | _quitButton->draw(window); | |
| 75 | ✗ | _homeButton->draw(window); | |
| 76 | ✗ | _funMode->draw(window); | |
| 77 | ✗ | _hzInput->draw(window, _core->getDeltaTime()); | |
| 78 | ✗ | } | |
| 79 | |||
| 80 | ✗ | void Menu::getBackHome() { | |
| 81 | ✗ | if (_core->_server.disconectFromServer() == true) | |
| 82 | ✗ | _core->_data.resetGame(); | |
| 83 | ✗ | _core->_upperState = GameState::DEFAULT; | |
| 84 | ✗ | _core->_state = GameState::HOME; | |
| 85 | ✗ | } | |
| 86 |