2 package org.simantics.district.network.ui;
4 import java.awt.geom.AffineTransform;
5 import java.awt.geom.Point2D;
6 import java.awt.geom.Rectangle2D;
7 import java.util.ArrayList;
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;
33 public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
35 public static final String NETWORK_DRAWING_NODE = "networkDrawingNode";
41 * A hint key for terminal pick distance in control pixels.
44 public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE");
47 * Default terminal pick distance in control pixels.
48 * @see #DEFAULT_PICK_DISTANCE
50 public static final double PICK_DIST = 10;
52 private NetworkDrawingNode node;
54 private DynamicVisualisationContributionsParticipant dynamicVisualisationContributionsParticipant;
55 private AffineTransform transform;
57 public NetworkDrawingParticipant(DynamicVisualisationContributionsParticipant dynamicVisualisationContributionsParticipant, AffineTransform transform) {
58 this.dynamicVisualisationContributionsParticipant = dynamicVisualisationContributionsParticipant;
59 this.transform = transform;
63 public void initSG(G2DParentNode parent) {
64 node = parent.addNode(NETWORK_DRAWING_NODE, NetworkDrawingNode.class);
65 node.setTransform(transform);
66 node.setNetworkDrawingParticipant(this);
70 protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
71 node.setDiagram(newDiagram);
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);
79 List<IElement> snap = diagram.getSnapshot();
81 hoverNodes2(pickables, true, isConnectionTool, currentMousePos);
82 // we repaint ourselves once the async calulation is ready
85 // boolean changed = false;
86 // changed |= hoverNodes(snap, false, isConnectionTool, currentMousePos);
87 // changed |= hoverNodes(pickables, true, isConnectionTool, currentMousePos);
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);
95 boolean changed = dynamicVisualisationContributionsParticipant.doHover(true, isConnectionTool);
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) {
102 node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
104 Resource mapElement = elem.getHint(ElementHints.KEY_OBJECT);
105 Resource runtimeDiagram = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
106 dynamicVisualisationContributionsParticipant.hoverNode(runtimeDiagram, mapElement, node);
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);
119 ((DistrictNetworkVertexNode) node).setMousePosition(p);
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);
127 ((HoverSensitiveNode)n).setMousePosition(p);
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) {
149 public AffineTransform getTransform() {