[Example] Migrate example to the new module
This commit is contained in:
1 parent
46f74381aa
commit
cf9e74b1d0
17 files changed
+489
-822
No files matched your search
@@ -0,0 +1,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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
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/5959729410";
|
||||
|
||||
private static final ImInt LINK_A = new ImInt();
|
||||
private static final ImInt LINK_B = new ImInt();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import imgui.ImGui;
|
||||
import imgui.app.Application;
|
||||
import imgui.type.ImBoolean;
|
||||
|
||||
public class Extra {
|
||||
private static final ImBoolean SHOW_DEMO_WINDOW = new ImBoolean(false);
|
||||
private static final ImBoolean SHOW_IMNODES_DEMO_WINDOW = new ImBoolean(false);
|
||||
private static final ImBoolean SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW = new ImBoolean(false);
|
||||
|
||||
private static final Graph GRAPH = new Graph();
|
||||
|
||||
public static void show(final Application app) {
|
||||
ImGui.colorEdit3("Background Color", app.getColorBg().getData());
|
||||
ImGui.checkbox("Show Demo Window", SHOW_DEMO_WINDOW);
|
||||
ImGui.checkbox("Show ImNodes Demo Window", SHOW_IMNODES_DEMO_WINDOW);
|
||||
ImGui.checkbox("Show imgui-node-editor Demo Window", SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW);
|
||||
|
||||
if (SHOW_DEMO_WINDOW.get()) {
|
||||
ImGui.showDemoWindow(SHOW_DEMO_WINDOW);
|
||||
}
|
||||
|
||||
if (SHOW_IMNODES_DEMO_WINDOW.get()) {
|
||||
ExampleImNodes.show(SHOW_IMNODES_DEMO_WINDOW, GRAPH);
|
||||
}
|
||||
|
||||
if (SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW.get()) {
|
||||
ExampleImGuiNodeEditor.show(SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW, GRAPH);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class Graph {
|
||||
public int nextNodeId = 1;
|
||||
public int nextPinId = 100;
|
||||
|
||||
public final Map<Integer, GraphNode> nodes = new HashMap<>();
|
||||
|
||||
public Graph() {
|
||||
final GraphNode first = createGraphNode();
|
||||
final GraphNode second = createGraphNode();
|
||||
first.outputNodeId = second.nodeId;
|
||||
}
|
||||
|
||||
public GraphNode createGraphNode() {
|
||||
final GraphNode node = new GraphNode(nextNodeId++, nextPinId++, nextPinId++);
|
||||
this.nodes.put(node.nodeId, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
public GraphNode findByInput(final long inputPinId) {
|
||||
for (GraphNode node : nodes.values()) {
|
||||
if (node.getInputPinId() == inputPinId) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public GraphNode findByOutput(final long outputPinId) {
|
||||
for (GraphNode node : nodes.values()) {
|
||||
if (node.getOutputPinId() == outputPinId) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static final class GraphNode {
|
||||
public final int nodeId;
|
||||
public final int inputPinId;
|
||||
public final int outputPinId;
|
||||
|
||||
public int outputNodeId = -1;
|
||||
|
||||
public GraphNode(final int nodeId, final int inputPinId, final int outputPintId) {
|
||||
this.nodeId = nodeId;
|
||||
this.inputPinId = inputPinId;
|
||||
this.outputPinId = outputPintId;
|
||||
}
|
||||
|
||||
public int getInputPinId() {
|
||||
return inputPinId;
|
||||
}
|
||||
|
||||
public int getOutputPinId() {
|
||||
return outputPinId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "Node " + (char) (64 + nodeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,16 @@ import imgui.ImGui;
|
||||
import imgui.ImGuiIO;
|
||||
import imgui.app.Application;
|
||||
import imgui.app.Configuration;
|
||||
import imgui.extension.imnodes.ImNodes;
|
||||
import imgui.flag.ImGuiConfigFlags;
|
||||
import imgui.flag.ImGuiInputTextFlags;
|
||||
import imgui.type.ImString;
|
||||
|
||||
public class Main extends Application {
|
||||
private final ImString str = new ImString(5);
|
||||
private final float[] flt = new float[1];
|
||||
private int count = 0;
|
||||
|
||||
public final class Main extends Application {
|
||||
@Override
|
||||
protected void configure(final Configuration config) {
|
||||
config.setTitle("Example Application");
|
||||
@@ -20,11 +27,29 @@ public final class Main extends Application {
|
||||
io.addConfigFlags(ImGuiConfigFlags.DockingEnable); // Enable Docking
|
||||
io.addConfigFlags(ImGuiConfigFlags.ViewportsEnable); // Enable Multi-Viewport / Platform Windows
|
||||
io.setConfigViewportsNoTaskBarIcon(true);
|
||||
|
||||
ImNodes.initialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disposeImGui() {
|
||||
ImNodes.shutdown();
|
||||
super.disposeImGui();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
ImGui.text("Hello, World");
|
||||
ImGui.text("Hello, World!");
|
||||
if (ImGui.button("Save")) {
|
||||
count++;
|
||||
}
|
||||
ImGui.sameLine();
|
||||
ImGui.text(String.valueOf(count));
|
||||
ImGui.inputText("string", str, ImGuiInputTextFlags.CallbackResize);
|
||||
ImGui.sliderFloat("float", flt, 0, 1);
|
||||
ImGui.separator();
|
||||
ImGui.text("Extra");
|
||||
Extra.show(this);
|
||||
}
|
||||
|
||||
public static void main(final String[] args) {
|
||||
|
||||
Reference in new issue
Block a user