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=e724a68dc46b0063e1129ae9d31ba744083416fc;hb=2d1fced131d496b57dae5d5422f6e250ae9ddf5c;hp=a089ecaf149e61653a6849c6bf536a523c94aab3;hpb=fb4e5f02481e918807be83e87239a4d91b4343f2;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 a089ecaf..e724a68d 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
@@ -13,12 +13,11 @@ 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.INode.PropertySetter;
import org.simantics.scenegraph.g2d.G2DNode;
import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.g2d.G2DRenderingHints;
import org.simantics.scenegraph.g2d.nodes.SVGNode;
import org.simantics.scenegraph.utils.GeometryUtils;
import org.simantics.scenegraph.utils.NodeUtil;
@@ -33,8 +32,11 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
private DistrictNetworkEdge edge;
private Rectangle2D bounds;
private transient Path2D path;
+ private transient Path2D detailedPath;
- private boolean scaleStroke = true;
+ private transient int zoomLevel = 0;
+
+ private static boolean scaleStroke = true;
private Color color;
private Double stroke;
private transient Color dynamicColor = null;
@@ -48,11 +50,15 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
private transient Point2D centerPoint;
+ private transient Point2D detailedCenterPoint;
+ private transient Point2D direction;
+ private transient Point2D detailedDirection;
+
private transient Rectangle2D symbolRect;
private transient AffineTransform symbolTransform;
private boolean hidden = false;
-
+
private Double arrowLength;
private static double startX;
@@ -60,6 +66,8 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
private static double endX;
private static double endY;
+ private Path2D arrowPath;
+
@Override
public void init() {
}
@@ -68,17 +76,23 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
public void render(Graphics2D g2d) {
AffineTransform ot = null;
AffineTransform t = getTransform();
+ double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
if (t != null && !t.isIdentity()) {
- ot = g2d.getTransform();
- g2d.transform(getTransform());
+ //ot = g2d.getTransform();
+ ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
+ if (ot == null)
+ ot = g2d.getTransform();
+ g2d.transform(t);
+ if (scaleStroke) {
+ AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
+ work.setTransform(ot);
+ work.concatenate(t);
+ scale = DistrictNetworkNodeUtils.getScale(work);
+ }
}
- double scale = 1.0;
- if (scaleStroke) {
- AffineTransform tr = g2d.getTransform();
- scale = DistrictNetworkNodeUtils.getScale(tr);
- }
-
+ zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
+
if (!hidden) {
Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
@@ -92,9 +106,8 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
} else {
bs = STROKE;
}
-
- int zoomLevel = MapScalingTransform.zoomLevel(ot);
- path = calculatePath(edge, path, zoomLevel > 15);
+
+ Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path;
if (isSelected()) {
g2d.setColor(SELECTION_COLOR);
@@ -111,61 +124,76 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
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;
-
+
+ Point2D centerPoint = getCenterPoint(zoomLevel);
+ Point2D direction = getDirection(zoomLevel);
+ double centerX = centerPoint.getX(), centerY = centerPoint.getY();
+ double deltaX = direction.getX(), deltaY = direction.getY();
+
+ // Ensure the line is always rendered on top of the edge
+ // to prevent overlap with static info rendered below it.
+ double odx = offset * deltaY;
+ double ody = offset * deltaX;
+ if (odx < 0) {
+ odx = -odx;
+ ody = -ody;
+ }
+
+ double x0 = centerX - l/2 * deltaX + ody;
+ double y0 = centerY - l/2 * deltaY - odx;
+ double x1 = centerX + (l/2 - w) * deltaX + ody;
+ double y1 = centerY + (l/2 - w) * deltaY - odx;
+
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);
+
+ arrowPath = new Path2D.Double();
+ arrowPath.moveTo(x1 + w * deltaX, y1 + w * deltaY);
+ arrowPath.lineTo(x1 + w * deltaY, y1 - w * deltaX);
+ arrowPath.lineTo(x1 - w * deltaY, y1 + w * deltaX);
+ arrowPath.closePath();
+ g2d.fill(arrowPath);
}
-
+
// Reset
g2d.setStroke(oldStroke);
g2d.setColor(oldColor);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
}
-
+
+ Point2D centerP = getCenterPoint(zoomLevel);
for (INode nn : getNodes()) {
- G2DNode g2dNode = (G2DNode)nn;
+ G2DNode g2dNode = (G2DNode) nn;
if (g2dNode instanceof SVGNode) {
+ // If the node is hidden from the start, then the center point cannot be calculated yet.
+ if (!isDefined(centerP))
+ break;
+
// Render SVG symbol
- double viewScaleRecip = 10;
- if (scaleStroke) {
- viewScaleRecip /= scale;
- }
-
- Point2D p = getCenterPoint();
- symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
+ double viewScaleRecip = scaleStroke ? 10 / scale : 10;
+ symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
-
g2dNode.setTransform(symbolTransform);
}
g2dNode.render(g2d);
}
-
+
if (ot != null)
g2d.setTransform(ot);
}
+ static boolean isDefined(Point2D p) {
+ return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
+ }
+
+ boolean renderDetailed(int zoomLevel) {
+ return zoomLevel > 13;
+ }
+
public float getStrokeWidth(AffineTransform tr, boolean selection) {
double scale = DistrictNetworkNodeUtils.getScale(tr);
float width = STROKE.getLineWidth() * getStrokeWidth(scale);
@@ -185,30 +213,16 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
}
public Path2D getPath() {
- return path;
+ return renderDetailed(zoomLevel) ? detailedPath : path;
}
-
- private Point2D getCenterPoint() {
- if (centerPoint == null)
- centerPoint = new Point2D.Double();
-
- Rectangle2D bounds = path.getBounds2D();
- centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
- return centerPoint;
+
+ public Point2D getCenterPoint(int zoomLevel) {
+ return renderDetailed(zoomLevel) ? detailedCenterPoint : 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 Point2D getDirection(int zoomLevel) {
+ return renderDetailed(zoomLevel) ? detailedDirection : direction;
+ }
public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
@@ -226,7 +240,6 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
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
@@ -254,12 +267,31 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
bounds = calculateBounds(oldBounds);
}
+ /**
+ * Calculates conservative bounds of the node provided by
+ * detailedPath
. These might be larger than the simplified bounds
+ * of path
but the conservative bounds ensure that spatial
+ * optimizations work properly.
+ */
private Rectangle2D calculateBounds(Rectangle2D rect) {
- return calculatePath(edge, null, false).getBounds2D();
+ if (detailedPath == null)
+ detailedPath = calculatePath(edge, detailedPath, true);
+ return detailedPath.getBounds2D();
}
public void setDNEdge(DistrictNetworkEdge edge) {
this.edge = edge;
+ path = calculatePath(edge, path, false);
+ detailedPath = calculatePath(edge, detailedPath, true);
+
+ centerPoint = new Point2D.Double();
+ detailedCenterPoint = new Point2D.Double();
+ direction = new Point2D.Double();
+ detailedDirection = new Point2D.Double();
+
+ DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
+ DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
+
updateBounds();
}
@@ -280,9 +312,9 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
public void setDynamicColor(Color color) {
this.dynamicColor = color;
}
-
+
@PropertySetter(value = "arrowLength")
- public void setArroLength(Double length) {
+ public void setArrowLength(Double length) {
arrowLength = length;
}
@@ -292,10 +324,10 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
if (nn instanceof SVGNode)
((SVGNode)nn).setData(value);
}
-
@PropertySetter(value = "hidden")
public void setHidden(Boolean value) {
this.hidden = value;
}
+
}