-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathExampleImNodes.java
115 lines (95 loc) · 4.32 KB
/
ExampleImNodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import imgui.extension.imnodes.ImNodes;
import imgui.extension.imnodes.ImNodesContext;
import imgui.extension.imnodes.flag.ImNodesPinShape;
import imgui.flag.ImGuiCond;
import imgui.flag.ImGuiMouseButton;
import imgui.internal.ImGui;
import imgui.type.ImBoolean;
import imgui.type.ImInt;
import java.awt.Desktop;
import java.net.URI;
public class ExampleImNodes {
private static final ImNodesContext CONTEXT = new ImNodesContext();
private static final String URL = "https://github.com/Nelarius/imnodes/tree/857cc86";
private static final ImInt LINK_A = new ImInt();
private static final ImInt LINK_B = new ImInt();
static {
ImNodes.createContext();
}
public static void show(final ImBoolean showImNodesWindow, final Graph graph) {
ImGui.setNextWindowSize(500, 400, ImGuiCond.Once);
ImGui.setNextWindowPos(ImGui.getMainViewport().getPosX() + 100, ImGui.getMainViewport().getPosY() + 100, ImGuiCond.Once);
if (ImGui.begin("ImNodes Demo", showImNodesWindow)) {
ImGui.text("This a demo graph editor for ImNodes");
ImGui.alignTextToFramePadding();
ImGui.text("Repo:");
ImGui.sameLine();
if (ImGui.button(URL)) {
try {
Desktop.getDesktop().browse(new URI(URL));
} catch (Exception e) {
e.printStackTrace();
}
}
ImNodes.editorContextSet(CONTEXT);
ImNodes.beginNodeEditor();
for (Graph.GraphNode node : graph.nodes.values()) {
ImNodes.beginNode(node.nodeId);
ImNodes.beginNodeTitleBar();
ImGui.text(node.getName());
ImNodes.endNodeTitleBar();
ImNodes.beginInputAttribute(node.getInputPinId(), ImNodesPinShape.CircleFilled);
ImGui.text("In");
ImNodes.endInputAttribute();
ImGui.sameLine();
ImNodes.beginOutputAttribute(node.getOutputPinId());
ImGui.text("Out");
ImNodes.endOutputAttribute();
ImNodes.endNode();
}
int uniqueLinkId = 1;
for (Graph.GraphNode node : graph.nodes.values()) {
if (graph.nodes.containsKey(node.outputNodeId)) {
ImNodes.link(uniqueLinkId++, node.getOutputPinId(), graph.nodes.get(node.outputNodeId).getInputPinId());
}
}
final boolean isEditorHovered = ImNodes.isEditorHovered();
ImNodes.endNodeEditor();
if (ImNodes.isLinkCreated(LINK_A, LINK_B)) {
final Graph.GraphNode source = graph.findByOutput(LINK_A.get());
final Graph.GraphNode target = graph.findByInput(LINK_B.get());
if (source != null && target != null && source.outputNodeId != target.nodeId) {
source.outputNodeId = target.nodeId;
}
}
if (ImGui.isMouseClicked(ImGuiMouseButton.Right)) {
final int hoveredNode = ImNodes.getHoveredNode();
if (hoveredNode != -1) {
ImGui.openPopup("node_context");
ImGui.getStateStorage().setInt(ImGui.getID("delete_node_id"), hoveredNode);
} else if (isEditorHovered) {
ImGui.openPopup("node_editor_context");
}
}
if (ImGui.isPopupOpen("node_context")) {
final int targetNode = ImGui.getStateStorage().getInt(ImGui.getID("delete_node_id"));
if (ImGui.beginPopup("node_context")) {
if (ImGui.button("Delete " + graph.nodes.get(targetNode).getName())) {
graph.nodes.remove(targetNode);
ImGui.closeCurrentPopup();
}
ImGui.endPopup();
}
}
if (ImGui.beginPopup("node_editor_context")) {
if (ImGui.button("Create New Node")) {
final Graph.GraphNode node = graph.createGraphNode();
ImNodes.setNodeScreenSpacePos(node.nodeId, ImGui.getMousePosX(), ImGui.getMousePosY());
ImGui.closeCurrentPopup();
}
ImGui.endPopup();
}
}
ImGui.end();
}
}