1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 03:12:36 +01:00

Make MenuEntry height a property of the Menu

This commit is contained in:
Daniel Evans 2016-10-19 23:34:08 +01:00
parent 613d386488
commit d62e8a6cd2
3 changed files with 34 additions and 34 deletions

View File

@ -33,35 +33,30 @@ public:
*/ */
class MenuEntry { class MenuEntry {
GameString text; GameString text;
float size;
std::function<void(void)> callback; std::function<void(void)> callback;
public: public:
MenuEntry(const std::string& n, std::function<void(void)> cb) MenuEntry(const std::string& n, std::function<void(void)> cb)
: text(GameStringUtil::fromString(n)), size(30.f), callback(cb) { : text(GameStringUtil::fromString(n)), callback(cb) {
} }
MenuEntry(const GameString& n, std::function<void(void)> cb, MenuEntry(const GameString& n, std::function<void(void)> cb)
float size = 30.f) : text(n), callback(cb) {
: text(n), size(size), callback(cb) {
} }
float getHeight() const { void draw(int font, float size, bool active, GameRenderer* r,
return size; glm::vec2& basis) {
}
void draw(int font, bool active, GameRenderer* r, glm::vec2& basis) {
TextRenderer::TextInfo ti; TextRenderer::TextInfo ti;
ti.font = font; ti.font = font;
ti.screenPosition = basis; ti.screenPosition = basis;
ti.text = text; ti.text = text;
ti.size = getHeight(); ti.size = size;
if (!active) { if (!active) {
ti.baseColour = glm::u8vec3(255); ti.baseColour = glm::u8vec3(255);
} else { } else {
ti.baseColour = glm::u8vec3(255, 255, 0); ti.baseColour = glm::u8vec3(255, 255, 0);
} }
r->text.renderText(ti); r->text.renderText(ti);
basis.y += getHeight(); basis.y += size;
} }
void activate(float clickX, float clickY) { void activate(float clickX, float clickY) {
@ -71,8 +66,9 @@ public:
} }
}; };
Menu(std::vector<MenuEntry> initial, int font = MenuDefaults::kFont) Menu(std::vector<MenuEntry> initial, int font = MenuDefaults::kFont,
: activeEntry(-1), font(font), entries(std::move(initial)) { float size = 30.f)
: activeEntry(-1), font(font), size(size), entries(std::move(initial)) {
} }
/** /**
@ -80,17 +76,18 @@ public:
* @return a shared pointer to the menu with the items * @return a shared pointer to the menu with the items
*/ */
static std::shared_ptr<Menu> create(std::vector<MenuEntry> items, static std::shared_ptr<Menu> create(std::vector<MenuEntry> items,
int font = MenuDefaults::kFont) { int font = MenuDefaults::kFont,
return std::make_shared<Menu>(std::move(items), font); float size = 30.f) {
return std::make_shared<Menu>(std::move(items), font, size);
} }
Menu& lambda(const GameString& n, std::function<void()> callback) { Menu& lambda(const GameString& n, std::function<void()> callback) {
entries.emplace_back(n, callback, 30.f); entries.emplace_back(n, callback);
return *this; return *this;
} }
Menu& lambda(const std::string& n, std::function<void(void)> callback) { Menu& lambda(const std::string& n, std::function<void(void)> callback) {
entries.emplace_back(GameStringUtil::fromString(n), callback, 30.f); entries.emplace_back(GameStringUtil::fromString(n), callback);
return *this; return *this;
} }
@ -108,18 +105,18 @@ public:
if (activeEntry >= 0 && i == (unsigned)activeEntry) { if (activeEntry >= 0 && i == (unsigned)activeEntry) {
active = true; active = true;
} }
entries[i].draw(font, active, r, basis); entries[i].draw(font, size, active, r, basis);
} }
} }
void hover(const float x, const float y) { void hover(const float x, const float y) {
glm::vec2 c(x - offset.x, y - offset.y); glm::vec2 c(x - offset.x, y - offset.y);
for (size_t i = 0; i < entries.size(); ++i) { for (size_t i = 0; i < entries.size(); ++i) {
if (c.y > 0.f && c.y < entries[i].getHeight()) { if (c.y > 0.f && c.y < size) {
activeEntry = i; activeEntry = i;
return; return;
} else { } else {
c.y -= entries[i].getHeight(); c.y -= size;
} }
} }
} }
@ -127,11 +124,11 @@ public:
void click(const float x, const float y) { void click(const float x, const float y) {
glm::vec2 c(x - offset.x, y - offset.y); glm::vec2 c(x - offset.x, y - offset.y);
for (auto it = entries.begin(); it != entries.end(); ++it) { for (auto it = entries.begin(); it != entries.end(); ++it) {
if (c.y > 0.f && c.y < (*it).getHeight()) { if (c.y > 0.f && c.y < size) {
(*it).activate(c.x, c.y); (*it).activate(c.x, c.y);
return; return;
} else { } else {
c.y -= (*it).getHeight(); c.y -= size;
} }
} }
} }
@ -158,6 +155,7 @@ public:
private: private:
int font; int font;
float size;
std::vector<MenuEntry> entries; std::vector<MenuEntry> entries;
}; };

