Added DebugInfo

This commit is contained in:
2025-12-25 15:58:57 +08:00
parent ad83a4f70d
commit 8a78bfef14
9 changed files with 290 additions and 7 deletions

View File

@@ -20,6 +20,7 @@ public:
label->setRect(x, y, UI::ButtonSize * 4, UI::ButtonSize * 2);
label->setText(text, {"SourceHanSansSC-Regular.otf", UI::UI_NORMAL_FONT_SIZE, {0, 0, 0, 255}});
label->setName(name);
label->setVisible(true);
return label;
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include <string>
#include <utility>
struct DebugData {
bool showFPS = true;
bool showMousePosition = true;
std::pair<int, int> mousePosition = {0, 0};
std::pair<int, int> mouseLogicalPostion = {0, 0};
int currentFPS = 0;
};

View File

@@ -0,0 +1,128 @@
#include "DebugOverlay.h"
#include "ui/base/UIWidgetFactory.h"
#include <iostream>
DebugOverlay::DebugOverlay(DebugData& debugData)
: m_debugData(debugData) {
}
DebugOverlay::~DebugOverlay() {
}
void DebugOverlay::initialize() {
auto fpsLabel = UIWidgetFactory::createStandardLabel(
"FPS",
"FPS: 0",
0,
0
);
m_labels.emplace(fpsLabel->getNameHash(), std::move(fpsLabel));
auto mousePhyicalPos = UIWidgetFactory::createStandardLabel(
"MousePhyscialPosition",
"PhyscialPos: 0, 0",
0,
40
);
m_labels.emplace(mousePhyicalPos->getNameHash(), std::move(mousePhyicalPos));
auto mouseLogicalPos = UIWidgetFactory::createStandardLabel(
"MouseLogicalPosition",
"LogicalPos: 0, 0",
0,
80
);
m_labels.emplace(mouseLogicalPos->getNameHash(), std::move(mouseLogicalPos));
}
const UIRenderData& DebugOverlay::getUIRenderData() {
collectRenderData();
return m_uiRenderData;
}
void DebugOverlay::collectRenderData() {
// 清理上一帧的数据
m_uiRenderData.buttons.clear();
m_uiRenderData.labels.clear();
for (auto& label : m_labels) {
if(!label.second->isVisible()) {
continue;
}
m_uiRenderData.labels.push_back(label.second->getLabelDate());
//std::cout << "Collect label: " << label.second->getNameHash() << "\n";
}
}
void DebugOverlay::updateDebugInfo() {
const auto key = makeHash("FPS");
auto fpsLabelIt = m_labels.find(key);
if (fpsLabelIt != m_labels.end()) {
auto fpsLabel = fpsLabelIt->second.get();
if (m_debugData.showFPS) {
fpsLabel->setText("FPS: " + std::to_string(m_debugData.currentFPS));
fpsLabel->setVisible(true);
} else {
fpsLabel->setVisible(false);
}
} else {
std::cerr << "FPS Label not found!\n";
}
const auto mouseKey = makeHash("MousePhyscialPosition");
auto mousePosLabelIt = m_labels.find(mouseKey);
if (mousePosLabelIt != m_labels.end()) {
auto mousePosLabel = mousePosLabelIt->second.get();
if (m_debugData.showMousePosition) {
if (!mousePosLabel) {
std::cerr << "Mouse PhyscialPosition Label not found!\n";
return;
}
mousePosLabel->setText(
"PhyscialPos: " +
std::to_string(m_debugData.mousePosition.first) + ", " +
std::to_string(m_debugData.mousePosition.second)
);
mousePosLabel->setVisible(true);
} else {
mousePosLabel->setVisible(false);
}
} else {
std::cerr << "Mouse PhyscialPosition Label not found!\n";
}
const auto mouseLogicalKey = makeHash("MouseLogicalPosition");
auto mouseLogicalPosLabelIt = m_labels.find(mouseLogicalKey);
if (mouseLogicalPosLabelIt != m_labels.end()) {
auto mouseLogicalPosLabel = mouseLogicalPosLabelIt->second.get();
if (m_debugData.showMousePosition) {
if (!mouseLogicalPosLabel) {
std::cerr << "Mouse LogicalPosition Label not found!\n";
return;
}
mouseLogicalPosLabel->setText(
"LogicalPos: " +
std::to_string(m_debugData.mouseLogicalPostion.first) + ", " +
std::to_string(m_debugData.mouseLogicalPostion.second)
);
mouseLogicalPosLabel->setVisible(true);
} else {
mouseLogicalPosLabel->setVisible(false);
}
} else {
std::cerr << "Mouse LogicalPosition Label not found!\n";
}
}

View File

@@ -0,0 +1,38 @@
#pragma once
#include "ui/components/Label.h"
#include "DebugData.h"
#include <memory>
#include <unordered_map>
class DebugOverlay {
public:
DebugOverlay(DebugData& debugData);
~DebugOverlay();
void initialize();
//void shutdown();
void collectRenderData();
const UIRenderData& getUIRenderData();
void updateDebugInfo();
// 应该添加:
DebugOverlay(const DebugOverlay&) = delete;
DebugOverlay& operator=(const DebugOverlay&) = delete;
DebugOverlay(DebugOverlay&&) = delete;
DebugOverlay& operator=(DebugOverlay&&) = delete;
private:
DebugData& m_debugData;
std::unordered_map<int, std::unique_ptr<Label>> m_labels;
UIRenderData m_uiRenderData;
size_t makeHash(const std::string& name) {
return std::hash<std::string>{}(name);
}
};