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