Content menu, content-pack dependencies

This commit is contained in:
MihailRis 2024-01-27 00:36:18 +03:00
parent abc3e47feb
commit c57b2d0de3
25 changed files with 230 additions and 21 deletions

BIN
res/content/base/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -2,6 +2,7 @@
menu.missing-content=Missing Content!
world.convert-request=Content indices have changed! Convert world files?
error.pack-not-found=Could not to find pack
error.dependency-not-found=Dependency pack is not found
world.delete-confirm=Do you want to delete world forever?
# Bindings

View File

@ -5,8 +5,10 @@ Ok=Ок
Cancel=Отмена
Back=Назад
Continue=Продолжить
Add=Добавить
error.pack-not-found=Не удалось найти пакет
error.dependency-not-found=Используемая зависимость не найдена
# Меню
menu.New World=Новый Мир

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -60,6 +60,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, bool allAssets) {
loader.add(ASSET_SHADER, SHADERS_FOLDER"/skybox_gen", "skybox_gen");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/menubg.png", "gui/menubg");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/delete_icon.png", "gui/delete_icon");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/no_icon.png", "gui/no_icon");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/misc/moon.png", "misc/moon");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/misc/sun.png", "misc/sun");
loader.add(ASSET_FONT, FONTS_FOLDER"/font", "normal");

View File

