Refactor, GUI::storage (map for direct access to panels)

This commit is contained in:
MihailRis 2023-11-17 14:26:54 +03:00
parent 4e1f19c911
commit 6392f63604
10 changed files with 75 additions and 54 deletions

View File

@ -43,14 +43,6 @@ void int2Bytes(int value, ubyte* dest, size_t offset){
dest[offset+3] = (char) (value >> 0 & 255);
}
float bytes2Float(ubyte* src, uint offset){
uint32_t value = ((src[offset] << 24) |
(src[offset+1] << 16) |
(src[offset+2] << 8) |
(src[offset+3]));
return *(float*)(&value);
}
WorldFiles::WorldFiles(path directory, bool generatorTestMode)
: directory(directory), generatorTestMode(generatorTestMode) {
compressionBuffer = new ubyte[CHUNK_DATA_LEN * 2];

View File

@ -12,6 +12,7 @@
#include "../../window/input.h"
#include "../../window/Camera.h"
using std::string;
using std::shared_ptr;
using namespace gui;
@ -101,6 +102,22 @@ void GUI::add(shared_ptr<UINode> panel) {
container->add(panel);
}
void GUI::remove(std::shared_ptr<UINode> panel) {
void GUI::remove(shared_ptr<UINode> panel) {
container->remove(panel);
}
void GUI::store(string name, shared_ptr<UINode> node) {
storage[name] = node;
}
shared_ptr<UINode> GUI::get(string name) {
auto found = storage.find(name);
if (found == storage.end()) {
return nullptr;
}
return found->second;
}
void GUI::remove(string name) {
storage.erase(name);
}

View File

@ -5,6 +5,7 @@
#include <vector>
#include <glm/glm.hpp>
#include <functional>
#include <unordered_map>
class Batch2D;
class Assets;
@ -50,6 +51,8 @@ namespace gui {
std::shared_ptr<UINode> hover = nullptr;
std::shared_ptr<UINode> pressed = nullptr;
std::shared_ptr<UINode> focus = nullptr;
std::unordered_map<std::string, std::shared_ptr<UINode>> storage;
Camera* uicamera;
public:
GUI();
@ -62,6 +65,9 @@ namespace gui {
void draw(Batch2D* batch, Assets* assets);
void add(std::shared_ptr<UINode> panel);
void remove(std::shared_ptr<UINode> panel);
void store(std::string name, std::shared_ptr<UINode> node);
std::shared_ptr<UINode> get(std::string name);
void remove(std::string name);
};
}

View File

@ -80,8 +80,9 @@ void Button::mouseRelease(GUI* gui, int x, int y) {
}
}
void Button::listenAction(onaction action) {
Button* Button::listenAction(onaction action) {
actions.push_back(action);
return this;
}
TextBox::TextBox(wstring placeholder, vec4 padding)

View File

@ -45,7 +45,7 @@ namespace gui {
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void mouseRelease(GUI*, int x, int y) override;
virtual void listenAction(onaction action);
virtual Button* listenAction(onaction action);
};
class TextBox : public Panel {

View File

@ -75,6 +75,10 @@ void Container::add(shared_ptr<UINode> node) {
refresh();
}
void Container::add(UINode* node) {
add(shared_ptr<UINode>(node));
}
void Container::remove(shared_ptr<UINode> selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),

View File

@ -33,6 +33,7 @@ namespace gui {
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void add(std::shared_ptr<UINode> node);
virtual void add(UINode* node);
virtual void remove(std::shared_ptr<UINode> node);
void listenInterval(float interval, ontimeout callback, int repeat=-1);
};

View File

@ -67,12 +67,16 @@ HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(en
return L"occlusion: "+wstring(this->occlusion ? L"on" : L"off");
})));
panel->add(shared_ptr<Label>(create_label([this, level]() {
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount)+L" visible: "+std::to_wstring(level->chunks->visible);
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount)+
L" visible: "+std::to_wstring(level->chunks->visible);
})));
panel->add(shared_ptr<Label>(create_label([this](){
std::wstringstream stream;
stream << std::hex << this->level->player->selectedVoxel.states;
return L"block-selected: "+std::to_wstring(this->level->player->selectedVoxel.id)+L" "+stream.str();
auto player = this->level->player;
return L"block-selected: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str();
})));
panel->add(shared_ptr<Label>(create_label([this](){
return L"seed: "+std::to_wstring(this->level->world->seed);
@ -89,7 +93,7 @@ HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(en
sub->add(shared_ptr<UINode>(label));
sub->color(vec4(0.0f));
// Coordinate input
// Coord input
TextBox* box = new TextBox(L"");
box->textSupplier([this, ax]() {
Hitbox* hitbox = this->level->player->hitbox;
@ -113,14 +117,14 @@ HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(en
Panel* pauseMenu = new Panel(vec2(350, 200));
pauseMenu->color(vec4(0.0f));
{
Button* button = new Button(L"Continue", vec4(12.0f, 10.0f, 12.0f, 12.0f));
Button* button = new Button(L"Continue", vec4(10.0f));
button->listenAction([this](GUI*){
this->pause = false;
});
pauseMenu->add(shared_ptr<UINode>(button));
}
{
Button* button = new Button(L"Save and Quit to Menu", vec4(12.0f, 10.0f, 12.0f, 12.0f));
Button* button = new Button(L"Save and Quit to Menu", vec4(10.f));
button->listenAction([this, engine](GUI*){
this->pauseMenu->visible(false);
engine->setScreen(shared_ptr<Screen>(new MenuScreen(engine)));

View File

@ -37,21 +37,16 @@ using std::filesystem::u8path;
using std::filesystem::directory_iterator;
using namespace gui;
// Replace this ugly piece of code
// With some normal pages system
Panel* create_main_menu_panel(Engine* engine, shared_ptr<UINode>* newWorldPanel) {
Panel* create_main_menu_panel(Engine* engine) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
panel->setCoord(vec2(10, 10));
{
auto button = new Button(L"New World", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([engine, panel, newWorldPanel](GUI*) {
panel->visible(false);
(*newWorldPanel)->visible(true);
});
panel->add(shared_ptr<UINode>(button));
}
panel->add((new Button(L"New World", vec4(10.f)))->listenAction([=](GUI* gui) {
panel->visible(false);
gui->get("new-world")->visible(true);
}));
Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f));
worldsPanel->color(vec4(0.1f));
for (auto const& entry : directory_iterator(enginefs::get_worlds_folder())) {
@ -67,21 +62,17 @@ Panel* create_main_menu_panel(Engine* engine, shared_ptr<UINode>* newWorldPanel)
auto screen = new LevelScreen(engine, world->load(settings));
engine->setScreen(shared_ptr<Screen>(screen));
});
worldsPanel->add(shared_ptr<UINode>(button));
worldsPanel->add(button);
}
panel->add(shared_ptr<UINode>(worldsPanel));
panel->add(worldsPanel);
{
Button* button = new Button(L"Quit", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([](GUI*) {
Window::setShouldClose(true);
});
panel->add(shared_ptr<UINode>(button));
}
panel->add((new Button(L"Quit", vec4(10.f)))->listenAction([](GUI*) {
Window::setShouldClose(true);
}));
return panel;
}
Panel* create_new_world_panel(Engine* engine, shared_ptr<UINode>* mainPanel) {
Panel* create_new_world_panel(Engine* engine) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
panel->setCoord(vec2(10, 10));
@ -89,10 +80,10 @@ Panel* create_new_world_panel(Engine* engine, shared_ptr<UINode>* mainPanel) {
TextBox* worldNameInput;
{
Label* label = new Label(L"World Name");
panel->add(shared_ptr<UINode>(label));
panel->add(label);
TextBox* input = new TextBox(L"New World", vec4(6.0f));
panel->add(shared_ptr<UINode>(input));
panel->add(input);
worldNameInput = input;
}
@ -103,12 +94,12 @@ Panel* create_new_world_panel(Engine* engine, shared_ptr<UINode>* mainPanel) {
uint64_t randseed = rand() ^ (rand() << 8) ^
(rand() << 16) ^ (rand() << 24) ^
((uint64_t)rand() << 32) ^ ((uint64_t)rand() << 40) ^
((uint64_t)rand() << 32) ^
((uint64_t)rand() << 40) ^
((uint64_t)rand() << 56);
TextBox* input = new TextBox(std::to_wstring(randseed), vec4(6.0f));
panel->add(shared_ptr<UINode>(input));
seedInput = input;
seedInput = new TextBox(std::to_wstring(randseed), vec4(6.0f));
panel->add(seedInput);
}
{
@ -158,26 +149,25 @@ Panel* create_new_world_panel(Engine* engine, shared_ptr<UINode>* mainPanel) {
auto screen = new LevelScreen(engine, world->load(settings));
engine->setScreen(shared_ptr<Screen>(screen));
});
panel->add(shared_ptr<UINode>(button));
panel->add(button);
}
{
Button* button = new Button(L"Back", vec4(10.0f));
button->listenAction([panel, mainPanel](GUI*) {
panel->visible(false);
(*mainPanel)->visible(true);
});
panel->add(shared_ptr<UINode>(button));
}
panel->add((new Button(L"Back", vec4(10.f)))->listenAction([=](GUI* gui) {
panel->visible(false);
gui->get("main-menu")->visible(true);
}));
return panel;
}
MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
GUI* gui = engine->getGUI();
panel = shared_ptr<UINode>(create_main_menu_panel(engine, &newWorldPanel));
newWorldPanel = shared_ptr<UINode>(create_new_world_panel(engine, &panel));
panel = shared_ptr<UINode>(create_main_menu_panel(engine));
newWorldPanel = shared_ptr<UINode>(create_new_world_panel(engine));
newWorldPanel->visible(false);
gui->store("main-menu", panel);
gui->store("new-world", newWorldPanel);
gui->add(panel);
gui->add(newWorldPanel);
@ -189,6 +179,10 @@ MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
MenuScreen::~MenuScreen() {
GUI* gui = engine->getGUI();
gui->remove("main-menu");
gui->remove("new-world");
gui->remove(newWorldPanel);
gui->remove(panel);
delete batch;

View File

@ -325,6 +325,8 @@ void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
blockXSprite(x, y, z, vec3(1, 1, 1), texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f);
break;
}
default:
break;
}
if (overflow)
return;