2 package org.simantics.district.network.ui;
4 import java.awt.geom.AffineTransform;
5 import java.awt.geom.Point2D;
6 import java.awt.geom.Rectangle2D;
7 import java.util.ArrayList;
10 import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
11 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
12 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
13 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
14 import org.simantics.district.network.ui.nodes.HoverSensitiveNode;
15 import org.simantics.district.network.ui.nodes.NetworkDrawingNode;
16 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
17 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
18 import org.simantics.g2d.diagram.IDiagram;
19 import org.simantics.g2d.diagram.handler.PickContext;
20 import org.simantics.g2d.diagram.handler.PickRequest;
21 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.scenegraph.Node;
24 import org.simantics.scenegraph.g2d.G2DParentNode;
25 import org.simantics.scenegraph.g2d.IG2DNode;
26 import org.simantics.utils.datastructures.hints.IHintContext.Key;
27 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
29 public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
31 public static final String NETWORK_DRAWING_NODE = "networkDrawingNode";
37 * A hint key for terminal pick distance in control pixels.
40 public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE");
43 * Default terminal pick distance in control pixels.
44 * @see #DEFAULT_PICK_DISTANCE
46 public static final double PICK_DIST = 10;
48 private NetworkDrawingNode node;
50 private AffineTransform transform;
52 public NetworkDrawingParticipant(AffineTransform transform) {
53 this.transform = transform;
57 public void initSG(G2DParentNode parent) {
58 node = parent.addNode(NETWORK_DRAWING_NODE, NetworkDrawingNode.class);
59 node.setTransform(transform);
60 node.setNetworkDrawingParticipant(this);
64 protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
65 node.setDiagram(newDiagram);
68 public boolean pickHoveredElement(Point2D currentMousePos, boolean isConnectionTool) {
69 PickRequest req = new PickRequest(new Rectangle2D.Double(currentMousePos.getX(), currentMousePos.getY(), 1e-8, 1e-8)).context(getContext());
70 List<IElement> pickables = new ArrayList<>();
71 pick.pick(diagram, req, pickables);
73 List<IElement> snap = diagram.getSnapshot();
75 boolean changed = false;
76 changed |= hoverNodes(snap, false, isConnectionTool, currentMousePos);
77 changed |= hoverNodes(pickables, true, isConnectionTool, currentMousePos);
81 private boolean hoverNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, Point2D p) {
82 boolean changed = false;
83 for (IElement elem : elements) {
84 Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
85 if (node instanceof DistrictNetworkVertexNode) {
86 changed |= ((DistrictNetworkVertexNode) node).hover(hover, isConnectionTool);
88 ((DistrictNetworkVertexNode) node).setMousePosition(p);
90 node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
91 if (node instanceof DistrictNetworkEdgeNode) {
92 for (IG2DNode n : ((DistrictNetworkEdgeNode) node).getNodes()) {
93 if (n instanceof HoverSensitiveNode) {
94 changed |= ((HoverSensitiveNode)n).hover(hover, isConnectionTool);
96 ((HoverSensitiveNode)n).setMousePosition(p);
105 public boolean isHoveringOverNode(Point2D currentMousePos) {
106 PickRequest req = new PickRequest(currentMousePos).context(getContext());
107 List<IElement> pickables = new ArrayList<IElement>();
108 pick.pick(diagram, req, pickables);
109 for (IElement elem : pickables) {
110 Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
111 if (node instanceof DistrictNetworkVertexNode) {
118 public AffineTransform getTransform() {