-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathConnectionLayer.java
More file actions
140 lines (120 loc) · 4.67 KB
/
ConnectionLayer.java
File metadata and controls
140 lines (120 loc) · 4.67 KB
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
package nodebox.client;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.util.PPaintContext;
import nodebox.node.Connection;
import nodebox.node.Node;
import nodebox.node.Port;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
public class ConnectionLayer extends PLayer {
private NetworkView networkView;
private Connection selection = null;
public ConnectionLayer(NetworkView networkView) {
this.networkView = networkView;
}
public NetworkView getNetworkView() {
return networkView;
}
public NodeBoxDocument getDocument() {
return networkView.getDocument();
}
public void select(Connection connection) {
selection = connection;
}
public void deselect() {
selection = null;
repaint();
}
public boolean hasSelection() {
return selection != null;
}
@Override
protected void paint(PPaintContext pPaintContext) {
// TODO: Draw parameter dependencies using implicitColor.
super.paint(pPaintContext);
Graphics2D g = pPaintContext.getGraphics();
Node activeNetwork = networkView.getActiveNetwork();
if (activeNetwork == null) return;
for (Connection c : activeNetwork.getConnections()) {
if (selection == c) {
g.setColor(Theme.CONNECTION_ACTION_COLOR);
} else {
g.setColor(Theme.CONNECTION_DEFAULT_COLOR);
}
paintConnection(g, c.getOutputNode(), c.getInput());
}
// Draw temporary connection
if (networkView.isConnecting() && networkView.getConnectionPoint() != null) {
// Set the color to some kind of yellow
g.setColor(Theme.CONNECTION_CONNECTING_COLOR);
Point2D pt = networkView.getConnectionPoint();
ConnectionLayer.paintConnection(g, networkView.getConnectionSource().getNode(), (float) pt.getX(), (float) pt.getY());
}
}
public static void paintConnection(Graphics2D g, Node outputNode, Port input) {
GeneralPath p = connectionPath(outputNode, input);
paintConnectionPath(g, p);
}
public static void paintConnection(Graphics2D g, Node outputNode, float x1, float y1) {
GeneralPath p = connectionPath(outputNode, x1, y1);
paintConnectionPath(g, p);
}
public static void paintConnectionPath(Graphics2D g, GeneralPath p) {
g.setStroke(new BasicStroke(2));
g.draw(p);
}
public static GeneralPath connectionPath(Node outputNode, Port input) {
float x1 = (float) (input.getNode().getX() + 1); // Compensate for selection border
float y1 = (float) (input.getNode().getY() + NodeView.getVerticalOffsetForPort(input) + NodeView.NODE_PORT_HEIGHT / 2);
return connectionPath(outputNode, x1, y1);
}
public static GeneralPath connectionPath(Node outputNode, float x1, float y1) {
GeneralPath p = new GeneralPath();
// Start position is at the middle right of the node.
float x0 = (float) (outputNode.getX() + NodeView.NODE_FULL_SIZE - 1); // Compensate for selection border
float y0 = (float) (outputNode.getY() + NodeView.NODE_FULL_SIZE / 2);
// End position is at the middle left of the node.
float dx = Math.abs(y1 - y0) / 2;
p.moveTo(x0, y0);
p.curveTo(x0 + dx, y0, x1 - dx, y1, x1, y1);
return p;
}
public Connection clickedConnection(Point2D p) {
// Make a rectangle out of the point that is slightly larger than the point itself.
Rectangle2D clickRect = new Rectangle2D.Double(p.getX() - 3, p.getY() - 3, 6, 6);
Node node = networkView.getActiveNetwork();
for (Connection c : node.getConnections()) {
GeneralPath gp = connectionPath(c.getOutputNode(), c.getInput());
if (gp.intersects(clickRect))
return c;
}
return null;
}
/**
* Delete the selected connection.
*/
public void deleteSelected() {
if (selection == null) return;
getDocument().disconnect(selection);
}
/**
* Handle the mouse clicked event.
* <p/>
* This method is called from the NetworkView to inform us that the background layer was clicked.
* Check if there is a connection under the mouse cursor and select it.
*
* @param e the input event
*/
public void mouseClickedEvent(PInputEvent e) {
Connection c = clickedConnection(e.getPosition());
if (c == null) {
deselect();
} else {
select(c);
repaint();
}
}
}