]> gerrit.simantics Code Review - simantics/district.git/blob
7dd55f5e1743ace66ebb402ec46c498ff155dd84
[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.util.ArrayList;
7 import java.util.List;
8
9 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
10 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
11 import org.simantics.district.network.ui.nodes.NetworkDrawingNode;
12 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
13 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
14 import org.simantics.g2d.diagram.IDiagram;
15 import org.simantics.g2d.diagram.handler.PickContext;
16 import org.simantics.g2d.diagram.handler.PickRequest;
17 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
18 import org.simantics.g2d.element.IElement;
19 import org.simantics.scenegraph.g2d.G2DNode;
20 import org.simantics.scenegraph.g2d.G2DParentNode;
21 import org.simantics.utils.datastructures.hints.IHintContext.Key;
22 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
23
24 public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
25
26     @Dependency
27     PickContext pick;
28     
29     /**
30      * A hint key for terminal pick distance in control pixels.
31      * @see #PICK_DIST
32      */
33     public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE");
34
35     /**
36      * Default terminal pick distance in control pixels.
37      * @see #DEFAULT_PICK_DISTANCE
38      */
39     public static final double PICK_DIST = 10;
40     
41     private NetworkDrawingNode node;
42
43     private AffineTransform transform;
44     
45     public NetworkDrawingParticipant(AffineTransform transform) {
46         this.transform = transform;
47     }
48
49     @SGInit
50     public void initSG(G2DParentNode parent) {
51         node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class);
52         node.setTransform(transform);
53         node.setNetworkDrawingParticipant(this);
54     }
55     
56     @Override
57     protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
58         node.setDiagram(newDiagram);
59     }
60
61     public boolean pickHoveredElement(Point2D currentMousePos) {
62         PickRequest req = new PickRequest(currentMousePos.getX(), currentMousePos.getY()).context(getContext());
63         List<IElement> pickables = new ArrayList<IElement>();
64         pick.pick(diagram, req, pickables);
65         
66         List<IElement> snap = new ArrayList<>(diagram.getSnapshot());
67         
68         snap.removeAll(pickables);
69         
70         boolean changed = false;
71         for (IElement sn : snap) {
72             G2DNode node = sn.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
73             if (node instanceof DistrictNetworkVertexNode) {
74                 if (((DistrictNetworkVertexNode) node).hover(false) && !changed) {
75                     changed = true;
76                 }
77             }
78         }
79         
80         for (IElement elem : pickables) {
81             G2DNode node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
82             if (node instanceof DistrictNetworkVertexNode) {
83                 if (((DistrictNetworkVertexNode) node).hover(true) && !changed) {
84                     changed = true;
85                 }
86             }
87         }
88         return changed;
89     }
90
91 }