X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=inline;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Fnodes%2FDistrictNetworkEdgeNode.java;h=0e5f30222a6f72682640dd0e70b0ae769ff727de;hb=refs%2Fchanges%2F84%2F4484%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..0e5f3022 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
@@ -8,15 +8,16 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
+import java.util.Optional;
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.G2DRenderingHints;
import org.simantics.scenegraph.g2d.nodes.SVGNode;
import org.simantics.scenegraph.utils.GeometryUtils;
import org.simantics.scenegraph.utils.NodeUtil;
@@ -25,17 +26,21 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
private static final long serialVersionUID = 8049769475036519806L;
- private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
+ public 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 transient Path2D path;
+ private transient Path2D detailedPath;
- private boolean scaleStroke = true;
+ private transient int zoomLevel = 0;
+
+ static final boolean scaleStroke = true;
private Color color;
private Double stroke;
private transient Color dynamicColor = null;
+ private transient Color eventColor = null;
// Dimensions for shut-off valve symbol
private static final double left = -0.25;
@@ -44,11 +49,22 @@ 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 Point2D detailedCenterPoint;
+ private transient Point2D direction;
+ private transient Point2D detailedDirection;
+
private transient Rectangle2D symbolRect;
private transient AffineTransform symbolTransform;
-
+
+ private boolean hidden = false;
+
+ private static double startX;
+ private static double startY;
+ private static double endX;
+ private static double endY;
+
@Override
public void init() {
}
@@ -57,95 +73,120 @@ 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);
+ }
}
- 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();
+ zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
- BasicStroke bs = null;
- 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));
- } else {
- bs = STROKE;
- }
- int zoomLevel = MapScalingTransform.zoomLevel(ot);
- path = calculatePath(edge, path, zoomLevel > 15);
+ if (!hidden) {
+ 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();
+
+ BasicStroke bs = null;
+ if (scaleStroke) {
+ bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
+ } else {
+ bs = STROKE;
+ }
- if (isSelected()) {
- g2d.setColor(SELECTION_COLOR);
- g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
+ Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path;
+
+ if (isSelected()) {
+ g2d.setColor(SELECTION_COLOR);
+ g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
+ g2d.draw(path);
+ }
+
+ g2d.setColor(dynamicColor != null ? dynamicColor : color);
+ g2d.setColor(eventColor != null ? eventColor : g2d.getColor());
+ g2d.setStroke(bs);
g2d.draw(path);
+
+ // Reset
+ g2d.setStroke(oldStroke);
+ g2d.setColor(oldColor);
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
}
- g2d.setColor(dynamicColor != null ? dynamicColor : color);
- g2d.setStroke(bs);
- g2d.draw(path);
-
- // 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);
-
+ Point2D centerP = getCenterPoint(zoomLevel);
for (INode nn : getNodes()) {
- G2DNode g2dNode = (G2DNode)nn;
- g2dNode.setTransform(symbolTransform);
+ 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 = scaleStroke ? 10 / scale : 10;
+ symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
+ symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
+ 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());
-// 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;
-// }
+ 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);
+ 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 renderDetailed(zoomLevel) ? detailedPath : path;
+ }
+
+ public Point2D getCenterPoint(int zoomLevel) {
+ return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
+ }
+
+ public Point2D getDirection(int zoomLevel) {
+ return renderDetailed(zoomLevel) ? detailedDirection : direction;
+ }
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();
@@ -157,7 +198,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
@@ -185,12 +225,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();
}
@@ -212,11 +271,50 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
this.dynamicColor = color;
}
+ @PropertySetter(value = "eventColor")
+ public void setEventColor(Color color) {
+ this.eventColor = color;
+ }
+
+ @PropertySetter(value = "arrowLength")
+ public void setArrowLength(Double length) {
+ // find if there is a child deferred arrow node
+ DistrictNetworkEdgeArrayNode child = getOrCreateNode(DistrictNetworkEdgeArrayNode.NODE_KEY, DistrictNetworkEdgeArrayNode.class);
+ child.setEdgeNode(this);
+ child.setArrowLength(length);
+ //arrowLength = length;
+ }
+
@PropertySetter(value = "SVG")
public void setSVG(String value) {
for (INode nn : this.getNodes())
if (nn instanceof SVGNode)
((SVGNode)nn).setData(value);
}
-
+
+ @PropertySetter(value = "hidden")
+ public void setHidden(Boolean value) {
+ this.hidden = value;
+ }
+
+ public void setStaticInformation(Optional staticInformation) {
+ DistrictNetworkStaticInfoNode child = getOrCreateNode(DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class);
+ child.setEdgeNode(this);
+ if (staticInformation.isPresent()) {
+ child.setInfo(staticInformation.get());
+ } else {
+ child.setInfo(null);
+ }
+ }
+
+ public void setInSimulation(Optional isInSimulation) {
+ if (!isInSimulation.isPresent()) {
+ removeNode(NotInSimulationNode.NODE_NAME);
+ } else {
+ NotInSimulationNode child = getOrCreateNode(NotInSimulationNode.NODE_NAME, NotInSimulationNode.class);
+ child.setZIndex(1000);
+ child.setIsInSimulation(isInSimulation.get());
+ }
+ }
+
}