| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | |||
| 2 | #include "imgui.h" | ||
| 3 | |||
| 4 | #include <math.h> // fmodf | ||
| 5 | |||
| 6 | // NB: You can use math functions/operators on ImVec2 if you #define IMGUI_DEFINE_MATH_OPERATORS and #include "imgui_internal.h" | ||
| 7 | // Here we only declare simple +/- operators so others don't leak into the demo code. | ||
| 8 | static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); } | ||
| 9 | static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); } | ||
| 10 | |||
| 11 | // Dummy data structure provided for the example. | ||
| 12 | // Note that we storing links as indices (not ID) to make example code shorter. | ||
| 13 | void ShowExampleAppCustomNodeGraph(bool* opened) | ||
| 14 | { | ||
| 15 | ✗ | ImGui::SetNextWindowSize(ImVec2(700, 600), ImGuiCond_FirstUseEver); | |
| 16 | ✗ | if (!ImGui::Begin("Example: Custom Node Graph", opened)) | |
| 17 | { | ||
| 18 | ✗ | ImGui::End(); | |
| 19 | ✗ | return; | |
| 20 | } | ||
| 21 | |||
| 22 | // Dummy | ||
| 23 | struct Node | ||
| 24 | { | ||
| 25 | int ID; | ||
| 26 | char Name[32]; | ||
| 27 | ImVec2 Pos, Size; | ||
| 28 | float Value; | ||
| 29 | ImVec4 Color; | ||
| 30 | int InputsCount, OutputsCount; | ||
| 31 | |||
| 32 | Node(int id, const char* name, const ImVec2& pos, float value, const ImVec4& color, int inputs_count, int outputs_count) { ID = id; strcpy(Name, name); Pos = pos; Value = value; Color = color; InputsCount = inputs_count; OutputsCount = outputs_count; } | ||
| 33 | |||
| 34 | ImVec2 GetInputSlotPos(int slot_no) const { return ImVec2(Pos.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)InputsCount + 1)); } | ||
| 35 | ImVec2 GetOutputSlotPos(int slot_no) const { return ImVec2(Pos.x + Size.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)OutputsCount + 1)); } | ||
| 36 | }; | ||
| 37 | struct NodeLink | ||
| 38 | { | ||
| 39 | int InputIdx, InputSlot, OutputIdx, OutputSlot; | ||
| 40 | |||
| 41 | NodeLink(int input_idx, int input_slot, int output_idx, int output_slot) { InputIdx = input_idx; InputSlot = input_slot; OutputIdx = output_idx; OutputSlot = output_slot; } | ||
| 42 | }; | ||
| 43 | |||
| 44 | // State | ||
| 45 | ✗ | static ImVector<Node> nodes; | |
| 46 | ✗ | static ImVector<NodeLink> links; | |
| 47 | static ImVec2 scrolling = ImVec2(0.0f, 0.0f); | ||
| 48 | static bool inited = false; | ||
| 49 | static bool show_grid = true; | ||
| 50 | static int node_selected = -1; | ||
| 51 | |||
| 52 | // Initialization | ||
| 53 | ✗ | ImGuiIO& io = ImGui::GetIO(); | |
| 54 | ✗ | if (!inited) | |
| 55 | { | ||
| 56 | ✗ | nodes.push_back(Node(0, "MainTex", ImVec2(40, 50), 0.5f, ImColor(255, 100, 100), 1, 1)); | |
| 57 | ✗ | nodes.push_back(Node(1, "BumpMap", ImVec2(40, 150), 0.42f, ImColor(200, 100, 200), 1, 1)); | |
| 58 | ✗ | nodes.push_back(Node(2, "Combine", ImVec2(270, 80), 1.0f, ImColor(0, 200, 100), 2, 2)); | |
| 59 | ✗ | links.push_back(NodeLink(0, 0, 2, 0)); | |
| 60 | ✗ | links.push_back(NodeLink(1, 0, 2, 1)); | |
| 61 | ✗ | inited = true; | |
| 62 | } | ||
| 63 | |||
| 64 | // Draw a list of nodes on the left side | ||
| 65 | ✗ | bool open_context_menu = false; | |
| 66 | ✗ | int node_hovered_in_list = -1; | |
| 67 | ✗ | int node_hovered_in_scene = -1; | |
| 68 | ✗ | ImGui::BeginChild("node_list", ImVec2(100, 0)); | |
| 69 | ✗ | ImGui::Text("Nodes"); | |
| 70 | ✗ | ImGui::Separator(); | |
| 71 | ✗ | for (int node_idx = 0; node_idx < nodes.Size; node_idx++) | |
| 72 | { | ||
| 73 | ✗ | Node* node = &nodes[node_idx]; | |
| 74 | ✗ | ImGui::PushID(node->ID); | |
| 75 | ✗ | if (ImGui::Selectable(node->Name, node->ID == node_selected)) | |
| 76 | ✗ | node_selected = node->ID; | |
| 77 | ✗ | if (ImGui::IsItemHovered()) | |
| 78 | { | ||
| 79 | ✗ | node_hovered_in_list = node->ID; | |
| 80 | ✗ | open_context_menu |= ImGui::IsMouseClicked(1); | |
| 81 | } | ||
| 82 | ✗ | ImGui::PopID(); | |
| 83 | } | ||
| 84 | ✗ | ImGui::EndChild(); | |
| 85 | |||
| 86 | ✗ | ImGui::SameLine(); | |
| 87 | ✗ | ImGui::BeginGroup(); | |
| 88 | |||
| 89 | ✗ | const float NODE_SLOT_RADIUS = 4.0f; | |
| 90 | ✗ | const ImVec2 NODE_WINDOW_PADDING(8.0f, 8.0f); | |
| 91 | |||
| 92 | // Create our child canvas | ||
| 93 | ✗ | ImGui::Text("Hold middle mouse button to scroll (%.2f,%.2f)", scrolling.x, scrolling.y); | |
| 94 | ✗ | ImGui::SameLine(ImGui::GetWindowWidth() - 100); | |
| 95 | ✗ | ImGui::Checkbox("Show grid", &show_grid); | |
| 96 | ✗ | ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(1, 1)); | |
| 97 | ✗ | ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); | |
| 98 | ✗ | ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(60, 60, 70, 200)); | |
| 99 | ✗ | ImGui::BeginChild("scrolling_region", ImVec2(0, 0), true, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoMove); | |
| 100 | ✗ | ImGui::PopStyleVar(); // WindowPadding | |
| 101 | ✗ | ImGui::PushItemWidth(120.0f); | |
| 102 | |||
| 103 | ✗ | const ImVec2 offset = ImGui::GetCursorScreenPos() + scrolling; | |
| 104 | ✗ | ImDrawList* draw_list = ImGui::GetWindowDrawList(); | |
| 105 | |||
| 106 | // Display grid | ||
| 107 | ✗ | if (show_grid) | |
| 108 | { | ||
| 109 | ✗ | ImU32 GRID_COLOR = IM_COL32(200, 200, 200, 40); | |
| 110 | ✗ | float GRID_SZ = 64.0f; | |
| 111 | ✗ | ImVec2 win_pos = ImGui::GetCursorScreenPos(); | |
| 112 | ✗ | ImVec2 canvas_sz = ImGui::GetWindowSize(); | |
| 113 | ✗ | for (float x = fmodf(scrolling.x, GRID_SZ); x < canvas_sz.x; x += GRID_SZ) | |
| 114 | ✗ | draw_list->AddLine(ImVec2(x, 0.0f) + win_pos, ImVec2(x, canvas_sz.y) + win_pos, GRID_COLOR); | |
| 115 | ✗ | for (float y = fmodf(scrolling.y, GRID_SZ); y < canvas_sz.y; y += GRID_SZ) | |
| 116 | ✗ | draw_list->AddLine(ImVec2(0.0f, y) + win_pos, ImVec2(canvas_sz.x, y) + win_pos, GRID_COLOR); | |
| 117 | } | ||
| 118 | |||
| 119 | // Display links | ||
| 120 | ✗ | draw_list->ChannelsSplit(2); | |
| 121 | ✗ | draw_list->ChannelsSetCurrent(0); // Background | |
| 122 | ✗ | for (int link_idx = 0; link_idx < links.Size; link_idx++) | |
| 123 | { | ||
| 124 | ✗ | NodeLink* link = &links[link_idx]; | |
| 125 | ✗ | Node* node_inp = &nodes[link->InputIdx]; | |
| 126 | ✗ | Node* node_out = &nodes[link->OutputIdx]; | |
| 127 | ✗ | ImVec2 p1 = offset + node_inp->GetOutputSlotPos(link->InputSlot); | |
| 128 | ✗ | ImVec2 p2 = offset + node_out->GetInputSlotPos(link->OutputSlot); | |
| 129 | ✗ | draw_list->AddBezierCubic(p1, p1 + ImVec2(+50, 0), p2 + ImVec2(-50, 0), p2, IM_COL32(200, 200, 100, 255), 3.0f); | |
| 130 | } | ||
| 131 | |||
| 132 | // Display nodes | ||
| 133 | ✗ | for (int node_idx = 0; node_idx < nodes.Size; node_idx++) | |
| 134 | { | ||
| 135 | ✗ | Node* node = &nodes[node_idx]; | |
| 136 | ✗ | ImGui::PushID(node->ID); | |
| 137 | ✗ | ImVec2 node_rect_min = offset + node->Pos; | |
| 138 | |||
| 139 | // Display node contents first | ||
| 140 | ✗ | draw_list->ChannelsSetCurrent(1); // Foreground | |
| 141 | ✗ | bool old_any_active = ImGui::IsAnyItemActive(); | |
| 142 | ✗ | ImGui::SetCursorScreenPos(node_rect_min + NODE_WINDOW_PADDING); | |
| 143 | ✗ | ImGui::BeginGroup(); // Lock horizontal position | |
| 144 | ✗ | ImGui::Text("%s", node->Name); | |
| 145 | ✗ | ImGui::SliderFloat("##value", &node->Value, 0.0f, 1.0f, "Alpha %.2f"); | |
| 146 | ✗ | ImGui::ColorEdit3("##color", &node->Color.x); | |
| 147 | ✗ | ImGui::EndGroup(); | |
| 148 | |||
| 149 | // Save the size of what we have emitted and whether any of the widgets are being used | ||
| 150 | ✗ | bool node_widgets_active = (!old_any_active && ImGui::IsAnyItemActive()); | |
| 151 | ✗ | node->Size = ImGui::GetItemRectSize() + NODE_WINDOW_PADDING + NODE_WINDOW_PADDING; | |
| 152 | ✗ | ImVec2 node_rect_max = node_rect_min + node->Size; | |
| 153 | |||
| 154 | // Display node box | ||
| 155 | ✗ | draw_list->ChannelsSetCurrent(0); // Background | |
| 156 | ✗ | ImGui::SetCursorScreenPos(node_rect_min); | |
| 157 | ✗ | ImGui::InvisibleButton("node", node->Size); | |
| 158 | ✗ | if (ImGui::IsItemHovered()) | |
| 159 | { | ||
| 160 | ✗ | node_hovered_in_scene = node->ID; | |
| 161 | ✗ | open_context_menu |= ImGui::IsMouseClicked(1); | |
| 162 | } | ||
| 163 | ✗ | bool node_moving_active = ImGui::IsItemActive(); | |
| 164 | ✗ | if (node_widgets_active || node_moving_active) | |
| 165 | ✗ | node_selected = node->ID; | |
| 166 | ✗ | if (node_moving_active && ImGui::IsMouseDragging(ImGuiMouseButton_Left)) | |
| 167 | ✗ | node->Pos = node->Pos + io.MouseDelta; | |
| 168 | |||
| 169 | ✗ | ImU32 node_bg_color = (node_hovered_in_list == node->ID || node_hovered_in_scene == node->ID || (node_hovered_in_list == -1 && node_selected == node->ID)) ? IM_COL32(75, 75, 75, 255) : IM_COL32(60, 60, 60, 255); | |
| 170 | ✗ | draw_list->AddRectFilled(node_rect_min, node_rect_max, node_bg_color, 4.0f); | |
| 171 | ✗ | draw_list->AddRect(node_rect_min, node_rect_max, IM_COL32(100, 100, 100, 255), 4.0f); | |
| 172 | ✗ | for (int slot_idx = 0; slot_idx < node->InputsCount; slot_idx++) | |
| 173 | ✗ | draw_list->AddCircleFilled(offset + node->GetInputSlotPos(slot_idx), NODE_SLOT_RADIUS, IM_COL32(150, 150, 150, 150)); | |
| 174 | ✗ | for (int slot_idx = 0; slot_idx < node->OutputsCount; slot_idx++) | |
| 175 | ✗ | draw_list->AddCircleFilled(offset + node->GetOutputSlotPos(slot_idx), NODE_SLOT_RADIUS, IM_COL32(150, 150, 150, 150)); | |
| 176 | |||
| 177 | ✗ | ImGui::PopID(); | |
| 178 | } | ||
| 179 | ✗ | draw_list->ChannelsMerge(); | |
| 180 | |||
| 181 | // Open context menu | ||
| 182 | ✗ | if (ImGui::IsMouseReleased(ImGuiMouseButton_Right)) | |
| 183 | ✗ | if (ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) || !ImGui::IsAnyItemHovered()) | |
| 184 | { | ||
| 185 | ✗ | node_selected = node_hovered_in_list = node_hovered_in_scene = -1; | |
| 186 | ✗ | open_context_menu = true; | |
| 187 | } | ||
| 188 | ✗ | if (open_context_menu) | |
| 189 | { | ||
| 190 | ✗ | ImGui::OpenPopup("context_menu"); | |
| 191 | ✗ | if (node_hovered_in_list != -1) | |
| 192 | ✗ | node_selected = node_hovered_in_list; | |
| 193 | ✗ | if (node_hovered_in_scene != -1) | |
| 194 | ✗ | node_selected = node_hovered_in_scene; | |
| 195 | } | ||
| 196 | |||
| 197 | // Draw context menu | ||
| 198 | ✗ | ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8, 8)); | |
| 199 | ✗ | if (ImGui::BeginPopup("context_menu")) | |
| 200 | { | ||
| 201 | ✗ | Node* node = node_selected != -1 ? &nodes[node_selected] : NULL; | |
| 202 | ✗ | ImVec2 scene_pos = ImGui::GetMousePosOnOpeningCurrentPopup() - offset; | |
| 203 | ✗ | if (node) | |
| 204 | { | ||
| 205 | ✗ | ImGui::Text("Node '%s'", node->Name); | |
| 206 | ✗ | ImGui::Separator(); | |
| 207 | ✗ | if (ImGui::MenuItem("Rename..", NULL, false, false)) {} | |
| 208 | ✗ | if (ImGui::MenuItem("Delete", NULL, false, false)) {} | |
| 209 | ✗ | if (ImGui::MenuItem("Copy", NULL, false, false)) {} | |
| 210 | } | ||
| 211 | else | ||
| 212 | { | ||
| 213 | ✗ | if (ImGui::MenuItem("Add")) { nodes.push_back(Node(nodes.Size, "New node", scene_pos, 0.5f, ImColor(100, 100, 200), 2, 2)); } | |
| 214 | ✗ | if (ImGui::MenuItem("Paste", NULL, false, false)) {} | |
| 215 | } | ||
| 216 | ✗ | ImGui::EndPopup(); | |
| 217 | } | ||
| 218 | ✗ | ImGui::PopStyleVar(); | |
| 219 | |||
| 220 | // Scrolling | ||
| 221 | ✗ | if (ImGui::IsWindowHovered() && !ImGui::IsAnyItemActive() && ImGui::IsMouseDragging(ImGuiMouseButton_Middle, 0.0f)) | |
| 222 | ✗ | scrolling = scrolling + io.MouseDelta; | |
| 223 | |||
| 224 | ✗ | ImGui::PopItemWidth(); | |
| 225 | ✗ | ImGui::EndChild(); | |
| 226 | ✗ | ImGui::PopStyleColor(); | |
| 227 | ✗ | ImGui::PopStyleVar(); | |
| 228 | ✗ | ImGui::EndGroup(); | |
| 229 | |||
| 230 | ✗ | ImGui::End(); | |
| 231 | } | ||
| 232 |