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%2FDistrictNetworkEdgeNode.java;h=61aec5ce55cc8285ad64b8b8fafc67984bc3c463;hb=refs%2Fchanges%2F69%2F2869%2F1;hp=a4f348eb11e1314b0b07119a7adf235861b453dc;hpb=e67c653b710b4f752acaeae332a29616bfc6e408;p=simantics%2Fdistrict.git 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 a4f348eb..61aec5ce 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 @@ -5,6 +5,7 @@ import java.awt.Color; import java.awt.Graphics2D; 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; @@ -44,11 +45,18 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection 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() { } @@ -69,11 +77,11 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection BasicStroke oldStroke = (BasicStroke) g2d.getStroke(); BasicStroke bs = null; + double scale = 1.0; if (scaleStroke) { - double scale = GeometryUtils.getScale(g2d.getTransform()); - scale = Math.max(10000, Math.min(scale, 50000)); - double str = stroke != null ? Math.abs(stroke) : 1.0; - bs = GeometryUtils.scaleStroke(STROKE, (float) (str / scale)); + AffineTransform tr = g2d.getTransform(); + scale = DistrictNetworkNodeUtils.getScale(tr); + bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale)); } else { bs = STROKE; } @@ -82,7 +90,7 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection if (isSelected()) { g2d.setColor(SELECTION_COLOR); - g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f)); + g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale))); g2d.draw(path); } @@ -90,6 +98,38 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection 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); @@ -97,8 +137,6 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection // Render SVG symbol double viewScaleRecip = 10; if (scaleStroke) { - double scale = GeometryUtils.getScale(g2d.getTransform()); - scale = Math.max(10000, Math.min(scale, 50000)); viewScaleRecip /= scale; } @@ -118,6 +156,28 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection 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; + } + + 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(); @@ -141,11 +201,10 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection // } public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) { - // 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 + 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(); @@ -211,6 +270,11 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection 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) {