Advertisement
BlueBear

ImGuiNodeGraph.cpp

Feb 4th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.50 KB | None | 0 0
  1. ///////////////////////////////HEADER///////////////////////////////////////
  2. #pragma once
  3.  
  4. #include <math.h>
  5. #include "imgui/imgui.h"
  6. #include "imgui/imgui_impl_win32.h"
  7. #include "imgui/imgui_impl_dx12.h"
  8.  
  9. static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
  10. static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
  11. static inline float Distance(ImVec2& a, ImVec2& b) { return sqrtf((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); }
  12.  
  13. // Dummy
  14. struct Node
  15. {
  16.     int     ID;
  17.     char    Name[32];
  18.     ImVec2  Pos, Size;
  19.     float   Value;
  20.     ImVec4  Color;
  21.     int     InputsCount, OutputsCount;
  22.  
  23.     Node(int id, const char* name, const ImVec2& pos, float value, const ImVec4& color, int inputs_count, int outputs_count) { ID = id; strncpy(Name, name, 31); Name[31] = 0; Pos = pos; Value = value; Color = color; InputsCount = inputs_count; OutputsCount = outputs_count; }
  24.  
  25.     ImVec2 GetInputSlotPos(int slot_no) const { return ImVec2(Pos.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)InputsCount + 1)); }
  26.     ImVec2 GetOutputSlotPos(int slot_no) const { return ImVec2(Pos.x + Size.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)OutputsCount + 1)); }
  27. };
  28. struct NodeLink
  29. {
  30.     int     InputIdx, InputSlot, OutputIdx, OutputSlot;
  31.  
  32.     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; }
  33. };
  34.  
  35. void NodeGraph();
  36.  
  37. ////////////////////////////////CODE///////////////////////////////////////
  38. #include "NodeGraph.hpp"
  39.  
  40. #include <Windows.h>
  41.  
  42.  
  43. void NodeGraph()
  44. {
  45.     static bool opened = true;
  46.     ImGui::SetNextWindowSize(ImVec2(700, 600), ImGuiSetCond_FirstUseEver);
  47.     if (!ImGui::Begin("Example: Custom Node Graph"))
  48.     {
  49.         ImGui::End();
  50.         return;
  51.     }
  52.  
  53.     static ImVector<Node> nodes;
  54.     static ImVector<NodeLink> links;
  55.     static bool inited = false;
  56.     static ImVec2 scrolling = ImVec2(0.0f, 0.0f);
  57.     static bool show_grid = true;
  58.     static int node_selected = -1;
  59.     if (!inited)
  60.     {
  61.         nodes.push_back(Node(0, "MainTex", ImVec2(40, 50), 0.5f, ImColor(255, 100, 100), 1, 3));
  62.         nodes.push_back(Node(1, "BumpMap", ImVec2(40, 150), 0.42f, ImColor(200, 100, 200), 1, 1));
  63.         nodes.push_back(Node(2, "Combine", ImVec2(270, 80), 1.0f, ImColor(0, 200, 100), 2, 2));
  64.         links.push_back(NodeLink(0, 0, 2, 0));
  65.         links.push_back(NodeLink(1, 0, 2, 1));
  66.         inited = true;
  67.     }
  68.  
  69.     // Draw a list of nodes on the left side
  70.     bool open_context_menu = false;
  71.     int node_hovered_in_list = -1;
  72.     int node_hovered_in_scene = -1;
  73.     ImGui::BeginChild("node_list", ImVec2(100, 0));
  74.     ImGui::Text("Nodes");
  75.     ImGui::Separator();
  76.     for (int node_idx = 0; node_idx < nodes.Size; node_idx++)
  77.     {
  78.         Node* node = &nodes[node_idx];
  79.         ImGui::PushID(node->ID);
  80.         if (ImGui::Selectable(node->Name, node->ID == node_selected))
  81.             node_selected = node->ID;
  82.         if (ImGui::IsItemHovered())
  83.         {
  84.             node_hovered_in_list = node->ID;
  85.             open_context_menu |= ImGui::IsMouseClicked(1);
  86.         }
  87.         ImGui::PopID();
  88.     }
  89.     ImGui::EndChild();
  90.  
  91.     ImGui::SameLine();
  92.     ImGui::BeginGroup();
  93.  
  94.     const float NODE_SLOT_RADIUS = 4.0f;
  95.     const ImVec2 NODE_WINDOW_PADDING(8.0f, 8.0f);
  96.  
  97.     // Create our child canvas
  98.     ImGui::Text("Hold middle mouse button to scroll (%.2f,%.2f)", scrolling.x, scrolling.y);
  99.     ImGui::SameLine(ImGui::GetWindowWidth() - 100);
  100.     ImGui::Checkbox("Show grid", &show_grid);
  101.     ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(1, 1));
  102.     ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
  103.     ImGui::PushStyleColor(ImGuiCol_ChildWindowBg, IM_COL32(60, 60, 70, 200));
  104.     ImGui::BeginChild("scrolling_region", ImVec2(0, 0), true, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoMove);
  105.     ImGui::PushItemWidth(120.0f);
  106.  
  107.     ImVec2 offset = ImGui::GetCursorScreenPos() + scrolling;
  108.     ImDrawList* draw_list = ImGui::GetWindowDrawList();
  109.     // Display grid
  110.     if (show_grid)
  111.     {
  112.         ImU32 GRID_COLOR = IM_COL32(200, 200, 200, 40);
  113.         float GRID_SZ = 64.0f;
  114.         ImVec2 win_pos = ImGui::GetCursorScreenPos();
  115.         ImVec2 canvas_sz = ImGui::GetWindowSize();
  116.         for (float x = fmodf(scrolling.x, GRID_SZ); x < canvas_sz.x; x += GRID_SZ)
  117.             draw_list->AddLine(ImVec2(x, 0.0f) + win_pos, ImVec2(x, canvas_sz.y) + win_pos, GRID_COLOR);
  118.         for (float y = fmodf(scrolling.y, GRID_SZ); y < canvas_sz.y; y += GRID_SZ)
  119.             draw_list->AddLine(ImVec2(0.0f, y) + win_pos, ImVec2(canvas_sz.x, y) + win_pos, GRID_COLOR);
  120.     }
  121.  
  122.     // Display links
  123.     draw_list->ChannelsSplit(2);
  124.     draw_list->ChannelsSetCurrent(0); // Background
  125.     for (int link_idx = 0; link_idx < links.Size; link_idx++)
  126.     {
  127.         NodeLink* link = &links[link_idx];
  128.         Node* node_inp = &nodes[link->InputIdx];
  129.         Node* node_out = &nodes[link->OutputIdx];
  130.         ImVec2 p1 = offset + node_inp->GetOutputSlotPos(link->InputSlot);
  131.         ImVec2 p2 = offset + node_out->GetInputSlotPos(link->OutputSlot);
  132.         float dist = Distance(p1, p2) * 0.5f;
  133.         draw_list->AddBezierCurve(p1, p1 + ImVec2(+dist, 0), p2 + ImVec2(-dist, 0), p2, IM_COL32(200, 200, 100, 255), 3.0f);
  134.     }
  135.  
  136.     //drag new link
  137.     draw_list->ChannelsSetCurrent(1);
  138.     if (ImGui::IsItemActive())
  139.     {
  140.         ImGuiIO& io = ImGui::GetIO();
  141.         NodeLink* link = nullptr;
  142.         for (int idx = 0; idx < nodes.Size; idx++)
  143.         {
  144.             //
  145.             Node* node_inp = &nodes[idx];
  146.             for (int linkidx = 0; linkidx < node_inp->OutputsCount; linkidx++)
  147.             {
  148.                 ImVec2 p1 = offset + node_inp->GetOutputSlotPos(linkidx);
  149.                 if (Distance(io.MouseClickedPos[0], p1) < NODE_SLOT_RADIUS * 2)
  150.                 {
  151.                     float dist = Distance(io.MouseClickedPos[0], io.MousePos) * 0.5f;
  152.                     ImGui::Text("Hold middle mouse button to scroll (%.2f,%.2f) is mouse released: %s", io.MousePos.x, io.MousePos.y, io.MouseReleased[0] ? "true" : "false");
  153.                     draw_list->AddBezierCurve(p1, p1 + ImVec2(+dist, 0), io.MousePos + ImVec2(-dist, 0), io.MousePos, IM_COL32(200, 200, 100, 255), 3.0f);
  154.                 }
  155.             }
  156.         }
  157.         draw_list->ChannelsSetCurrent(0);
  158.         // Draw a line between the button and the mouse cursor
  159.         //draw_list->PushClipRectFullScreen();
  160.        
  161.         //draw_list->PopClipRect();
  162.  
  163.         // Drag operations gets "unlocked" when the mouse has moved past a certain threshold (the default threshold is stored in io.MouseDragThreshold)
  164.         // You can request a lower or higher threshold using the second parameter of IsMouseDragging() and GetMouseDragDelta()
  165.         //ImVec2 value_raw = ImGui::GetMouseDragDelta(0, 0.0f);
  166.         //ImVec2 value_with_lock_threshold = ImGui::GetMouseDragDelta(0);
  167.         //ImVec2 mouse_delta = io.MouseDelta;
  168.         //ImGui::SameLine(); ImGui::Text("Raw (%.1f, %.1f), WithLockThresold (%.1f, %.1f), MouseDelta (%.1f, %.1f)", value_raw.x, value_raw.y, value_with_lock_threshold.x, value_with_lock_threshold.y, mouse_delta.x, mouse_delta.y);
  169.     }
  170.  
  171.     // Display nodes
  172.     for (int node_idx = 0; node_idx < nodes.Size; node_idx++)
  173.     {
  174.         Node* node = &nodes[node_idx];
  175.         ImGui::PushID(node->ID);
  176.         ImVec2 node_rect_min = offset + node->Pos;
  177.  
  178.         // Display node contents first
  179.         draw_list->ChannelsSetCurrent(1); // Foreground
  180.         bool old_any_active = ImGui::IsAnyItemActive();
  181.         ImGui::SetCursorScreenPos(node_rect_min + NODE_WINDOW_PADDING);
  182.         ImGui::BeginGroup(); // Lock horizontal position
  183.         ImGui::Text("%s", node->Name);
  184.         ImGui::SliderFloat("##value", &node->Value, 0.0f, 1.0f, "Alpha %.2f");
  185.         ImGui::ColorEdit3("##color", &node->Color.x);
  186.         ImGui::EndGroup();
  187.  
  188.         // Save the size of what we have emitted and whether any of the widgets are being used
  189.         bool node_widgets_active = (!old_any_active && ImGui::IsAnyItemActive());
  190.         node->Size = ImGui::GetItemRectSize() + NODE_WINDOW_PADDING + NODE_WINDOW_PADDING;
  191.         ImVec2 node_rect_max = node_rect_min + node->Size;
  192.  
  193.         // Display node box
  194.         draw_list->ChannelsSetCurrent(0); // Background
  195.         ImGui::SetCursorScreenPos(node_rect_min);
  196.         ImGui::InvisibleButton("node", node->Size - ImVec2(10.0f, 10.0f));
  197.         if (ImGui::IsItemHovered())
  198.         {
  199.             node_hovered_in_scene = node->ID;
  200.             open_context_menu |= ImGui::IsMouseClicked(1);
  201.         }
  202.         bool node_moving_active = ImGui::IsItemActive();
  203.         if (node_widgets_active || node_moving_active)
  204.             node_selected = node->ID;
  205.         if (node_moving_active && ImGui::IsMouseDragging(0))
  206.             node->Pos = node->Pos + ImGui::GetIO().MouseDelta;
  207.  
  208.         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);
  209.         draw_list->AddRectFilled(node_rect_min, node_rect_max, node_bg_color, 4.0f);
  210.         draw_list->AddRect(node_rect_min, node_rect_max, IM_COL32(100, 100, 100, 255), 4.0f);
  211.         for (int slot_idx = 0; slot_idx < node->InputsCount; slot_idx++)
  212.         {
  213.             draw_list->AddCircleFilled(offset + node->GetInputSlotPos(slot_idx), NODE_SLOT_RADIUS, IM_COL32(150, 150, 150, 150));
  214.         }
  215.         for (int slot_idx = 0; slot_idx < node->OutputsCount; slot_idx++)
  216.         {
  217.             draw_list->AddCircleFilled(offset + node->GetOutputSlotPos(slot_idx), NODE_SLOT_RADIUS, IM_COL32(150, 150, 150, 150));
  218.         }
  219.         ImGui::PopID();
  220.     }
  221.     draw_list->ChannelsMerge();
  222.  
  223.     // Open context menu
  224.     if (!ImGui::IsAnyItemHovered() && ImGui::IsMouseHoveringWindow() && ImGui::IsMouseClicked(1))
  225.     {
  226.         node_selected = node_hovered_in_list = node_hovered_in_scene = -1;
  227.         open_context_menu = true;
  228.     }
  229.     if (open_context_menu)
  230.     {
  231.         ImGui::OpenPopup("context_menu");
  232.         if (node_hovered_in_list != -1)
  233.             node_selected = node_hovered_in_list;
  234.         if (node_hovered_in_scene != -1)
  235.             node_selected = node_hovered_in_scene;
  236.     }
  237.  
  238.     // Draw context menu
  239.     ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8, 8));
  240.     if (ImGui::BeginPopup("context_menu"))
  241.     {
  242.         Node* node = node_selected != -1 ? &nodes[node_selected] : NULL;
  243.         ImVec2 scene_pos = ImGui::GetMousePosOnOpeningCurrentPopup() - offset;
  244.         if (node)
  245.         {
  246.             ImGui::Text("Node '%s'", node->Name);
  247.             ImGui::Separator();
  248.             if (ImGui::MenuItem("Rename..", NULL, false, false)) {}
  249.             if (ImGui::MenuItem("Delete", NULL, false, false)) {}
  250.             if (ImGui::MenuItem("Copy", NULL, false, false)) {}
  251.         }
  252.         else
  253.         {
  254.             if (ImGui::MenuItem("Add"))
  255.             {
  256.                 nodes.push_back(Node(nodes.Size, "New node", scene_pos, 0.5f, ImColor(100, 100, 200), 2, 2));
  257.             }
  258.             if (ImGui::MenuItem("Paste", NULL, false, false)) {}
  259.         }
  260.         ImGui::EndPopup();
  261.     }
  262.     ImGui::PopStyleVar();
  263.  
  264.     // Scrolling
  265.     if (ImGui::IsWindowHovered() && !ImGui::IsAnyItemActive() && ImGui::IsMouseDragging(2, 0.0f))
  266.         scrolling = scrolling + ImGui::GetIO().MouseDelta;
  267.  
  268.     ImGui::PopItemWidth();
  269.     ImGui::EndChild();
  270.     ImGui::PopStyleColor();
  271.     ImGui::PopStyleVar(2);
  272.     ImGui::EndGroup();
  273.  
  274.  
  275.     ImGui::End();
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement