world creation and generators menus moved to xml

This commit is contained in:
MihailRis 2024-04-19 05:34:02 +03:00
parent ef4294ab47
commit 4f299be681
15 changed files with 187 additions and 136 deletions

View File

@ -1,5 +1,5 @@
<panel size='400' color='0' interval='1' context='menu'>
<button onclick='menu.page="new-world"' padding='10'>@New World</button>
<button onclick='menu.page="new_world"' padding='10'>@New World</button>
<panel id='worlds' size='390,1' padding='5' color='#FFFFFF11' max-length='400'>
</panel>
<button onclick='menu.page="settings"' padding='10'>@Settings</button>

View File

@ -0,0 +1,17 @@
<panel size='400' color='0' interval='1' context='world'>
<label>@Name</label>
<textbox id='name_box'
validator='world_name_validator'
placeholder='New World'
padding='4'>
</textbox>
<label>@Seed</label>
<textbox id='seed_box' placeholder='-' padding='4'></textbox>
<button onclick='menu.page="world_generators"' id='generator_btn' padding='10'>
@World generator
</button>
<button onclick='create_world()' padding='10' margin='1,20,1,1'>
@Create World
</button>
<button onclick='menu:back()' padding='10'>@Back</button>
</panel>

View File

@ -0,0 +1,35 @@
settings = session.get_entry('new_world')
function world_name_validator(name)
return name:match("^[%w-\\.\\ ]+$") ~= nil and not world.exists(name)
end
function settings.generator_name(id)
local prefix, name = parse_path(id)
if prefix == "core" then
return gui.str(name, "world.generators")
else
return id
end
end
function create_world()
if not document.name_box.valid then
return
end
local name = document.name_box.text
local seed = document.seed_box.text
core.new_world(name, seed, settings.generator)
end
function on_open()
if settings.generator == nil then
settings.generator = core.get_default_generator()
end
document.generator_btn.text = string.format(
"%s: %s",
gui.str("World generator", "world"),
settings.generator_name(settings.generator)
)
document.seed_box.placeholder = tostring(math.random()):sub(3)
end

View File

@ -0,0 +1,3 @@
<panel size='400' color='#00000080' padding='8'>
<!-- content is generated in script -->
</panel>

View File

@ -0,0 +1,16 @@
settings = session.get_entry('new_world')
function on_open()
local names = core.get_generators()
table.sort(names)
local panel = document.root
for _,k in ipairs(names) do
panel:add(gui.template("generator", {
callback=string.format("settings.generator=%q menu:back()", k),
id=k,
name=settings.generator_name(k)
}))
end
panel:add("<button padding='10' onclick='menu:back()'>@Back</button>")
end

View File

@ -0,0 +1,4 @@
<container onclick='%{callback}' size='80,30' hover-color='#FFFFFF10'>
<label pos='80,4' size='300,25' color='#FFFFFF80' align='right'>[%{id}]</label>
<label pos='0,8'>%{name}</label>
</container>

View File

@ -215,6 +215,23 @@ function gui.template(name, params)
return text
end
session = {
entries={}
}
function session.get_entry(name)
local entry = session.entries[name]
if entry == nil then
entry = {}
session.entries[name] = entry
end
return entry
end
function session.reset_entry(name)
session.entries[name] = nil
end
-- Deprecated functions
block_index = block.index
block_name = block.name

View File

@ -31,7 +31,7 @@ world.Name=Название
world.World generator=Генератор мира
world.generators.default=Обычный
world.generators.flat=Плоский
menu.Create World=Создать Мир
world.Create World=Создать Мир
world.convert-request=Есть изменения в индексах! Конвертировать мир?
world.delete-confirm=Удалить мир безвозвратно?

View File

