package org.simantics.district.network.ui; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.List; import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement; import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode; 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.maps.MapScalingTransform; import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.g2d.G2DParentNode; 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) { PickRequest req = new PickRequest(currentMousePos.getX(), currentMousePos.getY()); List pickables = new ArrayList(); pick.pick(diagram, req, pickables); List snap = new ArrayList<>(diagram.getSnapshot()); snap.removeAll(pickables); boolean changed = false; for (IElement sn : snap) { G2DNode node = sn.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); if (node instanceof DistrictNetworkVertexNode) { if (((DistrictNetworkVertexNode) node).hover(false) && !changed) { changed = true; } } } for (IElement elem : pickables) { G2DNode node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); if (node instanceof DistrictNetworkVertexNode) { if (((DistrictNetworkVertexNode) node).hover(true) && !changed) { changed = true; } } } return changed; } }