refactor: add GUI instance reference to UI nodes

This commit is contained in:
MihailRis
2025-04-02 14:55:53 +03:00
parent 74a94f869c
commit 4c48afbb90
70 changed files with 1133 additions and 832 deletions
+31 -28
View File
@@ -35,8 +35,8 @@
using namespace gui;
static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
auto label = std::make_shared<Label>(L"-");
static std::shared_ptr<Label> create_label(GUI& gui, wstringsupplier supplier) {
auto label = std::make_shared<Label>(gui, L"-");
label->textSupplier(std::move(supplier));
return label;
}
@@ -50,7 +50,10 @@ std::shared_ptr<UINode> create_debug_panel(
Player& player,
bool allowDebugCheats
) {
auto panel = std::make_shared<Panel>(glm::vec2(300, 200), glm::vec4(5.0f), 2.0f);
auto& gui = engine.getGUI();
auto panel = std::make_shared<Panel>(
gui, glm::vec2(300, 200), glm::vec4(5.0f), 2.0f
);
panel->setId("hud.debug-panel");
panel->setPos(glm::vec2(10, 10));
@@ -87,48 +90,48 @@ std::shared_ptr<UINode> create_debug_panel(
lastTotalUpload = totalUpload;
});
panel->add(create_label([]() { return L"fps: "+fpsString;}));
panel->add(create_label(gui, []() { return L"fps: "+fpsString;}));
panel->add(create_label([]() {
panel->add(create_label(gui, []() {
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
}));
panel->add(create_label([]() {
panel->add(create_label(gui, []() {
int drawCalls = Mesh::drawCalls;
Mesh::drawCalls = 0;
return L"draw-calls: " + std::to_wstring(drawCalls);
}));
panel->add(create_label([]() {
panel->add(create_label(gui, []() {
return L"speakers: " + std::to_wstring(audio::count_speakers())+
L" streams: " + std::to_wstring(audio::count_streams());
}));
panel->add(create_label([]() {
panel->add(create_label(gui, []() {
return L"lua-stack: " + std::to_wstring(scripting::get_values_on_stack());
}));
panel->add(create_label([]() { return netSpeedString; }));
panel->add(create_label([&engine]() {
panel->add(create_label(gui, []() { return netSpeedString; }));
panel->add(create_label(gui, [&engine]() {
auto& settings = engine.getSettings();
bool culling = settings.graphics.frustumCulling.get();
return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off");
}));
panel->add(create_label([=]() {
panel->add(create_label(gui, [=]() {
return L"particles: " +
std::to_wstring(ParticlesRenderer::visibleParticles) +
L" emitters: " +
std::to_wstring(ParticlesRenderer::aliveEmitters);
}));
panel->add(create_label([&]() {
panel->add(create_label(gui, [&]() {
return L"chunks: "+std::to_wstring(level.chunks->size())+
L" visible: "+std::to_wstring(ChunksRenderer::visibleChunks);
}));
panel->add(create_label([&]() {
panel->add(create_label(gui, [&]() {
return L"entities: "+std::to_wstring(level.entities->size())+L" next: "+
std::to_wstring(level.entities->peekNextID());
}));
panel->add(create_label([&]() {
panel->add(create_label(gui, [&]() {
return L"players: "+std::to_wstring(level.players->size())+L" local: "+
std::to_wstring(player.getId());
}));
panel->add(create_label([&]() -> std::wstring {
panel->add(create_label(gui, [&]() -> std::wstring {
const auto& vox = player.selection.vox;
std::wstringstream stream;
stream << "r:" << vox.state.rotation << " s:"
@@ -141,7 +144,7 @@ std::shared_ptr<UINode> create_debug_panel(
L" "+stream.str();
}
}));
panel->add(create_label([&]() -> std::wstring {
panel->add(create_label(gui, [&]() -> std::wstring {
const auto& selection = player.selection;
const auto& vox = selection.vox;
if (vox.id == BLOCK_VOID) {
@@ -151,7 +154,7 @@ std::shared_ptr<UINode> create_debug_panel(
L" y: " + std::to_wstring(selection.actualPosition.y) +
L" z: " + std::to_wstring(selection.actualPosition.z);
}));
panel->add(create_label([&]() {
panel->add(create_label(gui, [&]() {
auto eid = player.getSelectedEntity();
if (eid == ENTITY_NONE) {
return std::wstring {L"entity: -"};
@@ -162,7 +165,7 @@ std::shared_ptr<UINode> create_debug_panel(
return std::wstring {L"entity: error (invalid UID)"};
}
}));
panel->add(create_label([&](){
panel->add(create_label(gui, [&](){
auto indices = level.content.getIndices();
if (auto def = indices->blocks.get(player.selection.vox.id)) {
return L"name: " + util::str2wstr_utf8(def->name);
@@ -170,23 +173,23 @@ std::shared_ptr<UINode> create_debug_panel(
return std::wstring {L"name: void"};
}
}));
panel->add(create_label([&](){
panel->add(create_label(gui, [&](){
return L"seed: "+std::to_wstring(level.getWorld()->getSeed());
}));
for (int ax = 0; ax < 3; ax++) {
auto sub = std::make_shared<Container>(glm::vec2(250, 27));
auto sub = std::make_shared<Container>(gui, glm::vec2(250, 27));
std::wstring str = L"x: ";
str[0] += ax;
auto label = std::make_shared<Label>(str);
auto label = std::make_shared<Label>(gui, str);
label->setMargin(glm::vec4(2, 3, 2, 3));
label->setSize(glm::vec2(20, 27));
sub->add(label);
sub->setColor(glm::vec4(0.0f));
// Coord input
auto box = std::make_shared<TextBox>(L"");
auto box = std::make_shared<TextBox>(gui, L"");
auto boxRef = box.get();
box->setTextSupplier([&player, ax]() {
return util::to_wstring(player.getPosition()[ax], 2);
@@ -212,7 +215,7 @@ std::shared_ptr<UINode> create_debug_panel(
panel->add(sub);
}
auto& worldInfo = level.getWorld()->getInfo();
panel->add(create_label([&](){
panel->add(create_label(gui, [&](){
int hour, minute, second;
timeutil::from_value(worldInfo.daytime, hour, minute, second);
@@ -222,20 +225,20 @@ std::shared_ptr<UINode> create_debug_panel(
return L"time: "+timeString;
}));
if (allowDebugCheats) {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 1.0f, 0.005f, 8);
auto bar = std::make_shared<TrackBar>(gui, 0.0f, 1.0f, 1.0f, 0.005f, 8);
bar->setSupplier([&]() {return worldInfo.daytime;});
bar->setConsumer([&](double val) {worldInfo.daytime = val;});
panel->add(bar);
}
if (allowDebugCheats) {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8);
auto bar = std::make_shared<TrackBar>(gui, 0.0f, 1.0f, 0.0f, 0.005f, 8);
bar->setSupplier([&]() {return worldInfo.fog;});
bar->setConsumer([&](double val) {worldInfo.fog = val;});
panel->add(bar);
}
{
auto checkbox = std::make_shared<FullCheckBox>(
L"Show Chunk Borders", glm::vec2(400, 24)
gui, L"Show Chunk Borders", glm::vec2(400, 24)
);
checkbox->setSupplier([=]() {
return WorldRenderer::showChunkBorders;
@@ -247,7 +250,7 @@ std::shared_ptr<UINode> create_debug_panel(
}
{
auto checkbox = std::make_shared<FullCheckBox>(
L"Show Hitboxes", glm::vec2(400, 24)
gui, L"Show Hitboxes", glm::vec2(400, 24)
);
checkbox->setSupplier([=]() {
return WorldRenderer::showEntitiesDebug;
@@ -259,7 +262,7 @@ std::shared_ptr<UINode> create_debug_panel(
}
{
auto checkbox = std::make_shared<FullCheckBox>(
L"Show Generator Minimap", glm::vec2(400, 24)
gui, L"Show Generator Minimap", glm::vec2(400, 24)
);
checkbox->setSupplier([=]() {
return Hud::showGeneratorMinimap;