diff --git a/rwgame/MenuSystem.hpp b/rwgame/MenuSystem.hpp index 40c663c4..b1299124 100644 --- a/rwgame/MenuSystem.hpp +++ b/rwgame/MenuSystem.hpp @@ -33,35 +33,30 @@ public: */ class MenuEntry { GameString text; - float size; std::function callback; public: MenuEntry(const std::string& n, std::function cb) - : text(GameStringUtil::fromString(n)), size(30.f), callback(cb) { + : text(GameStringUtil::fromString(n)), callback(cb) { } - MenuEntry(const GameString& n, std::function cb, - float size = 30.f) - : text(n), size(size), callback(cb) { + MenuEntry(const GameString& n, std::function cb) + : text(n), callback(cb) { } - float getHeight() const { - return size; - } - - void draw(int font, bool active, GameRenderer* r, glm::vec2& basis) { + void draw(int font, float size, bool active, GameRenderer* r, + glm::vec2& basis) { TextRenderer::TextInfo ti; ti.font = font; ti.screenPosition = basis; ti.text = text; - ti.size = getHeight(); + ti.size = size; if (!active) { ti.baseColour = glm::u8vec3(255); } else { ti.baseColour = glm::u8vec3(255, 255, 0); } r->text.renderText(ti); - basis.y += getHeight(); + basis.y += size; } void activate(float clickX, float clickY) { @@ -71,8 +66,9 @@ public: } }; - Menu(std::vector initial, int font = MenuDefaults::kFont) - : activeEntry(-1), font(font), entries(std::move(initial)) { + Menu(std::vector initial, int font = MenuDefaults::kFont, + 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 */ static std::shared_ptr create(std::vector items, - int font = MenuDefaults::kFont) { - return std::make_shared(std::move(items), font); + int font = MenuDefaults::kFont, + float size = 30.f) { + return std::make_shared(std::move(items), font, size); } Menu& lambda(const GameString& n, std::function callback) { - entries.emplace_back(n, callback, 30.f); + entries.emplace_back(n, callback); return *this; } Menu& lambda(const std::string& n, std::function callback) { - entries.emplace_back(GameStringUtil::fromString(n), callback, 30.f); + entries.emplace_back(GameStringUtil::fromString(n), callback); return *this; } @@ -108,18 +105,18 @@ public: if (activeEntry >= 0 && i == (unsigned)activeEntry) { 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) { 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()) { + if (c.y > 0.f && c.y < size) { activeEntry = i; return; } else { - c.y -= entries[i].getHeight(); + c.y -= size; } } } @@ -127,11 +124,11 @@ public: void click(const float x, const float y) { glm::vec2 c(x - offset.x, y - offset.y); 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); return; } else { - c.y -= (*it).getHeight(); + c.y -= size; } } } @@ -158,6 +155,7 @@ public: private: int font; + float size; std::vector entries; }; diff --git a/rwgame/states/DebugState.cpp b/rwgame/states/DebugState.cpp index 7290941d..8a9f78d3 100644 --- a/rwgame/states/DebugState.cpp +++ b/rwgame/states/DebugState.cpp @@ -55,7 +55,7 @@ std::shared_ptr DebugState::createDebugMenu() { {"Full Armour", [=] { player->getCurrentState().armour = 100.f; }}, {"Cull Here", [=] { game->getRenderer().setCullOverride(true, _debugCam); }}}, - kDebugFont); + kDebugFont, kDebugEntryHeight); menu->offset = kDebugMenuOffset; @@ -134,7 +134,7 @@ std::shared_ptr DebugState::createMapMenu() { } } }}}, - kDebugFont); + kDebugFont, kDebugEntryHeight); menu->offset = kDebugMenuOffset; return menu; @@ -142,7 +142,7 @@ std::shared_ptr DebugState::createMapMenu() { std::shared_ptr DebugState::createVehicleMenu() { auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}}, - kDebugFont); + kDebugFont, kDebugEntryHeight); const std::map kVehicleTypes = { {"Landstalker", 90}, {"Taxi", 110}, {"Firetruck", 97}, @@ -161,8 +161,9 @@ std::shared_ptr DebugState::createVehicleMenu() { } std::shared_ptr DebugState::createAIMenu() { - auto menu = Menu::create( - {{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, kDebugFont); + auto menu = + Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, + kDebugFont, kDebugEntryHeight); const std::map kPedTypes = { {"Triad", 12}, {"Cop", 1}, {"SWAT", 2}, @@ -189,8 +190,9 @@ std::shared_ptr DebugState::createAIMenu() { } std::shared_ptr DebugState::createWeaponMenu() { - auto menu = Menu::create( - {{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, kDebugFont); + auto menu = + Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, + kDebugFont, kDebugEntryHeight); for (int i = 1; i < kMaxInventorySlots; ++i) { auto& name = getWorld()->data->weaponData[i]->name; diff --git a/tests/test_menu.cpp b/tests/test_menu.cpp index 703edfd8..51cdaa23 100644 --- a/tests/test_menu.cpp +++ b/tests/test_menu.cpp @@ -15,7 +15,7 @@ BOOST_AUTO_TEST_CASE(menu_test_click) { BOOST_CHECK(!clickered); - float h = test.getEntries().at(0).getHeight(); + float h = 30.f; test.click(0.f, h + 1.f); @@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(menu_test_click_offset) { BOOST_CHECK(!clickered); - float h = test.getEntries().at(0).getHeight(); + float h = 30.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; }}, {"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); - test.hover(0.f, test.getEntries()[0].getHeight() + 0.1f); + test.hover(0.f, 30.f + 0.1f); BOOST_CHECK(test.activeEntry == 1); }