@ -67,8 +67,17 @@ ContentPack ContentPack::read(fs::path folder) {
root->str("id", pack.id);
root->str("title", pack.title);
root->str("version", pack.version);
root->str("creator", pack.creator);
root->str("description", pack.description);
pack.folder = folder;
auto dependencies = root->list("dependencies");
if (dependencies) {
for (size_t i = 0; i < dependencies->size(); i++) {
pack.dependencies.push_back(dependencies->str(i));
}
}
if (pack.id == "none")
throw contentpack_error(pack.id, folder,
"content-pack id is not specified");
@ -92,6 +101,12 @@ void ContentPack::scan(fs::path rootfolder,
}
}
void ContentPack::scan(EnginePaths* paths,
std::vector<ContentPack>& packs) {
scan(paths->getResources()/fs::path("content"), packs);
scan(paths->getWorldFolder()/fs::path("content"), packs);
}
std::vector<std::string> ContentPack::worldPacksList(fs::path folder) {
fs::path listfile = folder / fs::path("packs.list");
if (!fs::is_regular_file(listfile)) {
@ -111,8 +126,7 @@ fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std:
if (fs::is_directory(folder)) {
return folder;
}
throw contentpack_error(name, folder,
"could not to find pack '"+name+"'");
return folder;
}
void ContentPack::readPacks(const EnginePaths* paths,
@ -121,6 +135,10 @@ void ContentPack::readPacks(const EnginePaths* paths,
fs::path worldDir) {
for (const auto& name : packnames) {
fs::path packfolder = ContentPack::findPack(paths, worldDir, name);
if (!fs::is_directory(packfolder)) {
throw contentpack_error(name, packfolder,
"could not to find pack '"+name+"'");
}
packs.push_back(ContentPack::read(packfolder));
}
}

View File

@ -24,7 +24,10 @@ struct ContentPack {
std::string id = "none";
std::string title = "untitled";
std::string version = "0.0";
std::string creator = "";
std::string description = "no description";
std::filesystem::path folder;
std::vector<std::string> dependencies;
std::filesystem::path getContentFile() const;
@ -36,8 +39,12 @@ struct ContentPack {
static bool is_pack(std::filesystem::path folder);
static ContentPack read(std::filesystem::path folder);
static void scan(std::filesystem::path folder,
std::vector<ContentPack>& packs);
static void scan(EnginePaths* paths,
std::vector<ContentPack>& packs);
static std::vector<std::string> worldPacksList(std::filesystem::path folder);
static std::filesystem::path findPack(

View File

@ -176,7 +176,7 @@ void Engine::loadWorldContent(const fs::path& folder) {
void Engine::loadAllPacks() {
auto resdir = paths->getResources();
contentPacks.clear();
ContentPack::scan(resdir/fs::path("content"), contentPacks);
ContentPack::scan(paths, contentPacks);
}
void Engine::setScreen(std::shared_ptr<Screen> screen) {
@ -212,3 +212,7 @@ std::vector<ContentPack>& Engine::getContentPacks() {
EnginePaths* Engine::getPaths() {
return paths;
}
std::shared_ptr<Screen> Engine::getScreen() {
return screen;
}

View File

@ -63,6 +63,8 @@ public:
void loadContent();
void loadWorldContent(const fs::path& folder);
void loadAllPacks();
std::shared_ptr<Screen> getScreen();
};
#endif // SRC_ENGINE_H_

View File

@ -453,13 +453,18 @@ void WorldFiles::write(const World* world, const Content* content) {
}
void WorldFiles::writePacks(const World* world) {
auto packsFile = getPacksFile();
if (fs::is_regular_file(packsFile)) {
return;
}
const auto& packs = world->getPacks();
std::stringstream ss;
ss << "# autogenerated; do not modify\n";
for (const auto& pack : packs) {
ss << pack.id << "\n";
}
files::write_string(getPacksFile(), ss.str());
files::write_string(packsFile, ss.str());
}
void WorldFiles::writeIndices(const ContentIndices* indices) {
@ -574,3 +579,15 @@ bool WorldFiles::readPlayer(Player* player) {
}
return true;
}
void WorldFiles::addPack(const std::string& id) {
auto packs = files::read_list(getPacksFile());
packs.push_back(id);
std::stringstream ss;
ss << "# autogenerated; do not modify\n";
for (const auto& pack : packs) {
ss << pack << "\n";
}
files::write_string(getPacksFile(), ss.str());
}

View File

@ -146,6 +146,8 @@ public:
void write(const World* world, const Content* content);
void writePacks(const World* world);
void writeIndices(const ContentIndices* indices);
/* Append pack to packs.list without duplicate check */
void addPack(const std::string& id);
static const char* WORLD_FILE;
};

View File

@ -43,6 +43,10 @@ fs::path EnginePaths::getWorldsFolder() {
return userfiles/fs::path("worlds");
}
fs::path EnginePaths::getWorldFolder() {
return worldFolder;
}
std::vector<fs::path> EnginePaths::scanForWorlds() {
std::vector<fs::path> folders;

View File

@ -12,7 +12,7 @@ namespace fs = std::filesystem;
class EnginePaths {
fs::path userfiles {"."};
fs::path resources {"res"};
fs::path worldFolder {""};
fs::path worldFolder;
std::vector<ContentPack>* contentPacks = nullptr;
public:
fs::path getUserfiles() const;
@ -20,6 +20,7 @@ public:
fs::path getScreenshotFile(std::string ext);
fs::path getWorldsFolder();
fs::path getWorldFolder();
bool isWorldNameUsed(std::string name);
void setUserfiles(fs::path folder);

View File

@ -37,8 +37,6 @@
using glm::vec3;
using glm::vec4;
using glm::mat4;
using std::string;
using std::shared_ptr;
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend)
: engine(engine),
@ -76,7 +74,7 @@ bool WorldRenderer::drawChunk(size_t index,
if (!chunk->isLighted()) {
return false;
}
shared_ptr<Mesh> mesh = renderer->getOrRender(chunk.get());
auto mesh = renderer->getOrRender(chunk.get());
if (mesh == nullptr) {
return false;
}
@ -102,16 +100,15 @@ void WorldRenderer::drawChunks(Chunks* chunks,
Shader* shader) {
std::vector<size_t> indices;
for (size_t i = 0; i < chunks->volume; i++){
shared_ptr<Chunk> chunk = chunks->chunks[i];
if (chunk == nullptr)
if (chunks->chunks[i] == nullptr)
continue;
indices.push_back(i);
}
float px = camera->position.x / (float)CHUNK_W;
float pz = camera->position.z / (float)CHUNK_D;
std::sort(indices.begin(), indices.end(), [this, chunks, px, pz](size_t i, size_t j) {
shared_ptr<Chunk> a = chunks->chunks[i];
shared_ptr<Chunk> b = chunks->chunks[j];
auto a = chunks->chunks[i];
auto b = chunks->chunks[j];
return ((a->x + 0.5f - px)*(a->x + 0.5f - px) +
(a->z + 0.5f - pz)*(a->z + 0.5f - pz)
>

View File

@ -45,6 +45,7 @@ class Camera;
namespace gui {
typedef std::function<void()> runnable;
typedef std::function<void(const std::string&)> stringconsumer;
class UINode;
class Container;

View File

@ -81,7 +81,7 @@ namespace gui {
virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);};
glm::vec2 calcCoord() const;
virtual void setCoord(glm::vec2 coord);
glm::vec2 size() const;
virtual glm::vec2 size() const;
virtual void size(glm::vec2 size);
void _size(glm::vec2 size);
virtual void refresh() {};

View File

@ -22,6 +22,13 @@ const uint KEY_BACKSPACE = 259;
using namespace gui;
Label::Label(string text, string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
text_(util::str2wstr_utf8(text)),
fontName_(fontName) {
}
Label::Label(wstring text, string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
text_(text),
@ -64,6 +71,7 @@ void Label::size(vec2 sizenew) {
// ================================= Image ====================================
Image::Image(string texture, vec2 size) : UINode(vec2(), size), texture(texture) {
setInteractive(false);
}
void Image::draw(Batch2D* batch, Assets* assets) {

View File

@ -32,6 +32,7 @@ namespace gui {
std::string fontName_;
wstringsupplier supplier = nullptr;
public:
Label(std::string text, std::string fontName="normal");
Label(std::wstring text, std::string fontName="normal");
virtual Label& text(std::wstring text);
@ -40,6 +41,9 @@ namespace gui {
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual Label* textSupplier(wstringsupplier supplier);
virtual glm::vec2 size() const override {
return UINode::size();
}
virtual void size(glm::vec2 size) override;
};

View File

@ -4,6 +4,7 @@
#include <memory>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <filesystem>
#include <glm/glm.hpp>
@ -11,6 +12,8 @@
#include "gui/panels.h"
#include "gui/controls.h"
#include "screens.h"
#include "../coders/png.h"
#include "../util/stringutil.h"
#include "../files/engine_paths.h"
#include "../files/WorldConverter.h"
@ -98,6 +101,25 @@ void show_content_missing(Engine* engine, const Content* content,
subpanel->add(hpanel);
}
}
for (size_t i = 0; i < lut->countItems(); i++) {
// missing block
if (lut->getItemId(i) == ITEM_VOID) {
auto name = lut->getItemName(i);
Panel* hpanel = new Panel(vec2(500, 30));
hpanel->color(vec4(0.0f));
hpanel->orientation(Orientation::horizontal);
Label* namelabel = new Label(util::str2wstr_utf8(name));
namelabel->color(vec4(1.0f, 0.2f, 0.2f, 0.5f));
Label* typelabel = new Label(L"[item]");
typelabel->color(vec4(0.5f));
hpanel->add(typelabel);
hpanel->add(namelabel);
subpanel->add(hpanel);
}
}
subpanel->maxLength(400);
panel->add(subpanel);
@ -248,10 +270,109 @@ void create_main_menu_panel(Engine* engine, PagesControl* menu) {
}));
}
typedef std::function<void(const ContentPack& pack)> packconsumer;
std::shared_ptr<Panel> create_packs_panel(const std::vector<ContentPack>& packs, Engine* engine, bool backbutton, packconsumer callback) {
auto assets = engine->getAssets();
auto panel = std::make_shared<Panel>(vec2(400, 200), vec4(5.0f));
panel->color(vec4(1.0f, 1.0f, 1.0f, 0.07f));
panel->maxLength(400);
panel->scrollable(true);
for (auto& pack : packs) {
auto packpanel = std::make_shared<RichButton>(vec2(390, 80));
if (callback) {
packpanel->listenAction([=](GUI*) {
callback(pack);
});
}
auto idlabel = std::make_shared<Label>("["+pack.id+"]");
idlabel->color(vec4(1, 1, 1, 0.5f));
packpanel->add(idlabel, vec2(360-idlabel->size().x, 2));
auto titlelabel = std::make_shared<Label>(pack.title);
packpanel->add(titlelabel, vec2(78, 6));
std::string icon = pack.id+".icon";
if (assets->getTexture(icon) == nullptr) {
auto iconfile = pack.folder/fs::path("icon.png");
if (fs::is_regular_file(iconfile)) {
assets->store(png::load_texture(iconfile), icon);
} else {
icon = "gui/no_icon";
}
}
if (!pack.creator.empty()) {
auto creatorlabel = std::make_shared<Label>("@"+pack.creator);
creatorlabel->color(vec4(0.8f, 1.0f, 0.9f, 0.7f));
packpanel->add(creatorlabel, vec2(360-creatorlabel->size().x, 60));
}
auto descriptionlabel = std::make_shared<Label>(pack.description);
descriptionlabel->color(vec4(1, 1, 1, 0.7f));
packpanel->add(descriptionlabel, vec2(80, 28));
packpanel->add(std::make_shared<Image>(icon, glm::vec2(64)), vec2(8));
packpanel->color(vec4(0.06f, 0.12f, 0.18f, 0.7f));
panel->add(packpanel);
}
if (backbutton)
panel->add(guiutil::backButton(engine->getGUI()->getMenu()));
return panel;
}
// TODO: refactor
void create_content_panel(Engine* engine, PagesControl* menu) {
auto panel = create_page(engine, "content", 400, 0.0f, 5);
panel->add(new Label(L"work in progress"));
panel->add(guiutil::backButton(menu));
auto paths = engine->getPaths();
auto mainPanel = create_page(engine, "content", 400, 0.0f, 5);
std::vector<ContentPack> scanned;
ContentPack::scan(engine->getPaths(), scanned);
for (const auto& pack : engine->getContentPacks()) {
for (size_t i = 0; i < scanned.size(); i++) {
if (scanned[i].id == pack.id) {
scanned.erase(scanned.begin()+i);
i--;
}
}
}
auto panel = create_packs_panel(engine->getContentPacks(), engine, false, nullptr);
mainPanel->add(panel);
mainPanel->add(create_button(
langs::get(L"Add", L"content"), vec4(10.0f), vec4(1), [=](GUI* gui) {
auto panel = create_packs_panel(scanned, engine, true,
[=](const ContentPack& pack) {
auto screen = dynamic_cast<LevelScreen*>(engine->getScreen().get());
auto level = screen->getLevel();
auto world = level->getWorld();
auto worldFolder = paths->getWorldFolder();
for (const auto& dependency : pack.dependencies) {
fs::path folder = ContentPack::findPack(paths, worldFolder, dependency);
if (!fs::is_directory(folder)) {
guiutil::alert(gui, langs::get(L"error.dependency-not-found")+
L": "+util::str2wstr_utf8(dependency));
return;
}
if (!world->hasPack(dependency)) {
world->wfile->addPack(dependency);
}
}
world->wfile->addPack(pack.id);
std::string wname = world->getName();
engine->setScreen(nullptr);
engine->setScreen(std::make_shared<MenuScreen>(engine));
open_world(wname, engine);
});
menu->add("content-packs", panel);
menu->set("content-packs");
}));
mainPanel->add(guiutil::backButton(menu));
}
inline uint64_t str2seed(std::wstring seedstr) {
@ -476,7 +597,10 @@ void create_pause_panel(Engine* engine, PagesControl* menu) {
panel->add(create_button(L"Continue", vec4(10.0f), vec4(1), [=](GUI*){
menu->reset();
}));
panel->add(guiutil::gotoButton(L"Content", "content", menu));
panel->add(create_button(L"Content", vec4(10.0f), vec4(1), [=](GUI*) {
create_content_panel(engine, menu);
menu->set("content");
}));
panel->add(guiutil::gotoButton(L"Settings", "settings", menu));
panel->add(create_button(L"Save and Quit to Menu", vec4(10.f), vec4(1), [=](GUI*){
@ -493,7 +617,6 @@ void menus::create_menus(Engine* engine, PagesControl* menu) {
create_controls_panel(engine, menu);
create_pause_panel(engine, menu);
create_languages_panel(engine, menu);
create_content_panel(engine, menu);
create_main_menu_panel(engine, menu);
}

View File

@ -104,6 +104,7 @@ LevelScreen::~LevelScreen() {
auto world = level->getWorld();
world->write(level.get());
controller->onWorldQuit();
engine->getPaths()->setWorldFolder(fs::path());
}
void LevelScreen::updateHotkeys() {
@ -163,3 +164,7 @@ void LevelScreen::draw(float delta) {
}
}
}
Level* LevelScreen::getLevel() const {
return level.get();
}

View File

@ -53,6 +53,8 @@ public:
void update(float delta) override;
void draw(float delta) override;
Level* getLevel() const;
};
#endif // FRONTEND_SCREENS_H_

View File

@ -21,12 +21,13 @@ const float JUMP_FORCE = 8.0f;
Player::Player(glm::vec3 position, float speed) :
speed(speed),
chosenSlot(0),
inventory(new Inventory(40)),
camera(new Camera(position, glm::radians(90.0f))),
spCamera(new Camera(position, glm::radians(90.0f))),
tpCamera(new Camera(position, glm::radians(90.0f))),
currentCamera(camera),
hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f))),
inventory(new Inventory(40)) {
hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f)))
{
}
Player::~Player() {

View File

@ -35,11 +35,11 @@ class Player {
float speed;
int chosenSlot;
glm::vec3 spawnpoint {};
std::shared_ptr<Inventory> inventory;
public:
std::shared_ptr<Camera> camera, spCamera, tpCamera;
std::shared_ptr<Camera> currentCamera;
std::unique_ptr<Hitbox> hitbox;
std::shared_ptr<Inventory> inventory;
bool flight = false;
bool noclip = false;
bool debug = false;

View File

@ -110,6 +110,14 @@ void World::setName(const std::string& name) {
this->name = name;
}
bool World::hasPack(const std::string& id) const {
for (auto& pack : packs) {
if (pack.id == id)
return true;
}
return false;
}
void World::setSeed(uint64_t seed) {
this->seed = seed;
}

View File

@ -70,6 +70,7 @@ public:
void setName(const std::string& name);
void setSeed(uint64_t seed);
bool hasPack(const std::string& id) const;
std::string getName() const;
uint64_t getSeed() const;
const std::vector<ContentPack>& getPacks() const;