This commit is contained in:
MihailRis 2023-12-19 20:10:09 +03:00
parent b4caf888dc
commit c9db1acd91
6 changed files with 227 additions and 221 deletions

View File

@ -18,9 +18,9 @@ menu.Controls=Управление
menu.Back to Main Menu=Вернуться в Меню menu.Back to Main Menu=Вернуться в Меню
menu.Settings=Настройки menu.Settings=Настройки
menu.Content=Контент menu.Content=Контент
world.Seed=Зерно menu.Seed=Зерно
world.Name=Название menu.Name=Название
world.Create World=Создать Мир menu.Create World=Создать Мир
world.convert-request=Есть изменения в индексах! Конвертировать мир? world.convert-request=Есть изменения в индексах! Конвертировать мир?

View File

@ -18,9 +18,9 @@ menu.Controls=Управление
menu.Back to Main Menu=Вернуться на Главную menu.Back to Main Menu=Вернуться на Главную
menu.Settings=Настройки menu.Settings=Настройки
menu.Content=Наполнение menu.Content=Наполнение
world.Seed=Семя menu.Seed=Семя
world.Name=Название menu.Name=Название
world.Create World=Создать Мир menu.Create World=Создать Мир
world.convert-request=Есть изменения в индексах! Преобразовать мир? world.convert-request=Есть изменения в индексах! Преобразовать мир?

View File

