kanzi运行时节点状态展示

2024-04-03 12:28

本文主要是介绍kanzi运行时节点状态展示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景:有时候kanzi运行后节点不显示,可能是visible/opacity等属性设置不正确,排查困难。做一个实时节点树,方便查看节点信息。

1. 引入imgui

在vs工程里导入glad,glfw,imgui代码
vs属性-链接库,增加glfw3.lib
在这里插入图片描述

在这里插入图片描述

2. 实现框架

onProjectLoaded里创建子线程

CreateThread(NULL, 0, multiTask, 0, 0, NULL);

子线程负责创建glfw窗口,刷新,销毁窗口

DWORD WINAPI multiTask(LPVOID p) {FCreateWindow();while (!glfwWindowShouldClose(m_Window)) {Update();}Destory();return 0;
};

2.1 创建窗口

void FCreateWindow() {glfwInit();const char* glsl_version = "#version 130";glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);m_Window = glfwCreateWindow(m_WindowWidth, m_WindowHeight, "Xml", nullptr, nullptr);glfwMakeContextCurrent(m_Window);glfwSwapInterval(1); // Enable vsyncint status = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);printf("status-%d\n", status);printf("Vendor-%s\n", glGetString(GL_VENDOR));printf("Renderer-%s\n", glGetString(GL_RENDERER));printf("Version-%s\n", glGetString(GL_VERSION));IMGUI_CHECKVERSION();ImGui::CreateContext();ImGuiIO &io = ImGui::GetIO();(void)io;io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls// io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controlsio.ConfigFlags |= ImGuiConfigFlags_DockingEnable;	// Enable Dockingio.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows// io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoTaskBarIcons;// io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoMerge;float fontSize = 18.0f; // *2.0f;io.Fonts->AddFontFromFileTTF(("./font/Alimama_DongFangDaKai_Regular.ttf"), fontSize, NULL, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());io.FontDefault = io.Fonts->AddFontFromFileTTF(("./font/Alimama_DongFangDaKai_Regular.ttf"), fontSize, NULL, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());// Setup Dear ImGui styleImGui::StyleColorsDark();//ImGui::StyleColorsClassic();// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.ImGuiStyle &style = ImGui::GetStyle();if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable){style.WindowRounding = 0.0f;style.Colors[ImGuiCol_WindowBg].w = 1.0f;}// Setup Platform/Renderer bindingsImGui_ImplGlfw_InitForOpenGL(m_Window, true);ImGui_ImplOpenGL3_Init(glsl_version);
};

2.2 刷新

