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