import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
+import org.simantics.district.network.ModelledCRS;
import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
import org.simantics.scenegraph.g2d.G2DNode;
import org.simantics.scenegraph.utils.GeometryUtils;
private static final double height = 1;
private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
- private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
+ private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
private Stroke stroke = new BasicStroke(2);
private boolean scaleStroke = true;
private boolean hover;
+ private Color color;
+
+ private Rectangle2D bounds;
+
@Override
public void init() {
setZIndex(2);
Color oldColor = g2d.getColor();
Stroke oldStroke = g2d.getStroke();
- g2d.setColor(Color.RED);
+ g2d.setColor(color);
double scaleRecip = 1;
if (scaleStroke) {
}
scaleRecip = 8.0 * scaleRecip;
+ // Translate lat and lon to X and Y
+ Point2D res = calculatePoint2D(vertex);
+
Rectangle2D toDraw;
if (hover) {
- toDraw = new Rectangle2D.Double(HOVERED.getX() * scaleRecip, HOVERED.getY() * scaleRecip, HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
+ toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
} else {
- toDraw = new Rectangle2D.Double(NORMAL.getX() * scaleRecip, NORMAL.getY() * scaleRecip, NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
+ toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
}
// render
g2d.fill(toDraw);
g2d.setTransform(ot);
}
+ @Override
+ public Rectangle2D getBounds() {
+ return super.getBounds();
+ }
+
@Override
public Rectangle2D getBoundsInLocal() {
- return NORMAL;
+ 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();
+ }
+
+ private Rectangle2D calculateBounds(Rectangle2D rect) {
+ Point2D calcPoint = calculatePoint2D(vertex);
+ AffineTransform at = getTransform();
+ return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), 1 / at.getScaleX(), 1 / at.getScaleY()).getBounds2D();
+ }
+
+ private static Point2D calculatePoint2D(DistrictNetworkVertex vertex) {
+ Point2D point= vertex.getPoint();
+ double x = ModelledCRS.longitudeToX(point.getX());
+ double y = ModelledCRS.latitudeToY(point.getY());
+
+ // Apply the scaling
+ Point2D res = new Point2D.Double(x, y);
+ return res;
}
public void setVertex(DistrictNetworkVertex vertex) {
this.vertex = vertex;
+ updateBounds();
}
-
+
public boolean hover(boolean hover) {
// if (hover && LOGGER.isDebugEnabled())
// LOGGER.debug("Hovering " + this);
return changed;
}
+ public void setColor(Color color) {
+ this.color = color;
+ }
+
+ public Color getColor() {
+ return color;
+ }
+
}