@ -40,10 +40,6 @@
namespace fs = std::filesystem;
using namespace gui;
namespace menus {
extern std::string generatorID;
}
void menus::create_version_label(Engine* engine) {
auto gui = engine->getGUI();
auto vlabel = std::make_shared<gui::Label>(
@ -220,10 +216,7 @@ void menus::delete_world(std::string name, Engine* engine) {
}
void menus::create_menus(Engine* engine) {
menus::generatorID = WorldGenerators::getDefaultGeneratorID();
create_new_world_panel(engine);
create_settings_panel(engine);
create_world_generators_panel(engine);
auto menu = engine->getGUI()->getMenu();
menu->setPageLoader([=](auto name) {
@ -238,6 +231,4 @@ void menus::create_menus(Engine* engine) {
}
void menus::refresh_menus(Engine* engine) {
create_new_world_panel(engine);
create_world_generators_panel(engine);
}

View File

@ -20,12 +20,6 @@ namespace menus {
// implemented in menu_settings.cpp
extern void create_settings_panel(Engine* engine);
// implemented in menu_create_world.cpp
extern void create_new_world_panel(Engine* engine);
// implemented in menu_create_world.cpp
extern void create_world_generators_panel(Engine* engine);
extern std::shared_ptr<gui::Panel> create_packs_panel(
const std::vector<ContentPack>& packs,
Engine* engine,
@ -57,6 +51,13 @@ namespace menus {
std::vector<std::string> packs
);
void create_world(
Engine* engine,
const std::string& name,
const std::string& seedstr,
const std::string& generatorID
);
/// @brief Create development version label at the top-right screen corner
void create_version_label(Engine* engine);
void create_menus(Engine* engine);

View File

@ -20,12 +20,6 @@
using namespace gui;
std::shared_ptr<gui::Button> generatorTypeButton;
namespace menus {
std::string generatorID;
}
inline uint64_t randU64() {
srand(time(NULL));
return rand() ^ (rand() << 8) ^
@ -35,96 +29,27 @@ inline uint64_t randU64() {
((uint64_t)rand() << 56);
}
inline uint64_t str2seed(std::wstring seedstr) {
inline uint64_t str2seed(const std::string& seedstr) {
if (util::is_integer(seedstr)) {
try {
return std::stoull(seedstr);
} catch (const std::out_of_range& err) {
std::hash<std::wstring> hash;
std::hash<std::string> hash;
return hash(seedstr);
}
} else {
std::hash<std::wstring> hash;
std::hash<std::string> hash;
return hash(seedstr);
}
}
static std::string translate_generator_id(std::string& id) {
int delimiterPosition = id.find(":");
std::string pack = id.substr(0, delimiterPosition);
std::string generator = id.substr(delimiterPosition + 1);
if(pack == "core") {
return util::wstr2str_utf8(langs::get(util::str2wstr_utf8(generator), L"world.generators"));
} else {
return id;
}
}
void menus::create_world_generators_panel(Engine* engine) {
auto menu = engine->getGUI()->getMenu();
auto panel = menus::create_page(engine, "world_generators", 400, 0.5f, 1);
panel->setScrollable(true);
std::vector<std::string> generatorsIDs = WorldGenerators::getGeneratorsIDs();
std::sort(generatorsIDs.begin(), generatorsIDs.end());
for (std::string& id : generatorsIDs) {
const std::string& fullName = translate_generator_id(id);
auto button = std::make_shared<Container>(glm::vec2(80, 30));
auto idlabel = std::make_shared<Label>("["+id+"]");
idlabel->setColor(glm::vec4(1, 1, 1, 0.5f));
idlabel->setSize(glm::vec2(300, 25));
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;
generatorTypeButton->setText(langs::get(L"World generator", L"world") + (L": ") + util::str2wstr_utf8(translate_generator_id(menus::generatorID)));
menu->back();
}
);
panel->add(button);
}
panel->add(guiutil::backButton(menu));
}
void menus::create_new_world_panel(Engine* engine) {
auto panel = menus::create_page(engine, "new-world", 400, 0.0f, 1);
panel->add(std::make_shared<Label>(langs::get(L"Name", L"world")));
auto nameInput = std::make_shared<TextBox>(L"New World", glm::vec4(6.0f));
nameInput->setTextValidator([=](const std::wstring& text) {
EnginePaths* paths = engine->getPaths();
std::string textutf8 = util::wstr2str_utf8(text);
return util::is_valid_filename(text) &&
!paths->isWorldNameUsed(textutf8);
});
panel->add(nameInput);
panel->add(std::make_shared<Label>(langs::get(L"Seed", L"world")));
auto seedstr = std::to_wstring(randU64());
auto seedInput = std::make_shared<TextBox>(seedstr, glm::vec4(6.0f));
panel->add(seedInput);
generatorTypeButton = guiutil::gotoButton(
langs::get(L"World generator", L"world") + L": " +
util::str2wstr_utf8(translate_generator_id(menus::generatorID)),
"world_generators",
engine->getGUI()->getMenu()
);
panel->add(generatorTypeButton);
panel->add(menus::create_button(L"Create World", glm::vec4(10), glm::vec4(1, 20, 1, 1),
[=](GUI*) {
if (!nameInput->validate())
return;
std::string name = util::wstr2str_utf8(nameInput->getText());
uint64_t seed = str2seed(seedInput->getText());
std::cout << "world seed: " << seed << std::endl;
void menus::create_world(
Engine* engine,
const std::string& name,
const std::string& seedstr,
const std::string& generatorID
) {
uint64_t seed = str2seed(seedstr);
EnginePaths* paths = engine->getPaths();
auto folder = paths->getWorldsFolder()/fs::u8path(name);
@ -153,16 +78,13 @@ void menus::create_new_world_panel(Engine* engine) {
}
Level* level = World::create(
name, menus::generatorID, folder, seed,
name, generatorID, folder, seed,
engine->getSettings(),
engine->getContent(),
engine->getContentPacks()
);
level->getWorld()->wfile->createDirectories();
menus::generatorID = WorldGenerators::getDefaultGeneratorID();
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
}));
panel->add(guiutil::backButton(engine->getGUI()->getMenu()));
}
#endif // FRONTEND_MENU_MENU_CREATE_WORLD_H_

View File

@ -20,7 +20,7 @@
using namespace gui;
static void reopen_world(Engine* engine, World* world) {
std::string wname = world->wfile->getFolder().stem().u8string();
std::string wname = world->wfile->getFolder().filename().u8string();
engine->setScreen(nullptr);
engine->setScreen(std::make_shared<MenuScreen>(engine));
menus::open_world(wname, engine, true);

View File

@ -8,6 +8,7 @@
#include "../../../logic/LevelController.h"
#include "../../../window/Events.h"
#include "../../../window/Window.h"
#include "../../../world/WorldGenerators.h"
#include "../scripting.h"
#include <vector>
@ -29,6 +30,14 @@ static int l_get_worlds_list(lua_State* L) {
return 1;
}
static int l_new_world(lua_State* L) {
auto name = lua_tostring(L, 1);
auto seed = lua_tostring(L, 2);
auto generator = lua_tostring(L, 3);
menus::create_world(scripting::engine, name, seed, generator);
return 0;
}
static int l_open_world(lua_State* L) {
auto name = lua_tostring(L, 1);
scripting::engine->setScreen(nullptr);
@ -126,8 +135,27 @@ static int l_quit(lua_State* L) {
return 0;
}
static int l_get_default_generator(lua_State* L) {
lua_pushstring(L, WorldGenerators::getDefaultGeneratorID().c_str());
return 1;
}
static int l_get_generators(lua_State* L) {
const auto& generators = WorldGenerators::getGeneratorsIDs();
lua_createtable(L, generators.size(), 0);
int i = 0;
for (auto& id : generators) {
lua_pushstring(L, id.c_str());
lua_rawseti(L, -2, i + 1);
i++;
}
return 1;
}
const luaL_Reg corelib [] = {
{"get_worlds_list", lua_wrap_errors<l_get_worlds_list>},
{"new_world", lua_wrap_errors<l_new_world>},
{"open_world", lua_wrap_errors<l_open_world>},
{"close_world", lua_wrap_errors<l_close_world>},
{"delete_world", lua_wrap_errors<l_delete_world>},
@ -138,5 +166,7 @@ const luaL_Reg corelib [] = {
{"set_setting", lua_wrap_errors<l_set_setting>},
{"str_setting", lua_wrap_errors<l_str_setting>},
{"quit", lua_wrap_errors<l_quit>},
{"get_default_generator", lua_wrap_errors<l_get_default_generator>},
{"get_generators", lua_wrap_errors<l_get_generators>},
{NULL, NULL}
};

View File

@ -120,6 +120,9 @@ static bool getattr(lua_State* L, gui::TextBox* box, const std::string& attr) {
} else if (attr == "placeholder") {
lua_pushstring(L, util::wstr2str_utf8(box->getPlaceholder()).c_str());
return true;
} else if (attr == "valid") {
lua_pushboolean(L, box->validate());
return true;
}
return false;
}

View File

@ -3,8 +3,12 @@
#include "../scripting.h"
#include "../../../world/Level.h"
#include "../../../world/World.h"
#include "../../../engine.h"
#include <cmath>
#include <filesystem>
namespace fs = std::filesystem;
static int l_world_get_total_time(lua_State* L) {
lua_pushnumber(L, scripting::level->getWorld()->totalTime);
@ -27,10 +31,18 @@ static int l_world_get_seed(lua_State* L) {
return 1;
}
static int l_world_exists(lua_State* L) {
auto name = lua_tostring(L, 1);
auto worldsDir = scripting::engine->getPaths()->getWorldFolder(name);
lua_pushboolean(L, fs::is_directory(worldsDir));
return 1;
}
const luaL_Reg worldlib [] = {
{"get_total_time", lua_wrap_errors<l_world_get_total_time>},
{"get_day_time", lua_wrap_errors<l_world_get_day_time>},
{"set_day_time", lua_wrap_errors<l_world_set_day_time>},
{"get_seed", lua_wrap_errors<l_world_get_seed>},
{"exists", lua_wrap_errors<l_world_exists>},
{NULL, NULL}
};