add gui.alert & critical fixes

This commit is contained in:
MihailRis 2025-01-02 21:59:18 +03:00
parent b9ff1db086
commit a3d35cd10f
7 changed files with 52 additions and 25 deletions

View File

@ -86,25 +86,25 @@ bool menus::call(Engine& engine, runnable func) {
engine.setScreen(std::make_shared<MenuScreen>(engine));
// could not to find or read pack
guiutil::alert(
gui->getMenu(), langs::get(L"error.pack-not-found")+L": "+
engine, langs::get(L"error.pack-not-found")+L": "+
util::str2wstr_utf8(error.getPackId())
);
return false;
} catch (const assetload::error& error) {
engine.setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(
gui->getMenu(), langs::get(L"Assets Load Error", L"menu")+L":\n"+
engine, langs::get(L"Assets Load Error", L"menu")+L":\n"+
util::str2wstr_utf8(error.what())
);
return false;
} catch (const parsing_error& error) {
engine.setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(gui->getMenu(), util::str2wstr_utf8(error.errorLog()));
guiutil::alert(engine, util::str2wstr_utf8(error.errorLog()));
return false;
} catch (const std::runtime_error& error) {
engine.setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(
gui->getMenu(), langs::get(L"Content Error", L"menu")+L":\n"+
engine, langs::get(L"Content Error", L"menu")+L":\n"+
util::str2wstr_utf8(error.what())
);
return false;

View File

@ -27,12 +27,22 @@ std::shared_ptr<gui::UINode> guiutil::create(const std::string& source, scripten
}
void guiutil::alert(
const std::shared_ptr<gui::Menu>& menu,
Engine& engine,
const std::wstring& text,
const runnable& on_hidden
) {
auto panel = std::make_shared<Panel>(glm::vec2(500, 300), glm::vec4(8.0f), 8.0f);
panel->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
auto menu = engine.getGUI()->getMenu();
runnable on_hidden_final = [on_hidden, menu, &engine]() {
if (on_hidden) {
on_hidden();
} else {
menu->back();
}
menu->removePage("<alert>");
};
auto label = std::make_shared<Label>(text);
label->setMultiline(true);
@ -41,13 +51,18 @@ void guiutil::alert(
panel->add(std::make_shared<Button>(
langs::get(L"Ok"), glm::vec4(10.f),
[=](GUI*) {
if (on_hidden) {
on_hidden();
}
menu->back();
on_hidden_final();
}
));
panel->refresh();
panel->keepAlive(Events::keyCallbacks[keycode::ENTER].add([=](){
on_hidden_final();
return true;
}));
panel->keepAlive(Events::keyCallbacks[keycode::ESCAPE].add([=](){
on_hidden_final();
return true;
}));
menu->addPage("<alert>", panel);
menu->setPage("<alert>");
}
@ -74,21 +89,19 @@ void guiutil::confirm(
runnable on_confirm_final = [on_confirm, menu, &engine]() {
if (on_confirm) {
on_confirm();
} else {
menu->back();
}
menu->back();
engine.postRunnable([menu]() {
menu->removePage("<confirm>");
});
menu->removePage("<confirm>");
};
runnable on_deny_final = [on_deny, menu, &engine]() {
if (on_deny) {
on_deny();
} else {
menu->back();
}
menu->back();
engine.postRunnable([menu]() {
menu->removePage("<confirm>");
});
menu->removePage("<confirm>");
};
subpanel->add(std::make_shared<Button>(yestext, glm::vec4(8.f), [=](GUI*){

View File

@ -15,7 +15,7 @@ namespace guiutil {
std::shared_ptr<gui::UINode> create(const std::string& source, scriptenv env=0);
void alert(
const std::shared_ptr<gui::Menu>& menu,
Engine& engine,
const std::wstring& text,
const runnable& on_hidden=nullptr
);

View File

@ -151,7 +151,7 @@ static void load_world(
engine.onWorldOpen(std::move(level));
} catch (const world_load_error& error) {
guiutil::alert(
engine.getGUI()->getMenu(),
engine,
langs::get(L"Error") + L": " + util::str2wstr_utf8(error.what())
);
return;

View File

@ -780,6 +780,17 @@ static int l_gui_confirm(lua::State* L) {
return 0;
}
static int l_gui_alert(lua::State* L) {
auto message = lua::require_wstring(L, 1);
runnable onconfirm = nullptr;
if (lua::gettop(L) >= 2) {
lua::pushvalue(L, 2);
onconfirm = lua::create_runnable(L);
}
guiutil::alert(*engine, message, onconfirm);
return 0;
}
const luaL_Reg guilib[] = {
{"get_viewport", lua::wrap<l_gui_getviewport>},
{"getattr", lua::wrap<l_gui_getattr>},
@ -790,6 +801,7 @@ const luaL_Reg guilib[] = {
{"clear_markup", lua::wrap<l_gui_clear_markup>},
{"escape_markup", lua::wrap<l_gui_escape_markup>},
{"confirm", lua::wrap<l_gui_confirm>},
{"alert", lua::wrap<l_gui_alert>},
{"__reindex", lua::wrap<l_gui_reindex>},
{NULL, NULL}
};

View File

@ -241,8 +241,9 @@ runnable lua::create_runnable(State* L) {
return [=]() {
getglobal(L, LAMBDAS_TABLE);
getfield(L, *funcptr);
call_nothrow(L, 0);
pop(L);
if (call_nothrow(L, 0)) {
pop(L);
}
};
}
@ -253,10 +254,9 @@ supplier<bool> lua::create_simple_handler(State* L) {
getfield(L, *funcptr);
if (call_nothrow(L, 0)) {
bool result = toboolean(L, -1);
pop(L, 2);
pop(L);
return result;
} else {
pop(L);
return false;
}
};

View File

@ -12,7 +12,7 @@ namespace util {
int nextid = 1;
std::unordered_map<int, std::function<bool(Types...)>> handlers;
std::vector<int> order;
std::mutex mutex;
std::recursive_mutex mutex;
public:
HandlersList() = default;
@ -45,7 +45,9 @@ namespace util {
void notify(Types...args) {
std::lock_guard lock(mutex);
for (auto it = order.rbegin(); it != order.rend(); ++it) {
auto orderCopy = order;
for (auto it = orderCopy.rbegin(); it != orderCopy.rend(); ++it) {
if (handlers.at(*it)(args...)) {
break;
}