]> gerrit.simantics Code Review - simantics/district.git/blob
2e2b4293871114144b53fb50e73d99065ac7c10e
[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.geom.AffineTransform;
8 import java.awt.geom.Point2D;
9 import java.awt.geom.Rectangle2D;
10
11 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
12 import org.simantics.maps.MapScalingTransform;
13 import org.simantics.scenegraph.INode;
14 import org.simantics.scenegraph.ISelectionPainterNode;
15 import org.simantics.scenegraph.g2d.G2DNode;
16 import org.simantics.scenegraph.g2d.G2DParentNode;
17 import org.simantics.scenegraph.g2d.IG2DNode;
18 import org.simantics.scenegraph.g2d.nodes.SVGNode;
19 import org.simantics.scenegraph.utils.GeometryUtils;
20 import org.simantics.scenegraph.utils.NodeUtil;
21
22 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
23
24     //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
25
26     private static final long serialVersionUID = -2641639101400236719L;
27
28     private static final double left = -15;
29     private static final double top = left;
30     public static final double width = 30;
31     private static final double height = width;
32
33     private static final BasicStroke STROKE           = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
34     private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
35
36     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
37     private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
38
39     private DistrictNetworkVertex vertex;
40
41     private boolean scaleStroke = true;
42     private boolean hover;
43
44     private Color color;
45     private transient Color dynamicColor;
46
47     private Rectangle2D bounds;
48     private transient Point2D point;
49     private transient Rectangle2D rect;
50     private transient AffineTransform symbolTransform;
51
52     private double nodeSize = 1.0;
53
54     private boolean hidden = false;
55
56     @Override
57     public void init() {
58         setZIndex(2);
59     }
60
61     @Override
62     public void render(Graphics2D g2d) {
63         AffineTransform ot = null;
64         AffineTransform t = getTransform();
65         if (t != null && !t.isIdentity()) {
66             ot = g2d.getTransform();
67             g2d.transform(t);
68         }
69
70         // Translate lat and lon to X and Y
71         Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
72         
73         double viewScaleRecip = 1;
74         if (scaleStroke) {
75             viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform());
76         }
77         
78         if (!hidden && nodeSize > 0.0) {
79             Object oaaHint = null;
80             Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
81             if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
82                 oaaHint = aaHint;
83                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
84             }
85     
86             Color oldColor = g2d.getColor();
87             Color newColor = dynamicColor != null ? dynamicColor : color;
88             boolean changeColor = !oldColor.equals(newColor);
89     
90             double scaleRecip = viewScaleRecip * nodeSize;
91     
92             Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
93     
94             if (NodeUtil.isSelected(this, 1)) {
95                 changeColor = true;
96                 g2d.setColor(SELECTION_COLOR);
97                 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
98                 g2d.setStroke(ss);
99                 g2d.draw(toDraw);
100             }
101     
102             // render
103             if (changeColor)
104                 g2d.setColor(newColor);
105             g2d.fill(toDraw);
106             
107             // Reset settings
108             if (changeColor)
109                 g2d.setColor(oldColor);
110             if (oaaHint != null)
111                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
112         }
113
114         // Render SVG symbol
115         for (INode nn : getNodes()) {
116             G2DNode g2dNode = (G2DNode)nn;
117             if (nn instanceof SVGNode) {
118                 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
119                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
120                 g2dNode.setTransform(symbolTransform);
121             }
122             
123             g2dNode.render(g2d);
124         }
125         
126         if (ot != null)
127             g2d.setTransform(ot);
128     }
129
130     @Override
131     public Rectangle2D getBounds() {
132         return super.getBounds();
133     }
134
135     @Override
136     public Rectangle2D getBoundsInLocal() {
137         updateBounds();
138         return bounds;
139     }
140
141     private void updateBounds() {
142         Rectangle2D oldBounds = bounds;
143         if (oldBounds == null)
144             oldBounds = new Rectangle2D.Double();
145         bounds = calculateBounds(oldBounds);
146     }
147
148     @Override
149     public void setTransform(AffineTransform transform) {
150         super.setTransform(transform);
151         // Update bounds
152         updateBounds();
153     }
154
155     @Override
156     public AffineTransform getTransform() {
157         return super.getTransform();
158     }
159
160     private Rectangle2D calculateBounds(Rectangle2D rect) {
161         Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
162         AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
163         at.concatenate(MapScalingTransform.INSTANCE);
164         double x = calcPoint.getX();
165         double y = calcPoint.getY();
166         double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
167         double widthh = width * scaleRecip * nodeSize;
168         double heighth = height * scaleRecip * nodeSize;
169         if (rect == null)
170             rect = new Rectangle2D.Double();
171         rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
172         return rect;
173     }
174
175     public void setVertex(DistrictNetworkVertex vertex) {
176         this.vertex = vertex;
177         updateBounds();
178     }
179
180     @Override
181     public boolean hover(boolean hover, boolean isConnectionTool) {
182         // Only react to hover when the connection tool is active
183         boolean doHover = hover && isConnectionTool;
184         boolean changed = this.hover != doHover;
185         this.hover = doHover;
186         
187         for (IG2DNode child : getNodes()) {
188             if (child instanceof HoverSensitiveNode)
189                 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
190         }
191         
192         return changed;
193     }
194     
195     @Override
196     public void setMousePosition(Point2D p) {
197         for (IG2DNode child : getNodes()) {
198             if (child instanceof HoverSensitiveNode)
199                 ((HoverSensitiveNode) child).setMousePosition(p);
200         }
201     }
202
203     public void setColor(Color color) {
204         this.color = color;
205     }
206
207     public Color getColor() {
208         return color;
209     }
210
211     @PropertySetter(value = "SVG")
212     public void setSVG(String value) {
213         for (INode nn : this.getNodes())
214             if (nn instanceof SVGNode)
215                 ((SVGNode)nn).setData(value);
216     }
217     
218     
219     @PropertySetter(value = "size")
220     public void setSize(Double size) {
221         boolean changed = false;
222         if (size != null) {
223             changed = size != this.nodeSize;
224             this.nodeSize = size;
225         } else {
226             changed = this.nodeSize != 1.0;
227             this.nodeSize = 1.0;
228         }
229         if (changed)
230             updateBounds();
231     }
232
233     @PropertySetter(value = "dynamicColor")
234     public void setDynamicColor(Color color) {
235         this.dynamicColor = color;
236     }
237
238     @PropertySetter(value = "hidden")
239     public void setHidden(Boolean value) {
240         this.hidden = value;
241     }
242 }