X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Fnodes%2FDistrictNetworkVertexNode.java;h=0533e3117bf38b32a4f94191139fe55d8876e148;hb=4e9851013456ea1a8aa20eee5e33ccb97eed03ef;hp=0cd5ea884fd29058c2e12907eca01112a3e2dbe0;hpb=04d29917ceb4e34d2d9fc32ac4c7c8bdd2f4a732;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java index 0cd5ea88..0533e311 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java @@ -8,29 +8,33 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; -import org.simantics.district.network.ModelledCRS; import org.simantics.district.network.ui.adapters.DistrictNetworkVertex; +import org.simantics.maps.MapScalingTransform; +import org.simantics.scenegraph.INode; import org.simantics.scenegraph.ISelectionPainterNode; import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.IG2DNode; +import org.simantics.scenegraph.g2d.nodes.SVGNode; import org.simantics.scenegraph.utils.GeometryUtils; import org.simantics.scenegraph.utils.NodeUtil; -public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPainterNode { +public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode { //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class); private static final long serialVersionUID = -2641639101400236719L; - private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); - private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96); + private static final double left = -15; + private static final double top = left; + public static final double width = 30; + private static final double height = width; - private static final double left = -0.25; - private static final double top = -0.25; - private static final double width = 0.5; - private static final double height = 0.5; + private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); + private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96); private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height); - private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3); + private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2); private DistrictNetworkVertex vertex; @@ -43,8 +47,9 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain private Rectangle2D bounds; private transient Point2D point; private transient Rectangle2D rect; + private transient AffineTransform symbolTransform; - private double nodeSize = 1; + private double nodeSize = 1.0; @Override public void init() { @@ -60,7 +65,7 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain AffineTransform t = getTransform(); if (t != null && !t.isIdentity()) { ot = g2d.getTransform(); - g2d.transform(getTransform()); + g2d.transform(t); } Object oaaHint = null; @@ -76,20 +81,18 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain double viewScaleRecip = 1; if (scaleStroke) { - double scale = GeometryUtils.getScale(g2d.getTransform()); - scale = Math.max(10000, Math.min(scale, 50000)); - viewScaleRecip = 1.0 / scale; + viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform()); } double scaleRecip = viewScaleRecip * nodeSize; // Translate lat and lon to X and Y - Point2D p = point = calculatePoint2D(vertex, point); - Rectangle2D toDraw = calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip); + Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point); + Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip); if (NodeUtil.isSelected(this, 1)) { changeColor = true; g2d.setColor(SELECTION_COLOR); - BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) viewScaleRecip*5); + BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip); g2d.setStroke(ss); g2d.draw(toDraw); } @@ -98,25 +101,27 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain if (changeColor) g2d.setColor(newColor); g2d.fill(toDraw); - + // Reset settings if (changeColor) g2d.setColor(oldColor); if (oaaHint != null) - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); + + // Render SVG symbol + for (INode nn : getNodes()) { + G2DNode g2dNode = (G2DNode)nn; + if (nn instanceof SVGNode) { + symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform); + g2dNode.setTransform(symbolTransform); + } + g2dNode.render(g2d); + } + if (ot != null) g2d.setTransform(ot); } - private Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) { - if (result == null) - result = new Rectangle2D.Double(); - double mw = margin.getWidth(); - double mh = margin.getHeight(); - result.setFrame(p.getX() - (mw / 2 * scaleRecip), p.getY() - (mh / 2 * scaleRecip), mw * scaleRecip, mh * scaleRecip); - return result; - } - @Override public Rectangle2D getBounds() { return super.getBounds(); @@ -124,6 +129,7 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain @Override public Rectangle2D getBoundsInLocal() { + updateBounds(); return bounds; } @@ -147,20 +153,18 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain } private Rectangle2D calculateBounds(Rectangle2D rect) { - Point2D calcPoint = calculatePoint2D(vertex, point); - AffineTransform at = getTransform(); - return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), width / at.getScaleX(), height / at.getScaleY()).getBounds2D(); - } - - private static Point2D calculatePoint2D(DistrictNetworkVertex vertex, Point2D result) { - Point2D point = vertex.getPoint(); - double x = ModelledCRS.longitudeToX(point.getX()); - double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted - if (result == null) - result = new Point2D.Double(x, y); - else - result.setLocation(x, y); - return result; + Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point); + AffineTransform at = NodeUtil.getLocalToGlobalTransform(this); + at.concatenate(MapScalingTransform.INSTANCE); + double x = calcPoint.getX(); + double y = calcPoint.getY(); + double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at); + double widthh = width * scaleRecip * nodeSize; + double heighth = height * scaleRecip * nodeSize; + if (rect == null) + rect = new Rectangle2D.Double(); + rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth); + return rect; } public void setVertex(DistrictNetworkVertex vertex) { @@ -168,14 +172,18 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain updateBounds(); } - public boolean hover(boolean hover) { -// if (hover && LOGGER.isDebugEnabled()) -// LOGGER.debug("Hovering " + this); - boolean changed = false; - if (this.hover != hover) { - this.hover = hover; - changed = true; + @Override + public boolean hover(boolean hover, boolean isConnectionTool) { + // Only react to hover when the connection tool is active + boolean doHover = hover && isConnectionTool; + boolean changed = this.hover != doHover; + this.hover = doHover; + + for (IG2DNode child : getNodes()) { + if (child instanceof HoverSensitiveNode) + changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed; } + return changed; } @@ -187,6 +195,14 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain return color; } + @PropertySetter(value = "SVG") + public void setSVG(String value) { + for (INode nn : this.getNodes()) + if (nn instanceof SVGNode) + ((SVGNode)nn).setData(value); + } + + @PropertySetter(value = "size") public void setSize(Double size) { boolean changed = false;