]> gerrit.simantics Code Review - simantics/district.git/blob
e9d0713ddb2d4bb34758ad8e279cacae64308263
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics2D;
6 import java.awt.RenderingHints;
7 import java.awt.Stroke;
8 import java.awt.geom.AffineTransform;
9 import java.awt.geom.Point2D;
10 import java.awt.geom.Rectangle2D;
11
12 import org.simantics.district.network.ModelledCRS;
13 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.utils.GeometryUtils;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class DistrictNetworkVertexNode extends G2DNode {
20
21     private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
22     
23     private static final long serialVersionUID = -2641639101400236719L;
24     private DistrictNetworkVertex vertex;
25
26     private static final double left = -0.5;
27     private static final double top = -0.5;
28     private static final double width = 1;
29     private static final double height = 1;
30     
31     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
32     private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
33     
34     private Stroke stroke = new BasicStroke(2);
35     private boolean scaleStroke = true;
36     private boolean hover;
37
38     private Color color;
39
40     @Override
41     public void init() {
42         setZIndex(2);
43     }
44     
45     @Override
46     public void render(Graphics2D g2d) {
47         AffineTransform ot = null;
48         AffineTransform t = getTransform();
49         if (t != null && !t.isIdentity()) {
50             ot = g2d.getTransform();
51             g2d.transform(getTransform());
52         }
53         
54         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
55         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
56         
57         Color oldColor = g2d.getColor();
58         Stroke oldStroke = g2d.getStroke();
59         
60         g2d.setColor(color);
61         
62         double scaleRecip = 1;
63         if (scaleStroke) {
64             double scale = GeometryUtils.getScale(g2d.getTransform());
65             
66             //System.out.println("scale: " + scale);
67             scaleRecip = 1.0 / scale;
68         }
69         scaleRecip = 8.0 * scaleRecip;
70         
71         Rectangle2D toDraw;
72         if (hover) {
73             toDraw = new Rectangle2D.Double(HOVERED.getX() * scaleRecip, HOVERED.getY() * scaleRecip, HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
74         } else {
75             toDraw = new Rectangle2D.Double(NORMAL.getX() * scaleRecip, NORMAL.getY() * scaleRecip, NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
76         }
77         // render
78         g2d.fill(toDraw);
79         
80         // Reset stats
81         g2d.setColor(oldColor);
82         g2d.setStroke(oldStroke);
83         
84         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
85         
86         if (ot != null)
87             g2d.setTransform(ot);
88     }
89
90     @Override
91     public Rectangle2D getBoundsInLocal() {
92         if (hover)
93             return HOVERED;
94         return NORMAL;
95     }
96
97     public void setVertex(DistrictNetworkVertex vertex) {
98         this.vertex = vertex;
99     }
100     
101     public boolean hover(boolean hover) {
102 //        if (hover && LOGGER.isDebugEnabled())
103 //            LOGGER.debug("Hovering " + this);
104         boolean changed = false;
105         if (this.hover != hover) {
106             this.hover = hover;
107             changed = true;
108         }
109         return changed;
110     }
111
112     public void setColor(Color color) {
113         this.color = color;
114     }
115     
116     public Color getColor() {
117         return color;
118     }
119
120 }