1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +01:00

Convert Menu instances to std::optional

This commit is contained in:
Filip Gawin 2018-12-01 18:26:35 +01:00
parent 28155f5aa8
commit d5541ac91f
5 changed files with 32 additions and 27 deletions

View File

@ -1,8 +1,10 @@
#ifndef _GAME_MENUSYSTEM_HPP_ #ifndef _GAME_MENUSYSTEM_HPP_
#define _GAME_MENUSYSTEM_HPP_ #define _GAME_MENUSYSTEM_HPP_
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <glm/glm.hpp> #include <glm/glm.hpp>
@ -76,12 +78,11 @@ public:
/** /**
* @brief creates a menu from the given menu items * @brief creates a menu from the given menu items
* @return a shared pointer to the menu with the items * @return optional of menu with the items
*/ */
static std::shared_ptr<Menu> create(std::vector<MenuEntry> items, static std::optional<Menu> create(std::vector<MenuEntry> items,
int font = MenuDefaults::kFont, int font = MenuDefaults::kFont, float size = 30.f) {
float size = 30.f) { return std::make_optional<Menu>(std::move(items), font, size);
return std::make_shared<Menu>(std::move(items), font, size);
} }
Menu& lambda(const GameString& n, std::function<void()> callback) { Menu& lambda(const GameString& n, std::function<void()> callback) {

View File

@ -1,8 +1,12 @@
#ifndef RWGAME_STATE_HPP #ifndef RWGAME_STATE_HPP
#define RWGAME_STATE_HPP #define RWGAME_STATE_HPP
#include <optional>
#include <render/ViewCamera.hpp> #include <render/ViewCamera.hpp>
#include "GameWindow.hpp" #include "GameWindow.hpp"
#include "MenuSystem.hpp" #include "MenuSystem.hpp"
#include <SDL.h> #include <SDL.h>
#include <SDL_events.h> #include <SDL_events.h>
@ -12,8 +16,8 @@ class StateManager;
class State { class State {
public: public:
std::shared_ptr<Menu> menu; std::optional<Menu> menu;
std::shared_ptr<Menu> nextMenu; std::optional<Menu> nextMenu;
RWGame* game; RWGame* game;
@ -33,16 +37,17 @@ public:
virtual ~State() = default; virtual ~State() = default;
void enterMenu(const std::shared_ptr<Menu>& menu) { template<typename T>
nextMenu = menu; void enterMenu(T&& menu) {
nextMenu = std::forward<T>(menu);
} }
Menu* getCurrentMenu() { Menu* getCurrentMenu() {
if (nextMenu) { if (nextMenu) {
menu = nextMenu; menu = std::move(nextMenu);
nextMenu = nullptr; nextMenu = std::nullopt;
} }
return menu.get(); return &*menu;
} }
virtual void handleEvent(const SDL_Event& e); virtual void handleEvent(const SDL_Event& e);

View File

@ -33,7 +33,7 @@ static void jumpCharacter(RWGame* game, CharacterObject* player,
} }
} }
std::shared_ptr<Menu> DebugState::createDebugMenu() { std::optional<Menu> DebugState::createDebugMenu() {
CharacterObject* player = nullptr; CharacterObject* player = nullptr;
if (game->getWorld()->getPlayer()) { if (game->getWorld()->getPlayer()) {
player = game->getWorld()->getPlayer()->getCharacter(); player = game->getWorld()->getPlayer()->getCharacter();
@ -76,7 +76,7 @@ std::shared_ptr<Menu> DebugState::createDebugMenu() {
return menu; return menu;
} }
std::shared_ptr<Menu> DebugState::createMapMenu() { std::optional<Menu> DebugState::createMapMenu() {
CharacterObject* player = nullptr; CharacterObject* player = nullptr;
if (game->getWorld()->getPlayer()) { if (game->getWorld()->getPlayer()) {
player = game->getWorld()->getPlayer()->getCharacter(); player = game->getWorld()->getPlayer()->getCharacter();
@ -144,7 +144,7 @@ std::shared_ptr<Menu> DebugState::createMapMenu() {
return menu; return menu;
} }
std::shared_ptr<Menu> DebugState::createVehicleMenu() { std::optional<Menu> DebugState::createVehicleMenu() {
auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}}, auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight); kDebugFont, kDebugEntryHeight);
@ -177,7 +177,7 @@ std::shared_ptr<Menu> DebugState::createVehicleMenu() {
return menu; return menu;
} }
std::shared_ptr<Menu> DebugState::createAIMenu() { std::optional<Menu> DebugState::createAIMenu() {
auto menu = auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight); kDebugFont, kDebugEntryHeight);
@ -212,7 +212,7 @@ std::shared_ptr<Menu> DebugState::createAIMenu() {
return menu; return menu;
} }
std::shared_ptr<Menu> DebugState::createWeaponMenu() { std::optional<Menu> DebugState::createWeaponMenu() {
auto menu = auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight); kDebugFont, kDebugEntryHeight);
@ -226,7 +226,7 @@ std::shared_ptr<Menu> DebugState::createWeaponMenu() {
return menu; return menu;
} }
std::shared_ptr<Menu> DebugState::createWeatherMenu() { std::optional<Menu> DebugState::createWeatherMenu() {
auto menu = auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight); kDebugFont, kDebugEntryHeight);
@ -242,7 +242,7 @@ std::shared_ptr<Menu> DebugState::createWeatherMenu() {
return menu; return menu;
} }
std::shared_ptr<Menu> DebugState::createMissionsMenu() { std::optional<Menu> DebugState::createMissionsMenu() {
auto menu = auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeightMissions); kDebugFont, kDebugEntryHeightMissions);

View File

@ -11,13 +11,13 @@ class DebugState final : public State {
bool _sonicMode = false; bool _sonicMode = false;
bool _invertedY; bool _invertedY;
std::shared_ptr<Menu> createDebugMenu(); std::optional<Menu> createDebugMenu();
std::shared_ptr<Menu> createMapMenu(); std::optional<Menu> createMapMenu();
std::shared_ptr<Menu> createVehicleMenu(); std::optional<Menu> createVehicleMenu();
std::shared_ptr<Menu> createAIMenu(); std::optional<Menu> createAIMenu();
std::shared_ptr<Menu> createWeaponMenu(); std::optional<Menu> createWeaponMenu();
std::shared_ptr<Menu> createWeatherMenu(); std::optional<Menu> createWeatherMenu();
std::shared_ptr<Menu> createMissionsMenu(); std::optional<Menu> createMissionsMenu();
public: public:
DebugState(RWGame* game, const glm::vec3& vp = {}, DebugState(RWGame* game, const glm::vec3& vp = {},

View File

@ -1,4 +1,3 @@
#include <State.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(StateUnitTests) BOOST_AUTO_TEST_SUITE(StateUnitTests)