From b2b961552bff34fb5339b0a6ae55e35faf833280 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 11 Jan 2024 03:21:14 +0300 Subject: [PATCH] guiutil::alert text wrapping + refactor --- src/engine.cpp | 7 ++++++ src/engine.h | 4 ++++ src/files/WorldConverter.cpp | 4 ++-- src/files/WorldConverter.h | 10 ++++++--- src/frontend/gui/gui_util.cpp | 33 +++++++++++++++++++++------ src/frontend/gui/gui_util.h | 6 ++--- src/frontend/menu.cpp | 42 +++++++++-------------------------- 7 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index a125663e..ee3b4da1 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -164,6 +164,13 @@ void Engine::loadContent() { assets->extend(*new_assets.get()); } +void Engine::loadWorldContent(const fs::path& folder) { + contentPacks.clear(); + auto packNames = ContentPack::worldPacksList(folder); + ContentPack::readPacks(paths, contentPacks, packNames, folder); + loadContent(); +} + void Engine::loadAllPacks() { auto resdir = paths->getResources(); contentPacks.clear(); diff --git a/src/engine.h b/src/engine.h index 01d2cf07..d4b98b91 100644 --- a/src/engine.h +++ b/src/engine.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "typedefs.h" #include "settings.h" @@ -18,6 +19,8 @@ class Screen; class EnginePaths; class ResPaths; +namespace fs = std::filesystem; + namespace gui { class GUI; } @@ -58,6 +61,7 @@ public: std::vector& getContentPacks(); void setLanguage(std::string locale); void loadContent(); + void loadWorldContent(const fs::path& folder); void loadAllPacks(); }; diff --git a/src/files/WorldConverter.cpp b/src/files/WorldConverter.cpp index de171ab3..667fa94e 100644 --- a/src/files/WorldConverter.cpp +++ b/src/files/WorldConverter.cpp @@ -11,7 +11,7 @@ namespace fs = std::filesystem; WorldConverter::WorldConverter(fs::path folder, const Content* content, - const ContentLUT* lut) + std::shared_ptr lut) : lut(lut), content(content) { DebugSettings settings; wfile = new WorldFiles(folder, settings); @@ -60,7 +60,7 @@ void WorldConverter::convertNext() { Chunk::fromOld(data.get()); } if (lut) { - Chunk::convert(data.get(), lut); + Chunk::convert(data.get(), lut.get()); } wfile->put(gx, gz, data.get()); } diff --git a/src/files/WorldConverter.h b/src/files/WorldConverter.h index d18c37e9..99b52754 100644 --- a/src/files/WorldConverter.h +++ b/src/files/WorldConverter.h @@ -2,19 +2,23 @@ #define FILES_WORLD_CONVERTER_H_ #include +#include #include +namespace fs = std::filesystem; + class Content; class ContentLUT; class WorldFiles; class WorldConverter { WorldFiles* wfile; - const ContentLUT* const lut; + std::shared_ptr const lut; const Content* const content; - std::queue regions; + std::queue regions; public: - WorldConverter(std::filesystem::path folder, const Content* content, const ContentLUT* lut); + WorldConverter(fs::path folder, const Content* content, + std::shared_ptr lut); ~WorldConverter(); bool hasNext() const; diff --git a/src/frontend/gui/gui_util.cpp b/src/frontend/gui/gui_util.cpp index da6dfdde..4eefd41d 100644 --- a/src/frontend/gui/gui_util.cpp +++ b/src/frontend/gui/gui_util.cpp @@ -9,8 +9,6 @@ using namespace gui; using glm::vec2; using glm::vec4; -using std::string; -using std::wstring; Button* guiutil::backButton(PagesControl* menu) { return (new Button(langs::get(L"Back"), vec4(10.f)))->listenAction([=](GUI* gui) { @@ -18,18 +16,35 @@ Button* guiutil::backButton(PagesControl* menu) { }); } -Button* guiutil::gotoButton(wstring text, string page, PagesControl* menu) { +Button* guiutil::gotoButton( + std::wstring text, + const std::string& page, + PagesControl* menu) { text = langs::get(text, L"menu"); return (new Button(text, vec4(10.f)))->listenAction([=](GUI* gui) { menu->set(page); }); } -void guiutil::alert(GUI* gui, wstring text, gui::runnable on_hidden) { +void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden) { PagesControl* menu = gui->getMenu(); Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f); panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f)); - panel->add(new Label(text)); + + // TODO: implement built-in text wrapping + const int wrap_length = 60; + if (text.length() > wrap_length) { + size_t offset = 0; + int extra; + while ((extra = text.length() - offset) > 0) { + extra = std::min(extra, wrap_length); + std::wstring part = text.substr(offset, extra); + panel->add(new Label(part)); + offset += extra; + } + } else { + panel->add(new Label(text)); + } panel->add((new Button(langs::get(L"Ok"), vec4(10.f)))->listenAction([=](GUI* gui) { if (on_hidden) on_hidden(); @@ -40,8 +55,12 @@ void guiutil::alert(GUI* gui, wstring text, gui::runnable on_hidden) { menu->set(""); } -void guiutil::confirm(GUI* gui, wstring text, gui::runnable on_confirm, - wstring yestext, wstring notext) { +void guiutil::confirm( + GUI* gui, + const std::wstring& text, + gui::runnable on_confirm, + std::wstring yestext, + std::wstring notext) { if (yestext.empty()) yestext = langs::get(L"Yes"); if (notext.empty()) notext = langs::get(L"No"); diff --git a/src/frontend/gui/gui_util.h b/src/frontend/gui/gui_util.h index f2e0b80f..a18d0e9b 100644 --- a/src/frontend/gui/gui_util.h +++ b/src/frontend/gui/gui_util.h @@ -10,9 +10,9 @@ namespace gui { namespace guiutil { gui::Button* backButton(gui::PagesControl* menu); - gui::Button* gotoButton(std::wstring text, std::string page, gui::PagesControl* menu); - void alert(gui::GUI* gui, std::wstring text, gui::runnable on_hidden=nullptr); - void confirm(gui::GUI* gui, std::wstring text, gui::runnable on_confirm=nullptr, + gui::Button* gotoButton(std::wstring text, const std::string& page, gui::PagesControl* menu); + void alert(gui::GUI* gui, const std::wstring& text, gui::runnable on_hidden=nullptr); + void confirm(gui::GUI* gui, const std::wstring& text, gui::runnable on_confirm=nullptr, std::wstring yestext=L"", std::wstring notext=L""); } diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 3301e164..bcad0097 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -68,23 +68,8 @@ Button* create_button(std::wstring text, return btn; } -void show_content_error_page(Engine* engine, std::string message) { - auto* gui = engine->getGUI(); - auto* menu = gui->getMenu(); - auto panel = create_page(engine, "error", 800, 0.5f, 8); - - panel->add(new Label(langs::get(L"Content Error", L"menu"))); - panel->add(new Label(util::str2wstr_utf8(message))); - - panel->add((new Button(langs::get(L"Back to Main Menu", L"menu"), - vec4(8.0f))) - ->listenAction([=](GUI*){ - menu->back(); - })); - menu->set("error"); -} - -void show_content_missing(Engine* engine, const Content* content, ContentLUT* lut) { +void show_content_missing(Engine* engine, const Content* content, + std::shared_ptr lut) { auto* gui = engine->getGUI(); auto* menu = gui->getMenu(); auto panel = create_page(engine, "missing-content", 500, 0.5f, 8); @@ -126,7 +111,7 @@ void show_content_missing(Engine* engine, const Content* content, ContentLUT* lu void show_convert_request( Engine* engine, const Content* content, - ContentLUT* lut, + std::shared_ptr lut, fs::path folder) { guiutil::confirm(engine->getGUI(), langs::get(L"world.convert-request"), [=]() { @@ -136,7 +121,6 @@ void show_convert_request( converter->convertNext(); } converter->write(); - delete lut; }, L"", langs::get(L"Cancel")); } @@ -167,33 +151,29 @@ void create_languages_panel(Engine* engine, PagesControl* menu) { void open_world(std::string name, Engine* engine) { auto paths = engine->getPaths(); auto folder = paths->getWorldsFolder()/fs::u8path(name); - auto& packs = engine->getContentPacks(); - packs.clear(); try { - auto packNames = ContentPack::worldPacksList(folder); - ContentPack::readPacks(paths, packs, packNames, folder); + engine->loadWorldContent(folder); } catch (contentpack_error& error) { // could not to find or read pack guiutil::alert(engine->getGUI(), - langs::get(L"error.pack-not-found")+ - L": "+util::str2wstr_utf8(error.getPackId())); + langs::get(L"error.pack-not-found")+ + L": "+util::str2wstr_utf8(error.getPackId())); return; - } - try { - engine->loadContent(); } catch (const std::runtime_error& error) { - show_content_error_page(engine, error.what()); + guiutil::alert(engine->getGUI(), + langs::get(L"Content Error", L"menu")+ + L": "+util::str2wstr_utf8(error.what())); return; } + auto& packs = engine->getContentPacks(); auto* content = engine->getContent(); auto& settings = engine->getSettings(); fs::create_directories(folder); - ContentLUT* lut = World::checkIndices(folder, content); + std::shared_ptr lut (World::checkIndices(folder, content)); if (lut) { if (lut->hasMissingContent()) { show_content_missing(engine, content, lut); - delete lut; } else { show_convert_request(engine, content, lut, folder); }