void Update() {glfwPollEvents();//startImGui_ImplOpenGL3_NewFrame();ImGui_ImplGlfw_NewFrame();ImGui::NewFrame();{//  bool show_demo_window = true;// ImGui::ShowDemoWindow(&show_demo_window);ImGui::Begin("Project");if (g_pApplication) {ShowProjectTreeTop();}ImGui::End();bool show_demo_window = true;ImGui::ShowDemoWindow(&show_demo_window);}//endImGuiIO &io = ImGui::GetIO();ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);// RenderingImGui::Render();int display_w, display_h;glfwGetFramebufferSize(m_Window, &display_w, &display_h);glViewport(0, 0, display_w, display_h);glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);glClear(GL_COLOR_BUFFER_BIT);ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());io.DisplaySize = ImVec2(display_w, display_h);if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable){GLFWwindow *backup_current_context = glfwGetCurrentContext();ImGui::UpdatePlatformWindows();ImGui::RenderPlatformWindowsDefault();glfwMakeContextCurrent(backup_current_context);}glfwSwapBuffers(m_Window);};

2.3 销毁窗口

void Destory() {ImGui_ImplOpenGL3_Shutdown();ImGui_ImplGlfw_Shutdown();ImGui::DestroyContext();glfwDestroyWindow(m_Window);glfwTerminate();
};

3. 实现功能逻辑

遍历kanzi运行时节点,用tree展示

void ShowProjectTreeTop()
{static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_DefaultOpen;static bool align_label_with_current_x_position = false;static bool test_drag_and_drop = true;if (align_label_with_current_x_position)ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing());static int selection_mask = 0;int node_clicked = -1;ImGuiTreeNodeFlags node_flags = base_flags;const bool is_selected = (selection_mask & (1 << 0)) != 0;shared_ptr<Node> node = dynamic_pointer_cast<Node>(g_pApplication->getRoot());if (is_selected && node.get() == g_CurrentNode)node_flags |= ImGuiTreeNodeFlags_Selected;bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)0, node_flags, node->getName().c_str(), 0);if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) {node_clicked = 0;g_CurrentNode = node.get();}if (node_open){ShowProjectTree(node);ImGui::TreePop();}if (node_clicked != -1){// Update selection state// (process outside of tree loop to avoid visual inconsistencies during the clicking frame)if (ImGui::GetIO().KeyCtrl)selection_mask ^= (1 << node_clicked);          // CTRL+click to toggleelse //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, may want to preserve selection when clicking on item that is part of the selectionselection_mask = (1 << node_clicked);           // Click to single-select}if (align_label_with_current_x_position)ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing());}void ShowProjectTree(shared_ptr<Node>& node)
{static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_DefaultOpen;static bool align_label_with_current_x_position = false;static bool test_drag_and_drop = false;if (align_label_with_current_x_position)ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing());static int selection_mask = 0;int node_clicked = -1;size_t size = node->getAbstractChildCountOverride();for (unsigned int i = 0; i < size; ++i){shared_ptr<Node> object = dynamic_pointer_cast<Node>(node->getAbstractChildOverride(i));// Disable the default "open on single-click behavior" + set Selected flag according to our selection.// To alter selection we use IsItemClicked() && !IsItemToggledOpen(), so clicking on an arrow doesn't alter selection.ImGuiTreeNodeFlags node_flags = base_flags;const bool is_selected = (selection_mask & (1 << i)) != 0;if (is_selected && object.get() == g_CurrentNode)node_flags |= ImGuiTreeNodeFlags_Selected;std::string name = object->getName();if (object->getProperty(Node::VisibleProperty)) {}else {name += " NoVisible";node_flags |= ImGuiTreeNodeFlags_TextDisabled;}if (object->getProperty(Node::OpacityProperty)) {}else {//ImGui::TextDisabled("NoVisible");name += " NoOpacity";node_flags |= ImGuiTreeNodeFlags_TextDisabled;}bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, (char *)name.c_str(), i);if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) {node_clicked = i;g_CurrentNode = object.get();}if (node_open){if (object->getAbstractChildCountOverride() > 0){ShowProjectTree(object);}else {Viewport2DSharedPtr v2d = dynamic_pointer_cast<Viewport2D>(object);if (v2d) {//Viewport2D                SceneSharedPtr sc = v2d->getScene();if (sc->getChildCount() > 0){shared_ptr<Node3D> node = dynamic_pointer_cast<Node3D>(sc);ShowProjectTree3D(node);}}}ImGui::TreePop();}}if (node_clicked != -1){// Update selection state// (process outside of tree loop to avoid visual inconsistencies during the clicking frame)if (ImGui::GetIO().KeyCtrl)selection_mask ^= (1 << node_clicked);          // CTRL+click to toggleelse //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, may want to preserve selection when clicking on item that is part of the selectionselection_mask = (1 << node_clicked);           // Click to single-select}if (align_label_with_current_x_position)ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing());
}void ShowProjectTree3D(shared_ptr<Node3D>& node)
{static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_DefaultOpen;static bool align_label_with_current_x_position = false;static bool test_drag_and_drop = false;if (align_label_with_current_x_position)ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing());static int selection_mask = 0;int node_clicked = -1;size_t size = node->getChildCount();for (unsigned int i = 0; i < size; ++i){Node3DSharedPtr object = node->getChild(i);// Disable the default "open on single-click behavior" + set Selected flag according to our selection.// To alter selection we use IsItemClicked() && !IsItemToggledOpen(), so clicking on an arrow doesn't alter selection.ImGuiTreeNodeFlags node_flags = base_flags;const bool is_selected = (selection_mask & (1 << i)) != 0;if (is_selected && object.get() == g_CurrentNode)node_flags |= ImGuiTreeNodeFlags_Selected;std::string name = object->getName();if (object->getProperty(Node::VisibleProperty)) {}else {name += " NoVisible";node_flags |= ImGuiTreeNodeFlags_TextDisabled;}if (object->getProperty(Node::OpacityProperty)) {}else {//ImGui::TextDisabled("NoVisible");name += " NoOpacity";node_flags |= ImGuiTreeNodeFlags_TextDisabled;}bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, (char *)name.c_str(), i);if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) {node_clicked = i;g_CurrentNode = object.get();}if (node_open){if (object->getChildCount() > 0){ShowProjectTree3D(object);}ImGui::TreePop();}}if (node_clicked != -1){// Update selection state// (process outside of tree loop to avoid visual inconsistencies during the clicking frame)if (ImGui::GetIO().KeyCtrl)selection_mask ^= (1 << node_clicked);          // CTRL+click to toggleelse //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, may want to preserve selection when clicking on item that is part of the selectionselection_mask = (1 << node_clicked);           // Click to single-select}if (align_label_with_current_x_position)ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing());
}

4. 效果

运行kanzi程序后,会额外出来一个窗口,实时显示所有节点信息

在这里插入图片描述
节点无效置灰请参考 imgui tree节点无效置灰实现方案

5. 拓展

将来可以根据点击某一个节点,展示属性窗口。

这篇关于kanzi运行时节点状态展示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/872916

相关文章

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)

《k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)》本文记录在K8s上运行的MySQL/MariaDB备份方案,通过工具容器执行mysqldump,结合定时任务实... 目录前言一、获取需要备份的数据库的信息二、备份步骤1.准备工作(X86)1.准备工作(arm)2.手

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Java -jar命令如何运行外部依赖JAR包

《Java-jar命令如何运行外部依赖JAR包》在Java应用部署中,java-jar命令是启动可执行JAR包的标准方式,但当应用需要依赖外部JAR文件时,直接使用java-jar会面临类加载困... 目录引言:外部依赖JAR的必要性一、问题本质:类加载机制的限制1. Java -jar的默认行为2. 类加

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

eclipse如何运行springboot项目

《eclipse如何运行springboot项目》:本文主要介绍eclipse如何运行springboot项目问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目js录当在eclipse启动spring boot项目时出现问题解决办法1.通过cmd命令行2.在ecl

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.

Spring Boot项目打包和运行的操作方法

《SpringBoot项目打包和运行的操作方法》SpringBoot应用内嵌了Web服务器,所以基于SpringBoot开发的web应用也可以独立运行,无须部署到其他Web服务器中,下面以打包dem... 目录一、打包为JAR包并运行1.打包为可执行的 JAR 包2.运行 JAR 包二、打包为WAR包并运行

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见