]> gerrit.simantics Code Review - simantics/district.git/blob
91e48251bb7ed89efc0cf9fb5625a46b6dbbe335
[simantics/district.git] /
1
2 package org.simantics.district.network.ui;
3
4 import java.awt.geom.AffineTransform;
5 import java.awt.geom.Point2D;
6 import java.awt.geom.Rectangle2D;
7 import java.util.ArrayList;
8 import java.util.List;
9
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;
28
29 public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
30
31     @Dependency
32     PickContext pick;
33     
34     /**
35      * A hint key for terminal pick distance in control pixels.
36      * @see #PICK_DIST
37      */
38     public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE");
39
40     /**
41      * Default terminal pick distance in control pixels.
42      * @see #DEFAULT_PICK_DISTANCE
43      */
44     public static final double PICK_DIST = 10;
45     
46     private NetworkDrawingNode node;
47
48     private AffineTransform transform;
49     
50     public NetworkDrawingParticipant(AffineTransform transform) {
51         this.transform = transform;
52     }
53
54     @SGInit
55     public void initSG(G2DParentNode parent) {
56         node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class);
57         node.setTransform(transform);
58         node.setNetworkDrawingParticipant(this);
59     }
60     
61     @Override
62     protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
63         node.setDiagram(newDiagram);
64     }
65
66     public boolean pickHoveredElement(Point2D currentMousePos, boolean isConnectionTool) {
67         PickRequest req = new PickRequest(new Rectangle2D.Double(currentMousePos.getX(), currentMousePos.getY(), 1e-8, 1e-8)).context(getContext());
68         List<IElement> pickables = new ArrayList<IElement>();
69         pick.pick(diagram, req, pickables);
70         
71         List<IElement> snap = new ArrayList<>(diagram.getSnapshot());
72         
73         // snap.removeAll(pickables);
74         
75         boolean changed = false;
76         changed = hoverVertexNodes(snap, false, isConnectionTool, changed);
77         changed = hoverEdgeNodes(snap, false, isConnectionTool, changed);
78         changed = hoverVertexNodes(pickables, true, isConnectionTool, changed);
79         changed = hoverEdgeNodes(pickables, true, isConnectionTool, changed);
80         return changed;
81     }
82
83     private boolean hoverVertexNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, boolean changed) {
84         for (IElement elem : elements) {
85             Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
86             if (node instanceof DistrictNetworkVertexNode) {
87                 changed = ((DistrictNetworkVertexNode) node).hover(hover, isConnectionTool) || changed;
88             }
89         }
90         return changed;
91     }
92     
93     private boolean hoverEdgeNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, boolean changed) {
94         for (IElement elem : elements) {
95             Node node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
96             if (node instanceof DistrictNetworkEdgeNode) {
97                 for (IG2DNode n : ((DistrictNetworkEdgeNode) node).getNodes()) {
98                     if (n instanceof HoverSensitiveNode) {
99                         changed = ((HoverSensitiveNode)n).hover(hover, isConnectionTool) || changed;
100                     }
101                 }
102             }
103         }
104         return changed;
105     }
106     
107     public boolean isHoveringOverNode(Point2D currentMousePos) {
108         PickRequest req = new PickRequest(currentMousePos).context(getContext());
109         List<IElement> pickables = new ArrayList<IElement>();
110         pick.pick(diagram, req, pickables);
111         for (IElement elem : pickables) {
112             Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
113             if (node instanceof DistrictNetworkVertexNode) {
114                 return true;
115             }
116         }
117         return false;
118     }
119
120     public AffineTransform getTransform() {
121         return transform;
122     }
123 }