From 5af6b91b22a33e5e1abcf2f6375ccab0857b0091 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 10 Mar 2025 18:06:39 +0300 Subject: [PATCH] add debug GUI render mode (F8) --- src/constants.hpp | 2 + src/engine/Engine.cpp | 3 ++ src/graphics/core/Batch2D.cpp | 7 +++- src/graphics/core/Batch2D.hpp | 6 ++- src/graphics/render/TextsRenderer.cpp | 3 +- src/graphics/ui/GUI.cpp | 44 ++++++++++++++++++++-- src/graphics/ui/GUI.hpp | 10 ++++- src/graphics/ui/elements/Container.cpp | 6 +-- src/graphics/ui/elements/Container.hpp | 2 +- src/graphics/ui/elements/InventoryView.cpp | 2 +- src/graphics/ui/elements/Label.hpp | 5 ++- src/graphics/ui/elements/Menu.cpp | 4 +- src/graphics/ui/elements/Panel.cpp | 2 +- src/graphics/ui/elements/Panel.hpp | 6 +-- src/graphics/ui/elements/Plotter.cpp | 3 +- src/graphics/ui/elements/UINode.cpp | 2 +- src/logic/scripting/lua/libs/libgui.cpp | 2 +- src/window/Camera.cpp | 13 ++++--- 18 files changed, 93 insertions(+), 29 deletions(-) diff --git a/src/constants.hpp b/src/constants.hpp index 9337513f..1accd619 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -68,3 +68,5 @@ inline const std::string LAYOUTS_FOLDER = "layouts"; inline const std::string SOUNDS_FOLDER = "sounds"; inline const std::string MODELS_FOLDER = "models"; inline const std::string SKELETONS_FOLDER = "skeletons"; + +inline const std::string FONT_DEFAULT = "normal"; diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 80d774be..c9184383 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -163,6 +163,9 @@ void Engine::updateHotkeys() { if (Events::jpressed(keycode::F2)) { saveScreenshot(); } + if (Events::jpressed(keycode::F8)) { + gui->toggleDebug(); + } if (Events::jpressed(keycode::F11)) { settings.display.fullscreen.toggle(); } diff --git a/src/graphics/core/Batch2D.cpp b/src/graphics/core/Batch2D.cpp index b7ccf5ff..73ac9be2 100644 --- a/src/graphics/core/Batch2D.cpp +++ b/src/graphics/core/Batch2D.cpp @@ -142,7 +142,7 @@ void Batch2D::rect( bool flippedY, glm::vec4 tint ) { - if (index + 6*B2D_VERTEX_SIZE >= capacity) { + if (index + 6 * B2D_VERTEX_SIZE >= capacity) { flush(); } setPrimitive(DrawPrimitive::triangle); @@ -230,6 +230,11 @@ void Batch2D::rect( } void Batch2D::lineRect(float x, float y, float w, float h) { + if (index + 8 * B2D_VERTEX_SIZE >= capacity) { + flush(); + } + setPrimitive(DrawPrimitive::line); + vertex(x, y, 0.0f, 0.0f, color.r, color.g, color.b, color.a); vertex(x, y+h, 0.0f, 1.0f, color.r, color.g, color.b, color.a); diff --git a/src/graphics/core/Batch2D.hpp b/src/graphics/core/Batch2D.hpp index 5610f603..8db99d99 100644 --- a/src/graphics/core/Batch2D.hpp +++ b/src/graphics/core/Batch2D.hpp @@ -48,10 +48,14 @@ public: void sprite(float x, float y, float w, float h, float skew, int atlasRes, int index, glm::vec4 tint); void point(float x, float y, float r, float g, float b, float a); - void setColor(glm::vec4 color) { + void setColor(const glm::vec4& color) { this->color = color; } + void setColor(int r, int g, int b, int a=255) { + this->color = glm::vec4(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); + } + void resetColor() { this->color = glm::vec4(1.0f); } diff --git a/src/graphics/render/TextsRenderer.cpp b/src/graphics/render/TextsRenderer.cpp index 32681ce5..283dacaa 100644 --- a/src/graphics/render/TextsRenderer.cpp +++ b/src/graphics/render/TextsRenderer.cpp @@ -10,6 +10,7 @@ #include "graphics/core/Batch3D.hpp" #include "graphics/core/Shader.hpp" #include "presets/NotePreset.hpp" +#include "constants.hpp" TextsRenderer::TextsRenderer( Batch3D& batch, const Assets& assets, const Frustum& frustum @@ -44,7 +45,7 @@ void TextsRenderer::renderNote( } opacity = preset.xrayOpacity; } - const auto& font = assets.require("normal"); + const auto& font = assets.require(FONT_DEFAULT); glm::vec3 xvec = note.getAxisX(); glm::vec3 yvec = note.getAxisY(); diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index 55dd8ceb..7821973b 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -11,14 +11,15 @@ #include "frontend/UiDocument.hpp" #include "frontend/locale.hpp" #include "graphics/core/Batch2D.hpp" +#include "graphics/core/LineBatch.hpp" #include "graphics/core/Shader.hpp" +#include "graphics/core/Font.hpp" #include "graphics/core/DrawContext.hpp" #include "window/Events.hpp" #include "window/Window.hpp" #include "window/input.hpp" #include "window/Camera.hpp" -#include #include #include @@ -238,6 +239,39 @@ void GUI::draw(const DrawContext& pctx, const Assets& assets) { if (hover) { Window::setCursor(hover->getCursor()); } + if (hover && debug) { + auto pos = hover->calcPos(); + const auto& id = hover->getId(); + if (!id.empty()) { + auto& font = assets.require(FONT_DEFAULT); + auto text = util::str2wstr_utf8(id); + int width = font.calcWidth(text); + int height = font.getLineHeight(); + + batch2D->untexture(); + batch2D->setColor(0, 0, 0); + batch2D->rect(pos.x, pos.y, width, height); + + batch2D->resetColor(); + font.draw(*batch2D, text, pos.x, pos.y, nullptr, 0); + } + + batch2D->untexture(); + auto node = hover->getParent(); + while (node) { + auto pos = node->calcPos(); + auto size = node->getSize(); + + batch2D->setColor(0, 0, 255); + batch2D->lineRect(pos.x, pos.y, size.x-1, size.y-1); + + node = node->getParent(); + } + // debug draw + auto size = hover->getSize(); + batch2D->setColor(0, 255, 0); + batch2D->lineRect(pos.x, pos.y, size.x-1, size.y-1); + } } std::shared_ptr GUI::getFocused() const { @@ -252,8 +286,8 @@ void GUI::add(std::shared_ptr node) { container->add(std::move(node)); } -void GUI::remove(std::shared_ptr node) noexcept { - container->remove(std::move(node)); +void GUI::remove(UINode* node) noexcept { + container->remove(node); } void GUI::store(const std::string& name, std::shared_ptr node) { @@ -297,3 +331,7 @@ void GUI::setDoubleClickDelay(float delay) { float GUI::getDoubleClickDelay() const { return doubleClickDelay; } + +void GUI::toggleDebug() { + debug = !debug; +} diff --git a/src/graphics/ui/GUI.hpp b/src/graphics/ui/GUI.hpp index cb6b037e..1099cc89 100644 --- a/src/graphics/ui/GUI.hpp +++ b/src/graphics/ui/GUI.hpp @@ -14,6 +14,7 @@ class DrawContext; class Assets; class Camera; class Batch2D; +class LineBatch; /* Some info about padding and margin. @@ -73,6 +74,7 @@ namespace gui { float doubleClickTimer = 0.0f; float doubleClickDelay = 0.5f; bool doubleClicked = false; + bool debug = false; void actMouse(float delta); void actFocused(); @@ -113,7 +115,11 @@ namespace gui { void add(std::shared_ptr node); /// @brief Remove node from the main container - void remove(std::shared_ptr node) noexcept; + void remove(UINode* node) noexcept; + + void remove(const std::shared_ptr& node) noexcept { + return remove(node.get()); + } /// @brief Store node in the GUI nodes dictionary /// (does not add node to the main container) @@ -144,5 +150,7 @@ namespace gui { void setDoubleClickDelay(float delay); float getDoubleClickDelay() const; + + void toggleDebug(); }; } diff --git a/src/graphics/ui/elements/Container.cpp b/src/graphics/ui/elements/Container.cpp index 2d4eaf53..ddf12de7 100644 --- a/src/graphics/ui/elements/Container.cpp +++ b/src/graphics/ui/elements/Container.cpp @@ -172,11 +172,11 @@ void Container::add(const std::shared_ptr& node, glm::vec2 pos) { add(node); } -void Container::remove(const std::shared_ptr& selected) { +void Container::remove(UINode* selected) { selected->setParent(nullptr); nodes.erase(std::remove_if(nodes.begin(), nodes.end(), [selected](const std::shared_ptr& node) { - return node == selected; + return node.get() == selected; } ), nodes.end()); refresh(); @@ -185,7 +185,7 @@ void Container::remove(const std::shared_ptr& selected) { void Container::remove(const std::string& id) { for (auto& node : nodes) { if (node->getId() == id) { - return remove(node); + return remove(node.get()); } } } diff --git a/src/graphics/ui/elements/Container.hpp b/src/graphics/ui/elements/Container.hpp index c51d528a..79d3fe62 100644 --- a/src/graphics/ui/elements/Container.hpp +++ b/src/graphics/ui/elements/Container.hpp @@ -32,7 +32,7 @@ namespace gui { virtual void add(const std::shared_ptr& node); virtual void add(const std::shared_ptr& node, glm::vec2 pos); virtual void clear(); - virtual void remove(const std::shared_ptr& node); + virtual void remove(UINode* node); virtual void remove(const std::string& id); virtual void scrolled(int value) override; virtual void setScrollable(bool flag); diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index a5a2c8a5..099b9c1b 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -207,7 +207,7 @@ void SlotView::draw(const DrawContext& pctx, const Assets& assets) { drawItemIcon(batch, stack, item, assets, tint, pos); if (stack.getCount() > 1 || stack.getFields() != nullptr) { - const auto& font = assets.require("normal"); + const auto& font = assets.require(FONT_DEFAULT); drawItemInfo(batch, stack, item, font, pos); } } diff --git a/src/graphics/ui/elements/Label.hpp b/src/graphics/ui/elements/Label.hpp index 63637d5f..1fbf5f5e 100644 --- a/src/graphics/ui/elements/Label.hpp +++ b/src/graphics/ui/elements/Label.hpp @@ -1,6 +1,7 @@ #pragma once #include "UINode.hpp" +#include "constants.hpp" class Font; struct FontStylesScheme; @@ -61,8 +62,8 @@ namespace gui { std::unique_ptr styles; public: - Label(const std::string& text, std::string fontName="normal"); - Label(const std::wstring& text, std::string fontName="normal"); + Label(const std::string& text, std::string fontName=FONT_DEFAULT); + Label(const std::wstring& text, std::string fontName=FONT_DEFAULT); virtual ~Label(); diff --git a/src/graphics/ui/elements/Menu.cpp b/src/graphics/ui/elements/Menu.cpp index d059d693..d18f725a 100644 --- a/src/graphics/ui/elements/Menu.cpp +++ b/src/graphics/ui/elements/Menu.cpp @@ -55,7 +55,7 @@ void Menu::setPage(const std::string &name, bool history) { void Menu::setPage(Page page, bool history) { if (current.panel) { - Container::remove(current.panel); + Container::remove(current.panel.get()); if (history && !current.temporal) { pageStack.push(current); } @@ -104,7 +104,7 @@ void Menu::clearHistory() { void Menu::reset() { clearHistory(); if (current.panel) { - Container::remove(current.panel); + Container::remove(current.panel.get()); current = Page {"", nullptr}; } } diff --git a/src/graphics/ui/elements/Panel.cpp b/src/graphics/ui/elements/Panel.cpp index 70446d7d..4f3d91f2 100644 --- a/src/graphics/ui/elements/Panel.cpp +++ b/src/graphics/ui/elements/Panel.cpp @@ -63,7 +63,7 @@ void Panel::add(const std::shared_ptr &node) { fullRefresh(); } -void Panel::remove(const std::shared_ptr &node) { +void Panel::remove(UINode* node) { Container::remove(node); fullRefresh(); } diff --git a/src/graphics/ui/elements/Panel.hpp b/src/graphics/ui/elements/Panel.hpp index 5a6399ce..b2e12c1f 100644 --- a/src/graphics/ui/elements/Panel.hpp +++ b/src/graphics/ui/elements/Panel.hpp @@ -7,14 +7,14 @@ namespace gui { class Panel : public Container { protected: Orientation orientation = Orientation::vertical; - glm::vec4 padding {2.0f}; + glm::vec4 padding; float interval = 2.0f; int minLength = 0; int maxLength = 0; public: Panel( glm::vec2 size, - glm::vec4 padding=glm::vec4(2.0f), + glm::vec4 padding=glm::vec4(0.0f), float interval=2.0f ); virtual ~Panel(); @@ -25,7 +25,7 @@ namespace gui { Orientation getOrientation() const; virtual void add(const std::shared_ptr& node) override; - virtual void remove(const std::shared_ptr& node) override; + virtual void remove(UINode* node) override; virtual void refresh() override; virtual void fullRefresh() override; diff --git a/src/graphics/ui/elements/Plotter.cpp b/src/graphics/ui/elements/Plotter.cpp index 6cf484ea..860f6b69 100644 --- a/src/graphics/ui/elements/Plotter.cpp +++ b/src/graphics/ui/elements/Plotter.cpp @@ -5,6 +5,7 @@ #include "graphics/core/DrawContext.hpp" #include "assets/Assets.hpp" #include "util/stringutil.hpp" +#include "constants.hpp" using namespace gui; @@ -37,7 +38,7 @@ void Plotter::draw(const DrawContext& pctx, const Assets& assets) { } int current_point = static_cast(points[index % dmwidth]); - auto font = assets.get("normal"); + auto font = assets.get(FONT_DEFAULT); for (int y = 0; y < dmheight; y += labelsInterval) { std::wstring string; if (current_point/16 == y/labelsInterval) { diff --git a/src/graphics/ui/elements/UINode.cpp b/src/graphics/ui/elements/UINode.cpp index f6444f41..7a9e4b87 100644 --- a/src/graphics/ui/elements/UINode.cpp +++ b/src/graphics/ui/elements/UINode.cpp @@ -266,7 +266,7 @@ void UINode::moveInto( ) { auto parent = node->getParent(); if (auto container = dynamic_cast(parent)) { - container->remove(node); + container->remove(node.get()); } if (parent) { parent->scrolled(0); diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index cbb01517..273ee393 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -96,7 +96,7 @@ static int l_node_destruct(lua::State* L) { engine->getGUI()->postRunnable([node]() { auto parent = node->getParent(); if (auto container = dynamic_cast(parent)) { - container->remove(node); + container->remove(node.get()); } }); return 0; diff --git a/src/window/Camera.cpp b/src/window/Camera.cpp index f3780d0f..315acf56 100644 --- a/src/window/Camera.cpp +++ b/src/window/Camera.cpp @@ -33,14 +33,15 @@ glm::mat4 Camera::getProjection() const { constexpr float epsilon = 1e-6f; // 0.000001 float aspect_ratio = this->aspect; if (std::fabs(aspect_ratio) < epsilon) { - aspect_ratio = (float)Window::width / (float)Window::height; + aspect_ratio = Window::width / static_cast(Window::height); } - if (perspective) + if (perspective) { return glm::perspective(fov * zoom, aspect_ratio, near, far); - else if (flipped) - return glm::ortho(0.0f, fov * aspect_ratio, fov, 0.0f); - else - return glm::ortho(0.0f, fov * aspect_ratio, 0.0f, fov); + } else if (flipped) { + return glm::ortho(-0.5f, fov * aspect_ratio-0.5f, fov, 0.0f); + } else { + return glm::ortho(-0.5f, fov * aspect_ratio-0.5f, 0.0f, fov); + } } glm::mat4 Camera::getView(bool pos) const {