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%2FDistrictNetworkStaticInfoNode.java;h=a8544a9b1858032e85c293a8f6e62fe1260abda6;hb=a995d5d46f03f25bd77fb3f6d8349839886b2ec2;hp=94b4e89613363f31027683067b3ae49d9910e45e;hpb=db03c6473f55b3ed34d3158376f9af267d37b22c;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java index 94b4e896..a8544a9b 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java @@ -2,40 +2,41 @@ package org.simantics.district.network.ui.nodes; import java.awt.Color; import java.awt.Font; +import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; -import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; -import org.simantics.district.network.ui.styles.DistrictNetworkStaticInfoStyle; -import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.ParentNode; import org.simantics.scenegraph.g2d.G2DNode; -import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode; import org.simantics.scenegraph.utils.DPIUtil; import org.simantics.scenegraph.utils.NodeUtil; public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode { - private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5)); - - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = -1723122278582600873L; - private static final Point2D UNIT_X = new Point2D.Double(1.0, 0.0); + private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, DPIUtil.upscale(10)); public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO"; - + + public static final String STATIC_INFO_DEFERRED = "staticInfo"; + String info = null; Point2D origin = new Point2D.Double(); - Point2D direction = UNIT_X; - + Point2D direction = new Point2D.Double(); + + private DistrictNetworkEdgeNode edgeNode = null; + + private int prevZoomLevel = -1; + @Override public void render(Graphics2D g) { ParentNode root = (ParentNode) NodeUtil.getNearestParentOfType(this, RTreeNode.class); - DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(DistrictNetworkStaticInfoStyle.STATIC_INFO_DEFERRED) : null; + DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(STATIC_INFO_DEFERRED) : null; if (deferred != null) deferred.deferNode(g.getTransform(), this); else @@ -46,27 +47,25 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo public void renderDeferred(Graphics2D g) { if (info == null || "".equals(info)) return; - - AffineTransform oldTransform = g.getTransform(); - - // There has to be a better way to calculate the zoom level in this context... - AffineTransform baseTransform; - try { - baseTransform = ((G2DParentNode)getParent()).getTransform().createInverse(); - } catch (NoninvertibleTransformException e) { - baseTransform = new AffineTransform(); - } - - baseTransform.preConcatenate(oldTransform); - int zoomLevel = MapScalingTransform.zoomLevel(baseTransform); + int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL); if (zoomLevel < 17) return; - + + AffineTransform oldTransform = g.getTransform(); + Font oldFont = g.getFont(); Color oldColor = g.getColor(); Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - + + if (edgeNode != null && zoomLevel != prevZoomLevel) { + origin.setLocation(edgeNode.getCenterPoint(zoomLevel)); + Point2D dir = edgeNode.getDirection(zoomLevel); + double s = dir.getX() >= 0.0 ? 1.0 : -1.0; + direction.setLocation(s * dir.getX(), s * dir.getY()); + } + prevZoomLevel = zoomLevel; + doRender(g); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA); @@ -77,22 +76,24 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo private void doRender(Graphics2D g) { g.translate(origin.getX(), origin.getY()); - double scale = 1.5 / g.getTransform().getScaleX(); + double scale = 1.0 / g.getTransform().getScaleX(); g.scale(scale, scale); g.setFont(FONT); - int width1 = g.getFontMetrics().stringWidth(info); -// int height = g.getFontMetrics().getHeight(); - + FontMetrics fm = g.getFontMetrics(); + int width = fm.stringWidth(info); + int ascent = fm.getMaxAscent(); + g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY())); - g.translate(0, 10); - + g.translate(0, ascent+2); + +// int height = fm.getHeight(); // g.setColor(Color.WHITE); -// g.fillRect(-width1/2 - 5, -height-2, width1+10, height+4); - +// g.fillRect(-width/2 - 5, -ascent-1, width+10, height+2); g.setColor(Color.BLACK); - - g.drawString(info, -width1/2, 0); +// g.drawRect(-width/2 - 5, -ascent-1, width+10, height+2); + + g.drawString(info, -width/2, 0); } @Override @@ -101,11 +102,17 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo } public void setLocation(Point2D origin, Point2D direction) { - this.origin = origin; - this.direction = direction; + this.origin.setLocation(origin); + this.direction.setLocation(direction); } public void setInfo(String info) { this.info = info; } + + public void setEdgeNode(DistrictNetworkEdgeNode n) { + this.edgeNode = n; + // Ensure that origin/location are recalculated + prevZoomLevel = -1; + } }