1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-03 00:59:47 +02: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 {
GameString text;
float size;
std::function<void(void)> callback;
public:
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,
float size = 30.f)
: text(n), size(size), callback(cb) {
MenuEntry(const GameString& n, std::function<void(void)> 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<MenuEntry> initial, int font = MenuDefaults::kFont)
: activeEntry(-1), font(font), entries(std::move(initial)) {
Menu(std::vector<MenuEntry> 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<Menu> create(std::vector<MenuEntry> items,
int font = MenuDefaults::kFont) {
return std::make_shared<Menu>(std::move(items), font);
int font = MenuDefaults::kFont,
float size = 30.f) {
return std::make_shared<Menu>(std::move(items), font, size);
}
Menu& lambda(const GameString& n, std::function<void()> callback) {
entries.emplace_back(n, callback, 30.f);
entries.emplace_back(n, callback);
return *this;
}
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;
}
@ -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<MenuEntry> entries;
};

View File

@ -55,7 +55,7 @@ std::shared_ptr<Menu> 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<Menu> DebugState::createMapMenu() {
}
}
}}},
kDebugFont);
kDebugFont, kDebugEntryHeight);
menu->offset = kDebugMenuOffset;
return menu;
@ -142,7 +142,7 @@ std::shared_ptr<Menu> DebugState::createMapMenu() {
std::shared_ptr<Menu> DebugState::createVehicleMenu() {
auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}},
kDebugFont);
kDebugFont, kDebugEntryHeight);
const std::map<std::string, int> kVehicleTypes = {
{"Landstalker", 90}, {"Taxi", 110}, {"Firetruck", 97},
@ -161,8 +161,9 @@ std::shared_ptr<Menu> DebugState::createVehicleMenu() {
}
std::shared_ptr<Menu> DebugState::createAIMenu() {
auto menu = Menu::create(
{{"Back", [=] { this->enterMenu(createDebugMenu()); }}}, kDebugFont);
auto menu =
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
const std::map<std::string, int> kPedTypes = {
{"Triad", 12}, {"Cop", 1}, {"SWAT", 2},
@ -189,8 +190,9 @@ std::shared_ptr<Menu> DebugState::createAIMenu() {
}
std::shared_ptr<Menu> 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;

View File

@ -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);
}