guiutil::alert text wrapping + refactor

This commit is contained in:
MihailRis 2024-01-11 03:21:14 +03:00
parent e1d30732df
commit b2b961552b
7 changed files with 60 additions and 46 deletions

View File

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

View File

@ -5,6 +5,7 @@
#include <memory>
#include <vector>
#include <stdexcept>
#include <filesystem>
#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<ContentPack>& getContentPacks();
void setLanguage(std::string locale);
void loadContent();
void loadWorldContent(const fs::path& folder);
void loadAllPacks();
};

View File

@ -11,7 +11,7 @@ namespace fs = std::filesystem;
WorldConverter::WorldConverter(fs::path folder,
const Content* content,
const ContentLUT* lut)
std::shared_ptr<ContentLUT> 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());
}

View File

@ -2,19 +2,23 @@
#define FILES_WORLD_CONVERTER_H_
#include <queue>
#include <memory>
#include <filesystem>
namespace fs = std::filesystem;
class Content;
class ContentLUT;
class WorldFiles;
class WorldConverter {
WorldFiles* wfile;
const ContentLUT* const lut;
std::shared_ptr<ContentLUT> const lut;
const Content* const content;
std::queue<std::filesystem::path> regions;
std::queue<fs::path> regions;
public:
WorldConverter(std::filesystem::path folder, const Content* content, const ContentLUT* lut);
WorldConverter(fs::path folder, const Content* content,
std::shared_ptr<ContentLUT> lut);
~WorldConverter();
bool hasNext() const;

View File

@ -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));
// 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("<alert>");
}
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");

View File

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

View File

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