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