]> gerrit.simantics Code Review - simantics/district.git/blob
76151a83fd879b41b1fc8ce7e528f7f6d59d60f8
[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             if (ot == null)
71                 ot = g2d.getTransform();
72             g2d.transform(t);
73
74             if (scaleStroke) {
75                 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
76                 work.setTransform(ot);
77                 work.concatenate(t);
78                 viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
79             }
80         }
81
82         // Translate lat and lon to X and Y
83         Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
84
85         if (!hidden && nodeSize > 0.0) {
86             Object oaaHint = null;
87             Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
88             if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
89                 oaaHint = aaHint;
90                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
91             }
92     
93             Color oldColor = g2d.getColor();
94             Color newColor = dynamicColor != null ? dynamicColor : color;
95             boolean changeColor = !oldColor.equals(newColor);
96     
97             double scaleRecip = viewScaleRecip * nodeSize;
98     
99             Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
100     
101             if (NodeUtil.isSelected(this, 1)) {
102                 changeColor = true;
103                 g2d.setColor(SELECTION_COLOR);
104                 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
105                 g2d.setStroke(ss);
106                 g2d.draw(toDraw);
107             }
108     
109             // render
110             if (changeColor)
111                 g2d.setColor(newColor);
112             g2d.fill(toDraw);
113             
114             // Reset settings
115             if (changeColor)
116                 g2d.setColor(oldColor);
117             if (oaaHint != null)
118                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
119         }
120
121         // Render SVG symbol
122         for (INode nn : getNodes()) {
123             G2DNode g2dNode = (G2DNode)nn;
124             if (nn instanceof SVGNode) {
125                 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
126                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
127                 g2dNode.setTransform(symbolTransform);
128             }
129             
130             g2dNode.render(g2d);
131         }
132         
133         if (ot != null)
134             g2d.setTransform(ot);
135     }
136
137     @Override
138     public Rectangle2D getBounds() {
139         return super.getBounds();
140     }
141
142     @Override
143     public Rectangle2D getBoundsInLocal() {
144         updateBounds();
145         return bounds;
146     }
147
148     private void updateBounds() {
149         Rectangle2D oldBounds = bounds;
150         if (oldBounds == null)
151             oldBounds = new Rectangle2D.Double();
152         bounds = calculateBounds(oldBounds);
153     }
154
155     @Override
156     public void setTransform(AffineTransform transform) {
157         super.setTransform(transform);
158         // Update bounds
159         updateBounds();
160     }
161
162     private Rectangle2D calculateBounds(Rectangle2D rect) {
163         Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
164         AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
165         at.concatenate(MapScalingTransform.INSTANCE);
166         double x = calcPoint.getX();
167         double y = calcPoint.getY();
168         double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
169         double widthh = width * scaleRecip * nodeSize;
170         double heighth = height * scaleRecip * nodeSize;
171         if (rect == null)
172             rect = new Rectangle2D.Double();
173         rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
174         return rect;
175     }
176
177     public void setVertex(DistrictNetworkVertex vertex) {
178         this.vertex = vertex;
179         updateBounds();
180     }
181
182     public DistrictNetworkVertex getVertex() {
183         return vertex;
184     }
185
186     @Override
187     public boolean hover(boolean hover, boolean isConnectionTool) {
188         // Only react to hover when the connection tool is active
189         boolean doHover = hover && isConnectionTool;
190         boolean changed = this.hover != doHover;
191         this.hover = doHover;
192         
193         for (IG2DNode child : getNodes()) {
194             if (child instanceof HoverSensitiveNode)
195                 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
196         }
197         
198         return changed;
199     }
200     
201     @Override
202     public void setMousePosition(Point2D p) {
203         for (IG2DNode child : getNodes()) {
204             if (child instanceof HoverSensitiveNode)
205                 ((HoverSensitiveNode) child).setMousePosition(p);
206         }
207     }
208
209     public void setColor(Color color) {
210         this.color = color;
211     }
212
213     public Color getColor() {
214         return color;
215     }
216
217     @PropertySetter(value = "SVG")
218     public void setSVG(String value) {
219         for (INode nn : this.getNodes())
220             if (nn instanceof SVGNode)
221                 ((SVGNode)nn).setData(value);
222     }
223     
224     
225     @PropertySetter(value = "size")
226     public void setSize(Double size) {
227         boolean changed = false;
228         if (size != null) {
229             changed = size != this.nodeSize;
230             this.nodeSize = size;
231         } else {
232             changed = this.nodeSize != 1.0;
233             this.nodeSize = 1.0;
234         }
235         if (changed)
236             updateBounds();
237     }
238
239     @PropertySetter(value = "dynamicColor")
240     public void setDynamicColor(Color color) {
241         this.dynamicColor = color;
242     }
243
244     @PropertySetter(value = "hidden")
245     public void setHidden(Boolean value) {
246         this.hidden = value;
247     }
248 }