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
+4 -7
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)
>
+1
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;
+1 -1
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() {};
+8
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) {
+4
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;
};
+128 -5
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);
}
+5
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();
}
+2
View File
@@ -53,6 +53,8 @@ public:
void update(float delta) override;
void draw(float delta) override;
Level* getLevel() const;
};
#endif // FRONTEND_SCREENS_H_