1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 18:32:44 +01:00
openrw/rwgame/MenuSystem.hpp

183 lines
3.5 KiB
C++
Raw Normal View History

2013-12-25 20:54:22 +01:00
#ifndef _GAME_MENUSYSTEM_HPP_
#define _GAME_MENUSYSTEM_HPP_
#include <string>
#include <memory>
2014-01-01 01:37:16 +01:00
#include <glm/glm.hpp>
2015-02-07 23:55:06 +01:00
#include <render/GameRenderer.hpp>
2013-12-25 20:54:22 +01:00
#include <functional>
#include <algorithm>
2013-12-25 20:54:22 +01:00
/**
* Default values for menus that should match the look and feel of the original
*/
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";
}
2013-12-25 20:54:22 +01:00
class Menu
{
2015-02-07 23:55:06 +01:00
int font;
2013-12-25 20:54:22 +01:00
public:
Menu(int font = MenuDefaults::kFont)
2014-01-01 01:37:16 +01:00
: font(font), activeEntry(-1) {}
2013-12-25 20:54:22 +01:00
struct MenuEntry
{
GameString text;
2014-06-01 19:26:53 +02:00
float _size;
2013-12-25 20:54:22 +01:00
MenuEntry(const GameString& n, float size = 30.f)
: text(n)
, _size(size)
{
}
2013-12-25 20:54:22 +01:00
2014-06-01 19:26:53 +02:00
float getHeight() { return _size; }
2013-12-25 20:54:22 +01:00
2015-02-07 23:55:06 +01:00
virtual void draw(int font, bool active, GameRenderer* r, glm::vec2& basis)
2013-12-25 20:54:22 +01:00
{
2015-02-07 23:55:06 +01:00
TextRenderer::TextInfo ti;
ti.font = font;
ti.screenPosition = basis;
ti.text = text;
2015-02-07 23:55:06 +01:00
ti.size = getHeight();
if( ! active )
{
ti.baseColour = glm::u8vec3(255);
2015-02-07 23:55:06 +01:00
}
else
{
ti.baseColour = glm::u8vec3(255, 255, 0);
2015-02-07 23:55:06 +01:00
}
r->text.renderText(ti);
2013-12-27 00:18:55 +01:00
basis.y += getHeight();
2013-12-25 20:54:22 +01:00
}
2014-01-01 01:37:16 +01:00
virtual void activate(float clickX, float clickY) = 0;
2013-12-25 20:54:22 +01:00
};
2013-12-25 20:54:22 +01:00
struct Entry : public MenuEntry
{
std::function<void(void)> callback;
Entry(const GameString& title,
std::function<void(void)> cb,
float size)
: MenuEntry(title, size)
, callback(cb)
{
}
void activate(float clickX, float clickY)
{
RW_UNUSED(clickX);
RW_UNUSED(clickY);
callback();
}
2013-12-25 20:54:22 +01:00
};
static std::shared_ptr<MenuEntry> lambda(const GameString& n, std::function<void (void)> callback, float size = 30.f)
2013-12-25 20:54:22 +01:00
{
2014-06-01 19:26:53 +02:00
return std::shared_ptr<MenuEntry>(new Entry(n, callback, size));
2013-12-25 20:54:22 +01:00
}
static std::shared_ptr<MenuEntry> lambda(const std::string& n, std::function<void (void)> callback, float size = 30.f)
{
return lambda(GameStringUtil::fromString(n), callback, size);
}
2013-12-25 20:54:22 +01:00
std::vector<std::shared_ptr<MenuEntry>> entries;
2014-01-01 01:37:16 +01:00
/**
* Active Entry index
*/
int activeEntry;
glm::vec2 offset;
2013-12-25 20:54:22 +01:00
void addEntry(std::shared_ptr<MenuEntry> entry)
{
entries.push_back(entry);
}
2015-02-07 23:55:06 +01:00
void draw(GameRenderer* r)
2013-12-25 20:54:22 +01:00
{
2014-01-01 01:37:16 +01:00
glm::vec2 basis(offset);
for(size_t i = 0;
i < entries.size();
++i)
2013-12-25 20:54:22 +01:00
{
2015-02-07 23:55:06 +01:00
bool active = false;
if(activeEntry >= 0 && i == (unsigned) activeEntry)
{
active = true;
2014-01-01 01:37:16 +01:00
}
2015-02-07 23:55:06 +01:00
entries[i]->draw(font, active, r, basis);
2014-01-01 01:37:16 +01:00
}
}
void hover(const float x, const float y)
{
glm::vec2 c(x - offset.x, y - offset.y);
for(size_t i = 0;
i < entries.size();
++i)
{
if( c.y > 0.f && c.y < entries[i]->getHeight() ) {
activeEntry = i;
return;
}
else {
c.y -= entries[i]->getHeight();
}
2013-12-25 20:54:22 +01:00
}
}
void click(const float x, const float y)
{
2014-01-01 01:37:16 +01:00
glm::vec2 c(x - offset.x, y - offset.y);
2013-12-25 20:54:22 +01:00
for(auto it = entries.begin();
it != entries.end();
++it)
{
if( c.y > 0.f && c.y < (*it)->getHeight() ) {
2014-01-01 01:37:16 +01:00
(*it)->activate(c.x, c.y);
2013-12-25 20:54:22 +01:00
return;
}
else {
c.y -= (*it)->getHeight();
}
}
}
2014-01-01 01:37:16 +01:00
// Activates the menu entry at the current active index.
void activate()
{
2014-06-10 18:51:55 +02:00
if(activeEntry >= 0 && (unsigned) activeEntry < entries.size()) {
2014-01-01 01:37:16 +01:00
entries[activeEntry]->activate(0.f, 0.f);
}
}
void move(int movement)
{
activeEntry += movement;
if (activeEntry >= int(entries.size())) {
activeEntry = 0;
}
else if (activeEntry < 0) {
activeEntry = entries.size() - 1;
}
2014-01-01 01:37:16 +01:00
}
2013-12-25 20:54:22 +01:00
};
2014-06-01 19:26:53 +02:00
#endif