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