1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02:00
openrw/rwgame/MenuSystem.hpp

112 lines
2.6 KiB
C++
Raw Normal View History

2013-12-25 20:54:22 +01:00
#ifndef _GAME_MENUSYSTEM_HPP_
#define _GAME_MENUSYSTEM_HPP_
2016-09-09 22:13:20 +02:00
#include <algorithm>
#include <functional>
2018-12-26 21:48:16 +01:00
#include <initializer_list>
2016-09-09 22:13:20 +02:00
#include <memory>
#include <optional>
2016-09-09 22:13:20 +02:00
#include <string>
2018-12-09 22:43:42 +01:00
#include <vector>
2013-12-25 20:54:22 +01:00
2018-12-09 22:43:42 +01:00
#include <glm/vec2.hpp>
2018-12-09 22:43:42 +01:00
#include <fonts/GameTexts.hpp>
2018-06-21 16:49:05 +02:00
#include <rw/debug.hpp>
2018-12-09 22:43:42 +01:00
class GameRenderer;
/**
* Default values for menus that should match the look and feel of the original
*/
2016-09-09 22:13:20 +02:00
namespace MenuDefaults {
constexpr int kFont = 1;
constexpr const char* kStartGameId = "FET_SAN";
constexpr const char* kResumeGameId = "FEM_RES";
constexpr const char* kLoadGameId = "FET_LG";
constexpr const char* kDebugId = "FEM_DBG";
constexpr const char* kOptionsId = "FET_OPT";
constexpr const char* kQuitGameId = "FET_QG";
}
2016-10-18 23:00:53 +02:00
/**
* @brief Implements user navigable menus
*
* This is a temporary implementation
*/
2016-09-09 22:13:20 +02:00
class Menu {
2013-12-25 20:54:22 +01:00
public:
2016-10-18 23:00:53 +02:00
/**
* @brief Handles rendering and dispatch of menu items
*/
class MenuEntry {
2016-09-09 22:13:20 +02:00
GameString text;
2016-10-18 23:00:53 +02:00
std::function<void(void)> callback;
2016-09-09 22:13:20 +02:00
2016-10-18 23:00:53 +02:00
public:
MenuEntry(const std::string& n, const std::function<void(void)>& cb)
: text(GameStringUtil::fromString(n, FONT_PRICEDOWN)), callback(cb) {
2016-10-18 23:00:53 +02:00
}
MenuEntry(const GameString& n, const std::function<void(void)>& cb)
: text(n), callback(cb) {
2016-09-09 22:13:20 +02:00
}
2018-12-01 18:57:06 +01:00
void draw(font_t font, float size, bool active, GameRenderer& r,
2018-12-09 22:43:42 +01:00
glm::vec2& basis);
2016-09-09 22:13:20 +02:00
void activate(float clickX, float clickY) {
RW_UNUSED(clickX);
RW_UNUSED(clickY);
callback();
}
};
2018-12-26 21:48:16 +01:00
Menu(std::initializer_list<MenuEntry>&& initial,
const glm::vec2& offset = glm::vec2(), int font = MenuDefaults::kFont,
float size = 30.f)
2018-12-26 21:48:16 +01:00
: activeEntry(-1)
, offset(offset)
, font(font)
, size(size)
, entries(initial) {
2016-09-09 22:13:20 +02:00
}
2016-10-18 23:00:53 +02:00
Menu& lambda(const GameString& n, std::function<void()> callback) {
entries.emplace_back(n, callback);
2016-10-18 23:00:53 +02:00
return *this;
}
Menu& lambda(const std::string& n, std::function<void(void)> callback) {
entries.emplace_back(GameStringUtil::fromString(n, FONT_PRICEDOWN), callback);
2016-10-18 23:00:53 +02:00
return *this;
}
2016-09-09 22:13:20 +02:00
/**
* Active Entry index
*/
int activeEntry;
2018-12-09 22:43:42 +01:00
void draw(GameRenderer& r);
2016-09-09 22:13:20 +02:00
2018-12-09 22:43:42 +01:00
void hover(const float x, const float y);
2016-09-09 22:13:20 +02:00
2018-12-09 22:43:42 +01:00
void click(const float x, const float y);
2016-09-09 22:13:20 +02:00
// Activates the menu entry at the current active index.
2018-12-09 22:43:42 +01:00
void activate();
2016-09-09 22:13:20 +02:00
2018-12-09 22:43:42 +01:00
void move(int movement);
2016-10-18 23:00:53 +02:00
const std::vector<MenuEntry>& getEntries() const {
return entries;
}
private:
2018-12-26 21:48:16 +01:00
glm::vec2 offset{};
2016-10-18 23:00:53 +02:00
int font;
float size;
2016-10-18 23:00:53 +02:00
std::vector<MenuEntry> entries;
2013-12-25 20:54:22 +01:00
};
2014-06-01 19:26:53 +02:00
#endif