View File

@ -55,7 +55,7 @@ std::shared_ptr<Menu> DebugState::createDebugMenu() {
{"Full Armour", [=] { player->getCurrentState().armour = 100.f; }}, {"Full Armour", [=] { player->getCurrentState().armour = 100.f; }},
{"Cull Here", {"Cull Here",
[=] { game->getRenderer().setCullOverride(true, _debugCam); }}}, [=] { game->getRenderer().setCullOverride(true, _debugCam); }}},
kDebugFont); kDebugFont, kDebugEntryHeight);
menu->offset = kDebugMenuOffset; menu->offset = kDebugMenuOffset;
@ -134,7 +134,7 @@ std::shared_ptr<Menu> DebugState::createMapMenu() {
} }
} }
}}}, }}},
kDebugFont); kDebugFont, kDebugEntryHeight);
menu->offset = kDebugMenuOffset; menu->offset = kDebugMenuOffset;
return menu; return menu;
@ -142,7 +142,7 @@ std::shared_ptr<Menu> DebugState::createMapMenu() {
std::shared_ptr<Menu> DebugState::createVehicleMenu() { std::shared_ptr<Menu> DebugState::createVehicleMenu() {
auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}}, auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}},
kDebugFont); kDebugFont, kDebugEntryHeight);
const std::map<std::string, int> kVehicleTypes = { const std::map<std::string, int> kVehicleTypes = {
{"Landstalker", 90}, {"Taxi", 110}, {"Firetruck", 97}, {"Landstalker", 90}, {"Taxi", 110}, {"Firetruck", 97},
@ -161,8 +161,9 @@ std::shared_ptr<Menu> DebugState::createVehicleMenu() {
} }
std::shared_ptr<Menu> DebugState::createAIMenu() { std::shared_ptr<Menu> DebugState::createAIMenu() {
auto menu = Menu::create( auto menu =
{{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, kDebugFont); Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
const std::map<std::string, int> kPedTypes = { const std::map<std::string, int> kPedTypes = {
{"Triad", 12}, {"Cop", 1}, {"SWAT", 2}, {"Triad", 12}, {"Cop", 1}, {"SWAT", 2},
@ -189,8 +190,9 @@ std::shared_ptr<Menu> DebugState::createAIMenu() {
} }
std::shared_ptr<Menu> DebugState::createWeaponMenu() { std::shared_ptr<Menu> DebugState::createWeaponMenu() {
auto menu = Menu::create( auto menu =
{{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, kDebugFont); Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
for (int i = 1; i < kMaxInventorySlots; ++i) { for (int i = 1; i < kMaxInventorySlots; ++i) {
auto& name = getWorld()->data->weaponData[i]->name; auto& name = getWorld()->data->weaponData[i]->name;

View File

@ -15,7 +15,7 @@ BOOST_AUTO_TEST_CASE(menu_test_click) {
BOOST_CHECK(!clickered); BOOST_CHECK(!clickered);
float h = test.getEntries().at(0).getHeight(); float h = 30.f;
test.click(0.f, h + 1.f); test.click(0.f, h + 1.f);
@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(menu_test_click_offset) {
BOOST_CHECK(!clickered); BOOST_CHECK(!clickered);
float h = test.getEntries().at(0).getHeight(); float h = 30.f;
test.click(201.f, 200.f + h + 1.f); test.click(201.f, 200.f + h + 1.f);
@ -79,10 +79,10 @@ BOOST_AUTO_TEST_CASE(menu_test_hover_index) {
Menu test({{"Test1", [&] { clickindex = 0; }}, Menu test({{"Test1", [&] { clickindex = 0; }},
{"Test2", [&] { clickindex = 1; }}}); {"Test2", [&] { clickindex = 1; }}});
test.hover(0.f, test.getEntries()[0].getHeight() - 0.1f); test.hover(0.f, 30.f - 0.1f);
BOOST_CHECK(test.activeEntry == 0); BOOST_CHECK(test.activeEntry == 0);
test.hover(0.f, test.getEntries()[0].getHeight() + 0.1f); test.hover(0.f, 30.f + 0.1f);
BOOST_CHECK(test.activeEntry == 1); BOOST_CHECK(test.activeEntry == 1);
} }