1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-21 18:02:43 +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_
#define _GAME_MENUSYSTEM_HPP_
#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <glm/glm.hpp>
@ -76,12 +78,11 @@ public:
/**
* @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,
int font = MenuDefaults::kFont,
float size = 30.f) {
return std::make_shared<Menu>(std::move(items), font, size);
static std::optional<Menu> create(std::vector<MenuEntry> items,
int font = MenuDefaults::kFont, float size = 30.f) {
return std::make_optional<Menu>(std::move(items), font, size);
}
Menu& lambda(const GameString& n, std::function<void()> callback) {

View File

@ -1,8 +1,12 @@
#ifndef RWGAME_STATE_HPP
#define RWGAME_STATE_HPP
#include <optional>
#include <render/ViewCamera.hpp>
#include "GameWindow.hpp"
#include "MenuSystem.hpp"
#include <SDL.h>
#include <SDL_events.h>
@ -12,8 +16,8 @@ class StateManager;
class State {
public:
std::shared_ptr<Menu> menu;
std::shared_ptr<Menu> nextMenu;
std::optional<Menu> menu;
std::optional<Menu> nextMenu;
RWGame* game;
@ -33,16 +37,17 @@ public:
virtual ~State() = default;
void enterMenu(const std::shared_ptr<Menu>& menu) {
nextMenu = menu;
template<typename T>
void enterMenu(T&& menu) {
nextMenu = std::forward<T>(menu);
}
Menu* getCurrentMenu() {
if (nextMenu) {
menu = nextMenu;
nextMenu = nullptr;
menu = std::move(nextMenu);
nextMenu = std::nullopt;
}
return menu.get();
return &*menu;
}
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;
if (game->getWorld()->getPlayer()) {
player = game->getWorld()->getPlayer()->getCharacter();
@ -76,7 +76,7 @@ std::shared_ptr<Menu> DebugState::createDebugMenu() {
return menu;
}
std::shared_ptr<Menu> DebugState::createMapMenu() {
std::optional<Menu> DebugState::createMapMenu() {
CharacterObject* player = nullptr;
if (game->getWorld()->getPlayer()) {
player = game->getWorld()->getPlayer()->getCharacter();
@ -144,7 +144,7 @@ std::shared_ptr<Menu> DebugState::createMapMenu() {
return menu;
}
std::shared_ptr<Menu> DebugState::createVehicleMenu() {
std::optional<Menu> DebugState::createVehicleMenu() {
auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
@ -177,7 +177,7 @@ std::shared_ptr<Menu> DebugState::createVehicleMenu() {
return menu;
}
std::shared_ptr<Menu> DebugState::createAIMenu() {
std::optional<Menu> DebugState::createAIMenu() {
auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
@ -212,7 +212,7 @@ std::shared_ptr<Menu> DebugState::createAIMenu() {
return menu;
}
std::shared_ptr<Menu> DebugState::createWeaponMenu() {
std::optional<Menu> DebugState::createWeaponMenu() {
auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
@ -226,7 +226,7 @@ std::shared_ptr<Menu> DebugState::createWeaponMenu() {
return menu;
}
std::shared_ptr<Menu> DebugState::createWeatherMenu() {
std::optional<Menu> DebugState::createWeatherMenu() {
auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
@ -242,7 +242,7 @@ std::shared_ptr<Menu> DebugState::createWeatherMenu() {
return menu;
}
std::shared_ptr<Menu> DebugState::createMissionsMenu() {
std::optional<Menu> DebugState::createMissionsMenu() {
auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeightMissions);

View File

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

View File

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