]> gerrit.simantics Code Review - simantics/district.git/blob
8dcd5183a6cb1545ad201b681f45fefbe9d5adc6
[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 * 2, top * 2, width * 2, height * 2);
31     
32     private Stroke stroke = new BasicStroke(2);
33     private boolean scaleStroke = true;
34     private boolean hover;
35
36     @Override
37     public void init() {
38         setZIndex(2);
39     }
40     
41     @Override
42     public void render(Graphics2D g2d) {
43         AffineTransform ot = null;
44         AffineTransform t = getTransform();
45         if (t != null && !t.isIdentity()) {
46             ot = g2d.getTransform();
47             g2d.transform(getTransform());
48         }
49         
50         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
51         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
52         
53         Color oldColor = g2d.getColor();
54         Stroke oldStroke = g2d.getStroke();
55         
56         g2d.setColor(Color.RED);
57         
58         double scaleRecip = 1;
59         if (scaleStroke) {
60             double scale = GeometryUtils.getScale(g2d.getTransform());
61             
62             //System.out.println("scale: " + scale);
63             scaleRecip = 1.0 / scale;
64         }
65         scaleRecip = 8.0 * scaleRecip;
66         
67         Rectangle2D toDraw;
68         if (hover) {
69             toDraw = new Rectangle2D.Double(HOVERED.getX() * scaleRecip, HOVERED.getY() * scaleRecip, HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
70         } else {
71             toDraw = new Rectangle2D.Double(NORMAL.getX() * scaleRecip, NORMAL.getY() * scaleRecip, NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
72         }
73         // render
74         g2d.fill(toDraw);
75         
76         // Reset stats
77         g2d.setColor(oldColor);
78         g2d.setStroke(oldStroke);
79         
80         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
81         
82         if (ot != null)
83             g2d.setTransform(ot);
84     }
85
86     @Override
87     public Rectangle2D getBoundsInLocal() {
88         return NORMAL;
89     }
90
91     public void setVertex(DistrictNetworkVertex vertex) {
92         this.vertex = vertex;
93     }
94
95     public boolean hover(boolean hover) {
96 //        if (hover && LOGGER.isDebugEnabled())
97 //            LOGGER.debug("Hovering " + this);
98         boolean changed = false;
99         if (this.hover != hover) {
100             this.hover = hover;
101             changed = true;
102         }
103         return changed;
104     }
105
106 }