import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
+import org.simantics.db.Resource;
+import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
+import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
+import org.simantics.district.network.ui.nodes.HoverSensitiveNode;
import org.simantics.district.network.ui.nodes.NetworkDrawingNode;
+import org.simantics.district.network.ui.participants.DynamicVisualisationContributionsParticipant;
import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
import org.simantics.g2d.diagram.IDiagram;
import org.simantics.g2d.diagram.handler.PickContext;
import org.simantics.g2d.diagram.handler.PickRequest;
import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
+import org.simantics.g2d.element.ElementHints;
import org.simantics.g2d.element.IElement;
-import org.simantics.maps.MapScalingTransform;
-import org.simantics.scenegraph.g2d.G2DNode;
+import org.simantics.scenegraph.Node;
import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.g2d.IG2DNode;
import org.simantics.utils.datastructures.hints.IHintContext.Key;
import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
- @Dependency
+ public static final String NETWORK_DRAWING_NODE = "networkDrawingNode";
+
+ @Dependency
PickContext pick;
/**
private NetworkDrawingNode node;
+ private DynamicVisualisationContributionsParticipant dynamicVisualisationContributionsParticipant;
private AffineTransform transform;
- public NetworkDrawingParticipant(AffineTransform transform) {
+ public NetworkDrawingParticipant(DynamicVisualisationContributionsParticipant dynamicVisualisationContributionsParticipant, AffineTransform transform) {
+ this.dynamicVisualisationContributionsParticipant = dynamicVisualisationContributionsParticipant;
this.transform = transform;
}
@SGInit
public void initSG(G2DParentNode parent) {
- node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class);
+ node = parent.addNode(NETWORK_DRAWING_NODE, NetworkDrawingNode.class);
node.setTransform(transform);
node.setNetworkDrawingParticipant(this);
}
node.setDiagram(newDiagram);
}
- public boolean pickHoveredElement(Point2D currentMousePos) {
- PickRequest req = new PickRequest(currentMousePos.getX(), currentMousePos.getY());
- List<IElement> pickables = new ArrayList<IElement>();
+ public boolean pickHoveredElement(Point2D currentMousePos, boolean isConnectionTool) {
+ PickRequest req = new PickRequest(new Rectangle2D.Double(currentMousePos.getX(), currentMousePos.getY(), 1e-8, 1e-8)).context(getContext());
+ List<IElement> pickables = new ArrayList<>();
pick.pick(diagram, req, pickables);
+
+ List<IElement> snap = diagram.getSnapshot();
+
+ hoverNodes2(pickables, true, isConnectionTool, currentMousePos);
+ // we repaint ourselves once the async calulation is ready
+ return false;
- List<IElement> snap = new ArrayList<>(diagram.getSnapshot());
-
- snap.removeAll(pickables);
-
+// boolean changed = false;
+// changed |= hoverNodes(snap, false, isConnectionTool, currentMousePos);
+// changed |= hoverNodes(pickables, true, isConnectionTool, currentMousePos);
+ //return changed;
+ }
+
+ private boolean hoverNodes2(List<IElement> elements, boolean hover, boolean isConnectionTool, Point2D p) {
+ if (elements == null || elements.isEmpty()) {
+ return dynamicVisualisationContributionsParticipant.doHover(false, isConnectionTool);
+ } else {
+ boolean changed = dynamicVisualisationContributionsParticipant.doHover(true, isConnectionTool);
+ if (changed) {
+ // we prefer the first picked element only
+ IElement elem = elements.get(0);
+ G2DParentNode node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
+ if (node instanceof DistrictNetworkVertexNode) {
+ } else {
+ node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
+ }
+ Resource mapElement = elem.getHint(ElementHints.KEY_OBJECT);
+ Resource runtimeDiagram = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
+ dynamicVisualisationContributionsParticipant.hoverNode(runtimeDiagram, mapElement, node);
+ }
+ return changed;
+ }
+ }
+
+ private boolean hoverNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, Point2D p) {
boolean changed = false;
- for (IElement sn : snap) {
- G2DNode node = sn.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
+ for (IElement elem : elements) {
+ Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
if (node instanceof DistrictNetworkVertexNode) {
- if (((DistrictNetworkVertexNode) node).hover(false) && !changed) {
- changed = true;
+ changed |= ((DistrictNetworkVertexNode) node).hover(hover, isConnectionTool);
+ if (hover)
+ ((DistrictNetworkVertexNode) node).setMousePosition(p);
+ } else {
+ node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
+ if (node instanceof DistrictNetworkEdgeNode) {
+ for (IG2DNode n : ((DistrictNetworkEdgeNode) node).getNodes()) {
+ if (n instanceof HoverSensitiveNode) {
+ changed |= ((HoverSensitiveNode)n).hover(hover, isConnectionTool);
+ if (hover)
+ ((HoverSensitiveNode)n).setMousePosition(p);
+ }
+ }
}
}
}
-
+ return changed;
+ }
+
+ public boolean isHoveringOverNode(Point2D currentMousePos) {
+ PickRequest req = new PickRequest(currentMousePos).context(getContext());
+ List<IElement> pickables = new ArrayList<IElement>();
+ pick.pick(diagram, req, pickables);
for (IElement elem : pickables) {
- G2DNode node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
+ Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
if (node instanceof DistrictNetworkVertexNode) {
- if (((DistrictNetworkVertexNode) node).hover(true) && !changed) {
- changed = true;
- }
+ return true;
}
}
- return changed;
+ return false;
}
+ public AffineTransform getTransform() {
+ return transform;
+ }
}