]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java
Improve HoverInfoStyle performance for district network diagrams
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / NetworkDrawingParticipant.java
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.db.Resource;
11 import org.simantics.diagram.ui.DiagramModelHints;
12 import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
13 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
14 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
15 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
16 import org.simantics.district.network.ui.nodes.HoverSensitiveNode;
17 import org.simantics.district.network.ui.nodes.NetworkDrawingNode;
18 import org.simantics.district.network.ui.participants.DynamicVisualisationContributionsParticipant;
19 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
20 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.diagram.handler.PickContext;
23 import org.simantics.g2d.diagram.handler.PickRequest;
24 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
25 import org.simantics.g2d.element.ElementHints;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.scenegraph.Node;
28 import org.simantics.scenegraph.g2d.G2DParentNode;
29 import org.simantics.scenegraph.g2d.IG2DNode;
30 import org.simantics.utils.datastructures.hints.IHintContext.Key;
31 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
32
33 public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
34
35     public static final String NETWORK_DRAWING_NODE = "networkDrawingNode";
36
37         @Dependency
38     PickContext pick;
39     
40     /**
41      * A hint key for terminal pick distance in control pixels.
42      * @see #PICK_DIST
43      */
44     public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE");
45
46     /**
47      * Default terminal pick distance in control pixels.
48      * @see #DEFAULT_PICK_DISTANCE
49      */
50     public static final double PICK_DIST = 10;
51     
52     private NetworkDrawingNode node;
53
54     private DynamicVisualisationContributionsParticipant dynamicVisualisationContributionsParticipant;
55     private AffineTransform transform;
56     
57     public NetworkDrawingParticipant(DynamicVisualisationContributionsParticipant dynamicVisualisationContributionsParticipant, AffineTransform transform) {
58         this.dynamicVisualisationContributionsParticipant = dynamicVisualisationContributionsParticipant;
59         this.transform = transform;
60     }
61
62     @SGInit
63     public void initSG(G2DParentNode parent) {
64         node = parent.addNode(NETWORK_DRAWING_NODE, NetworkDrawingNode.class);
65         node.setTransform(transform);
66         node.setNetworkDrawingParticipant(this);
67     }
68     
69     @Override
70     protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
71         node.setDiagram(newDiagram);
72     }
73
74     public boolean pickHoveredElement(Point2D currentMousePos, boolean isConnectionTool) {
75         PickRequest req = new PickRequest(new Rectangle2D.Double(currentMousePos.getX(), currentMousePos.getY(), 1e-8, 1e-8)).context(getContext());
76         List<IElement> pickables = new ArrayList<>();
77         pick.pick(diagram, req, pickables);
78
79         List<IElement> snap = diagram.getSnapshot();
80
81         hoverNodes2(pickables, true, isConnectionTool, currentMousePos);
82         // we repaint ourselves once the async calulation is ready
83         return false;
84         
85 //        boolean changed = false;
86 //        changed |= hoverNodes(snap, false, isConnectionTool, currentMousePos);
87 //        changed |= hoverNodes(pickables, true, isConnectionTool, currentMousePos);
88         //return changed;
89     }
90
91     private boolean hoverNodes2(List<IElement> elements, boolean hover, boolean isConnectionTool, Point2D p) {
92         if (elements == null || elements.isEmpty()) {
93             return dynamicVisualisationContributionsParticipant.doHover(false, isConnectionTool);
94         } else {
95             boolean changed = dynamicVisualisationContributionsParticipant.doHover(true, isConnectionTool);
96             if (changed) {
97                 // we prefer the first picked element only
98                 IElement elem = elements.get(0);
99                 G2DParentNode node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
100                 if (node instanceof DistrictNetworkVertexNode) {
101                 } else {
102                     node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
103                 }
104                 Resource mapElement = elem.getHint(ElementHints.KEY_OBJECT);
105                 Resource runtimeDiagram = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
106                 dynamicVisualisationContributionsParticipant.hoverNode(runtimeDiagram, mapElement, node);
107             }
108             return changed;
109         }
110     }
111
112     private boolean hoverNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, Point2D p) {
113         boolean changed = false;
114         for (IElement elem : elements) {
115             Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
116             if (node instanceof DistrictNetworkVertexNode) {
117                 changed |= ((DistrictNetworkVertexNode) node).hover(hover, isConnectionTool);
118                 if (hover)
119                     ((DistrictNetworkVertexNode) node).setMousePosition(p);
120             } else {
121                 node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
122                 if (node instanceof DistrictNetworkEdgeNode) {
123                     for (IG2DNode n : ((DistrictNetworkEdgeNode) node).getNodes()) {
124                         if (n instanceof HoverSensitiveNode) {
125                             changed |= ((HoverSensitiveNode)n).hover(hover, isConnectionTool);
126                             if (hover)
127                                 ((HoverSensitiveNode)n).setMousePosition(p);
128                         }
129                     }
130                 }
131             }
132         }
133         return changed;
134     }
135
136     public boolean isHoveringOverNode(Point2D currentMousePos) {
137         PickRequest req = new PickRequest(currentMousePos).context(getContext());
138         List<IElement> pickables = new ArrayList<IElement>();
139         pick.pick(diagram, req, pickables);
140         for (IElement elem : pickables) {
141             Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
142             if (node instanceof DistrictNetworkVertexNode) {
143                 return true;
144             }
145         }
146         return false;
147     }
148
149     public AffineTransform getTransform() {
150         return transform;
151     }
152 }