-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
ExampleImGuiNodeEditor.java
124 lines (103 loc) · 4.74 KB
/
ExampleImGuiNodeEditor.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
116
117
118
119
120
121
122
123
124
import imgui.extension.nodeditor.NodeEditor;
import imgui.extension.nodeditor.NodeEditorConfig;
import imgui.extension.nodeditor.NodeEditorContext;
import imgui.extension.nodeditor.flag.NodeEditorPinKind;
import imgui.flag.ImGuiCond;
import imgui.internal.ImGui;
import imgui.type.ImBoolean;
import imgui.type.ImLong;
import java.awt.Desktop;
import java.net.URI;
public class ExampleImGuiNodeEditor {
private static final NodeEditorContext CONTEXT;
private static final String URL = "https://github.com/thedmd/imgui-node-editor/tree/687a72f940";
static {
NodeEditorConfig config = new NodeEditorConfig();
config.setSettingsFile(null);
CONTEXT = new NodeEditorContext(config);
}
public static void show(final ImBoolean showImNodeEditorWindow, final Graph graph) {
ImGui.setNextWindowSize(500, 400, ImGuiCond.Once);
ImGui.setNextWindowPos(ImGui.getMainViewport().getPosX() + 100, ImGui.getMainViewport().getPosY() + 200, ImGuiCond.Once);
if (ImGui.begin("imgui-node-editor Demo", showImNodeEditorWindow)) {
ImGui.text("This a demo graph editor for imgui-node-editor");
ImGui.alignTextToFramePadding();
ImGui.text("Repo:");
ImGui.sameLine();
if (ImGui.button(URL)) {
try {
Desktop.getDesktop().browse(new URI(URL));
} catch (Exception e) {
e.printStackTrace();
}
}
if (ImGui.button("Navigate to content")) {
NodeEditor.navigateToContent(1);
}
NodeEditor.setCurrentEditor(CONTEXT);
NodeEditor.begin("Node Editor");
for (Graph.GraphNode node : graph.nodes.values()) {
NodeEditor.beginNode(node.nodeId);
ImGui.text(node.getName());
NodeEditor.beginPin(node.getInputPinId(), NodeEditorPinKind.Input);
ImGui.text("-> In");
NodeEditor.endPin();
ImGui.sameLine();
NodeEditor.beginPin(node.getOutputPinId(), NodeEditorPinKind.Output);
ImGui.text("Out ->");
NodeEditor.endPin();
NodeEditor.endNode();
}
if (NodeEditor.beginCreate()) {
final ImLong a = new ImLong();
final ImLong b = new ImLong();
if (NodeEditor.queryNewLink(a, b)) {
final Graph.GraphNode source = graph.findByOutput(a.get());
final Graph.GraphNode target = graph.findByInput(b.get());
if (source != null && target != null && source.outputNodeId != target.nodeId && NodeEditor.acceptNewItem()) {
source.outputNodeId = target.nodeId;
}
}
}
NodeEditor.endCreate();
int uniqueLinkId = 1;
for (Graph.GraphNode node : graph.nodes.values()) {
if (graph.nodes.containsKey(node.outputNodeId)) {
NodeEditor.link(uniqueLinkId++, node.getOutputPinId(), graph.nodes.get(node.outputNodeId).getInputPinId());
}
}
NodeEditor.suspend();
final long nodeWithContextMenu = NodeEditor.getNodeWithContextMenu();
if (nodeWithContextMenu != -1) {
ImGui.openPopup("node_context");
ImGui.getStateStorage().setInt(ImGui.getID("delete_node_id"), (int) nodeWithContextMenu);
}
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 (NodeEditor.showBackgroundContextMenu()) {
ImGui.openPopup("node_editor_context");
}
if (ImGui.beginPopup("node_editor_context")) {
if (ImGui.button("Create New Node")) {
final Graph.GraphNode node = graph.createGraphNode();
final float canvasX = NodeEditor.toCanvasX(ImGui.getMousePosX());
final float canvasY = NodeEditor.toCanvasY(ImGui.getMousePosY());
NodeEditor.setNodePosition(node.nodeId, canvasX, canvasY);
ImGui.closeCurrentPopup();
}
ImGui.endPopup();
}
NodeEditor.resume();
NodeEditor.end();
}
ImGui.end();
}
}