imnodes.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #pragma once
  2. #include <stddef.h>
  3. typedef int ImNodesCol; // -> enum ImNodesCol_
  4. typedef int ImNodesStyleVar; // -> enum ImNodesStyleVar_
  5. typedef int ImNodesStyleFlags; // -> enum ImNodesStyleFlags_
  6. typedef int ImNodesPinShape; // -> enum ImNodesPinShape_
  7. typedef int ImNodesAttributeFlags; // -> enum ImNodesAttributeFlags_
  8. typedef int ImNodesMiniMapLocation; // -> enum ImNodesMiniMapLocation_
  9. enum ImNodesCol_
  10. {
  11. ImNodesCol_NodeBackground = 0,
  12. ImNodesCol_NodeBackgroundHovered,
  13. ImNodesCol_NodeBackgroundSelected,
  14. ImNodesCol_NodeOutline,
  15. ImNodesCol_TitleBar,
  16. ImNodesCol_TitleBarHovered,
  17. ImNodesCol_TitleBarSelected,
  18. ImNodesCol_Link,
  19. ImNodesCol_LinkHovered,
  20. ImNodesCol_LinkSelected,
  21. ImNodesCol_Pin,
  22. ImNodesCol_PinHovered,
  23. ImNodesCol_BoxSelector,
  24. ImNodesCol_BoxSelectorOutline,
  25. ImNodesCol_GridBackground,
  26. ImNodesCol_GridLine,
  27. ImNodesCol_MiniMapBackground,
  28. ImNodesCol_MiniMapBackgroundHovered,
  29. ImNodesCol_MiniMapOutline,
  30. ImNodesCol_MiniMapOutlineHovered,
  31. ImNodesCol_MiniMapNodeBackground,
  32. ImNodesCol_MiniMapNodeBackgroundHovered,
  33. ImNodesCol_MiniMapNodeBackgroundSelected,
  34. ImNodesCol_MiniMapNodeOutline,
  35. ImNodesCol_MiniMapLink,
  36. ImNodesCol_MiniMapLinkSelected,
  37. ImNodesCol_COUNT
  38. };
  39. enum ImNodesStyleVar_
  40. {
  41. ImNodesStyleVar_GridSpacing = 0,
  42. ImNodesStyleVar_NodeCornerRounding,
  43. ImNodesStyleVar_NodePaddingHorizontal,
  44. ImNodesStyleVar_NodePaddingVertical,
  45. ImNodesStyleVar_NodeBorderThickness,
  46. ImNodesStyleVar_LinkThickness,
  47. ImNodesStyleVar_LinkLineSegmentsPerLength,
  48. ImNodesStyleVar_LinkHoverDistance,
  49. ImNodesStyleVar_PinCircleRadius,
  50. ImNodesStyleVar_PinQuadSideLength,
  51. ImNodesStyleVar_PinTriangleSideLength,
  52. ImNodesStyleVar_PinLineThickness,
  53. ImNodesStyleVar_PinHoverRadius,
  54. ImNodesStyleVar_PinOffset
  55. };
  56. enum ImNodesStyleFlags_
  57. {
  58. ImNodesStyleFlags_None = 0,
  59. ImNodesStyleFlags_NodeOutline = 1 << 0,
  60. ImNodesStyleFlags_GridLines = 1 << 2
  61. };
  62. enum ImNodesPinShape_
  63. {
  64. ImNodesPinShape_Circle,
  65. ImNodesPinShape_CircleFilled,
  66. ImNodesPinShape_Triangle,
  67. ImNodesPinShape_TriangleFilled,
  68. ImNodesPinShape_Quad,
  69. ImNodesPinShape_QuadFilled
  70. };
  71. // This enum controls the way the attribute pins behave.
  72. enum ImNodesAttributeFlags_
  73. {
  74. ImNodesAttributeFlags_None = 0,
  75. // Allow detaching a link by left-clicking and dragging the link at a pin it is connected to.
  76. // NOTE: the user has to actually delete the link for this to work. A deleted link can be
  77. // detected by calling IsLinkDestroyed() after EndNodeEditor().
  78. ImNodesAttributeFlags_EnableLinkDetachWithDragClick = 1 << 0,
  79. // Visual snapping of an in progress link will trigger IsLink Created/Destroyed events. Allows
  80. // for previewing the creation of a link while dragging it across attributes. See here for demo:
  81. // https://github.com/Nelarius/imnodes/issues/41#issuecomment-647132113 NOTE: the user has to
  82. // actually delete the link for this to work. A deleted link can be detected by calling
  83. // IsLinkDestroyed() after EndNodeEditor().
  84. ImNodesAttributeFlags_EnableLinkCreationOnSnap = 1 << 1
  85. };
  86. struct ImNodesIO
  87. {
  88. struct EmulateThreeButtonMouse
  89. {
  90. EmulateThreeButtonMouse();
  91. // The keyboard modifier to use in combination with mouse left click to pan the editor view.
  92. // Set to NULL by default. To enable this feature, set the modifier to point to a boolean
  93. // indicating the state of a modifier. For example,
  94. //
  95. // ImNodes::GetIO().EmulateThreeButtonMouse.Modifier = &ImGui::GetIO().KeyAlt;
  96. const bool* Modifier;
  97. } EmulateThreeButtonMouse;
  98. struct LinkDetachWithModifierClick
  99. {
  100. LinkDetachWithModifierClick();
  101. // Pointer to a boolean value indicating when the desired modifier is pressed. Set to NULL
  102. // by default. To enable the feature, set the modifier to point to a boolean indicating the
  103. // state of a modifier. For example,
  104. //
  105. // ImNodes::GetIO().LinkDetachWithModifierClick.Modifier = &ImGui::GetIO().KeyCtrl;
  106. //
  107. // Left-clicking a link with this modifier pressed will detach that link. NOTE: the user has
  108. // to actually delete the link for this to work. A deleted link can be detected by calling
  109. // IsLinkDestroyed() after EndNodeEditor().
  110. const bool* Modifier;
  111. } LinkDetachWithModifierClick;
  112. // Holding alt mouse button pans the node area, by default middle mouse button will be used
  113. // Set based on ImGuiMouseButton values
  114. int AltMouseButton;
  115. ImNodesIO();
  116. };
  117. struct ImNodesStyle
  118. {
  119. float GridSpacing;
  120. float NodeCornerRounding;
  121. float NodePaddingHorizontal;
  122. float NodePaddingVertical;
  123. float NodeBorderThickness;
  124. float LinkThickness;
  125. float LinkLineSegmentsPerLength;
  126. float LinkHoverDistance;
  127. // The following variables control the look and behavior of the pins. The default size of each
  128. // pin shape is balanced to occupy approximately the same surface area on the screen.
  129. // The circle radius used when the pin shape is either ImNodesPinShape_Circle or
  130. // ImNodesPinShape_CircleFilled.
  131. float PinCircleRadius;
  132. // The quad side length used when the shape is either ImNodesPinShape_Quad or
  133. // ImNodesPinShape_QuadFilled.
  134. float PinQuadSideLength;
  135. // The equilateral triangle side length used when the pin shape is either
  136. // ImNodesPinShape_Triangle or ImNodesPinShape_TriangleFilled.
  137. float PinTriangleSideLength;
  138. // The thickness of the line used when the pin shape is not filled.
  139. float PinLineThickness;
  140. // The radius from the pin's center position inside of which it is detected as being hovered
  141. // over.
  142. float PinHoverRadius;
  143. // Offsets the pins' positions from the edge of the node to the outside of the node.
  144. float PinOffset;
  145. // By default, ImNodesStyleFlags_NodeOutline and ImNodesStyleFlags_Gridlines are enabled.
  146. ImNodesStyleFlags Flags;
  147. // Set these mid-frame using Push/PopColorStyle. You can index this color array with with a
  148. // ImNodesCol value.
  149. unsigned int Colors[ImNodesCol_COUNT];
  150. ImNodesStyle();
  151. };
  152. enum ImNodesMiniMapLocation_
  153. {
  154. ImNodesMiniMapLocation_BottomLeft,
  155. ImNodesMiniMapLocation_BottomRight,
  156. ImNodesMiniMapLocation_TopLeft,
  157. ImNodesMiniMapLocation_TopRight,
  158. };
  159. struct ImGuiContext;
  160. struct ImVec2;
  161. struct ImNodesContext;
  162. // An editor context corresponds to a set of nodes in a single workspace (created with a single
  163. // Begin/EndNodeEditor pair)
  164. //
  165. // By default, the library creates an editor context behind the scenes, so using any of the imnodes
  166. // functions doesn't require you to explicitly create a context.
  167. struct ImNodesEditorContext;
  168. // Callback type used to specify special behavior when hovering a node in the minimap
  169. typedef void (*ImNodesMiniMapNodeHoveringCallback)(int, void*);
  170. namespace ImNodes
  171. {
  172. // Call this function if you are compiling imnodes in to a dll, separate from ImGui. Calling this
  173. // function sets the GImGui global variable, which is not shared across dll boundaries.
  174. void SetImGuiContext(ImGuiContext* ctx);
  175. ImNodesContext* CreateContext();
  176. void DestroyContext(ImNodesContext* ctx = NULL); // NULL = destroy current context
  177. ImNodesContext* GetCurrentContext();
  178. void SetCurrentContext(ImNodesContext* ctx);
  179. ImNodesEditorContext* EditorContextCreate();
  180. void EditorContextFree(ImNodesEditorContext*);
  181. void EditorContextSet(ImNodesEditorContext*);
  182. ImVec2 EditorContextGetPanning();
  183. void EditorContextResetPanning(const ImVec2& pos);
  184. void EditorContextMoveToNode(const int node_id);
  185. ImNodesIO& GetIO();
  186. // Returns the global style struct. See the struct declaration for default values.
  187. ImNodesStyle& GetStyle();
  188. // Style presets matching the dear imgui styles of the same name.
  189. void StyleColorsDark(); // on by default
  190. void StyleColorsClassic();
  191. void StyleColorsLight();
  192. // The top-level function call. Call this before calling BeginNode/EndNode. Calling this function
  193. // will result the node editor grid workspace being rendered.
  194. void BeginNodeEditor();
  195. void EndNodeEditor();
  196. // Add a navigable minimap to the editor; call before EndNodeEditor after all
  197. // nodes and links have been specified
  198. void MiniMap(
  199. const float minimap_size_fraction = 0.2f,
  200. const ImNodesMiniMapLocation location = ImNodesMiniMapLocation_TopLeft,
  201. const ImNodesMiniMapNodeHoveringCallback node_hovering_callback = NULL,
  202. void* node_hovering_callback_data = NULL);
  203. // Use PushColorStyle and PopColorStyle to modify ImNodesStyle::Colors mid-frame.
  204. void PushColorStyle(ImNodesCol item, unsigned int color);
  205. void PopColorStyle();
  206. void PushStyleVar(ImNodesStyleVar style_item, float value);
  207. void PopStyleVar();
  208. // id can be any positive or negative integer, but INT_MIN is currently reserved for internal use.
  209. void BeginNode(int id);
  210. void EndNode();
  211. ImVec2 GetNodeDimensions(int id);
  212. // Place your node title bar content (such as the node title, using ImGui::Text) between the
  213. // following function calls. These functions have to be called before adding any attributes, or the
  214. // layout of the node will be incorrect.
  215. void BeginNodeTitleBar();
  216. void EndNodeTitleBar();
  217. // Attributes are ImGui UI elements embedded within the node. Attributes can have pin shapes
  218. // rendered next to them. Links are created between pins.
  219. //
  220. // The activity status of an attribute can be checked via the IsAttributeActive() and
  221. // IsAnyAttributeActive() function calls. This is one easy way of checking for any changes made to
  222. // an attribute's drag float UI, for instance.
  223. //
  224. // Each attribute id must be unique.
  225. // Create an input attribute block. The pin is rendered on left side.
  226. void BeginInputAttribute(int id, ImNodesPinShape shape = ImNodesPinShape_CircleFilled);
  227. void EndInputAttribute();
  228. // Create an output attribute block. The pin is rendered on the right side.
  229. void BeginOutputAttribute(int id, ImNodesPinShape shape = ImNodesPinShape_CircleFilled);
  230. void EndOutputAttribute();
  231. // Create a static attribute block. A static attribute has no pin, and therefore can't be linked to
  232. // anything. However, you can still use IsAttributeActive() and IsAnyAttributeActive() to check for
  233. // attribute activity.
  234. void BeginStaticAttribute(int id);
  235. void EndStaticAttribute();
  236. // Push a single AttributeFlags value. By default, only AttributeFlags_None is set.
  237. void PushAttributeFlag(ImNodesAttributeFlags flag);
  238. void PopAttributeFlag();
  239. // Render a link between attributes.
  240. // The attributes ids used here must match the ids used in Begin(Input|Output)Attribute function
  241. // calls. The order of start_attr and end_attr doesn't make a difference for rendering the link.
  242. void Link(int id, int start_attribute_id, int end_attribute_id);
  243. // Enable or disable the ability to click and drag a specific node.
  244. void SetNodeDraggable(int node_id, const bool draggable);
  245. // The node's position can be expressed in three coordinate systems:
  246. // * screen space coordinates, -- the origin is the upper left corner of the window.
  247. // * editor space coordinates -- the origin is the upper left corner of the node editor window
  248. // * grid space coordinates, -- the origin is the upper left corner of the node editor window,
  249. // translated by the current editor panning vector (see EditorContextGetPanning() and
  250. // EditorContextResetPanning())
  251. // Use the following functions to get and set the node's coordinates in these coordinate systems.
  252. void SetNodeScreenSpacePos(int node_id, const ImVec2& screen_space_pos);
  253. void SetNodeEditorSpacePos(int node_id, const ImVec2& editor_space_pos);
  254. void SetNodeGridSpacePos(int node_id, const ImVec2& grid_pos);
  255. ImVec2 GetNodeScreenSpacePos(const int node_id);
  256. ImVec2 GetNodeEditorSpacePos(const int node_id);
  257. ImVec2 GetNodeGridSpacePos(const int node_id);
  258. // Returns true if the current node editor canvas is being hovered over by the mouse, and is not
  259. // blocked by any other windows.
  260. bool IsEditorHovered();
  261. // The following functions return true if a UI element is being hovered over by the mouse cursor.
  262. // Assigns the id of the UI element being hovered over to the function argument. Use these functions
  263. // after EndNodeEditor() has been called.
  264. bool IsNodeHovered(int* node_id);
  265. bool IsLinkHovered(int* link_id);
  266. bool IsPinHovered(int* attribute_id);
  267. // Use The following two functions to query the number of selected nodes or links in the current
  268. // editor. Use after calling EndNodeEditor().
  269. int NumSelectedNodes();
  270. int NumSelectedLinks();
  271. // Get the selected node/link ids. The pointer argument should point to an integer array with at
  272. // least as many elements as the respective NumSelectedNodes/NumSelectedLinks function call
  273. // returned.
  274. void GetSelectedNodes(int* node_ids);
  275. void GetSelectedLinks(int* link_ids);
  276. // Clears the list of selected nodes/links. Useful if you want to delete a selected node or link.
  277. void ClearNodeSelection();
  278. void ClearLinkSelection();
  279. // Use the following functions to add or remove individual nodes or links from the current editors
  280. // selection. Note that all functions require the id to be an existing valid id for this editor.
  281. // Select-functions has the precondition that the object is currently considered unselected.
  282. // Clear-functions has the precondition that the object is currently considered selected.
  283. // Preconditions listed above can be checked via IsNodeSelected/IsLinkSelected if not already
  284. // known.
  285. void SelectNode(int node_id);
  286. void ClearNodeSelection(int node_id);
  287. bool IsNodeSelected(int node_id);
  288. void SelectLink(int link_id);
  289. void ClearLinkSelection(int link_id);
  290. bool IsLinkSelected(int link_id);
  291. // Was the previous attribute active? This will continuously return true while the left mouse button
  292. // is being pressed over the UI content of the attribute.
  293. bool IsAttributeActive();
  294. // Was any attribute active? If so, sets the active attribute id to the output function argument.
  295. bool IsAnyAttributeActive(int* attribute_id = NULL);
  296. // Use the following functions to query a change of state for an existing link, or new link. Call
  297. // these after EndNodeEditor().
  298. // Did the user start dragging a new link from a pin?
  299. bool IsLinkStarted(int* started_at_attribute_id);
  300. // Did the user drop the dragged link before attaching it to a pin?
  301. // There are two different kinds of situations to consider when handling this event:
  302. // 1) a link which is created at a pin and then dropped
  303. // 2) an existing link which is detached from a pin and then dropped
  304. // Use the including_detached_links flag to control whether this function triggers when the user
  305. // detaches a link and drops it.
  306. bool IsLinkDropped(int* started_at_attribute_id = NULL, bool including_detached_links = true);
  307. // Did the user finish creating a new link?
  308. bool IsLinkCreated(
  309. int* started_at_attribute_id,
  310. int* ended_at_attribute_id,
  311. bool* created_from_snap = NULL);
  312. bool IsLinkCreated(
  313. int* started_at_node_id,
  314. int* started_at_attribute_id,
  315. int* ended_at_node_id,
  316. int* ended_at_attribute_id,
  317. bool* created_from_snap = NULL);
  318. // Was an existing link detached from a pin by the user? The detached link's id is assigned to the
  319. // output argument link_id.
  320. bool IsLinkDestroyed(int* link_id);
  321. // Use the following functions to write the editor context's state to a string, or directly to a
  322. // file. The editor context is serialized in the INI file format.
  323. const char* SaveCurrentEditorStateToIniString(size_t* data_size = NULL);
  324. const char* SaveEditorStateToIniString(
  325. const ImNodesEditorContext* editor,
  326. size_t* data_size = NULL);
  327. void LoadCurrentEditorStateFromIniString(const char* data, size_t data_size);
  328. void LoadEditorStateFromIniString(ImNodesEditorContext* editor, const char* data, size_t data_size);
  329. void SaveCurrentEditorStateToIniFile(const char* file_name);
  330. void SaveEditorStateToIniFile(const ImNodesEditorContext* editor, const char* file_name);
  331. void LoadCurrentEditorStateFromIniFile(const char* file_name);
  332. void LoadEditorStateFromIniFile(ImNodesEditorContext* editor, const char* file_name);
  333. } // namespace ImNodes