contentpack removal feature + refactor

This commit is contained in:
MihailRis
2024-03-11 13:21:06 +03:00
parent 3f0c710a47
commit daaba374a3
24 changed files with 345 additions and 295 deletions
+25 -19
View File
@@ -54,7 +54,7 @@ inline uint64_t randU64() {
void menus::create_version_label(Engine* engine) {
auto gui = engine->getGUI();
auto vlabel = std::make_shared<gui::Label>(
util::str2wstr_utf8(ENGINE_VERSION_STRING " development build ")
util::str2wstr_utf8(ENGINE_VERSION_STRING+" development build ")
);
vlabel->setZIndex(1000);
vlabel->setColor(glm::vec4(1, 1, 1, 0.5f));
@@ -210,9 +210,7 @@ void create_world_generators_panel(Engine* engine) {
idlabel->setAlign(Align::right);
button->add(idlabel, glm::vec2(80, 4));
button->add(std::make_shared<Label>(fullName), glm::vec2(0, 8));
button->listenAction(
[=](GUI*) {
menus::generatorID = id;
@@ -224,21 +222,23 @@ void create_world_generators_panel(Engine* engine) {
panel->add(guiutil::backButton(menu));
}
void menus::open_world(std::string name, Engine* engine) {
void menus::open_world(std::string name, Engine* engine, bool confirmConvert) {
auto paths = engine->getPaths();
auto folder = paths->getWorldsFolder()/fs::u8path(name);
try {
engine->loadWorldContent(folder);
} catch (const 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()));
guiutil::alert(
engine->getGUI(), langs::get(L"error.pack-not-found")+L": "+
util::str2wstr_utf8(error.getPackId())
);
return;
} catch (const std::runtime_error& error) {
guiutil::alert(engine->getGUI(),
langs::get(L"Content Error", L"menu")+
L": "+util::str2wstr_utf8(error.what()));
guiutil::alert(
engine->getGUI(), langs::get(L"Content Error", L"menu")+L": "+
util::str2wstr_utf8(error.what())
);
return;
}
paths->setWorldFolder(folder);
@@ -252,18 +252,25 @@ void menus::open_world(std::string name, Engine* engine) {
if (lut->hasMissingContent()) {
show_content_missing(engine, content, lut);
} else {
show_convert_request(engine, content, lut, folder, [=](){
open_world(name, engine);
});
if (confirmConvert) {
show_process_panel(engine, std::make_shared<WorldConverter>(folder, content, lut), [=](){
open_world(name, engine, false);
});
} else {
show_convert_request(engine, content, lut, folder, [=](){
open_world(name, engine, false);
});
}
}
} else {
try {
Level* level = World::load(folder, settings, content, packs);
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
} catch (const world_load_error& error) {
guiutil::alert(engine->getGUI(),
langs::get(L"Error")+
L": "+util::str2wstr_utf8(error.what()));
guiutil::alert(
engine->getGUI(), langs::get(L"Error")+L": "+
util::str2wstr_utf8(error.what())
);
return;
}
}
@@ -284,7 +291,7 @@ std::shared_ptr<Panel> create_worlds_panel(Engine* engine) {
btn->setColor(glm::vec4(0.06f, 0.12f, 0.18f, 0.7f));
btn->setHoverColor(glm::vec4(0.09f, 0.17f, 0.2f, 0.6f));
btn->listenAction([=](GUI*) {
menus::open_world(name, engine);
menus::open_world(name, engine, false);
});
btn->add(std::make_shared<Label>(namews), glm::vec2(8, 8));
@@ -296,8 +303,7 @@ std::shared_ptr<Panel> create_worlds_panel(Engine* engine) {
delbtn->setHoverColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.17f));
delbtn->listenAction([=](GUI* gui) {
guiutil::confirm(gui, langs::get(L"delete-confirm", L"world")+
L" ("+util::str2wstr_utf8(folder.u8string())+L")", [=]()
{
L" ("+util::str2wstr_utf8(folder.u8string())+L")", [=]() {
std::cout << "deleting " << folder.u8string() << std::endl;
fs::remove_all(folder);
menus::refresh_menus(engine);
+9 -3
View File
@@ -4,16 +4,22 @@
#include <string>
class Engine;
class Level;
class LevelController;
namespace menus {
// implemented in menu_settings.cpp
extern void create_settings_panel(Engine* engine);
// implemented in menu_pause.cpp
extern void create_pause_panel(Engine* engine, Level* level);
extern void create_pause_panel(Engine* engine, LevelController* controller);
void open_world(std::string name, Engine* engine);
/// @brief Load world, convert if required and set to LevelScreen.
/// @param name world name
/// @param engine engine instance
/// @param confirmConvert automatically confirm convert if requested
void open_world(std::string name, Engine* engine, bool confirmConvert);
/// @brief Create development version label at the top-right screen corner
void create_version_label(Engine* engine);
void create_menus(Engine* engine);
void refresh_menus(Engine* engine);
+17 -9
View File
@@ -9,6 +9,8 @@
#include "../../coders/png.h"
#include "../../util/stringutil.h"
#include "../../files/WorldFiles.h"
#include "../../content/ContentLUT.h"
#include "../../logic/LevelController.h"
#include <glm/glm.hpp>
@@ -96,14 +98,15 @@ std::shared_ptr<Panel> create_packs_panel(
}
static void reopen_world(Engine* engine, World* world) {
std::string wname = world->getName();
std::string wname = world->wfile->directory.stem().u8string();
engine->setScreen(nullptr);
engine->setScreen(std::make_shared<MenuScreen>(engine));
menus::open_world(wname, engine);
menus::open_world(wname, engine, true);
}
// TODO: refactor
void create_content_panel(Engine* engine, Level* level) {
void create_content_panel(Engine* engine, LevelController* controller) {
auto level = controller->getLevel();
auto menu = engine->getGUI()->getMenu();
auto paths = engine->getPaths();
auto mainPanel = menus::create_page(engine, "content", 550, 0.0f, 5);
@@ -122,16 +125,18 @@ void create_content_panel(Engine* engine, Level* level) {
auto panel = create_packs_panel(
engine->getContentPacks(), engine, false, nullptr,
[=](const ContentPack& pack) {
auto world = level->getWorld();
auto runtime = engine->getContent()->getPackRuntime(pack.id);
if (runtime->getStats().hasSavingContent()) {
guiutil::confirm(engine->getGUI(), langs::get(L"remove-confirm", L"pack")+
L" ("+util::str2wstr_utf8(pack.id)+L")", [=]() {
// FIXME: work in progress
controller->saveWorld();
world->wfile->removePack(world, pack.id);
reopen_world(engine, world);
});
} else {
auto world = level->getWorld();
controller->saveWorld();
world->wfile->removePack(world, pack.id);
reopen_world(engine, world);
}
}
@@ -155,6 +160,7 @@ void create_content_panel(Engine* engine, Level* level) {
}
}
world->wfile->addPack(world, pack.id);
controller->saveWorld();
reopen_world(engine, world);
}, nullptr);
menu->addPage("content-packs", panel);
@@ -163,7 +169,7 @@ void create_content_panel(Engine* engine, Level* level) {
mainPanel->add(guiutil::backButton(menu));
}
void menus::create_pause_panel(Engine* engine, Level* level) {
void menus::create_pause_panel(Engine* engine, LevelController* controller) {
auto menu = engine->getGUI()->getMenu();
auto panel = create_page(engine, "pause", 400, 0.0f, 1);
@@ -171,13 +177,15 @@ void menus::create_pause_panel(Engine* engine, Level* level) {
menu->reset();
}));
panel->add(create_button(L"Content", glm::vec4(10.0f), glm::vec4(1), [=](GUI*) {
create_content_panel(engine, level);
create_content_panel(engine, controller);
menu->setPage("content");
}));
panel->add(guiutil::gotoButton(L"Settings", "settings", menu));
panel->add(create_button(L"Save and Quit to Menu", glm::vec4(10.f), glm::vec4(1), [=](GUI*){
// save world and destroy LevelScreen
// save world
controller->saveWorld();
// destroy LevelScreen and run quit callbacks
engine->setScreen(nullptr);
// create and go to menu screen
engine->setScreen(std::make_shared<MenuScreen>(engine));