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
+8 -5
View File
@@ -13,14 +13,13 @@
#include "../logic/LevelController.h"
#include "../logic/PlayerController.h"
LevelFrontend::LevelFrontend(Level* level, Assets* assets)
: level(level),
LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets)
: level(controller->getLevel()),
controller(controller),
assets(assets),
contentCache(std::make_unique<ContentGfxCache>(level->content, assets)),
blocksAtlas(BlocksPreview::build(contentCache.get(), assets, level->content))
{}
void LevelFrontend::observe(LevelController* controller) {
{
controller->getPlayerController()->listenBlockInteraction(
[=](Player*, glm::ivec3 pos, const Block* def, BlockInteraction type) {
auto material = level->content->findBlockMaterial(def->material);
@@ -85,3 +84,7 @@ ContentGfxCache* LevelFrontend::getContentGfxCache() const {
Atlas* LevelFrontend::getBlocksAtlas() const {
return blocksAtlas.get();
}
LevelController* LevelFrontend::getController() const {
return controller;
}
+4 -3
View File
@@ -12,19 +12,20 @@ class LevelController;
class LevelFrontend {
Level* level;
LevelController* controller;
Assets* assets;
std::unique_ptr<ContentGfxCache> contentCache;
std::unique_ptr<Atlas> blocksAtlas;
public:
LevelFrontend(Level* level, Assets* assets);
LevelFrontend(LevelController* controller, Assets* assets);
~LevelFrontend();
void observe(LevelController* controller);
Level* getLevel() const;
Assets* getAssets() const;
ContentGfxCache* getContentGfxCache() const;
Atlas* getBlocksAtlas() const;
LevelController* getController() const;
};
#endif // FRONTEND_LEVEL_FRONTEND_H_
+1 -1
View File
@@ -547,7 +547,7 @@ void Hud::setPause(bool pause) {
auto menu = gui->getMenu();
if (pause) {
menus::create_pause_panel(engine, frontend->getLevel());
menus::create_pause_panel(engine, frontend->getController());
menu->setPage("pause");
} else {
menu->reset();
+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));
+11 -7
View File
@@ -96,11 +96,11 @@ LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
menu->reset();
controller = std::make_unique<LevelController>(settings, level);
frontend = std::make_unique<LevelFrontend>(level, assets);
frontend = std::make_unique<LevelFrontend>(controller.get(), assets);
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get(), controller->getPlayer());
hud = std::make_unique<Hud>(engine, frontend.get(), controller->getPlayer());
frontend->observe(controller.get());
backlight = settings.graphics.backlight;
@@ -120,11 +120,7 @@ LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
}
LevelScreen::~LevelScreen() {
std::cout << "-- writing world" << std::endl;
scripting::on_frontend_close();
controller->onWorldSave();
auto world = controller->getLevel()->getWorld();
world->write(controller->getLevel());
controller->onWorldQuit();
engine->getPaths()->setWorldFolder(fs::path());
}
@@ -192,3 +188,11 @@ void LevelScreen::draw(float delta) {
hud->draw(ctx);
}
}
void LevelScreen::onEngineShutdown() {
controller->saveWorld();
}
LevelController* LevelScreen::getLevelController() const {
return controller.get();
}
+6 -1
View File
@@ -15,7 +15,7 @@ class LevelFrontend;
class LevelController;
class TextureAnimator;
/* Screen is a mainloop state */
/// @brief Screen is a mainloop state
class Screen {
protected:
Engine* engine;
@@ -25,6 +25,7 @@ public:
virtual ~Screen();
virtual void update(float delta) = 0;
virtual void draw(float delta) = 0;
virtual void onEngineShutdown() {};
};
class MenuScreen : public Screen {
@@ -52,6 +53,10 @@ public:
void update(float delta) override;
void draw(float delta) override;
void onEngineShutdown() override;
LevelController* getLevelController() const;
};
#endif // FRONTEND_SCREENS_H_