import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
-import java.awt.Stroke;
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.district.network.ui.adapters.DistrictNetworkEdgeElementFactory;
+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.nodes.SVGNode;
import org.simantics.scenegraph.utils.GeometryUtils;
import org.simantics.scenegraph.utils.NodeUtil;
-public class DistrictNetworkEdgeNode extends G2DNode {
+public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode {
private static final long serialVersionUID = 8049769475036519806L;
-
- private static final Stroke SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
- private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
-
+
+ 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 DistrictNetworkEdge edge;
private Rectangle2D bounds;
-
- private static final Stroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
- private boolean scaleStroke = true;
+ private transient Path2D path;
+ private boolean scaleStroke = true;
private Color color;
-
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;
+
+ private Double arrowLength;
+
+ private static double startX;
+ private static double startY;
+ private static double endX;
+ private static double endY;
@Override
public void init() {
}
-
+
@Override
public void render(Graphics2D g2d) {
-
AffineTransform ot = null;
AffineTransform t = getTransform();
if (t != null && !t.isIdentity()) {
ot = g2d.getTransform();
g2d.transform(getTransform());
}
-
+
Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color oldColor = g2d.getColor();
BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
-// boolean selected = isSelected();
-// if (selected) {
-// Path2D selectionPath = edge.getPath();
-// Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
-// g2d.setColor(SELECTION_COLOR);
-// g2d.fill(selectionShape);
-// }
-//
- g2d.setColor(color);
- if (STROKE != null) {
- if (scaleStroke && STROKE instanceof BasicStroke) {
- double str;
- if (stroke != null)
- str = stroke;
- else
- str = 1.0;
- BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (str / GeometryUtils.getScale(g2d.getTransform())));
- g2d.setStroke(bs);
- } else {
- g2d.setStroke(STROKE);
- }
+
+ BasicStroke bs = null;
+ double scale = 1.0;
+ if (scaleStroke) {
+ AffineTransform tr = g2d.getTransform();
+ scale = DistrictNetworkNodeUtils.getScale(tr);
+ bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
+ } else {
+ bs = STROKE;
+ }
+ int zoomLevel = MapScalingTransform.zoomLevel(ot);
+ path = calculatePath(edge, path, zoomLevel > 15);
+
+ if (isSelected()) {
+ g2d.setColor(SELECTION_COLOR);
+ g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
+ g2d.draw(path);
}
- g2d.draw(calculatePath(edge));
+ g2d.setColor(dynamicColor != null ? dynamicColor : color);
+ g2d.setStroke(bs);
+ g2d.draw(path);
+
+ // Draw arrow
+ if (arrowLength != null) {
+ g2d.setColor(Color.BLACK);
+ float lw = STROKE.getLineWidth() / (float)scale;
+ g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
+
+ double l = arrowLength;
+ double w = 2 * (double) lw * Math.signum(l);
+ if (Math.abs(w) > Math.abs(l)) w = l;
+ double offset = 2 * (double) lw;
+
+ double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
+ double deltaX = endX - startX, deltaY = endY - startY;
+ double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
+ deltaX /= length;
+ deltaY /= length;
+
+ double x0 = centerX - l/2 * deltaX + offset * deltaY;
+ double y0 = centerY - l/2 * deltaY - offset * deltaX;
+ double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
+ double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
+
+ g2d.draw(new Line2D.Double(x0, y0, x1, y1));
+
+ Path2D path = new Path2D.Double();
+ path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
+ path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
+ path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
+ path.closePath();
+ g2d.fill(path);
+ }
// Reset
g2d.setStroke(oldStroke);
g2d.setColor(oldColor);
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
+ // Render SVG symbol
+ double viewScaleRecip = 10;
+ if (scaleStroke) {
+ 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;
+ if (g2dNode instanceof SVGNode)
+ g2dNode.setTransform(symbolTransform);
+ g2dNode.render(g2d);
+ }
+
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
+
if (ot != null)
g2d.setTransform(ot);
}
+
+ public float getStrokeWidth(AffineTransform tr, boolean selection) {
+ double scale = DistrictNetworkNodeUtils.getScale(tr);
+ float width = STROKE.getLineWidth() * getStrokeWidth(scale);
+ if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
+ return width;
+ }
- public static Path2D calculatePath(DistrictNetworkEdge edge) {
- // Convert to screen coordinates
- double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
- double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
- double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
- double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
-
- // render
- Path2D path = new Path2D.Double();
- path.moveTo(startX, startY);
- path.lineTo(endX, endY);
+ private float getStrokeWidth(double scale) {
+ if (scaleStroke) {
+ double str = stroke != null ? Math.abs(stroke) : 1.0;
+ float strokeWidth = (float) (str / scale);
+ return strokeWidth;
+ }
+ else {
+ return 1.f;
+ }
+ }
+
+ public Path2D getPath() {
return path;
}
+
+ 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());
+// double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
+// double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
+// double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
+//
+// if (result == null)
+// result = new Line2D.Double();
+// result.setLine(startX, startY, endX, endY);
+// return result;
+// }
+
+ public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
+ startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
+ startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
+ endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
+ endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
+
+ if (result == null) {
+ result = new Path2D.Double();
+ } else {
+ result.reset();
+ }
+ result.moveTo(startX, startY);
+ if (detailed) {
+ double[] detailedGeometry = edge.getGeometry();
+ if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
+ // ok, lets do this
+
+ for (int i = 0; i < detailedGeometry.length; i += 2) {
+ double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
+ double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
+ result.lineTo(x, y);
+ }
+ }
+ }
+ result.lineTo(endX, endY);
+ return result;
+ }
private boolean isSelected() {
return NodeUtil.isSelected(this, 1);
public Rectangle2D getBoundsInLocal() {
return bounds;
}
-
+
private void updateBounds() {
Rectangle2D oldBounds = bounds;
if (oldBounds == null)
}
private Rectangle2D calculateBounds(Rectangle2D rect) {
- return calculatePath(edge).getBounds2D();
+ return calculatePath(edge, null, false).getBounds2D();
}
public void setDNEdge(DistrictNetworkEdge edge) {
public void setColor(Color color) {
this.color = color;
}
-
+
public Color getColor() {
return color;
}
@PropertySetter(value = "stroke")
public void setStroke(Double stroke) {
- this.stroke = stroke / 100;
+ this.stroke = stroke;
}
+ @PropertySetter(value = "dynamicColor")
+ public void setDynamicColor(Color color) {
+ this.dynamicColor = color;
+ }
+
+ @PropertySetter(value = "arrowLength")
+ public void setArroLength(Double length) {
+ arrowLength = length;
+ }
+
+ @PropertySetter(value = "SVG")
+ public void setSVG(String value) {
+ for (INode nn : this.getNodes())
+ if (nn instanceof SVGNode)
+ ((SVGNode)nn).setData(value);
+ }
+
}