]> gerrit.simantics Code Review - simantics/district.git/blob
51ae30e07f58113363020d4f34b248b10c4ad331
[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.G2DRenderingHints;
18 import org.simantics.scenegraph.g2d.IG2DNode;
19 import org.simantics.scenegraph.g2d.nodes.SVGNode;
20 import org.simantics.scenegraph.utils.GeometryUtils;
21 import org.simantics.scenegraph.utils.NodeUtil;
22
23 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
24
25     //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
26
27     private static final long serialVersionUID = -2641639101400236719L;
28
29     private static final double left = -15;
30     private static final double top = left;
31     public static final double width = 30;
32     private static final double height = width;
33
34     private static final BasicStroke STROKE           = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
35     private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
36
37     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
38     private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
39
40     private DistrictNetworkVertex vertex;
41
42     private static boolean scaleStroke = true;
43     private boolean hover;
44
45     private Color color;
46     private transient Color dynamicColor;
47
48     private Rectangle2D bounds;
49     private transient Point2D point;
50     private transient Rectangle2D rect;
51     private transient AffineTransform symbolTransform;
52
53     private double nodeSize = 1.0;
54
55     private boolean hidden = false;
56
57     @Override
58     public void init() {
59         setZIndex(2);
60     }
61
62     @Override
63     public void render(Graphics2D g2d) {
64         AffineTransform ot = null;
65         AffineTransform t = getTransform();
66         double viewScaleRecip = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_RECIPROCAL_UNDER_SPATIAL_ROOT) : 1.0;
67         if (t != null && !t.isIdentity()) {
68             //ot = g2d.getTransform();
69             ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
70             g2d.transform(t);
71
72             if (scaleStroke) {
73                 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
74                 work.setTransform(ot);
75                 work.concatenate(t);
76                 viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
77             }
78         }
79
80         // Translate lat and lon to X and Y
81         Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
82
83         if (!hidden && nodeSize > 0.0) {
84             Object oaaHint = null;
85             Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
86             if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
87                 oaaHint = aaHint;
88                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
89             }
90     
91             Color oldColor = g2d.getColor();
92             Color newColor = dynamicColor != null ? dynamicColor : color;
93             boolean changeColor = !oldColor.equals(newColor);
94     
95             double scaleRecip = viewScaleRecip * nodeSize;
96     
97             Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
98     
99             if (NodeUtil.isSelected(this, 1)) {
100                 changeColor = true;
101                 g2d.setColor(SELECTION_COLOR);
102                 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
103                 g2d.setStroke(ss);
104                 g2d.draw(toDraw);
105             }
106     
107             // render
108             if (changeColor)
109                 g2d.setColor(newColor);
110             g2d.fill(toDraw);
111             
112             // Reset settings
113             if (changeColor)
114                 g2d.setColor(oldColor);
115             if (oaaHint != null)
116                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
117         }
118
119         // Render SVG symbol
120         for (INode nn : getNodes()) {
121             G2DNode g2dNode = (G2DNode)nn;
122             if (nn instanceof SVGNode) {
123                 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
124                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
125                 g2dNode.setTransform(symbolTransform);
126             }
127             
128             g2dNode.render(g2d);
129         }
130         
131         if (ot != null)
132             g2d.setTransform(ot);
133     }
134
135     @Override
136     public Rectangle2D getBounds() {
137         return super.getBounds();
138     }
139
140     @Override
141     public Rectangle2D getBoundsInLocal() {
142         updateBounds();
143         return bounds;
144     }
145
146     private void updateBounds() {
147         Rectangle2D oldBounds = bounds;
148         if (oldBounds == null)
149             oldBounds = new Rectangle2D.Double();
150         bounds = calculateBounds(oldBounds);
151     }
152
153     @Override
154     public void setTransform(AffineTransform transform) {
155         super.setTransform(transform);
156         // Update bounds
157         updateBounds();
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 }