mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(ui): add background to Label widget and refactor debug overlay
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Cubed/ui/rect.hpp"
|
||||||
#include "Cubed/ui/text.hpp"
|
#include "Cubed/ui/text.hpp"
|
||||||
#include "Cubed/ui/ui_vertex_data.hpp"
|
#include "Cubed/ui/ui_vertex_data.hpp"
|
||||||
#include "Cubed/ui/widget.hpp"
|
#include "Cubed/ui/widget.hpp"
|
||||||
@@ -17,7 +18,9 @@ public:
|
|||||||
|
|
||||||
Label& set_color(Color color);
|
Label& set_color(Color color);
|
||||||
Label& set_scale(float scale);
|
Label& set_scale(float scale);
|
||||||
|
Label& enable_background();
|
||||||
|
Label& set_background(Color color, float alpha);
|
||||||
|
Label& set_background_alpha(float alpha);
|
||||||
const UIVertexData& data() const;
|
const UIVertexData& data() const;
|
||||||
|
|
||||||
const TextStyle& text_style() const;
|
const TextStyle& text_style() const;
|
||||||
@@ -38,6 +41,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
TextStyle m_text;
|
TextStyle m_text;
|
||||||
UIVertexData m_data;
|
UIVertexData m_data;
|
||||||
|
std::unique_ptr<Rect> m_background;
|
||||||
float m_real_width = 0.0f;
|
float m_real_width = 0.0f;
|
||||||
float m_real_height = 0.0f;
|
float m_real_height = 0.0f;
|
||||||
float m_offset_x = 0.0f;
|
float m_offset_x = 0.0f;
|
||||||
|
|||||||
@@ -19,15 +19,34 @@ std::unique_ptr<DebugCollector>& DebugCollector::get_ptr() {
|
|||||||
}
|
}
|
||||||
void DebugCollector::destory() { get_ptr().reset(); }
|
void DebugCollector::destory() { get_ptr().reset(); }
|
||||||
void DebugCollector::init(int width, int height) {
|
void DebugCollector::init(int width, int height) {
|
||||||
constexpr float SCALE = 0.6f;
|
constexpr float SCALE = 0.7f;
|
||||||
|
constexpr Color COLOR = Color::GRAY;
|
||||||
|
constexpr float ALPHA = 0.6f;
|
||||||
|
|
||||||
m_widget.set_window_size(width, height);
|
m_widget.set_window_size(width, height);
|
||||||
m_widget.set_spacing(15);
|
m_widget.set_spacing(15);
|
||||||
m_widget.set_anchor(Anchor::TOP_LEFT);
|
m_widget.set_anchor(Anchor::TOP_LEFT);
|
||||||
m_widget.set_offset({0, 5});
|
m_widget.set_offset({0, 5});
|
||||||
m_widget.set_child_anchor(ColumnLayoutAnchor::LEFT);
|
m_widget.set_child_anchor(ColumnLayoutAnchor::LEFT);
|
||||||
|
|
||||||
|
auto add_label = [&](std::string_view text, std::string_view key = "") {
|
||||||
|
auto& label = m_widget.add_child<Label>();
|
||||||
|
label.enable_background()
|
||||||
|
.set_background(COLOR, ALPHA)
|
||||||
|
.set_color(Color::WHITE)
|
||||||
|
.set_text(text)
|
||||||
|
.set_scale(SCALE);
|
||||||
|
if (!key.empty()) {
|
||||||
|
auto [_, insert] =
|
||||||
|
m_component.try_emplace(std::string(key), &label);
|
||||||
|
if (!insert) {
|
||||||
|
Logger::error("DebugCollector Key {} already esist", key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// version_text
|
// version_text
|
||||||
auto& version_text = m_widget.add_child<Label>();
|
|
||||||
std::string version{"Version: " CUBED_VERSION};
|
std::string version{"Version: " CUBED_VERSION};
|
||||||
|
|
||||||
#ifdef DEBUG_MODE
|
#ifdef DEBUG_MODE
|
||||||
@@ -36,87 +55,56 @@ void DebugCollector::init(int width, int height) {
|
|||||||
version.append("-release");
|
version.append("-release");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
version_text.set_color(Color::WHITE).set_text(version).set_scale(SCALE);
|
add_label(version);
|
||||||
|
|
||||||
{
|
add_label(Tools::get_compiler_info());
|
||||||
auto& compiler = m_widget.add_child<Label>();
|
|
||||||
compiler.set_text(Tools::get_compiler_info()).set_scale(SCALE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// fps
|
// fps
|
||||||
auto& fps_text = m_widget.add_child<Label>();
|
|
||||||
fps_text.set_text("FPS: 0").set_scale(SCALE);
|
add_label("FPS: 0", "fps");
|
||||||
m_component.try_emplace("fps", &fps_text);
|
|
||||||
|
|
||||||
// player_pos
|
// player_pos
|
||||||
auto& player_pos_text = m_widget.add_child<Label>();
|
add_label("x: 0.00 y: 0.00 z: 0.00", "player_pos");
|
||||||
player_pos_text.set_text("x: 0.00 y: 0.00 z: 0.00").set_scale(SCALE);
|
|
||||||
m_component.try_emplace("player_pos", &player_pos_text);
|
|
||||||
|
|
||||||
// rendered_chunk
|
// rendered_chunk
|
||||||
auto& rendered_chunk_text = m_widget.add_child<Label>();
|
add_label("Rendered Chunk: 0", "rendered_chunk");
|
||||||
rendered_chunk_text.set_text("Rendered Chunk: 0").set_scale(SCALE);
|
|
||||||
m_component.try_emplace("rendered_chunk", &rendered_chunk_text);
|
|
||||||
|
|
||||||
// rss
|
// rss
|
||||||
auto& rss_text = m_widget.add_child<Label>();
|
add_label("RSS: 0mb", "rss");
|
||||||
rss_text.set_text("RSS: 0mb").set_scale(SCALE);
|
|
||||||
m_component.try_emplace("rss", &rss_text);
|
|
||||||
|
|
||||||
// os
|
// os
|
||||||
std::string os;
|
std::string os;
|
||||||
auto& os_text = m_widget.add_child<Label>();
|
std::string os_text;
|
||||||
os_text.set_scale(SCALE);
|
|
||||||
if (Tools::get_os_version(os)) {
|
if (Tools::get_os_version(os)) {
|
||||||
os_text.set_text("OS: " + os);
|
os_text = "OS: " + os;
|
||||||
Logger::info("OS System: {}", os);
|
Logger::info("OS System: {}", os);
|
||||||
} else {
|
} else {
|
||||||
os_text.set_text("OS: Unknown");
|
os_text = "OS: Unknown";
|
||||||
}
|
}
|
||||||
|
add_label(os_text);
|
||||||
{
|
{
|
||||||
std::string wm{"WM: "};
|
std::string wm{"WM: "};
|
||||||
wm.append(Tools::detect_wm());
|
wm.append(Tools::detect_wm());
|
||||||
auto& wm_label = m_widget.add_child<Label>();
|
add_label(wm);
|
||||||
wm_label.set_text(wm).set_scale(SCALE);
|
|
||||||
}
|
}
|
||||||
// cpu
|
// cpu
|
||||||
auto& cpu_text = m_widget.add_child<Label>();
|
add_label("CPU: " + Tools::get_cpu_info());
|
||||||
cpu_text.set_text("CPU: " + Tools::get_cpu_info()).set_scale(SCALE);
|
|
||||||
|
|
||||||
// gpu
|
// gpu
|
||||||
auto& gpu_text = m_widget.add_child<Label>();
|
add_label(std::string{"GPU: "} +
|
||||||
gpu_text
|
reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
|
||||||
.set_text(std::string{"GPU: "} +
|
|
||||||
reinterpret_cast<const char*>(glGetString(GL_RENDERER)))
|
|
||||||
.set_scale(SCALE);
|
|
||||||
|
|
||||||
// opengl_version
|
// opengl_version
|
||||||
auto& opengl_version_text = m_widget.add_child<Label>();
|
add_label("OpenGL: " + std::to_string(GLVersion.major) + "." +
|
||||||
opengl_version_text
|
std::to_string(GLVersion.minor));
|
||||||
.set_text("OpenGL: " + std::to_string(GLVersion.major) + "." +
|
|
||||||
std::to_string(GLVersion.minor))
|
add_label(std::format("VideoDiver: {}", SDL_GetCurrentVideoDriver()));
|
||||||
.set_scale(SCALE);
|
|
||||||
{
|
|
||||||
auto& video_driver = m_widget.add_child<Label>();
|
|
||||||
video_driver
|
|
||||||
.set_text(
|
|
||||||
std::format("VideoDiver: {}", SDL_GetCurrentVideoDriver()))
|
|
||||||
.set_scale(SCALE);
|
|
||||||
}
|
|
||||||
// speed
|
// speed
|
||||||
auto& speed_text = m_widget.add_child<Label>();
|
add_label("Speed: 0 m/s", "speed");
|
||||||
speed_text.set_text("Speed: 0 m/s").set_scale(SCALE);
|
|
||||||
m_component.try_emplace("speed", &speed_text);
|
add_label("Window W: {} H: {}", "window_size");
|
||||||
{
|
|
||||||
auto& window_size = m_widget.add_child<Label>();
|
add_label("FrameBuffer W: {} H: {}", "frame_buffer");
|
||||||
window_size.set_text("Window W: {} H: {}").set_scale(SCALE);
|
|
||||||
m_component.try_emplace("window_size", &window_size);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
auto& frame_size = m_widget.add_child<Label>();
|
|
||||||
frame_size.set_text("FrameBuffer W: {} H: {}").set_scale(SCALE);
|
|
||||||
m_component.try_emplace("frame_buffer", &frame_size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget& DebugCollector::get_widget() { return m_widget; }
|
Widget& DebugCollector::get_widget() { return m_widget; }
|
||||||
|
|||||||
@@ -19,9 +19,37 @@ Label& Label::set_scale(float scale) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Label::on_update(float dt) { (void)dt; }
|
Label& Label::enable_background() {
|
||||||
|
if (m_background) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
m_background = std::make_unique<Rect>(this);
|
||||||
|
m_background->set_fill(true);
|
||||||
|
m_background->set_anchor(Anchor::TOP_LEFT);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Label& Label::set_background(Color color, float alpha) {
|
||||||
|
m_background->set_color(color).set_alpha(alpha);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Label& Label::set_background_alpha(float alpha) {
|
||||||
|
m_background->set_alpha(alpha);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
void Label::on_render(Renderer& renderer) { renderer.render_lable(*this); }
|
void Label::on_update(float dt) {
|
||||||
|
if (m_background) {
|
||||||
|
m_background->update(dt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Label::on_render(Renderer& renderer) {
|
||||||
|
|
||||||
|
if (m_background) {
|
||||||
|
m_background->render(renderer);
|
||||||
|
}
|
||||||
|
renderer.render_lable(*this);
|
||||||
|
}
|
||||||
|
|
||||||
void Label::update_vertices() {
|
void Label::update_vertices() {
|
||||||
auto textmesh = Font::get().vertices(m_text.text);
|
auto textmesh = Font::get().vertices(m_text.text);
|
||||||
|
|||||||
Reference in New Issue
Block a user