diff --git a/Inc/Nodes_And_Connectors.hpp b/Inc/Nodes_And_Connectors.hpp index cf83732..a3622e1 100644 --- a/Inc/Nodes_And_Connectors.hpp +++ b/Inc/Nodes_And_Connectors.hpp @@ -190,6 +190,9 @@ public: // 接口测试节点 class Connector_Test_Node : public Abs_Node { +private: + float node_width = 0; + public: // 显式禁用默认构造 Connector_Test_Node() = delete; @@ -209,6 +212,9 @@ public: // DLT信息输入类,起始点 class MSG_Input_Node : public Abs_Node { +private: + float node_width = 0; + public: // 显式禁用默认构造 MSG_Input_Node() = delete; diff --git a/Src/Nodes_And_Connectors.cpp b/Src/Nodes_And_Connectors.cpp index 1413de8..c49d697 100644 --- a/Src/Nodes_And_Connectors.cpp +++ b/Src/Nodes_And_Connectors.cpp @@ -1,4 +1,5 @@ #include "Nodes_And_Connectors.hpp" +#include "Json_Utilities.hpp" #include "imgui_internal.h" // 客制化圆形带X按钮 @@ -334,7 +335,7 @@ Connector_Test_Node::Connector_Test_Node(Independent_ID_Generator *Node_ID_Gener // CSV 相关接口 this->Connector_List.push_back(std::make_shared(Connector_ID_Generator)); // [2] -> Column - this->Connector_List.push_back(std::make_shared(Connector_ID_Generator)); // [3] -> Column + this->Connector_List.push_back(std::make_shared(Connector_ID_Generator)); // [3] Column -> } } @@ -352,14 +353,21 @@ void Connector_Test_Node::Show(void) this->initial_position = ImVec2(0, 0); } + // 全宽适配 + bool all_width_fit = true; + // 绘制节点 ImNodes::BeginNode(this->id); { + // 获取节点当前长宽维度 + ImVec2 dimension = ImNodes::GetNodeDimensions(this->id); // 标题部分 ImNodes::BeginNodeTitleBar(); { - ImGui::Text(u8"接口测试用节点"); + const char *title_str = u8"接口测试用节点"; + + ImGui::Text(title_str); ImGui::SameLine(); { ImGui::TextDisabled(u8"(?)"); @@ -379,49 +387,71 @@ void Connector_Test_Node::Show(void) ImGui::SameLine(); // 节点宽度调整 - ImGui::Dummy(ImVec2(120.0f, 0.0f)); + auto button_diameter = ImGui::GetTextLineHeight(); + auto text_width = ImGui::CalcTextSize(title_str).x + ImGui::CalcTextSize(u8"(?)").x; + auto Tittle_Str_Width = text_width + ImNodes::GetStyle().NodePadding.x * 2 + ImGui::GetStyle().ItemSpacing.x * 2; + // 宽度判定 + if (dimension.x < Tittle_Str_Width + button_diameter + ImGui::GetStyle().ItemSpacing.x) + all_width_fit = false; + + ImGui::Dummy(ImVec2(this->node_width - Tittle_Str_Width - button_diameter - ImGui::GetStyle().ItemSpacing.x, 0.0f)); ImGui::SameLine(); + // 绘制关闭按钮 if (CircleButtonWithX("#X", ImGui::GetTextLineHeight() / 2)) this->close_flag = true; } ImNodes::EndNodeTitleBar(); - // 绘制DLT相关接口组 + // 节点主体绘制 { - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, ImGui::GetStyle().ItemSpacing.y)); - this->Connector_List[0]->Show(); - ImGui::SameLine(); - ImGui::Text("-> DLT-In"); - ImGui::SameLine(); - ImGui::Dummy(ImVec2(ImNodes::GetNodeDimensions(this->id).x - ImGui::CalcTextSize("-> DLT-In").x - ImGui::CalcTextSize("DLT-Out ->").x - (ImNodes::GetStyle().NodePadding.x + ImGui::GetStyle().ItemSpacing.x) * 2, 0.0f)); - ImGui::SameLine(); - ImGui::Text("DLT-Out ->"); - ImGui::SameLine(); - this->Connector_List[1]->Show(); + // 绘制DLT相关接口组 + { + // 宽度判定 + auto text_width = ImGui::CalcTextSize("-> DLT-In").x + ImGui::CalcTextSize("DLT-Out ->").x; + if (dimension.x < text_width + ImNodes::GetStyle().NodePadding.x * 2) + all_width_fit = false; + + this->Connector_List[0]->Show(); + ImGui::SameLine(); + ImGui::Text("-> DLT-In"); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(this->node_width - text_width - ImNodes::GetStyle().NodePadding.x * 2, 0.0f)); + ImGui::SameLine(); + ImGui::Text("DLT-Out ->"); + ImGui::SameLine(); + this->Connector_List[1]->Show(); + } + + // 绘制CSV列相关接口组 + { + // 宽度判定 + auto text_width = ImGui::CalcTextSize("-> CSV-Column-In").x + ImGui::CalcTextSize("CSV-Column-Out ->").x; + if (dimension.x < text_width + ImNodes::GetStyle().NodePadding.x * 2 + ImGui::CalcTextSize("A").x) + all_width_fit = false; + if (this->node_width == 0) + this->node_width = text_width + ImNodes::GetStyle().NodePadding.x * 2 + ImGui::CalcTextSize("A").x; + + this->Connector_List[2]->Show(); + ImGui::SameLine(); + ImGui::Text("-> CSV-Column-In"); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(this->node_width - text_width - ImNodes::GetStyle().NodePadding.x * 2, 0.0f)); + ImGui::SameLine(); + ImGui::Text("CSV-Column-Out ->"); + ImGui::SameLine(); + this->Connector_List[3]->Show(); + } ImGui::PopStyleVar(); } - // 绘制CSV列相关接口组 - { - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, ImGui::GetStyle().ItemSpacing.y)); - - this->Connector_List[2]->Show(); - ImGui::SameLine(); - ImGui::Text("-> CSV-Column-In"); - ImGui::SameLine(); - ImGui::Dummy(ImVec2(ImNodes::GetNodeDimensions(this->id).x - ImGui::CalcTextSize("-> CSV-Column-In").x - ImGui::CalcTextSize("CSV-Column-Out ->").x - (ImNodes::GetStyle().NodePadding.x + ImGui::GetStyle().ItemSpacing.x) * 2, 0.0f)); - ImGui::SameLine(); - ImGui::Text("CSV-Column-Out ->"); - ImGui::SameLine(); - this->Connector_List[3]->Show(); - - ImGui::PopStyleVar(); - } + // 使用递进的方式自适应宽度 + if (all_width_fit == false) + this->node_width += 1.0f; } ImNodes::EndNode(); } @@ -446,17 +476,12 @@ MSG_Input_Node::MSG_Input_Node(Independent_ID_Generator *Node_ID_Generator, { // 消息输出节点 this->Connector_List.push_back(std::make_shared(Connector_ID_Generator)); - this->Connector_List.push_back(std::make_shared(Connector_ID_Generator)); } } // 绘制节点 void MSG_Input_Node::Show(void) { - - // auto ImNodesStyle = ImNodes::GetStyle(); - // auto ImGuiStyle = ImGui::GetStyle(); - if (this->close_flag == true) return; @@ -467,6 +492,9 @@ void MSG_Input_Node::Show(void) this->initial_position = ImVec2(0, 0); } + // 全宽适配 + bool all_width_fit = true; + // 绘制节点 ImNodes::BeginNode(this->id); { @@ -474,7 +502,9 @@ void MSG_Input_Node::Show(void) // 标题部分 ImNodes::BeginNodeTitleBar(); { - ImGui::Text(u8"DLT-信息输入节点"); + const char *title_str = u8"DLT-信息输入节点"; + + ImGui::Text(title_str); ImGui::SameLine(); { ImGui::TextDisabled(u8"(?)"); @@ -492,43 +522,53 @@ void MSG_Input_Node::Show(void) ImGui::PopStyleVar(); } ImGui::SameLine(); - // ImGui::Dummy(ImVec2(node_width - ImGui::CalcTextSize(u8"DLT-信息输入节点").x - ImGui::GetTextLineHeight(), 0.0f)); // 节点宽度调整 - ImGui::Dummy(ImVec2(20.0f, 0.0f)); + ImVec2 dimension = ImNodes::GetNodeDimensions(this->id); + auto button_diameter = ImGui::GetTextLineHeight(); + auto text_width = ImGui::CalcTextSize(title_str).x + ImGui::CalcTextSize(u8"(?)").x; + auto Tittle_Str_Width = text_width + ImNodes::GetStyle().NodePadding.x * 2 + ImGui::GetStyle().ItemSpacing.x * 2; + if (this->node_width == 0) + this->node_width = Tittle_Str_Width + button_diameter + ImGui::GetStyle().ItemSpacing.x; + + if (dimension.x < Tittle_Str_Width + button_diameter + ImGui::GetStyle().ItemSpacing.x) + all_width_fit = false; + + ImGui::Dummy(ImVec2(this->node_width - Tittle_Str_Width - button_diameter - ImGui::GetStyle().ItemSpacing.x, 0.0f)); ImGui::SameLine(); + // 绘制关闭按钮 - if (CircleButtonWithX("#X", ImGui::GetTextLineHeight() / 2)) + if (CircleButtonWithX("#X", button_diameter / 2)) this->close_flag = true; } ImNodes::EndNodeTitleBar(); // 节点主体部分 { - } + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, ImGui::GetStyle().ItemSpacing.y)); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, ImGui::GetStyle().ItemSpacing.y)); + // 靠右占位 + ImVec2 dimension = ImNodes::GetNodeDimensions(this->id); + auto text_width = ImGui::CalcTextSize(u8"DLT-Out ->").x; - ImVec2 dimension = ImNodes::GetNodeDimensions(this->id); - ImVec2 text_size = ImGui::CalcTextSize(u8"OUT ->"); - if (dimension.x > text_size.x) - { - ImGui::Dummy(ImVec2(dimension.x - text_size.x - (ImNodes::GetStyle().NodePadding.x + ImGui::GetStyle().ItemSpacing.x) * 2, 0.0f)); + if (dimension.x < text_width + ImNodes::GetStyle().NodePadding.x * 2) + all_width_fit = false; + + ImGui::Dummy(ImVec2(this->node_width - text_width - ImNodes::GetStyle().NodePadding.x * 2, 0.0f)); ImGui::SameLine(); - } - ImGui::Text(u8"OUT ->"); - ImGui::SameLine(); - /** - * @todo 自定义接口绘制 - */ - for (auto Connector : this->Connector_List) - { - Connector->Show(); + // 绘制DLT输出接口 + ImGui::Text(u8"DLT-Out ->"); + ImGui::SameLine(); + this->Connector_List[0]->Show(); + + ImGui::PopStyleVar(); } - ImGui::PopStyleVar(); + // 使用递进的方式自适应宽度 + if (all_width_fit == false) + this->node_width += 1.0f; } ImNodes::EndNode(); }