process page test

This commit is contained in:
MihailRis 2024-04-23 04:51:00 +03:00
parent a34fff3515
commit f8f6ffc0c5
11 changed files with 58 additions and 33 deletions

4
res/layouts/process.xml Normal file
View File

@ -0,0 +1,4 @@
<panel size='400' padding='8' interval='1' color='#00000080'>
<label id='title_label'>???</label>
<label id='progress_label'>-</label>
</panel>

View File

@ -0,0 +1,10 @@
function on_progress(done, total)
local progress = done / total
document.progress_label.text = string.format(
"%s/%s (%s%%)", done, total, math.floor(progress*100)
)
end
function on_open(title)
document.title_label.text = title
end

View File

@ -227,11 +227,7 @@ void Engine::loadAssets() {
}
}
}
if (assets) {
assets->extend(*new_assets);
} else {
assets.reset(new_assets.release());
}
assets.reset(new_assets.release());
}
void Engine::loadContent() {

View File

@ -16,6 +16,7 @@ namespace gui {
struct uidocscript {
bool onopen : 1;
bool onprogress : 1;
bool onclose : 1;
};

View File

@ -49,7 +49,7 @@ void menus::create_menus(Engine* engine) {
});
}
void menus::show(Engine* engine, const std::string& name, std::vector<std::unique_ptr<dynamic::Value>> args) {
UiDocument* menus::show(Engine* engine, const std::string& name, std::vector<std::unique_ptr<dynamic::Value>> args) {
auto menu = engine->getGUI()->getMenu();
auto file = engine->getResPaths()->find("layouts/"+name+".xml");
auto fullname = "core:layouts/"+name;
@ -59,35 +59,23 @@ void menus::show(Engine* engine, const std::string& name, std::vector<std::uniqu
scripting::on_ui_open(document, std::move(args));
menu->addPage(name, document->getRoot());
menu->setPage(name);
return document;
}
void menus::show_process_panel(Engine* engine, std::shared_ptr<Task> task, std::wstring text) {
auto menu = engine->getGUI()->getMenu();
auto panel = std::dynamic_pointer_cast<gui::Panel>(guiutil::create(
"<panel size='400' padding='8' interval='1' color='#00000080/>"
));
if (!text.empty()) {
panel->add(std::make_shared<gui::Label>(langs::get(text)));
}
auto label = std::make_shared<gui::Label>(L"0%");
panel->add(label);
using namespace dynamic;
uint initialWork = task->getWorkTotal();
panel->listenInterval(0.01f, [=]() {
auto menu = engine->getGUI()->getMenu();
menu->reset();
std::vector<std::unique_ptr<dynamic::Value>> args;
args.emplace_back(Value::of(util::wstr2str_utf8(langs::get(text))));
auto doc = menus::show(engine, "process", std::move(args));
std::dynamic_pointer_cast<Container>(doc->getRoot())->listenInterval(0.01f, [=]() {
task->update();
uint tasksDone = task->getWorkDone();
float progress = tasksDone/static_cast<float>(initialWork);
label->setText(
std::to_wstring(tasksDone)+
L"/"+std::to_wstring(initialWork)+L" ("+
std::to_wstring(static_cast<int>(progress*100))+L"%)"
);
scripting::on_ui_progress(doc, tasksDone, initialWork);
});
menu->reset();
menu->addPage("process", panel);
menu->setPage("process", false);
}

View File

@ -8,6 +8,8 @@
class Task;
class Engine;
class UiDocument;
namespace dynamic {
class Value;
}
@ -15,8 +17,15 @@ namespace dynamic {
namespace menus {
/// @brief Create development version label at the top-right screen corner
void create_version_label(Engine* engine);
void create_menus(Engine* engine);
void show(Engine* engine, const std::string& name, std::vector<std::unique_ptr<dynamic::Value>> args);
UiDocument* show(
Engine* engine,
const std::string& name,
std::vector<std::unique_ptr<dynamic::Value>> args
);
void show_process_panel(Engine* engine, std::shared_ptr<Task> task, std::wstring text=L"");
}

View File

@ -8,7 +8,10 @@
#include "../../window/Camera.h"
#include "../../engine.h"
MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
MenuScreen::MenuScreen(Engine* engine) : Screen(engine) {
engine->getContentPacks().clear();
engine->loadContent();
auto menu = engine->getGUI()->getMenu();
menu->reset();
menu->setPage("main");

View File

@ -49,9 +49,9 @@ void Menu::setPage(std::string name, bool history) {
void Menu::setPage(Page page, bool history) {
if (current.panel) {
Container::remove(current.panel);
}
if (history) {
pageStack.push(current);
if (history) {
pageStack.push(current);
}
}
current = page;
Container::add(current.panel);

View File

@ -1,4 +1,5 @@
#include "LevelController.h"
#include "../files/WorldFiles.h"
#include "../debug/Logger.h"
#include "../world/Level.h"
#include "../world/World.h"
@ -46,6 +47,7 @@ void LevelController::update(float delta, bool input, bool pause) {
}
void LevelController::saveWorld() {
level->getWorld()->wfile->createDirectories();
logger.info() << "writing world";
scripting::on_world_save();
level->getWorld()->write(level.get());

View File

@ -240,6 +240,15 @@ void scripting::on_ui_open(
});
}
void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) {
std::string name = layout->getId() + ".progress";
emit_event(name, [=] (lua::LuaState* state) {
state->pushinteger(workDone);
state->pushinteger(workTotal);
return 2;
});
}
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
std::string name = layout->getId() + ".close";
emit_event(name, [inventory] (lua::LuaState* state) {
@ -330,6 +339,7 @@ void scripting::load_layout_script(scriptenv senv, std::string prefix, fs::path
state->loadbuffer(env, src, file.u8string());
state->callNoThrow(0);
script.onopen = register_event(env, "on_open", prefix+".open");
script.onprogress = register_event(env, "on_progress", prefix+".progress");
script.onclose = register_event(env, "on_close", prefix+".close");
}

View File

@ -79,6 +79,8 @@ namespace scripting {
std::vector<std::unique_ptr<dynamic::Value>> args
);
void on_ui_progress(UiDocument* layout, int workDone, int totalWork);
/// @brief Called on UI view close
void on_ui_close(UiDocument* layout, Inventory* inventory);