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;
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(
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));
@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);
}
}
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;
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() {
}
// 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());
this.dynamicColor = color;
}
+ @PropertySetter(value = "SVG")
+ public void setSVG(String value) {
+ for (INode nn : this.getNodes())
+ if (nn instanceof SVGNode)
+ ((SVGNode)nn).setData(value);
+ }
+
}
--- /dev/null
+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;
+ }
+
+}
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;
private Rectangle2D bounds;
private transient Point2D point;
private transient Rectangle2D rect;
+ private transient AffineTransform symbolTransform;
private double nodeSize = 1;
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;
// 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();
}
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();