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.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.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.IElement; 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 PickContext pick; /** * A hint key for terminal pick distance in control pixels. * @see #PICK_DIST */ public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE"); /** * Default terminal pick distance in control pixels. * @see #DEFAULT_PICK_DISTANCE */ public static final double PICK_DIST = 10; private NetworkDrawingNode node; private AffineTransform transform; public NetworkDrawingParticipant(AffineTransform transform) { this.transform = transform; } @SGInit public void initSG(G2DParentNode parent) { node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class); node.setTransform(transform); node.setNetworkDrawingParticipant(this); } @Override protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) { node.setDiagram(newDiagram); } 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 = new ArrayList<>(diagram.getSnapshot()); // snap.removeAll(pickables); boolean changed = false; changed = hoverVertexNodes(snap, false, isConnectionTool, changed); changed = hoverEdgeNodes(snap, false, isConnectionTool, changed); changed = hoverVertexNodes(pickables, true, isConnectionTool, changed); changed = hoverEdgeNodes(pickables, true, isConnectionTool, changed); return changed; } private boolean hoverVertexNodes(List elements, boolean hover, boolean isConnectionTool, boolean changed) { for (IElement elem : elements) { Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); if (node instanceof DistrictNetworkVertexNode) { changed = ((DistrictNetworkVertexNode) node).hover(hover, isConnectionTool) || changed; } } return changed; } private boolean hoverEdgeNodes(List elements, boolean hover, boolean isConnectionTool, boolean changed) { for (IElement elem : elements) { Node 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) || changed; } } } } 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) { Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); if (node instanceof DistrictNetworkVertexNode) { return true; } } return false; } public AffineTransform getTransform() { return transform; } }