add debug GUI render mode (F8)

This commit is contained in:
MihailRis 2025-03-10 18:06:39 +03:00
parent a74a4fcf53
commit 5af6b91b22
18 changed files with 93 additions and 29 deletions

View File

@ -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";

View File

@ -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();
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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<Font>("normal");
const auto& font = assets.require<Font>(FONT_DEFAULT);
glm::vec3 xvec = note.getAxisX();
glm::vec3 yvec = note.getAxisY();

View File

@ -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 <iostream>
#include <algorithm>
#include <utility>
@ -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>(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<UINode> GUI::getFocused() const {
@ -252,8 +286,8 @@ void GUI::add(std::shared_ptr<UINode> node) {
container->add(std::move(node));
}
void GUI::remove(std::shared_ptr<UINode> 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<UINode> node) {
@ -297,3 +331,7 @@ void GUI::setDoubleClickDelay(float delay) {
float GUI::getDoubleClickDelay() const {
return doubleClickDelay;
}
void GUI::toggleDebug() {
debug = !debug;
}

View File

@ -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<UINode> node);
/// @brief Remove node from the main container
void remove(std::shared_ptr<UINode> node) noexcept;
void remove(UINode* node) noexcept;
void remove(const std::shared_ptr<UINode>& 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();
};
}

View File

@ -172,11 +172,11 @@ void Container::add(const std::shared_ptr<UINode>& node, glm::vec2 pos) {
add(node);
}
void Container::remove(const std::shared_ptr<UINode>& selected) {
void Container::remove(UINode* selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
[selected](const std::shared_ptr<UINode>& node) {
return node == selected;
return node.get() == selected;
}
), nodes.end());
refresh();
@ -185,7 +185,7 @@ void Container::remove(const std::shared_ptr<UINode>& selected) {
void Container::remove(const std::string& id) {
for (auto& node : nodes) {
if (node->getId() == id) {
return remove(node);
return remove(node.get());
}
}
}

View File

@ -32,7 +32,7 @@ namespace gui {
virtual void add(const std::shared_ptr<UINode>& node);
virtual void add(const std::shared_ptr<UINode>& node, glm::vec2 pos);
virtual void clear();
virtual void remove(const std::shared_ptr<UINode>& node);
virtual void remove(UINode* node);
virtual void remove(const std::string& id);
virtual void scrolled(int value) override;
virtual void setScrollable(bool flag);

View File

@ -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<Font>("normal");
const auto& font = assets.require<Font>(FONT_DEFAULT);
drawItemInfo(batch, stack, item, font, pos);
}
}

View File

@ -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<FontStylesScheme> 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();

View File

@ -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};
}
}

View File

@ -63,7 +63,7 @@ void Panel::add(const std::shared_ptr<UINode> &node) {
fullRefresh();
}
void Panel::remove(const std::shared_ptr<UINode> &node) {
void Panel::remove(UINode* node) {
Container::remove(node);
fullRefresh();
}

View File

@ -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<UINode>& node) override;
virtual void remove(const std::shared_ptr<UINode>& node) override;
virtual void remove(UINode* node) override;
virtual void refresh() override;
virtual void fullRefresh() override;

View File

@ -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<int>(points[index % dmwidth]);
auto font = assets.get<Font>("normal");
auto font = assets.get<Font>(FONT_DEFAULT);
for (int y = 0; y < dmheight; y += labelsInterval) {
std::wstring string;
if (current_point/16 == y/labelsInterval) {

View File

@ -266,7 +266,7 @@ void UINode::moveInto(
) {
auto parent = node->getParent();
if (auto container = dynamic_cast<Container*>(parent)) {
container->remove(node);
container->remove(node.get());
}
if (parent) {
parent->scrolled(0);

View File

@ -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<Container*>(parent)) {
container->remove(node);
container->remove(node.get());
}
});
return 0;

View File

@ -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<float>(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 {