/
GraphWindow.java
1149 lines (991 loc) · 51.9 KB
/
GraphWindow.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package visual.scripting;
import imgui.ImColor;
import imgui.ImDrawList;
import imgui.ImVec2;
import imgui.extension.nodeditor.NodeEditor;
import imgui.extension.nodeditor.NodeEditorConfig;
import imgui.extension.nodeditor.NodeEditorContext;
import imgui.extension.nodeditor.flag.NodeEditorPinKind;
import imgui.extension.nodeditor.flag.NodeEditorStyleColor;
import imgui.extension.nodeditor.flag.NodeEditorStyleVar;
import imgui.extension.texteditor.TextEditor;
import imgui.flag.*;
import imgui.type.*;
import org.lwjgl.opengl.GL11;
import visual.scripting.node.*;
import visual.scripting.pin.Pin;
import visual.scripting.pin.PinUnset;
import visual.scripting.ui.Button;
import visual.scripting.ui.ConfirmSaveDialog;
import visual.scripting.ui.ListView;
import visual.scripting.ui.UiComponent;
import visual.scripting.ui.listeners.LeftClickListener;
import visual.scripting.ui.listeners.HoverListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static imgui.ImGui.*;
import static imgui.ImGui.getCursorScreenPosX;
import static org.lwjgl.glfw.GLFW.*;
public class GraphWindow {
private boolean requiresSave = false;
private final ImBoolean closable = new ImBoolean(true);
private ImGuiWindow window;
private String id;
protected Graph graph;
private NodeEditorContext context;
// private boolean windowFocused;
// private ArrayList<NodeBuilder> nodesTypes = new ArrayList<>();
protected final ArrayList<Class<? extends Node>> nodeList = new ArrayList<>();
private final ArrayList<Node> nodeInstanceCache = new ArrayList<>();
private final ImLong LINKA = new ImLong();
private final ImLong LINKB = new ImLong();
private final NodeCompiler nodeCompiler = new NodeCompiler();
private final TextEditor EDITOR = new TextEditor();
public Map<Integer, ImVec2> nodeQPos = new HashMap<>();
private int nodeNavigateTo = -1;
private boolean firstFrame = true;
private long holdingPinID = -1;
private long lastHoldingPinID = -1;
private long lastActivePin = -1;
private ImVec2 cursorPos;
private ImString nodeSearch = new ImString();
// private Pin.DataType curSelectedPinDataType = null;
private Pin curSelectedPinDataType = null;
private int openSourcePreview = 0;
private int currentNodeSourceID = -1;
private float h = 0f;
private Texture texture;
private boolean justLoadedFromFile = false;
private boolean justOpenedContextMenu = false;
private ConfirmSaveDialog saveDialog = new ConfirmSaveDialog();
//UiComponents
private Button convertAndSaveBtn = new Button("Save & Convert");
private Button clearGraphBtn = new Button("Clear Graph");
private Button showFlowBtn = new Button("Show Flow");
private ListView nodeListView = new ListView("Node List");
protected GraphWindow(){
graph = new Graph("");
}
public GraphWindow(ImGuiWindow window, String filename, Graph graph){
this.window = window;
this.id = filename;
this.graph = graph;
justLoadedFromFile = true;
init();
}
public GraphWindow(ImGuiWindow window, String fileName, String language){
this.window = window;
this.id = fileName;
graph = new Graph(language);
// graph.addNode(new NodeEntry(graph));
init();
}
private void init(){
NodeEditorConfig config = new NodeEditorConfig();
config.setSettingsFile(null);
context = new NodeEditorContext(config);
//add built-in nodes to the list
addNodeToList(NodeSplitFlow.class);
addNodeToList(NodeEntry.class);
addNodeToList(NodeVisualTest.class);
addNodeToList(NodeTime.class);
addNodeToList(NodeLongToFloat.class);
addNodeToList(NodeReroute.class);
//add a starter node to the graph
addNodeToList(NodeVariable.class);
for(VisualScriptingPlugin plugin : ImGuiWindow.pluginManager.getExtensions(VisualScriptingPlugin.class)){
plugin.init(this);
}
//LINKA.get();
try {
texture = TextureLoader.loadTexture("white.png");
} catch (IOException e) {
e.printStackTrace();
}
convertAndSaveBtn.addLeftClickListener(new LeftClickListener() {
@Override
public void onClicked() {
save();
}
});
convertAndSaveBtn.addHoverListener(new HoverListener() {
@Override
public void onHovered() {
setNextWindowPos(getMousePosOnOpeningCurrentPopupX(), getMousePosOnOpeningCurrentPopupY() + 10, ImGuiCond.Always);
beginTooltip();
textUnformatted("Saves & converts nodes to source code");
endTooltip();
}
});
clearGraphBtn.addLeftClickListener(new LeftClickListener() {
@Override
public void onClicked() {
requiresSave = true;
graph.getNodes().clear();
}
});
showFlowBtn.addLeftClickListener(() -> {
int uniqueLinkId = 1;
for (Node node : graph.getNodes().values()) {
for (Pin pin : node.outputPins) {
if (pin.connectedTo != -1) {
NodeEditor.flow(uniqueLinkId++);
}
}
}
});
nodeListView.addLeftClickListener(() -> {
System.out.println(nodeListView.getSelectedItem());
});
}
private void save(){
//used to convert the nodes to source text
//Converts to Source
String text = nodeCompiler.compile(graph);
File file = new File(ImGuiWindow.workingDir.getAbsolutePath().toString() + File.separator + id + "." + graph.getLanguage());
System.out.println(file);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedWriter br = new BufferedWriter(new FileWriter(file));
br.write(text);
br.close();
} catch (IOException e) {
e.printStackTrace();
}
EDITOR.setText(text);
//Saves Node Graph information (nodes, nodes positions, node links etc...)
GraphSaver.save(id + "." + graph.getLanguage(), graph);
requiresSave = false;
}
/**
* Shows the Graphs window
*/
//int colID = 0;
public void show(float menuBarHeight){
cursorPos = getMousePos();
graph.update();
setNextWindowSize(GLFWWindow.getWidth(), GLFWWindow.getHeight() - menuBarHeight, ImGuiCond.Once);
setNextWindowPos(getMainViewport().getPosX(), getMainViewport().getPosY() + menuBarHeight, ImGuiCond.Once);
if(begin(id, closable, ImGuiWindowFlags.NoCollapse)){
//checks is value has been changed from clicking the close button
if(!closable.get()){
if(requiresSave){
int state = saveDialog.show();
if(state == 0) {
System.out.println("Don't save");
requiresSave = false;
GL11.glDeleteTextures(texture.ID);
window.removeGraphWindow(this);
}else if (state == 1){
System.out.println("Save");
save();
GL11.glDeleteTextures(texture.ID);
window.removeGraphWindow(this);
}
}else {
GL11.glDeleteTextures(texture.ID);
window.removeGraphWindow(this);
}
}
convertAndSaveBtn.show();
//clears all nodes from the graph and resets the graph
clearGraphBtn.show();
showFlowBtn.show();
if(beginTabBar("TabBar")) {
if(beginTabItem("NodeEditor")) {
// if(beginChild("SideBar", 100, 350))
if(beginTabBar("ContentTabBar")) {
if (beginTabItem("ContentArea")) {
beginGroup();
{
// nodeListView.show();
beginGroup();
dummy(200, h);
{
text("Node List");
for (Node node : graph.getNodes().values()) {
pushID(node.getID());
if (button(node.getName())) {
nodeNavigateTo = node.getID();
}
popID();
}
}
endGroup();
invisibleButton("invisButonToMoveLikeSplitter", 200, 50);
if (isItemActive()) {
h += getIO().getMouseDeltaY();
}
beginGroup();
text("Variable List");
for (int i = 0; i < 10; i++) {
button("placeholder var: " + i);
}
endGroup();
}
endGroup();
}
endTabItem();
}
endTabBar();
sameLine();
NodeEditor.setCurrentEditor(context);
NodeEditor.getStyle().setNodeRounding(2.0f);
// NodeEditor.pushStyleColor(NodeEditorStyleColor.LinkSelRect, 255, 0, 0, 255);
// TestNodeEditor.nodeStyleEditor();
ImVec2 headerMin = null;
ImVec2 headerMax = null;
float headerMaxY = 0;
beginGroup();
{
NodeEditor.begin("Editor");
{
NodeEditor.pushStyleColor(NodeEditorStyleColor.Flow, .5f, .5f, .5f, 1);
NodeEditor.pushStyleColor(NodeEditorStyleColor.FlowMarker, 1, 1, 1, 1);
//check if loaded from save file
if (justLoadedFromFile) {
System.out.println("Setting node positions from loaded file");
for (Node node : graph.getNodes().values()) {
NodeEditor.setNodePosition(node.getID(), node.getLoadedPosition().x, node.getLoadedPosition().y);
}
justLoadedFromFile = false;
}
for (Node node : graph.getNodes().values()) {
if(firstFrame){
node.posX = NodeEditor.getNodePositionX(node.getID());
node.posY = NodeEditor.getNodePositionY(node.getID());
}
if(NodeEditor.getNodePositionX(node.getID()) != node.posX){
node.posX = NodeEditor.getNodePositionX(node.getID());
requiresSave = true;
}
if(NodeEditor.getNodePositionY(node.getID()) != node.posY){
node.posY = NodeEditor.getNodePositionY(node.getID());
requiresSave = true;
}
float maxWidth = 0;
// headerMin = new ImVec2();
// headerMax = new ImVec2();
NodeEditor.pushStyleVar(NodeEditorStyleVar.NodePadding, 8, 4, 8, 8);
NodeEditor.beginNode(node.getID());
{
// if(button("NextId")){
//// colID++;
// }
if(node.hasTitleBar()) {
text(node.getName());
headerMin = getItemRectMin();
// dummy(getItemRectMax().x, 2);
headerMaxY = getItemRectMax().y;
newLine();
}else{
// dummy(10, 10);
}
for (UiComponent uiComponent : node.uiComponents) {
uiComponent.show();
}
//add node pins
int max = Math.max(node.outputPins.size(), node.inputPins.size());
for (int i = 0; i < max; i++) {
if (node.inputPins.size() > i) {
Pin inPin = node.inputPins.get(i);
// addPin(inPin);
NodeEditor.beginPin(inPin.getID(), NodeEditorPinKind.Input);//
drawPinShapeAndColor(inPin);
dummy(10, 10);
if(inPin.getName().length() > 0) {
sameLine();
text(inPin.getName());
}
NodeEditor.pinPivotAlignment(0f, .5f);
NodeEditor.endPin();
if (isItemClicked() && holdingPinID == -1) {
lastActivePin = inPin.getID();
}
if (isItemHovered()) {
setNextWindowPos(NodeEditor.toScreenX(getMousePosX()), NodeEditor.toScreenY(getMousePosY() + 10), ImGuiCond.Always);
beginTooltip();
textUnformatted(inPin.getName());
if(inPin.getData() != null) {
// textUnformatted("Type: " + inPin.getDataType());
textUnformatted("Value: " + inPin.getData().value);
}
endTooltip();
}
sameLine();
beginGroup();
configurePinUI(inPin);
endGroup();
}
if (node.width != -1) {
if(!node.hasTitleBar()){
sameLine(1);
}else {
sameLine(node.width - 10);
}
}
if (node.outputPins.size() > i) {
Pin outPin = node.outputPins.get(i);
// if (outPin.getDataType() == Pin.DataType.SPACER) {
// NodeEditor.beginPin(outPin.getID(), NodeEditorPinKind.Output);
// dummy(1f, 1f);
// NodeEditor.endPin();
// } else
{
// beginGroup();
//// configurePinUI(outPin);
// text(outPin.getName());
// endGroup();
// sameLine();
if(!node.hasTitleBar()) {
// beginGroup();
text(outPin.getName());
// endGroup();
sameLine();
}
NodeEditor.beginPin(outPin.getID(), NodeEditorPinKind.Output);
if(outPin.getName().length() > 0) {
textUnformatted(outPin.getName());
sameLine();
}
drawPinShapeAndColor(outPin);
dummy(10, 10);
NodeEditor.pinPivotAlignment(1f, .5f);
// sameLine();
ImVec2 pos = getCursorPos();
NodeEditor.endPin();
if (isItemHovered()) {
setNextWindowPos(NodeEditor.toScreenX(getMousePosX()), NodeEditor.toScreenY(getMousePosY() + 10), ImGuiCond.Always);
beginTooltip();
textUnformatted(outPin.getName());
if(outPin.getData() != null) {
// textUnformatted("Type: " + outPin.getDataType());
textUnformatted("Value: " + outPin.getData().value);
}
endTooltip();
}
}
if (isItemClicked() && holdingPinID == -1) {
lastActivePin = outPin.getID();
}
// addPin(outPin);
} else {
dummy(10, 10);
}
if(node.hasTitleBar()) {
newLine();
}
if(maxWidth < getItemRectMax().x){
maxWidth = getItemRectMax().x;
}
}
// NodeEditor.group(50, 50);
headerMax = new ImVec2(NodeEditor.getNodePositionX(node.getID()) < 0 ? getItemRectMax().x : maxWidth, headerMaxY);
if(node.getError().length() > 0) {
pushStyleColor(ImGuiCol.Text, ImColor.intToColor(255, 0, 0, 255));
text(node.getError());
popStyleColor();
}
}
NodeEditor.endNode();
//handle node movement with arrow keys
//TODO figure out how to consume input
int size = 10;
long[] selectedNode = new long[size];
if(isKeyPressed(GLFW_KEY_UP)){
NodeEditor.getSelectedNodes(selectedNode, size);
for(long id : selectedNode){
if(id != 0){
NodeEditor.setNodePosition(id, NodeEditor.getNodePositionX(id), NodeEditor.getNodePositionY(id) - 1);
}
}
}
if(isKeyPressed(GLFW_KEY_DOWN)){
NodeEditor.getSelectedNodes(selectedNode, size);
for(long id : selectedNode){
if(id != 0){
NodeEditor.setNodePosition(id, NodeEditor.getNodePositionX(id), NodeEditor.getNodePositionY(id) + 1);
}
}
}
if(isKeyPressed(GLFW_KEY_LEFT)){
NodeEditor.getSelectedNodes(selectedNode, size);
for(long id : selectedNode){
if(id != 0){
NodeEditor.setNodePosition(id, NodeEditor.getNodePositionX(id) - 1, NodeEditor.getNodePositionY(id));
}
}
}
if(isKeyPressed(GLFW_KEY_RIGHT)){
NodeEditor.getSelectedNodes(selectedNode, size);
for(long id : selectedNode){
if(id != 0){
NodeEditor.setNodePosition(id, NodeEditor.getNodePositionX(id) + 1, NodeEditor.getNodePositionY(id));
}
}
}
if (isItemVisible() && node.hasTitleBar()) {
int alpha = (int) (getStyle().getAlpha() * 255);
ImDrawList drawList = NodeEditor.getNodeBackgroundDrawList(node.getID());
float halfBorderWidth = NodeEditor.getStyle().getNodeBorderWidth() * 0.5f;
//headerColor = 0;
float uvX = (headerMax.x - headerMin.x) / (4.0f * texture.width);
float uvY = (headerMax.y - headerMin.y) / (4.0f * texture.height);
if ((headerMax.x > headerMin.x) && (headerMax.y > headerMin.y)) {
drawList.addImageRounded(texture.ID, headerMin.x - (8 - halfBorderWidth), headerMin.y - (4 - halfBorderWidth), headerMax.x + (8 - halfBorderWidth), headerMax.y + (0), 0, 0, uvX, uvY, rgbToInt(node.getRed(), node.getGreen(), node.getBlue(), node.getAlpha()), NodeEditor.getStyle().getNodeRounding(), ImDrawFlags.RoundCornersTop);
}
ImVec2 headerSeparatorMin = new ImVec2(headerMin.x, headerMin.y);
ImVec2 headerSeparatorMax = new ImVec2(headerMax.x, headerMax.y);
if ((headerSeparatorMax.x > headerSeparatorMin.x) && (headerSeparatorMax.y > headerSeparatorMin.y)) {
drawList.addLine(headerMin.x - 8 - halfBorderWidth, headerMax.y, headerMax.x + (8 - halfBorderWidth), headerMax.y, rgbToInt(node.getRed(), node.getGreen(), node.getBlue(), 96 * alpha / (3 * 255)), 1);
}
}
// ImRect rect = new ImRect(getItemRectMin(), getItemRectMax());
// getWindowDrawList().addRectFilled(rect.min.x, rect.min.y, rect.max.x, rect.max.y, TestNodeEditor.rgbToInt(0, 0, 255));
if (node.width == -1) {
node.width = NodeEditor.getNodeSizeX(node.getID());
}
//calculate connected pins values
for (int i = 0; i < node.outputPins.size(); i++) {
Pin pin = node.outputPins.get(i);
if (pin.connectedTo != -1) {
//find the input pin that is connected to this output pin
Pin otherPin = graph.findPinById(pin.connectedTo);
//TODO decide if we automatically set connected pin values or let the plugin handle it
//
// if(otherPin.getData() != null && pin.getData() != null) {
// pin.getData().value = otherPin.getData().getValue();
// }
}
}
node.execute();
}
//link node pins together
int uniqueLinkId = 1;
for (Node node : graph.getNodes().values()) {
for (Pin pin : node.outputPins) {
if (pin.connectedTo != -1) {
// float[] pincolor = getPinColor(pin);
NodeEditor.pushStyleVar(NodeEditorStyleVar.FlowMarkerDistance, 50);
NodeEditor.pushStyleVar(NodeEditorStyleVar.FlowDuration, 1000);
NodeEditor.pushStyleVar(NodeEditorStyleVar.FlowSpeed, 25);
NodeEditor.link(uniqueLinkId++, pin.getID(), pin.connectedTo, pin.getColor().x, pin.getColor().y, pin.getColor().z, pin.getColor().w, 1);
NodeEditor.popStyleVar(3);
}
}
}
}
// ImVec2 previewRect = getItemRectMax();
// windowFocused = isWindowHovered();
if (NodeEditor.beginCreate()) {
holdingPinID = lastActivePin;
// curSelectedPinDataType = graph.findPinById((int) lastActivePin).getDataType();
curSelectedPinDataType = graph.findPinById((int) lastActivePin);
checkPinConnections();
} else {
//Open context menu if pin link dropped without connecting to another pin
if (holdingPinID != -1 && !(LINKA.get() != 0 || LINKB.get() != 0)) {
lastHoldingPinID = holdingPinID;
holdingPinID = -1;
curSelectedPinDataType = null;
// System.out.println(LINKA.get() + " : " + LINKB.get());
// setNextWindowPos(cursorPos.x, cursorPos.y, ImGuiCond.Always);
setNextWindowPos(cursorPos.x, cursorPos.y, ImGuiCond.Appearing);
openPopup("context_menu" + id);
justOpenedContextMenu = true;
}
LINKA.set(0);
LINKB.set(0);
}
NodeEditor.endCreate();
if (NodeEditor.beginDelete()) {
int size = 10;
long[] list = new long[size];
NodeEditor.getSelectedNodes(list, size);
ImLong link1 = new ImLong();
ImLong link2 = new ImLong();
ImLong link3 = new ImLong();
if (NodeEditor.queryDeletedLink(link1, link2, link3)) {
Pin pin1 = graph.findPinById((int) link2.get());
Pin pin2 = graph.findPinById((int) link3.get());
pin1.connectedTo = -1;
pin2.connectedTo = -1;
requiresSave = true;
}
ImLong nodeID = new ImLong();
if(NodeEditor.queryDeletedNode(nodeID)){
graph.removeNode((int)nodeID.get());
requiresSave = true;
}
}
NodeEditor.endDelete();
NodeEditor.suspend();
ImVec2 nodeSpawnPos = getMousePos();
//Double-clicked on link
long doubleClickLinkID = NodeEditor.getDoubleClickedLink();
if(doubleClickLinkID != 0){
System.out.println(doubleClickLinkID);
}
if (nodeNavigateTo != -1) {
NodeEditor.selectNode(nodeNavigateTo, false);
NodeEditor.navigateToSelection(false, 1.5f);
nodeNavigateTo = -1;
}
final long pinWithContextMenu = NodeEditor.getPinWithContextMenu();
if (pinWithContextMenu != -1) {
openPopup("pin_menu" + id);
getStateStorage().setInt(getID("node_pin_id"), (int) pinWithContextMenu);
}
if (isPopupOpen("pin_menu" + id)) {
final int targetPin = getStateStorage().getInt(getID("node_pin_id"));
Pin pin = graph.findPinById(targetPin);
if (pin.isCanDelete()) {
if (beginPopup("pin_menu" + id)) {
if (menuItem("Delete Pin")) {
pin.getNode().removePinById(targetPin);
closeCurrentPopup();
}
}
endPopup();
}
}
final long nodeWithContextMenu = NodeEditor.getNodeWithContextMenu();
if (nodeWithContextMenu != -1) {
openPopup("node_menu" + id);
getStateStorage().setInt(getID("delete_node_id"), (int) nodeWithContextMenu);
}
if (isPopupOpen("node_menu" + id)) {
final int targetNode = getStateStorage().getInt(getID("delete_node_id"));
if (beginPopup("node_menu" + id)) {
if (menuItem("Duplicate " + graph.getNodes().get(targetNode).getName())) {
Node newInstance = null;
try {
Node target = graph.getNodes().get(targetNode);
newInstance = target.getClass().getDeclaredConstructor(Graph.class).newInstance(graph);
graph.addNode(newInstance);
newInstance.init();
nodeQPos.put(newInstance.getID(), new ImVec2());
NodeEditor.setNodePosition(newInstance.getID(), NodeEditor.toCanvasX(getCursorScreenPosX()), NodeEditor.toCanvasY(getCursorScreenPosY()));
for (int i = 0; i < newInstance.inputPins.size(); i++) {
Global.setPinValue(newInstance.inputPins.get(i), String.valueOf(target.inputPins.get(i).getData().value));
}
//output pins are usually set based on the input pins no need to duplicate
// for (int i = 0; i < newInstance.outputPins.size(); i++) {
// Global.setPinValue(newInstance.outputPins.get(i), String.valueOf(target.outputPins.get(i).getData().value));
// }
} catch (Exception e) {
e.printStackTrace();
}
closeCurrentPopup();
}
separator();
if (menuItem("Delete " + graph.getNodes().get(targetNode).getName())) {
NodeEditor.deleteNode(targetNode);
// graph.removeNode(targetNode);
closeCurrentPopup();
}
if (menuItem("Preview Source")) {
currentNodeSourceID = targetNode;
openSourcePreview = 1;
}
}
endPopup();
}
if (openSourcePreview == 1) {
openSourcePreview = 0;
openPopup("PreviewSource");
}
if (isPopupOpen("PreviewSource")) {
if (beginPopupModal("PreviewSource", ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoResize)) {
StringBuilder sb = new StringBuilder();
graph.getNodes().get(currentNodeSourceID).printSource(sb);
text(sb.toString());
if (button("Close")) {
closeCurrentPopup();
}
endPopup();
// System.out.println(sb);
}
}
final long linkWithContextMenu = NodeEditor.getLinkWithContextMenu();
if (linkWithContextMenu != -1) {
openPopup("link_menu" + id);
getStateStorage().setInt(getID("delete_link_id"), (int) linkWithContextMenu);
}
if (isPopupOpen("link_menu" + id)) {
final int targetLink = getStateStorage().getInt(getID("delete_link_id"));
if (beginPopup("link_menu" + id)) {
if (menuItem("Delete Link")) {
System.out.println(targetLink);
if (NodeEditor.deleteLink(targetLink)) {
System.out.println("Deleted link");
}
closeCurrentPopup();
}
}
endPopup();
}
if (NodeEditor.showBackgroundContextMenu()) {
openPopup("context_menu" + id);
justOpenedContextMenu = true;
}
if (isPopupOpen("context_menu" + id)) {
if (beginPopup("context_menu" + id)) {
ImVec2 newNodePosition = nodeSpawnPos;
//get all loaded nodes and show them in the right click menu
if (nodeInstanceCache.isEmpty()) {
//Create new instances from nodeList and store for later use
//this stops the constant spawning of new instances
for (Class<? extends Node> node : nodeList) {
Constructor<? extends Node> nodeClass = null;
Node instance = null;
try {
nodeClass = node.getDeclaredConstructor(Graph.class);
instance = nodeClass.newInstance(graph);
nodeInstanceCache.add(instance);
} catch (Exception e) {
e.printStackTrace();
}
createContextMenuItem(instance, 0);
}
} else {
if(justOpenedContextMenu) {
setKeyboardFocusHere(0);
justOpenedContextMenu = false;
}
inputTextWithHint("##", "Search node", nodeSearch);
for (Node instance : nodeInstanceCache) {
createContextMenuItem(instance, 0);
}
}
endPopup();
}
}
//Popup handling for inside Graph WIP
// for(Popup popup : PopupHandler.openPopups){
Popup popup = PopupHandler.currentPopup;
if(popup != null){
setNextWindowPos(cursorPos.x, cursorPos.y, ImGuiCond.Appearing);
if(isPopupOpen(popup.id.toString())){
if(beginPopup(popup.id.toString())){
//context here
if(popup.show()){
PopupHandler.remove(popup);
closeCurrentPopup();
};
}
endPopup();
}
}
NodeEditor.resume();
NodeEditor.end();
}
endGroup();
//TODO try to add panel on the right side of the graph
// sameLine();
// beginGroup();
// dummy(200, 200);
// {
// text("Other List");
// for (Node node : graph.getNodes().values()) {
// pushID(node.getID() + "1");
// if(button(node.getName())){
// nodeNavigateTo = node.getID();
// }
// popID();
// }
// }
// endGroup();
endTabItem();
}
if(beginTabItem("Output")){
EDITOR.render("output");
EDITOR.setReadOnly(true);
endTabItem();
}
}
endTabBar();
}
end();
//Navigate to default node when opening the Graph Window
if(firstFrame){
if(graph.getNodes().values().size() > 0){
nodeNavigateTo = 0;
}
firstFrame = false;
}
}
// private float[] getPinColor(Pin pin){
// float[] color = new float[4];
// //TODO set link colors here
// color[0] = 1;
// color[1] = 1;
// color[2] = 1;
// color[3] = 1;
// return color;
// }
private void createContextMenuItem(Node instance, int depth) {
if(nodeSearch.get().toLowerCase().length() > 0){
if(!instance.getName().toLowerCase().contains(nodeSearch.get())){
return;
}
}
if(instance.getCategory() != null) {
String[] languages = instance.getLanguages();
boolean shouldAdd = false;
for(String lang : languages){
if(lang.equals(graph.getLanguage())){
shouldAdd = true;
break;
}
}
if(languages.length <= 0){
shouldAdd = true;
}
if(!shouldAdd){
return;
}
String[] cats = instance.getCategory().split("\\.");
// for (int i = 0; i < cats.length; i++) {
// System.out.println(cats[i]);
if (beginMenu(cats[depth])) {
if(depth + 1 < cats.length) {
createContextMenuItem(instance, depth + 1);
}else{
if (menuItem(instance.getName())) {
try {
Node newInstance = instance.getClass().getDeclaredConstructor(Graph.class).newInstance(graph);
graph.addNode(newInstance);
newInstance.init();
nodeQPos.put(newInstance.getID(), new ImVec2());
NodeEditor.setNodePosition(newInstance.getID(), NodeEditor.toCanvasX(getCursorScreenPosX()), NodeEditor.toCanvasY(getCursorScreenPosY()));
requiresSave = true;
autoConnectLink(newInstance);
} catch (Exception e) {
e.printStackTrace();
return;
}
closeCurrentPopup();
}
}
endMenu();
}
// }
}else {
if (menuItem(instance.getName())) {
try {
Node newInstance = instance.getClass().getDeclaredConstructor(Graph.class).newInstance(graph);
graph.addNode(newInstance);
newInstance.init();
nodeQPos.put(newInstance.getID(), new ImVec2());
NodeEditor.setNodePosition(newInstance.getID(), NodeEditor.toCanvasX(getCursorScreenPosX()), NodeEditor.toCanvasY(getCursorScreenPosY()));
requiresSave = true;
autoConnectLink(newInstance);
} catch (Exception e) {
e.printStackTrace();
return;
}
closeCurrentPopup();
}
}
}
private void autoConnectLink(Node newInstance){
//check if context menu opened by dragging a pin
if(lastHoldingPinID != -1){
Pin pin = graph.findPinById((int)lastActivePin);
System.out.println(pin.getPinType());
switch (pin.getPinType()){
case Input:
for (int i = 0; i < newInstance.outputPins.size(); i++) {
Pin instancePin = newInstance.outputPins.get(i);
// if(instancePin.getDataType() == pin.getDataType())
if(instancePin.getClass() == pin.getClass())
{
System.out.println("Found input");
//if a successful connection is made then return/break
if(pin.connect(instancePin)){
break;
}
}
}
break;
case Output:
for (int i = 0; i < newInstance.inputPins.size(); i++) {
Pin instancePin = newInstance.inputPins.get(i);
// if(instancePin.getDataType() == pin.getDataType())
if(instancePin.getClass() == pin.getClass())
{
System.out.println("Found Output");
//if a successful connection is made then return/break
if(pin.connect(instancePin)){
break;
}
}
}
break;
}
lastHoldingPinID = -1;
}
}
/**
* Called by Plugins to a node to the context menu and allow it to be created in the graph
*/
public void addNodeToList(Class<? extends Node> node){
nodeList.add(node);
}
/**
* sets the shape of the pin type and the color
*/
private void drawPinShapeAndColor(Pin pin){
float size = 10f;
float posX = getCursorPosX();
float posY = getCursorPosY();
boolean pinDragSame = true;
if(curSelectedPinDataType != null){
pinDragSame = pin.getClass() == curSelectedPinDataType.getClass();
}
pin.draw(getWindowDrawList(), posX, posY, pin.connectedTo != -1, pinDragSame);
}
/**
* checks if 2 pins should be connected and checks if the pin should be disconnected
*/