From 3f3ff2a1f7d3ebd58e07124010eb27d6a0d064ed Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 27 Jan 2024 03:44:44 +0300 Subject: [PATCH] refactor --- src/content/Content.h | 10 ++++++++ src/content/ContentLUT.cpp | 17 ++++++++++++++ src/content/ContentLUT.h | 9 ++++++- src/frontend/menu.cpp | 48 +++++++++++--------------------------- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/content/Content.h b/src/content/Content.h index bbf25635..dc865ff2 100644 --- a/src/content/Content.h +++ b/src/content/Content.h @@ -19,6 +19,16 @@ enum class contenttype { none, block, item }; +inline const char* contenttype_name(contenttype type) { + switch (type) { + case contenttype::none: return "none"; + case contenttype::block: return "block"; + case contenttype::item: return "item"; + default: + return "unknown"; + } +} + class namereuse_error: public std::runtime_error { contenttype type; public: diff --git a/src/content/ContentLUT.cpp b/src/content/ContentLUT.cpp index 3adf398c..e27ef1da 100644 --- a/src/content/ContentLUT.cpp +++ b/src/content/ContentLUT.cpp @@ -80,3 +80,20 @@ ContentLUT* ContentLUT::create(const fs::path& filename, return nullptr; } } + +std::vector ContentLUT::getMissingContent() const { + std::vector entries; + for (size_t i = 0; i < blocks.size(); i++) { + if (blocks[i] == BLOCK_VOID) { + auto& name = blockNames[i]; + entries.push_back(contententry {contenttype::block, name}); + } + } + for (size_t i = 0; i < items.size(); i++) { + if (items[i] == ITEM_VOID) { + auto& name = itemNames[i]; + entries.push_back(contententry {contenttype::item, name}); + } + } + return entries; +} diff --git a/src/content/ContentLUT.h b/src/content/ContentLUT.h index f97d87f6..6219c696 100644 --- a/src/content/ContentLUT.h +++ b/src/content/ContentLUT.h @@ -8,9 +8,14 @@ #include "../typedefs.h" #include "../constants.h" +#include "Content.h" + namespace fs = std::filesystem; -class Content; +struct contententry { + contenttype type; + std::string name; +}; // TODO: make it unified for all types of content @@ -84,6 +89,8 @@ public: inline size_t countItems() const { return items.size(); } + + std::vector getMissingContent() const; }; #endif // CONTENT_CONTENT_LUT_H_ diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 6f8957af..9e2159d4 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -83,42 +83,20 @@ void show_content_missing(Engine* engine, const Content* content, Panel* subpanel = new Panel(vec2(500, 100)); subpanel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f)); - for (size_t i = 0; i < lut->countBlocks(); i++) { - // missing block - if (lut->getBlockId(i) == BLOCK_VOID) { - auto name = lut->getBlockName(i); - Panel* hpanel = new Panel(vec2(500, 30)); - hpanel->color(vec4(0.0f)); - hpanel->orientation(Orientation::horizontal); - - Label* namelabel = new Label(util::str2wstr_utf8(name)); - namelabel->color(vec4(1.0f, 0.2f, 0.2f, 0.5f)); + for (auto& entry : lut->getMissingContent()) { + Panel* hpanel = new Panel(vec2(500, 30)); + hpanel->color(vec4(0.0f)); + hpanel->orientation(Orientation::horizontal); + + Label* namelabel = new Label(util::str2wstr_utf8(entry.name)); + namelabel->color(vec4(1.0f, 0.2f, 0.2f, 0.5f)); - Label* typelabel = new Label(L"[block]"); - typelabel->color(vec4(0.5f)); - hpanel->add(typelabel); - hpanel->add(namelabel); - subpanel->add(hpanel); - } - } - - for (size_t i = 0; i < lut->countItems(); i++) { - // missing block - if (lut->getItemId(i) == ITEM_VOID) { - auto name = lut->getItemName(i); - Panel* hpanel = new Panel(vec2(500, 30)); - hpanel->color(vec4(0.0f)); - hpanel->orientation(Orientation::horizontal); - - Label* namelabel = new Label(util::str2wstr_utf8(name)); - namelabel->color(vec4(1.0f, 0.2f, 0.2f, 0.5f)); - - Label* typelabel = new Label(L"[item]"); - typelabel->color(vec4(0.5f)); - hpanel->add(typelabel); - hpanel->add(namelabel); - subpanel->add(hpanel); - } + auto contentname = util::str2wstr_utf8(contenttype_name(entry.type)); + Label* typelabel = new Label(L"["+contentname+L"]"); + typelabel->color(vec4(0.5f)); + hpanel->add(typelabel); + hpanel->add(namelabel); + subpanel->add(hpanel); } subpanel->maxLength(400); panel->add(subpanel);