@ -64,7 +64,9 @@ Button::Button(shared_ptr<UINode> content, glm::vec4 padding) : Panel(vec2(32,32
scrollable(false); scrollable(false);
} }
Button::Button(wstring text, glm::vec4 padding) : Panel(vec2(32,32), padding, 0) { Button::Button(wstring text, glm::vec4 padding, glm::vec4 margin)
: Panel(vec2(32,32), padding, 0) {
this->margin(margin);
Label* label = new Label(text); Label* label = new Label(text);
label->align(Align::center); label->align(Align::center);
this->label = shared_ptr<UINode>(label); this->label = shared_ptr<UINode>(label);

View File

@ -48,7 +48,9 @@ namespace gui {
std::shared_ptr<UINode> label = nullptr; std::shared_ptr<UINode> label = nullptr;
public: public:
Button(std::shared_ptr<UINode> content, glm::vec4 padding=glm::vec4(2.0f)); Button(std::shared_ptr<UINode> content, glm::vec4 padding=glm::vec4(2.0f));
Button(std::wstring text, glm::vec4 padding=glm::vec4(2.0f)); Button(std::wstring text,
glm::vec4 padding=glm::vec4(2.0f),
glm::vec4 margin=glm::vec4(1.0f));
virtual void drawBackground(Batch2D* batch, Assets* assets); virtual void drawBackground(Batch2D* batch, Assets* assets);

View File

@ -19,6 +19,7 @@ Button* guiutil::backButton(PagesControl* menu) {
} }
Button* guiutil::gotoButton(wstring text, string page, PagesControl* menu) { Button* guiutil::gotoButton(wstring text, string page, PagesControl* menu) {
text = langs::get(text, L"menu");
return (new Button(text, vec4(10.f)))->listenAction([=](GUI* gui) { return (new Button(text, vec4(10.f)))->listenAction([=](GUI* gui) {
menu->set(page); menu->set(page);
}); });

View File

@ -28,47 +28,55 @@
using glm::vec2; using glm::vec2;
using glm::vec4; using glm::vec4;
using std::make_unique;
using std::unique_ptr;
using std::shared_ptr;
namespace fs = std::filesystem; namespace fs = std::filesystem;
using namespace gui; using namespace gui;
Panel* create_main_menu_panel(Engine* engine, PagesControl* menu); inline uint64_t randU64() {
Panel* create_new_world_panel(Engine* engine, PagesControl* menu); return rand() ^ (rand() << 8) ^
Panel* create_controls_panel(Engine* engine, PagesControl* menu); (rand() << 16) ^ (rand() << 24) ^
Panel* create_settings_panel(Engine* engine, PagesControl* menu); ((uint64_t)rand() << 32) ^
Panel* create_pause_panel(Engine* engine, PagesControl* menu); ((uint64_t)rand() << 40) ^
Panel* create_languages_panel(Engine* engine, PagesControl* menu); ((uint64_t)rand() << 56);
Panel* create_content_panel(Engine* engine, PagesControl* menu);
void menus::create_menus(Engine* engine, PagesControl* menu) {
menu->add("new-world", create_new_world_panel(engine, menu));
menu->add("settings", create_settings_panel(engine, menu));
menu->add("controls", create_controls_panel(engine, menu));
menu->add("pause", create_pause_panel(engine, menu));
menu->add("languages", create_languages_panel(engine, menu));
menu->add("content", create_content_panel(engine, menu));
menu->add("main", create_main_menu_panel(engine, menu));
} }
void menus::refresh_menus(Engine* engine, PagesControl* menu) { std::shared_ptr<Panel> create_page(
menu->add("main", create_main_menu_panel(engine, menu)); Engine* engine,
std::string name,
int width,
float opacity,
int interval) {
PagesControl* menu = engine->getGUI()->getMenu();
Panel* panel = new Panel(vec2(width, 200), vec4(8.0f), interval);
panel->color(vec4(0.0f, 0.0f, 0.0f, opacity));
std::shared_ptr<Panel> ptr (panel);
menu->add(name, ptr);
return ptr;
} }
void show_content_select(GUI* gui, const EnginePaths* paths, Button* create_button(std::wstring text,
std::vector<ContentPack>& packs) { glm::vec4 padding,
glm::vec4 margin,
gui::onaction action) {
auto btn = new Button(langs::get(text, L"menu"),
padding, margin);
btn->listenAction(action);
return btn;
}
void show_content_select(GUI* gui, Engine* engine) {
PagesControl* menu = gui->getMenu(); PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(200, 200), vec4(8.0f), 8.0f); Panel* panel = new Panel(vec2(200, 200), vec4(8.0f), 8.0f);
auto resdir = paths->getResources(); auto resdir = engine->getPaths()->getResources();
std::vector<ContentPack> allpacks; std::vector<ContentPack> allpacks;
ContentPack::scan(resdir / fs::path("content"), allpacks); ContentPack::scan(resdir / fs::path("content"), allpacks);
for (ContentPack& pack : allpacks) { for (ContentPack& pack : allpacks) {
bool selected = false; bool selected = false;
for (ContentPack& spack : packs) { for (ContentPack& spack : engine->getContentPacks()) {
if (spack.id == pack.id) { if (spack.id == pack.id) {
selected = true; selected = true;
break; break;
@ -91,10 +99,11 @@ void show_content_select(GUI* gui, const EnginePaths* paths,
menu->set("content-select"); menu->set("content-select");
} }
void show_content_missing(GUI* gui, const Content* content, ContentLUT* lut) { void show_content_missing(Engine* engine, const Content* content, ContentLUT* lut) {
PagesControl* menu = gui->getMenu(); auto* gui = engine->getGUI();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f); auto* menu = gui->getMenu();
panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f)); auto panel = create_page(engine, "missing-content", 500, 0.5f, 8);
panel->add(new Label(langs::get(L"menu.missing-content"))); panel->add(new Label(langs::get(L"menu.missing-content")));
Panel* subpanel = new Panel(vec2(500, 100)); Panel* subpanel = new Panel(vec2(500, 100));
@ -121,20 +130,23 @@ void show_content_missing(GUI* gui, const Content* content, ContentLUT* lut) {
subpanel->maxLength(400); subpanel->maxLength(400);
panel->add(subpanel); panel->add(subpanel);
panel->add((new Button(langs::get(L"Back to Main Menu", L"menu"), vec4(8.0f)))->listenAction([=](GUI*){ panel->add((new Button(langs::get(L"Back to Main Menu", L"menu"),
vec4(8.0f)))
->listenAction([=](GUI*){
menu->back(); menu->back();
})); }));
menu->add("missing-content", panel);
menu->set("missing-content"); menu->set("missing-content");
} }
void show_convert_request(GUI* gui, const Content* content, ContentLUT* lut, void show_convert_request(
fs::path folder) { Engine* engine,
guiutil::confirm(gui, langs::get(L"world.convert-request"), const Content* content,
ContentLUT* lut,
fs::path folder) {
guiutil::confirm(engine->getGUI(), langs::get(L"world.convert-request"),
[=]() { [=]() {
std::cout << "Convert the world: " << folder.string() << std::endl;
// TODO: add multithreading here // TODO: add multithreading here
auto converter = make_unique<WorldConverter>(folder, content, lut); auto converter = std::make_unique<WorldConverter>(folder, content, lut);
while (converter->hasNext()) { while (converter->hasNext()) {
converter->convertNext(); converter->convertNext();
} }
@ -143,9 +155,10 @@ void show_convert_request(GUI* gui, const Content* content, ContentLUT* lut,
}, L"", langs::get(L"Cancel")); }, L"", langs::get(L"Cancel"));
} }
Panel* create_languages_panel(Engine* engine, PagesControl* menu) { void create_languages_panel(Engine* engine, PagesControl* menu) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); auto panel = create_page(engine, "languages", 400, 0.5f, 1);
panel->scrollable(true); panel->scrollable(true);
std::vector<std::string> locales; std::vector<std::string> locales;
for (auto& entry : langs::locales_info) { for (auto& entry : langs::locales_info) {
locales.push_back(entry.first); locales.push_back(entry.first);
@ -164,96 +177,110 @@ Panel* create_languages_panel(Engine* engine, PagesControl* menu) {
panel->add(button); panel->add(button);
} }
panel->add(guiutil::backButton(menu)); panel->add(guiutil::backButton(menu));
return panel;
} }
Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) { void open_world(std::string name, Engine* engine) {
EnginePaths* paths = engine->getPaths(); // TODO: complete and move somewhere
auto paths = engine->getPaths();
auto resdir = paths->getResources();
auto folder = paths->getWorldsFolder()/fs::u8path(name);
auto& packs = engine->getContentPacks();
packs.clear();
auto packnames = ContentPack::worldPacksList(folder);
for (auto name : packnames) {
fs::path packfolder;
try {
packfolder = ContentPack::findPack(paths, name);
} catch (std::runtime_error& error) {
guiutil::alert(engine->getGUI(),
langs::get(L"error.pack-not-found")+
L": "+util::str2wstr_utf8(name));
return;
}
packs.push_back(ContentPack::read(packfolder));
}
engine->loadContent();
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); auto* content = engine->getContent();
panel->color(vec4(0.0f)); auto& settings = engine->getSettings();
fs::create_directories(folder);
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);
}
} else {
Level* level = World::load(folder, settings, content);
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
}
}
panel->add(guiutil::gotoButton(langs::get(L"New World", L"menu"), "new-world", menu)); Panel* create_worlds_panel(Engine* engine) {
auto panel = new Panel(vec2(390, 200), vec4(5.0f));
panel->color(vec4(1.0f, 1.0f, 1.0f, 0.07f));
panel->maxLength(400);
Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f)); auto paths = engine->getPaths();
worldsPanel->color(vec4(1.0f, 1.0f, 1.0f, 0.07f));
worldsPanel->maxLength(400);
fs::path worldsFolder = paths->getWorldsFolder(); fs::path worldsFolder = paths->getWorldsFolder();
if (std::filesystem::is_directory(worldsFolder)) { if (fs::is_directory(worldsFolder)) {
for (auto const& entry : fs::directory_iterator(worldsFolder)) { for (auto entry : fs::directory_iterator(worldsFolder)) {
if (!entry.is_directory()) { if (!entry.is_directory()) {
continue; continue;
} }
std::string name = entry.path().filename().u8string(); auto name = entry.path().filename().u8string();
Button* button = new Button(util::str2wstr_utf8(name), auto btn = new Button(util::str2wstr_utf8(name),
vec4(10.0f, 8.0f, 10.0f, 8.0f)); vec4(10.0f, 8.0f, 10.0f, 8.0f));
button->color(vec4(1.0f, 1.0f, 1.0f, 0.1f)); btn->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
button->listenAction([=](GUI* gui) { btn->listenAction([=](GUI*) {
// TODO: complete and move somewhere open_world(name, engine);
auto folder = paths->getWorldsFolder()/fs::u8path(name);
auto resdir = engine->getPaths()->getResources();
auto& packs = engine->getContentPacks();
packs.clear();
auto packnames = ContentPack::worldPacksList(folder);
for (auto name : packnames) {
fs::path packfolder;
try {
packfolder = ContentPack::findPack(paths, name);
} catch (std::runtime_error& error) {
guiutil::alert(gui, langs::get(L"error.pack-not-found")+
L": "+util::str2wstr_utf8(name));
return;
}
packs.push_back(ContentPack::read(packfolder));
}
engine->loadContent();
auto* content = engine->getContent();
auto& settings = engine->getSettings();
std::filesystem::create_directories(folder);
ContentLUT* lut = World::checkIndices(folder, content);
if (lut) {
if (lut->hasMissingContent()) {
show_content_missing(gui, content, lut);
delete lut;
} else {
show_convert_request(gui, content, lut, folder);
}
} else {
Level* level = World::load(folder, settings, content);
auto screen = new LevelScreen(engine, level);
engine->setScreen(shared_ptr<Screen>(screen));
}
}); });
worldsPanel->add(button); panel->add(btn);
} }
} }
panel->add(worldsPanel); return panel;
panel->add(guiutil::gotoButton(langs::get(L"Settings", L"menu"), "settings", menu)); }
panel->add(guiutil::gotoButton(langs::get(L"Content", L"menu"), "content", menu));
panel->add((new Button(langs::get(L"Quit", L"menu"), vec4(10.f)))->listenAction([](GUI* gui) { void create_main_menu_panel(Engine* engine, PagesControl* menu) {
auto panel = create_page(engine, "main", 400, 0.0f, 1);
panel->add(guiutil::gotoButton(L"New World", "new-world", menu));
panel->add(create_worlds_panel(engine));
panel->add(guiutil::gotoButton(L"Settings", "settings", menu));
panel->add(guiutil::gotoButton(L"Content", "content", menu));
panel->add((new Button(langs::get(L"Quit", L"menu"), vec4(10.f)))
->listenAction([](GUI* gui) {
Window::setShouldClose(true); Window::setShouldClose(true);
})); }));
return panel;
} }
Panel* create_content_panel(Engine* engine, PagesControl* menu) { void create_content_panel(Engine* engine, PagesControl* menu) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 5.0f); auto panel = create_page(engine, "content", 400, 0.0f, 5);
panel->color(vec4(0.0f));
panel->add(new Label(L"work in progress")); panel->add(new Label(L"work in progress"));
panel->add(guiutil::backButton(menu)); panel->add(guiutil::backButton(menu));
return panel;
} }
Panel* create_new_world_panel(Engine* engine, PagesControl* menu) { inline uint64_t str2seed(std::wstring seedstr) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); if (util::is_integer(seedstr)) {
panel->color(vec4(0.0f)); try {
return std::stoull(seedstr);
} catch (const std::out_of_range& err) {
std::hash<std::wstring> hash;
return hash(seedstr);
}
} else {
std::hash<std::wstring> hash;
return hash(seedstr);
}
}
TextBox* worldNameInput; void create_new_world_panel(Engine* engine, PagesControl* menu) {
{ auto panel = create_page(engine, "new-world", 400, 0.0f, 1);
TextBox* worldNameInput; {
Label* label = new Label(langs::get(L"Name", L"world")); Label* label = new Label(langs::get(L"Name", L"world"));
panel->add(label); panel->add(label);
@ -262,93 +289,66 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) {
worldNameInput = input; worldNameInput = input;
} }
TextBox* seedInput; TextBox* seedInput; {
{ panel->add(std::make_shared<Label>(langs::get(L"Seed", L"world")));
Label* label = new Label(langs::get(L"Seed", L"world")); seedInput = new TextBox(std::to_wstring(randU64()), vec4(6.0f));
panel->add(shared_ptr<UINode>(label));
uint64_t randseed = rand() ^ (rand() << 8) ^
(rand() << 16) ^ (rand() << 24) ^
((uint64_t)rand() << 32) ^
((uint64_t)rand() << 40) ^
((uint64_t)rand() << 56);
seedInput = new TextBox(std::to_wstring(randseed), vec4(6.0f));
panel->add(seedInput); panel->add(seedInput);
} }
panel->add((new Button(langs::get(L"Content", L"menu"), vec4(10.0f)))->listenAction([=](GUI* gui){ panel->add((new Button(langs::get(L"Content", L"menu"), vec4(10.0f)))
show_content_select(gui, engine->getPaths(), engine->getContentPacks()); ->listenAction([=](GUI* gui){
show_content_select(gui, engine);
})); }));
{ vec4 basecolor = worldNameInput->color();
Button* button = new Button(langs::get(L"Create World", L"world"), vec4(10.0f)); panel->add(create_button( L"Create World", vec4(10), vec4(1, 20, 1, 1),
button->margin(vec4(1, 20, 1, 1)); [=](GUI*) {
vec4 basecolor = worldNameInput->color(); std::wstring name = worldNameInput->text();
button->listenAction([=](GUI*) { std::string nameutf8 = util::wstr2str_utf8(name);
std::wstring name = worldNameInput->text(); EnginePaths* paths = engine->getPaths();
std::string nameutf8 = util::wstr2str_utf8(name);
EnginePaths* paths = engine->getPaths();
// Basic validation // Basic validation
if (!util::is_valid_filename(name) || if (!util::is_valid_filename(name) ||
paths->isWorldNameUsed(nameutf8)) { paths->isWorldNameUsed(nameutf8)) {
// blink red two times // blink red two times
panel->listenInterval(0.1f, [worldNameInput, basecolor]() { panel->listenInterval(0.1f, [worldNameInput, basecolor]() {
static bool flag = true; static bool flag = true;
if (flag) { if (flag) {
worldNameInput->color(vec4(0.3f, 0.0f, 0.0f, 0.5f)); worldNameInput->color(vec4(0.3f, 0.0f, 0.0f, 0.5f));
} else { } else {
worldNameInput->color(basecolor); worldNameInput->color(basecolor);
}
flag = !flag;
}, 4);
return;
}
std::wstring seedstr = seedInput->text();
uint64_t seed;
if (util::is_integer(seedstr)) {
try {
seed = std::stoull(seedstr);
} catch (const std::out_of_range& err) {
std::hash<std::wstring> hash;
seed = hash(seedstr);
} }
} else { flag = !flag;
std::hash<std::wstring> hash; }, 4);
seed = hash(seedstr); return;
} }
std::cout << "world seed: " << seed << std::endl;
auto folder = paths->getWorldsFolder()/fs::u8path(nameutf8); std::wstring seedstr = seedInput->text();
std::filesystem::create_directories(folder); uint64_t seed = str2seed(seedstr);
std::cout << "world seed: " << seed << std::endl;
// TODO: complete and move somewhere auto folder = paths->getWorldsFolder()/fs::u8path(nameutf8);
auto resdir = engine->getPaths()->getResources(); fs::create_directories(folder);
auto& packs = engine->getContentPacks();
packs.clear();
packs.push_back(ContentPack::read(resdir/fs::path("content/base")));
engine->loadContent();
Level* level = World::create(nameutf8,
folder,
seed,
engine->getSettings(),
engine->getContent());
auto screen = new LevelScreen(engine, level);
engine->setScreen(shared_ptr<Screen>(screen));
});
panel->add(button);
}
// TODO: complete and move somewhere
auto resdir = engine->getPaths()->getResources();
auto& packs = engine->getContentPacks();
packs.clear();
packs.push_back(ContentPack::read(resdir/fs::path("content/base")));
engine->loadContent();
Level* level = World::create(nameutf8,
folder,
seed,
engine->getSettings(),
engine->getContent());
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
}));
panel->add(guiutil::backButton(menu)); panel->add(guiutil::backButton(menu));
return panel;
} }
Panel* create_controls_panel(Engine* engine, PagesControl* menu) { void create_controls_panel(Engine* engine, PagesControl* menu) {
Panel* panel = new Panel(vec2(400, 200), vec4(2.0f), 1.0f); auto panel = create_page(engine, "controls", 400, 0.0f, 1);
panel->color(vec4(0.0f));
/* Camera sensitivity setting track bar */{ /* Camera sensitivity setting track bar */{
panel->add((new Label(L""))->textSupplier([=]() { panel->add((new Label(L""))->textSupplier([=]() {
@ -387,12 +387,10 @@ Panel* create_controls_panel(Engine* engine, PagesControl* menu) {
} }
panel->add(scrollPanel); panel->add(scrollPanel);
panel->add(guiutil::backButton(menu)); panel->add(guiutil::backButton(menu));
return panel;
} }
Panel* create_settings_panel(Engine* engine, PagesControl* menu) { void create_settings_panel(Engine* engine, PagesControl* menu) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); auto panel = create_page(engine, "settings", 400, 0.0f, 1);
panel->color(vec4(0.0f));
/* Load Distance setting track bar */{ /* Load Distance setting track bar */{
panel->add((new Label(L""))->textSupplier([=]() { panel->add((new Label(L""))->textSupplier([=]() {
@ -498,36 +496,39 @@ Panel* create_settings_panel(Engine* engine, PagesControl* menu) {
panel->add(checkpanel); panel->add(checkpanel);
} }
{ std::string langName = langs::locales_info.at(langs::current->getId()).name;
std::string langName = langs::locales_info.at(langs::current->getId()).name; panel->add(guiutil::gotoButton(
panel->add(guiutil::gotoButton( langs::get(L"Language", L"settings")+L": "+
langs::get(L"Language", L"settings")+L": "+ util::str2wstr_utf8(langName),
util::str2wstr_utf8(langName), "languages", menu));
"languages", menu));
}
panel->add(guiutil::gotoButton(langs::get(L"Controls", L"menu"), "controls", menu)); panel->add(guiutil::gotoButton(L"Controls", "controls", menu));
panel->add(guiutil::backButton(menu)); panel->add(guiutil::backButton(menu));
return panel;
} }
Panel* create_pause_panel(Engine* engine, PagesControl* menu) { void create_pause_panel(Engine* engine, PagesControl* menu) {
Panel* panel = new Panel(vec2(400, 200)); auto panel = create_page(engine, "pause", 400, 0.0f, 1);
panel->color(vec4(0.0f));
{ panel->add(create_button(L"Continue", vec4(10.0f), vec4(1), [=](GUI*){
Button* button = new Button(langs::get(L"Continue", L"menu"), vec4(10.0f)); menu->reset();
button->listenAction([=](GUI*){ }));
menu->reset(); panel->add(guiutil::gotoButton(L"Settings", "settings", menu));
});
panel->add(shared_ptr<UINode>(button)); panel->add(create_button(L"Save and Quit to Menu", vec4(10.f), vec4(1), [=](GUI*){
} engine->setScreen(std::make_shared<MenuScreen>(engine));
panel->add(guiutil::gotoButton(langs::get(L"Settings", L"menu"), "settings", menu)); }));
{ }
Button* button = new Button(langs::get(L"Save and Quit to Menu", L"menu"), vec4(10.f));
button->listenAction([engine](GUI*){ void menus::create_menus(Engine* engine, PagesControl* menu) {
engine->setScreen(shared_ptr<Screen>(new MenuScreen(engine))); create_new_world_panel(engine, menu);
}); create_settings_panel(engine, menu);
panel->add(shared_ptr<UINode>(button)); create_controls_panel(engine, menu);
} create_pause_panel(engine, menu);
return panel; create_languages_panel(engine, menu);
create_content_panel(engine, menu);
create_main_menu_panel(engine, menu);
}
void menus::refresh_menus(Engine* engine, PagesControl* menu) {
create_main_menu_panel(engine, menu);
} }