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