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>
|
2016-04-14 02:21:25 +02:00
|
|
|
#include <algorithm>
|
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:
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
Menu(int font)
|
2014-01-01 01:37:16 +01:00
|
|
|
: font(font), activeEntry(-1) {}
|
2013-12-25 20:54:22 +01:00
|
|
|
|
|
|
|
struct MenuEntry
|
|
|
|
{
|
|
|
|
std::string name;
|
2014-06-01 19:26:53 +02:00
|
|
|
float _size;
|
2013-12-25 20:54:22 +01:00
|
|
|
|
2016-04-14 02:21:25 +02:00
|
|
|
MenuEntry(const std::string& n, float size = 30.f) : name(n), _size(size)
|
|
|
|
{
|
|
|
|
std::transform(name.begin(), name.end(), name.begin(), toupper);
|
|
|
|
}
|
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 = name;
|
|
|
|
ti.size = getHeight();
|
|
|
|
if( ! active )
|
|
|
|
{
|
2016-05-07 19:29:08 +02:00
|
|
|
ti.baseColour = glm::u8vec3(255);
|
2015-02-07 23:55:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-07 19:29:08 +02:00
|
|
|
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
|
|
|
};
|
2016-06-20 21:35:54 +02:00
|
|
|
|
2013-12-25 20:54:22 +01:00
|
|
|
struct Entry : public MenuEntry
|
|
|
|
{
|
2016-06-20 21:35:54 +02:00
|
|
|
std::function<void(void)> callback;
|
|
|
|
|
|
|
|
Entry(const std::string& 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
|
|
|
};
|
2016-06-20 21:35:54 +02:00
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
static std::shared_ptr<MenuEntry> lambda(const std::string& 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
|
|
|
}
|
|
|
|
|
|
|
|
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 = std::min<const int>(entries.size()-1, std::max<const int>(0, activeEntry + movement));
|
|
|
|
}
|
2013-12-25 20:54:22 +01:00
|
|
|
};
|
|
|
|
|
2014-06-01 19:26:53 +02:00
|
|
|
#endif
|