X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2FNetworkDrawingParticipant.java;h=77ec735f6d2cc14c9f6013127244b6e82fac3ace;hb=refs%2Fchanges%2F72%2F3472%2F1;hp=a340d8591f625f0fdb78a7de27edaec6b34c3112;hpb=16ee01dc5a40981c58fd5b478b89552e5814e8bb;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java index a340d859..77ec735f 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java @@ -1,28 +1,40 @@ package org.simantics.district.network.ui; +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.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; /** @@ -38,10 +50,19 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant { public static final double PICK_DIST = 10; private NetworkDrawingNode node; + + private DynamicVisualisationContributionsParticipant dynamicVisualisationContributionsParticipant; + private 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); } @@ -50,34 +71,82 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant { node.setDiagram(newDiagram); } - public boolean pickHoveredElement(Point2D currentMousePos) { - PickRequest req = new PickRequest(currentMousePos.getX(), currentMousePos.getY()); - List pickables = new ArrayList(); + 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 pickables = new ArrayList<>(); pick.pick(diagram, req, pickables); + + List snap = diagram.getSnapshot(); + + hoverNodes2(pickables, true, isConnectionTool, currentMousePos); + // we repaint ourselves once the async calulation is ready + return false; - List 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 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 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 pickables = new ArrayList(); + 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; + } }