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>
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
MenuEntry(const std::string& n, float size = 30.f) : name(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 = name;
|
|
|
|
ti.size = getHeight();
|
|
|
|
if( ! active )
|
|
|
|
{
|
|
|
|
ti.baseColour = glm::vec3(1.f, 1.f, 1.f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ti.baseColour = glm::vec3(1.f, 1.f, 0.f);
|
|
|
|
}
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
struct Entry : public MenuEntry
|
|
|
|
{
|
|
|
|
std::function<void (void)> callback;
|
|
|
|
|
2014-06-01 19:26:53 +02:00
|
|
|
Entry(const std::string& title, std::function<void (void)> cb, float size)
|
|
|
|
: MenuEntry(title, size), callback(cb) {}
|
2013-12-25 20:54:22 +01:00
|
|
|
|
2014-01-01 01:37:16 +01:00
|
|
|
void activate(float clickX, float clickY) { callback(); }
|
2013-12-25 20:54:22 +01: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
|