package org.simantics.district.network.ui.nodes; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import org.simantics.district.network.ui.adapters.DistrictNetworkVertex; 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 DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode { //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class); private static final long serialVersionUID = -2641639101400236719L; private static final double left = -0.00005; private static final double top = left; public static final double width = 0.0001; private static final double height = width; private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96); private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height); private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3); private DistrictNetworkVertex vertex; private boolean scaleStroke = true; private boolean hover; private Color color; private transient Color dynamicColor; private Rectangle2D bounds; private transient Point2D point; private transient Rectangle2D rect; private transient AffineTransform symbolTransform; private double nodeSize = 1; @Override public void init() { setZIndex(2); } @Override public void render(Graphics2D g2d) { if (nodeSize <= 0.0) return; AffineTransform ot = null; AffineTransform t = getTransform(); if (t != null && !t.isIdentity()) { ot = g2d.getTransform(); g2d.transform(t); } Object oaaHint = null; Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) { oaaHint = aaHint; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); } Color oldColor = g2d.getColor(); Color newColor = dynamicColor != null ? dynamicColor : color; boolean changeColor = !oldColor.equals(newColor); double viewScaleRecip = 1; if (scaleStroke) { viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform()); } double scaleRecip = viewScaleRecip * nodeSize; // Translate lat and lon to X and Y Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point); Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip); if (NodeUtil.isSelected(this, 1)) { changeColor = true; g2d.setColor(SELECTION_COLOR); BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) (viewScaleRecip*0.5)); g2d.setStroke(ss); g2d.draw(toDraw); } // render if (changeColor) g2d.setColor(newColor); g2d.fill(toDraw); // Reset settings if (changeColor) g2d.setColor(oldColor); if (oaaHint != null) g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); // Render SVG symbol for (INode nn : getNodes()) { G2DNode g2dNode = (G2DNode)nn; if (nn instanceof SVGNode) { symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform); g2dNode.setTransform(symbolTransform); } g2dNode.render(g2d); } if (ot != null) g2d.setTransform(ot); } @Override public Rectangle2D getBounds() { return super.getBounds(); } @Override public Rectangle2D getBoundsInLocal() { return bounds; } private void updateBounds() { Rectangle2D oldBounds = bounds; if (oldBounds == null) oldBounds = new Rectangle2D.Double(); bounds = calculateBounds(oldBounds); } @Override public void setTransform(AffineTransform transform) { super.setTransform(transform); // Update bounds updateBounds(); } @Override public AffineTransform getTransform() { return super.getTransform(); } private Rectangle2D calculateBounds(Rectangle2D rect) { Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point); AffineTransform at = getTransform(); double x = calcPoint.getX(); double y = calcPoint.getY(); double widthh = width / at.getScaleX(); double heighth = height / at.getScaleY(); return new Rectangle2D.Double(x - widthh, y - heighth, widthh * 2, heighth * 2).getBounds2D(); } public void setVertex(DistrictNetworkVertex vertex) { this.vertex = vertex; updateBounds(); } public boolean hover(boolean hover) { // if (hover && LOGGER.isDebugEnabled()) // LOGGER.debug("Hovering " + this); boolean changed = false; if (this.hover != hover) { this.hover = hover; changed = true; } return changed; } public void setColor(Color color) { this.color = color; } public Color getColor() { return color; } @PropertySetter(value = "SVG") public void setSVG(String value) { for (INode nn : this.getNodes()) if (nn instanceof SVGNode) ((SVGNode)nn).setData(value); } @PropertySetter(value = "size") public void setSize(Double size) { boolean changed = false; if (size != null) { changed = size != this.nodeSize; this.nodeSize = size; } else { changed = this.nodeSize != 1.0; this.nodeSize = 1.0; } if (changed) updateBounds(); } @PropertySetter(value = "dynamicColor") public void setDynamicColor(Color color) { this.dynamicColor = color; } }