/
NetworkView.java
1101 lines (942 loc) · 38.7 KB
/
NetworkView.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 nodebox.client;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import nodebox.node.*;
import nodebox.ui.PaneView;
import nodebox.ui.Platform;
import nodebox.ui.Theme;
import org.python.google.common.base.Joiner;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
public class NetworkView extends JComponent implements PaneView, KeyListener, MouseListener, MouseWheelListener, MouseMotionListener {
public static final int GRID_CELL_SIZE = 48;
public static final int NODE_MARGIN = 6;
public static final int NODE_PADDING = 5;
public static final int NODE_WIDTH = GRID_CELL_SIZE * 3 - NODE_MARGIN * 2;
public static final int NODE_HEIGHT = GRID_CELL_SIZE - NODE_MARGIN * 2;
public static final int NODE_ICON_SIZE = 26;
public static final int GRID_OFFSET = 6;
public static final int PORT_WIDTH = 10;
public static final int PORT_HEIGHT = 3;
public static final int PORT_SPACING = 10;
public static final Dimension NODE_DIMENSION = new Dimension(NODE_WIDTH, NODE_HEIGHT);
public static final String SELECT_PROPERTY = "NetworkView.select";
public static final String HIGHLIGHT_PROPERTY = "highlight";
public static final String RENDER_PROPERTY = "render";
public static final String NETWORK_PROPERTY = "network";
private static Map<String, BufferedImage> nodeImageCache = new HashMap<String, BufferedImage>();
private static BufferedImage nodeGeneric;
public static final float MIN_ZOOM = 0.05f;
public static final float MAX_ZOOM = 1.0f;
public static final Map<String, Color> PORT_COLORS = Maps.newHashMap();
public static final Color DEFAULT_PORT_COLOR = Color.WHITE;
public static final Color NODE_BACKGROUND_COLOR = new Color(123, 154, 152);
public static final Color PORT_HOVER_COLOR = Color.YELLOW;
public static final Color TOOLTIP_BACKGROUND_COLOR = new Color(254, 255, 215);
public static final Color TOOLTIP_STROKE_COLOR = Color.DARK_GRAY;
public static final Color TOOLTIP_TEXT_COLOR = Color.DARK_GRAY;
public static final Color DRAG_SELECTION_COLOR = new Color(255, 255, 255, 100);
public static final BasicStroke DRAG_SELECTION_STROKE = new BasicStroke(1f);
public static final BasicStroke CONNECTION_STROKE = new BasicStroke(2);
private static Cursor defaultCursor, panCursor;
private final NodeBoxDocument document;
private JPopupMenu networkMenu;
private Point networkMenuLocation;
private JPopupMenu nodeMenu;
private Point nodeMenuLocation;
// View state
private double viewX, viewY, viewScale = 1;
private transient AffineTransform viewTransform = null;
private transient AffineTransform inverseViewTransform = null;
private Set<String> selectedNodes = new HashSet<String>();
// Interaction state
private boolean isDraggingNodes = false;
private boolean isSpacePressed = false;
private boolean isShiftPressed = false;
private boolean isDragSelecting = false;
private ImmutableMap<String, nodebox.graphics.Point> dragPositions = ImmutableMap.of();
private NodePort overInput;
private Node overOutput;
private Node connectionOutput;
private NodePort connectionInput;
private Point2D connectionPoint;
private boolean startDragging;
private Point2D dragStartPoint;
private Point2D dragCurrentPoint;
static {
Image panCursorImage;
try {
if (Platform.onWindows())
panCursorImage = ImageIO.read(new File("res/view-cursor-pan-32.png"));
else
panCursorImage = ImageIO.read(new File("res/view-cursor-pan.png"));
Toolkit toolkit = Toolkit.getDefaultToolkit();
panCursor = toolkit.createCustomCursor(panCursorImage, new Point(0, 0), "PanCursor");
defaultCursor = Cursor.getDefaultCursor();
nodeGeneric = ImageIO.read(new File("res/node-generic.png"));
} catch (IOException e) {
throw new RuntimeException(e);
}
PORT_COLORS.put(Port.TYPE_INT, new Color(116, 119, 121));
PORT_COLORS.put(Port.TYPE_FLOAT, new Color(116, 119, 121));
PORT_COLORS.put(Port.TYPE_STRING, new Color(92, 90, 91));
PORT_COLORS.put(Port.TYPE_BOOLEAN, new Color(92, 90, 91));
PORT_COLORS.put(Port.TYPE_POINT, new Color(119, 154, 173));
PORT_COLORS.put(Port.TYPE_COLOR, new Color(94, 85, 112));
PORT_COLORS.put("geometry", new Color(20, 20, 20));
PORT_COLORS.put("list", new Color(76, 137, 174));
PORT_COLORS.put("data", new Color(52, 85, 129));
}
/**
* Tries to find an image representation for the node.
* The image should be located near the library, and have the same name as the library.
* <p/>
* If this node has no image, the prototype is searched to find its image. If no image could be found,
* a generic image is returned.
*
* @param node the node
* @param nodeRepository the list of nodes to look for the icon
* @return an Image object.
*/
public static BufferedImage getImageForNode(Node node, NodeRepository nodeRepository) {
for (NodeLibrary library : nodeRepository.getLibraries()) {
BufferedImage img = findNodeImage(library, node);
if (img != null) {
return img;
}
}
if (node.getPrototype() != null) {
return getImageForNode(node.getPrototype(), nodeRepository);
} else {
return nodeGeneric;
}
}
public static BufferedImage findNodeImage(NodeLibrary library, Node node) {
if (node == null || node.getImage() == null || node.getImage().isEmpty()) return null;
if (!library.getRoot().hasChild(node)) return null;
File libraryFile = library.getFile();
if (libraryFile != null) {
File libraryDirectory = libraryFile.getParentFile();
if (libraryDirectory != null) {
File nodeImageFile = new File(libraryDirectory, node.getImage());
if (nodeImageFile.exists()) {
return readNodeImage(nodeImageFile);
}
}
}
return null;
}
public static BufferedImage readNodeImage(File nodeImageFile) {
String imagePath = nodeImageFile.getAbsolutePath();
if (nodeImageCache.containsKey(imagePath)) {
return nodeImageCache.get(imagePath);
} else {
try {
BufferedImage image = ImageIO.read(nodeImageFile);
nodeImageCache.put(imagePath, image);
return image;
} catch (IOException e) {
return null;
}
}
}
public NetworkView(NodeBoxDocument document) {
this.document = document;
setBackground(Theme.NETWORK_BACKGROUND_COLOR);
initEventHandlers();
initMenus();
}
private void initEventHandlers() {
setFocusable(true);
// This is disabled so we can detect the tab key.
setFocusTraversalKeysEnabled(false);
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
addMouseWheelListener(this);
}
private void initMenus() {
networkMenu = new JPopupMenu();
networkMenu.add(new NewNodeAction());
networkMenu.add(new ResetViewAction());
networkMenu.add(new GoUpAction());
nodeMenu = new JPopupMenu();
nodeMenu.add(new SetRenderedAction());
nodeMenu.add(new RenameAction());
nodeMenu.add(new DeleteAction());
nodeMenu.add(new GroupIntoNetworkAction(null));
nodeMenu.add(new GoInAction());
}
public NodeBoxDocument getDocument() {
return document;
}
public Node getActiveNetwork() {
return document.getActiveNetwork();
}
//// Events ////
/**
* Refresh the nodes and connections cache.
*/
public void updateAll() {
updateNodes();
updateConnections();
}
public void updateNodes() {
repaint();
}
public void updateConnections() {
repaint();
}
public void updatePosition(Node node) {
updateConnections();
}
public void checkErrorAndRepaint() {
//if (!networkError && !activeNetwork.hasError()) return;
//networkError = activeNetwork.hasError();
repaint();
}
public void codeChanged(Node node, boolean changed) {
repaint();
}
//// Model queries ////
public Node getActiveNode() {
return document.getActiveNode();
}
private ImmutableList<Node> getNodes() {
return getDocument().getActiveNetwork().getChildren();
}
private ImmutableList<Node> getNodesReversed() {
return getNodes().reverse();
}
private Iterable<Connection> getConnections() {
return getDocument().getActiveNetwork().getConnections();
}
public static boolean isPublished(Node network, Node childNode, Port childPort) {
return network.hasPublishedInput(childNode.getName(), childPort.getName());
}
//// Painting the nodes ////
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
// Draw background
g2.setColor(Theme.NETWORK_BACKGROUND_COLOR);
g2.fill(g.getClipBounds());
// Paint the grid
// (The grid is not really affected by the view transform)
paintGrid(g2);
// Set the view transform
AffineTransform originalTransform = g2.getTransform();
g2.transform(getViewTransform());
paintNodes(g2);
paintConnections(g2);
paintCurrentConnection(g2);
paintPortTooltip(g2);
paintDragSelection(g2);
// Restore original transform
g2.setTransform(originalTransform);
}
private void paintGrid(Graphics2D g) {
g.setColor(Theme.NETWORK_GRID_COLOR);
int gridCellSize = (int) Math.round(GRID_CELL_SIZE * viewScale);
int gridOffset = (int) Math.round(GRID_OFFSET * viewScale);
if (gridCellSize < 10) return;
int transformOffsetX = (int) (viewX % gridCellSize);
int transformOffsetY = (int) (viewY % gridCellSize);
for (int y = -gridCellSize; y < getHeight() + gridCellSize; y += gridCellSize) {
g.drawLine(0, y - gridOffset + transformOffsetY, getWidth(), y - gridOffset + transformOffsetY);
}
for (int x = -gridCellSize; x < getWidth() + gridCellSize; x += gridCellSize) {
g.drawLine(x - gridOffset + transformOffsetX, 0, x - gridOffset + transformOffsetX, getHeight());
}
}
private void paintConnections(Graphics2D g) {
g.setColor(Theme.CONNECTION_DEFAULT_COLOR);
g.setStroke(CONNECTION_STROKE);
for (Connection connection : getConnections()) {
paintConnection(g, connection);
}
}
private void paintConnection(Graphics2D g, Connection connection) {
Node outputNode = findNodeWithName(connection.getOutputNode());
Node inputNode = findNodeWithName(connection.getInputNode());
Port inputPort = inputNode.getInput(connection.getInputPort());
g.setColor(portTypeColor(outputNode.getOutputType()));
Rectangle outputRect = nodeRect(outputNode);
Rectangle inputRect = nodeRect(inputNode);
paintConnectionLine(g, outputRect.x + 4, outputRect.y + outputRect.height + 1, inputRect.x + portOffset(inputNode, inputPort) + 4, inputRect.y - 4);
}
private void paintCurrentConnection(Graphics2D g) {
g.setColor(Theme.CONNECTION_DEFAULT_COLOR);
if (connectionOutput != null) {
Rectangle outputRect = nodeRect(connectionOutput);
g.setColor(portTypeColor(connectionOutput.getOutputType()));
paintConnectionLine(g, outputRect.x + 4, outputRect.y + outputRect.height + 1, (int) connectionPoint.getX(), (int) connectionPoint.getY());
}
}
private static void paintConnectionLine(Graphics2D g, int x0, int y0, int x1, int y1) {
double dy = Math.abs(y1 - y0);
if (dy < GRID_CELL_SIZE) {
g.drawLine(x0, y0, x1, y1);
} else {
double halfDx = Math.abs(x1 - x0) / 2;
GeneralPath p = new GeneralPath();
p.moveTo(x0, y0);
p.curveTo(x0, y0 + halfDx, x1, y1 - halfDx, x1, y1);
g.draw(p);
}
}
private void paintNodes(Graphics2D g) {
g.setColor(Theme.NETWORK_NODE_NAME_COLOR);
for (Node node : getNodes()) {
Port hoverInputPort = overInput != null && overInput.node == node.getName() ? findNodeWithName(overInput.node).getInput(overInput.port) : null;
BufferedImage icon = getImageForNode(node, getDocument().getNodeRepository());
paintNode(g, getActiveNetwork(), node, icon, isSelected(node), isRendered(node), connectionOutput, hoverInputPort, overOutput == node);
}
}
public static Color portTypeColor(String type) {
Color portColor = PORT_COLORS.get(type);
return portColor == null ? DEFAULT_PORT_COLOR : portColor;
}
private void paintNode(Graphics2D g, Node network, Node node, BufferedImage icon, boolean selected, boolean rendered, Node connectionOutput, Port hoverInputPort, boolean hoverOutput) {
Rectangle r = nodeRect(node);
String outputType = node.getOutputType();
// Draw selection ring
if (selected) {
g.setColor(Color.WHITE);
g.fillRect(r.x, r.y, NODE_WIDTH, NODE_HEIGHT);
}
// Draw node
g.setColor(portTypeColor(outputType));
if (selected) {
g.fillRect(r.x + 2, r.y + 2, NODE_WIDTH - 4, NODE_HEIGHT - 4);
} else {
g.fillRect(r.x, r.y, NODE_WIDTH, NODE_HEIGHT);
}
// Draw render flag
if (rendered) {
g.setColor(Color.WHITE);
GeneralPath gp = new GeneralPath();
gp.moveTo(r.x + NODE_WIDTH - 2, r.y + NODE_HEIGHT - 20);
gp.lineTo(r.x + NODE_WIDTH - 2, r.y + NODE_HEIGHT - 2);
gp.lineTo(r.x + NODE_WIDTH - 20, r.y + NODE_HEIGHT - 2);
g.fill(gp);
}
// Draw input ports
g.setColor(Color.WHITE);
int portX = 0;
for (Port input : node.getInputs()) {
if (hoverInputPort == input) {
g.setColor(PORT_HOVER_COLOR);
} else {
g.setColor(portTypeColor(input.getType()));
}
// Highlight ports that match the dragged connection type
int portHeight = PORT_HEIGHT;
if (connectionOutput != null) {
if (connectionOutput.getOutputType().equals(input.getType())) {
portHeight = PORT_HEIGHT * 2;
} else {
portHeight = 1;
}
}
if (isPublished(network, node, input)) {
Point2D topLeft = inverseViewTransformPoint(new Point(4, 0));
g.setColor(portTypeColor(input.getType()));
g.setStroke(CONNECTION_STROKE);
paintConnectionLine(g, (int)topLeft.getX(), (int)topLeft.getY(), r.x + portX + 4, r.y-2);
}
g.fillRect(r.x + portX, r.y - portHeight, PORT_WIDTH, portHeight);
portX += PORT_WIDTH + PORT_SPACING;
}
// Draw output port
if (hoverOutput && connectionOutput == null) {
g.setColor(PORT_HOVER_COLOR);
} else {
g.setColor(portTypeColor(outputType));
}
g.fillRect(r.x, r.y + NODE_HEIGHT, PORT_WIDTH, PORT_HEIGHT);
// Draw icon
g.drawImage(icon, r.x + NODE_PADDING, r.y + NODE_PADDING, NODE_ICON_SIZE, NODE_ICON_SIZE, null);
g.setColor(Color.WHITE);
g.drawString(node.getName(), r.x + NODE_ICON_SIZE + NODE_PADDING * 2 + 2, r.y + 22);
}
private void paintPortTooltip(Graphics2D g) {
if (overInput != null) {
Node overInputNode = findNodeWithName(overInput.node);
Port overInputPort = overInputNode.getInput(overInput.port);
Rectangle r = inputPortRect(overInputNode, overInputPort);
Point2D pt = new Point2D.Double(r.getX(), r.getY() + 11);
String text = String.format("%s (%s)", overInput.port, overInputPort.getType());
paintTooltip(g, pt, text);
} else if (overOutput != null && connectionOutput == null) {
Rectangle r = outputPortRect(overOutput);
Point2D pt = new Point2D.Double(r.getX(), r.getY() + 11);
String text = String.format("output (%s)", overOutput.getOutputType());
paintTooltip(g, pt, text);
}
}
private static void paintTooltip(Graphics2D g, Point2D point, String text) {
FontMetrics fontMetrics = g.getFontMetrics();
int textWidth = fontMetrics.stringWidth(text);
int verticalOffset = 10;
Rectangle r = new Rectangle((int) point.getX(), (int) point.getY() + verticalOffset, textWidth, fontMetrics.getHeight());
r.grow(4, 3);
g.setColor(TOOLTIP_STROKE_COLOR);
g.drawRoundRect(r.x, r.y, r.width, r.height, 8, 8);
g.setColor(TOOLTIP_BACKGROUND_COLOR);
g.fillRoundRect(r.x, r.y, r.width, r.height, 8, 8);
g.setColor(TOOLTIP_TEXT_COLOR);
g.drawString(text, (float) point.getX(), (float) point.getY() + fontMetrics.getAscent() + verticalOffset);
}
private void paintDragSelection(Graphics2D g) {
if (isDragSelecting) {
Rectangle r = dragSelectRect();
g.setColor(DRAG_SELECTION_COLOR);
g.setStroke(DRAG_SELECTION_STROKE);
g.fill(r);
// To get a smooth line we need to subtract one from the width and height.
g.drawRect((int) r.getX(), (int) r.getY(), (int) r.getWidth() - 1, (int) r.getHeight() - 1);
}
}
private Rectangle dragSelectRect() {
int x0 = (int) dragStartPoint.getX();
int y0 = (int) dragStartPoint.getY();
int x1 = (int) dragCurrentPoint.getX();
int y1 = (int) dragCurrentPoint.getY();
int x = Math.min(x0, x1);
int y = Math.min(y0, y1);
int w = (int) Math.abs(dragCurrentPoint.getX() - dragStartPoint.getX());
int h = (int) Math.abs(dragCurrentPoint.getY() - dragStartPoint.getY());
return new Rectangle(x, y, w, h);
}
private static Rectangle nodeRect(Node node) {
return new Rectangle(nodePoint(node), NODE_DIMENSION);
}
private static Rectangle inputPortRect(Node node, Port port) {
Point pt = nodePoint(node);
Rectangle portRect = new Rectangle(pt.x + portOffset(node, port), pt.y - PORT_HEIGHT, PORT_WIDTH, PORT_HEIGHT);
growHitRectangle(portRect);
return portRect;
}
private static Rectangle outputPortRect(Node node) {
Point pt = nodePoint(node);
Rectangle portRect = new Rectangle(pt.x, pt.y + NODE_HEIGHT, PORT_WIDTH, PORT_HEIGHT);
growHitRectangle(portRect);
return portRect;
}
private static void growHitRectangle(Rectangle r) {
r.grow(2, 2);
}
private static Point nodePoint(Node node) {
int nodeX = ((int) node.getPosition().getX()) * GRID_CELL_SIZE;
int nodeY = ((int) node.getPosition().getY()) * GRID_CELL_SIZE;
return new Point(nodeX, nodeY);
}
private Point pointToGridPoint(Point e) {
Point2D pt = getInverseViewTransform().transform(e, null);
return new Point(
(int) Math.floor(pt.getX() / GRID_CELL_SIZE),
(int) Math.floor(pt.getY() / GRID_CELL_SIZE));
}
private static int portOffset(Node node, Port port) {
int portIndex = node.getInputs().indexOf(port);
return (PORT_WIDTH + PORT_SPACING) * portIndex;
}
//// View Transform ////
private void setViewTransform(double viewX, double viewY, double viewScale) {
this.viewX = viewX;
this.viewY = viewY;
this.viewScale = viewScale;
this.viewTransform = null;
this.inverseViewTransform = null;
}
private AffineTransform getViewTransform() {
if (viewTransform == null) {
viewTransform = new AffineTransform();
viewTransform.translate(viewX, viewY);
viewTransform.scale(viewScale, viewScale);
}
return viewTransform;
}
private AffineTransform getInverseViewTransform() {
if (inverseViewTransform == null) {
try {
inverseViewTransform = getViewTransform().createInverse();
} catch (NoninvertibleTransformException e) {
inverseViewTransform = new AffineTransform();
}
}
return inverseViewTransform;
}
private void resetViewTransform() {
setViewTransform(0, 0, 1);
repaint();
}
//// View queries ////
private Node findNodeWithName(String name) {
return getActiveNetwork().getChild(name);
}
public Node getNodeAt(Point2D point) {
for (Node node : getNodesReversed()) {
Rectangle r = nodeRect(node);
if (r.contains(point)) {
return node;
}
}
return null;
}
public Node getNodeAt(MouseEvent e) {
return getNodeAt(e.getPoint());
}
public Node getNodeWithOutputPortAt(Point2D point) {
for (Node node : getNodesReversed()) {
Rectangle r = outputPortRect(node);
if (r.contains(point)) {
return node;
}
}
return null;
}
public NodePort getInputPortAt(Point2D point) {
for (Node node : getNodesReversed()) {
for (Port port : node.getInputs()) {
Rectangle r = inputPortRect(node, port);
if (r.contains(point)) {
return NodePort.of(node.getName(), port.getName());
}
}
}
return null;
}
//// Selections ////
public boolean isRendered(Node node) {
return getActiveNetwork().getRenderedChild() == node;
}
public boolean isSelected(Node node) {
return (selectedNodes.contains(node.getName()));
}
public void select(Node node) {
selectedNodes.add(node.getName());
}
/**
* Select this node, and only this node.
* <p/>
* All other selected nodes will be deselected.
*
* @param node The node to select. If node is null, everything is deselected.
*/
public void singleSelect(Node node) {
if (selectedNodes.size() == 1 && selectedNodes.contains(node.getName())) return;
selectedNodes.clear();
if (node != null && getActiveNetwork().hasChild(node)) {
selectedNodes.add(node.getName());
firePropertyChange(SELECT_PROPERTY, null, selectedNodes);
document.setActiveNode(node);
}
repaint();
}
public void select(Iterable<Node> nodes) {
selectedNodes.clear();
for (Node node : nodes) {
selectedNodes.add(node.getName());
}
}
public void toggleSelection(Node node) {
checkNotNull(node);
if (selectedNodes.isEmpty()) {
singleSelect(node);
} else {
if (selectedNodes.contains(node.getName())) {
selectedNodes.remove(node.getName());
} else {
selectedNodes.add(node.getName());
}
firePropertyChange(SELECT_PROPERTY, null, selectedNodes);
repaint();
}
}
public void deselectAll() {
if (selectedNodes.isEmpty()) return;
selectedNodes.clear();
firePropertyChange(SELECT_PROPERTY, null, selectedNodes);
document.setActiveNode((Node) null);
repaint();
}
public Iterable<String> getSelectedNodeNames() {
return selectedNodes;
}
public Iterable<Node> getSelectedNodes() {
ImmutableList.Builder<Node> b = new ImmutableList.Builder<nodebox.node.Node>();
for (String name : getSelectedNodeNames()) {
b.add(findNodeWithName(name));
}
return b.build();
}
public void deleteSelection() {
document.removeNodes(getSelectedNodes());
}
//// Network navigation ////
private void goUp() {
if (getDocument().getActiveNetworkPath().equals("/")) return;
Iterable<String> it = Splitter.on("/").split(getDocument().getActiveNetworkPath());
int parts = Iterables.size(it);
String path = parts - 1 > 1 ? Joiner.on("/").join(Iterables.limit(it, parts - 1)) : "/";
getDocument().setActiveNetwork(path);
}
//// Input Events ////
public void keyTyped(KeyEvent e) {
switch (e.getKeyChar()) {
case KeyEvent.VK_BACK_SPACE:
getDocument().deleteSelection();
break;
}
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SHIFT) {
isShiftPressed = true;
} else if (keyCode == KeyEvent.VK_SPACE) {
isSpacePressed = true;
setCursor(panCursor);
} else if (keyCode == KeyEvent.VK_UP) {
moveSelectedNodes(0, -1);
} else if (keyCode == KeyEvent.VK_RIGHT) {
moveSelectedNodes(1, 0);
} else if (keyCode == KeyEvent.VK_DOWN) {
moveSelectedNodes(0, 1);
} else if (keyCode == KeyEvent.VK_LEFT) {
moveSelectedNodes(-1, 0);
}
}
private void moveSelectedNodes(int dx, int dy) {
for (Node node : getSelectedNodes()) {
getDocument().setNodePosition(node, node.getPosition().moved(dx, dy));
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
isShiftPressed = false;
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
isSpacePressed = false;
setCursor(defaultCursor);
}
}
public boolean isSpacePressed() {
return isSpacePressed;
}
public void mouseClicked(MouseEvent e) {
Point2D pt = inverseViewTransformPoint(e.getPoint());
if (e.getButton() == MouseEvent.BUTTON1) {
if (e.getClickCount() == 1) {
Node clickedNode = getNodeAt(pt);
if (clickedNode == null) {
deselectAll();
} else {
if (isShiftPressed) {
toggleSelection(clickedNode);
} else {
singleSelect(clickedNode);
}
}
} else if (e.getClickCount() == 2) {
Node clickedNode = getNodeAt(pt);
if (clickedNode == null) {
Point gridPoint = pointToGridPoint(e.getPoint());
getDocument().showNodeSelectionDialog(gridPoint);
} else {
document.setRenderedNode(clickedNode);
}
}
}
}
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
Point pt = e.getPoint();
NodePort nodePort = getInputPortAt(inverseViewTransformPoint(pt));
if (nodePort != null) {
JPopupMenu pMenu = new JPopupMenu();
pMenu.add(new PublishAction(nodePort));
pMenu.show(this, e.getX(), e.getY());
} else if (selectedNodes.size() > 1) {
JPopupMenu pMenu = new JPopupMenu();
pMenu.add(new GroupIntoNetworkAction(pointToGridPoint(e.getPoint())));
pMenu.show(this, e.getX(), e.getY());
} else {
Node pressedNode = getNodeAt(inverseViewTransformPoint(pt));
if (pressedNode != null) {
nodeMenuLocation = pt;
nodeMenu.show(this, e.getX(), e.getY());
} else {
networkMenuLocation = pt;
networkMenu.show(this, e.getX(), e.getY());
}
}
} else {
// If the space bar and mouse is pressed, we're getting ready to pan the view.
if (isSpacePressed) {
// When panning the view use the original mouse point, not the one affected by the view transform.
dragStartPoint = e.getPoint();
return;
}
Point2D pt = inverseViewTransformPoint(e.getPoint());
// Check if we're over an output port.
connectionOutput = getNodeWithOutputPortAt(pt);
if (connectionOutput != null) return;
// Check if we're over a connected input port.
connectionInput = getInputPortAt(pt);
if (connectionInput != null) {
// We're over a port, but is it connected?
Connection c = getActiveNetwork().getConnection(connectionInput.node, connectionInput.port);
// Disconnect it, but start a new connection on the same node immediately.
if (c != null) {
getDocument().disconnect(c);
connectionOutput = getActiveNetwork().getChild(c.getOutputNode());
connectionPoint = pt;
}
return;
}
// Check if we're pressing a node.
Node pressedNode = getNodeAt(pt);
if (pressedNode != null) {
// Don't immediately set "isDragging."
// We wait until we actually drag the first time to do the work.
startDragging = true;
return;
}
// We're creating a drag selection.
isDragSelecting = true;
dragStartPoint = pt;
}
}
public void mouseReleased(MouseEvent e) {
isDraggingNodes = false;
isDragSelecting = false;
if (connectionOutput != null && connectionInput != null) {
getDocument().connect(connectionOutput.getName(), connectionInput.node, connectionInput.port);
}
connectionOutput = null;
if (e.isPopupTrigger()) {
networkMenu.show(this, e.getX(), e.getY());
}
repaint();
}
public void mouseEntered(MouseEvent e) {
grabFocus();
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
Point2D pt = inverseViewTransformPoint(e.getPoint());
// Panning the view has the first priority.
if (isSpacePressed) {
// When panning the view use the original mouse point, not the one affected by the view transform.
Point2D offset = minPoint(e.getPoint(), dragStartPoint);
setViewTransform(viewX + offset.getX(), viewY + offset.getY(), viewScale);
dragStartPoint = e.getPoint();
repaint();
return;
}
if (connectionOutput != null) {
repaint();
connectionInput = getInputPortAt(pt);
connectionPoint = pt;
overOutput = getNodeWithOutputPortAt(pt);
overInput = getInputPortAt(pt);
}
if (startDragging) {
startDragging = false;
Node pressedNode = getNodeAt(pt);
if (pressedNode != null) {
if (selectedNodes.isEmpty() || !selectedNodes.contains(pressedNode.getName())) {
singleSelect(pressedNode);
}
isDraggingNodes = true;
dragPositions = selectedNodePositions();
dragStartPoint = pt;
} else {
isDraggingNodes = false;
}
}
if (isDraggingNodes) {
Point2D offset = minPoint(pt, dragStartPoint);
int gridX = (int) Math.round(offset.getX() / GRID_CELL_SIZE);
int gridY = (int) Math.round(offset.getY() / (float) GRID_CELL_SIZE);
for (String name : selectedNodes) {
nodebox.graphics.Point originalPosition = dragPositions.get(name);
nodebox.graphics.Point newPosition = originalPosition.moved(gridX, gridY);
getDocument().setNodePosition(findNodeWithName(name), newPosition);
}
}
if (isDragSelecting) {
dragCurrentPoint = pt;
Rectangle r = dragSelectRect();
selectedNodes.clear();
for (Node node : getNodes()) {
if (r.intersects(nodeRect(node))) {
selectedNodes.add(node.getName());
}
}
repaint();
}
}
public void mouseMoved(MouseEvent e) {
Point2D pt = inverseViewTransformPoint(e.getPoint());
overOutput = getNodeWithOutputPortAt(pt);
overInput = getInputPortAt(pt);
// It is probably very inefficient to repaint the view every time the mouse moves.
repaint();
}
public void mouseWheelMoved(MouseWheelEvent e) {
double scaleDelta = 1F - e.getWheelRotation() / 10F;
double newViewScale = viewScale * scaleDelta;
if (newViewScale < MIN_ZOOM) {
scaleDelta = MIN_ZOOM / viewScale;
} else if (newViewScale > MAX_ZOOM) {
scaleDelta = MAX_ZOOM / viewScale;
}
double vx = viewX - (e.getX() - viewX) * (scaleDelta - 1);
double vy = viewY - (e.getY() - viewY) * (scaleDelta - 1);
setViewTransform(vx, vy, viewScale * scaleDelta);
repaint();
}
private ImmutableMap<String, nodebox.graphics.Point> selectedNodePositions() {
ImmutableMap.Builder<String, nodebox.graphics.Point> b = ImmutableMap.builder();
for (String nodeName : selectedNodes) {
b.put(nodeName, findNodeWithName(nodeName).getPosition());
}
return b.build();
}
private Point2D inverseViewTransformPoint(Point p) {
Point2D pt = new Point2D.Double(p.getX(), p.getY());
return getInverseViewTransform().transform(pt, null);
}
private Point2D minPoint(Point2D a, Point2D b) {
return new Point2D.Double(a.getX() - b.getX(), a.getY() - b.getY());
}
private class NewNodeAction extends AbstractAction {
private NewNodeAction() {
super("New Node");
}
public void actionPerformed(ActionEvent e) {
Point gridPoint = pointToGridPoint(networkMenuLocation);
getDocument().showNodeSelectionDialog(gridPoint);
}
}
private class ResetViewAction extends AbstractAction {
private ResetViewAction() {
super("Reset View");
}
public void actionPerformed(ActionEvent e) {
resetViewTransform();
}
}
private class GoUpAction extends AbstractAction {
private GoUpAction() {
super("Go Up");
}
public void actionPerformed(ActionEvent e) {
goUp();