From: Reino Ruusu Date: Wed, 20 Feb 2019 17:46:23 +0000 (+0200) Subject: Added symbol rendering for edges. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=db34439af303d45eb67cee78cb3f68c9b6666da4;p=simantics%2Fdistrict.git Added symbol rendering for edges. gitlab #27 Change-Id: I67e49bdec2f722121dd261c755ad7722469a3bc9 --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java index 9595f232..d4758007 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java @@ -28,6 +28,7 @@ import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.SimpleElementLayers; import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.SVGNode; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; import org.slf4j.Logger; @@ -37,6 +38,7 @@ public class DistrictNetworkEdgeElement { public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE"); public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE"); + public static final Key KEY_DN_EDGE_SYMBOL_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_SYMBOL_NODE"); public static final ElementClass CLASS = ElementClass.compile( @@ -66,6 +68,9 @@ public class DistrictNetworkEdgeElement { if (node == null) { node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class); edgeElement.setHint(KEY_DN_EDGE_NODE, node); + + SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class); + edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol); } node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE)); @@ -79,8 +84,10 @@ public class DistrictNetworkEdgeElement { @Override public void cleanup(IElement edge) { + ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_SYMBOL_NODE); ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE); edge.removeHint(KEY_DN_EDGE_NODE); + edge.removeHint(KEY_DN_EDGE_SYMBOL_NODE); } } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java index 9f8c9a23..00a1427f 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java @@ -7,16 +7,20 @@ import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Line2D; import java.awt.geom.Path2D; +import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import org.simantics.district.network.ModelledCRS; import org.simantics.district.network.ui.DistrictNetworkEdge; +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.nodes.SVGNode; import org.simantics.scenegraph.utils.GeometryUtils; import org.simantics.scenegraph.utils.NodeUtil; -public class DistrictNetworkEdgeNode extends G2DNode implements ISelectionPainterNode { +public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode { private static final long serialVersionUID = 8049769475036519806L; @@ -32,6 +36,18 @@ public class DistrictNetworkEdgeNode extends G2DNode implements ISelectionPainte private Double stroke; private transient Color dynamicColor = null; + // Dimensions for shut-off valve symbol + 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 Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height); + + private transient Point2D centerPoint; + private transient Rectangle2D symbolRect; + private transient AffineTransform symbolTransform; + @Override public void init() { } @@ -76,12 +92,40 @@ public class DistrictNetworkEdgeNode extends G2DNode implements ISelectionPainte // Reset g2d.setStroke(oldStroke); g2d.setColor(oldColor); + + // Render SVG symbol + double viewScaleRecip = 10; + if (scaleStroke) { + double scale = GeometryUtils.getScale(g2d.getTransform()); + scale = Math.max(10000, Math.min(scale, 50000)); + viewScaleRecip /= scale; + } + + Point2D p = getCenterPoint(); + symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip); + symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform); + + for (INode nn : getNodes()) { + G2DNode g2dNode = (G2DNode)nn; + g2dNode.setTransform(symbolTransform); + g2dNode.render(g2d); + } + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); if (ot != null) g2d.setTransform(ot); } + private Point2D getCenterPoint() { + if (centerPoint == null) + centerPoint = new Point2D.Double(); + + Rectangle2D bounds = path.getBounds2D(); + centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY()); + return centerPoint; + } + public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) { // Convert to screen coordinates double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX()); @@ -155,4 +199,11 @@ public class DistrictNetworkEdgeNode extends G2DNode implements ISelectionPainte this.dynamicColor = color; } + @PropertySetter(value = "SVG") + public void setSVG(String value) { + for (INode nn : this.getNodes()) + if (nn instanceof SVGNode) + ((SVGNode)nn).setData(value); + } + } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkNodeUtils.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkNodeUtils.java new file mode 100644 index 00000000..8a369e26 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkNodeUtils.java @@ -0,0 +1,38 @@ +package org.simantics.district.network.ui.nodes; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; + +import org.simantics.district.network.ModelledCRS; + +public class DistrictNetworkNodeUtils { + + public static 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; + } + + public static Point2D calculatePoint2D(Point2D point, Point2D result) { + 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; + } + + public static AffineTransform getTransformToRectangle(Rectangle2D toDraw, AffineTransform transform) { + if (transform == null) + transform = new AffineTransform(); + + transform.setTransform(toDraw.getWidth(), 0.0, 0.0, toDraw.getHeight(), toDraw.getCenterX(), toDraw.getCenterY()); + return transform; + } + +} 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 10cfa905..377c7f7e 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,7 +8,6 @@ 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.scenegraph.INode; import org.simantics.scenegraph.ISelectionPainterNode; @@ -46,6 +45,7 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti private Rectangle2D bounds; private transient Point2D point; private transient Rectangle2D rect; + private transient AffineTransform symbolTransform; private double nodeSize = 1; @@ -86,8 +86,8 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti 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; @@ -110,23 +110,16 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti // Render SVG symbol for (INode nn : getNodes()) { - ((G2DNode)nn).setTransform(new AffineTransform(toDraw.getWidth(), 0.0, 0.0, toDraw.getHeight(), toDraw.getCenterX(), toDraw.getCenterY())); - ((G2DNode)nn).render(g2d); + G2DNode g2dNode = (G2DNode)nn; + 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(); @@ -157,22 +150,11 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti } private Rectangle2D calculateBounds(Rectangle2D rect) { - Point2D calcPoint = calculatePoint2D(vertex, point); + Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), 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; - } - public void setVertex(DistrictNetworkVertex vertex) { this.vertex = vertex; updateBounds();