]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java
First prototype of HSV color space based dynamic DN element coloring
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DistrictNetworkVertexNode.java
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.ModelledCRS;
12 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
13 import org.simantics.scenegraph.ISelectionPainterNode;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.utils.GeometryUtils;
16 import org.simantics.scenegraph.utils.NodeUtil;
17
18 public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPainterNode {
19
20     //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
21
22     private static final long serialVersionUID = -2641639101400236719L;
23
24     private static final BasicStroke STROKE           = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
25     private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
26
27     private static final double left = -0.25;
28     private static final double top = -0.25;
29     private static final double width = 0.5;
30     private static final double height = 0.5;
31
32     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
33     private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
34
35     private DistrictNetworkVertex vertex;
36
37     private boolean scaleStroke = true;
38     private boolean hover;
39
40     private Color color;
41     private transient Color dynamicColor;
42
43     private Rectangle2D bounds;
44
45     private double nodeSize = 1;
46
47     @Override
48     public void init() {
49         setZIndex(2);
50     }
51
52     @Override
53     public void render(Graphics2D g2d) {
54         if (nodeSize <= 0.0)
55             return;
56
57         AffineTransform ot = null;
58         AffineTransform t = getTransform();
59         if (t != null && !t.isIdentity()) {
60             ot = g2d.getTransform();
61             g2d.transform(getTransform());
62         }
63
64         Object oaaHint = null;
65         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
66         if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
67             oaaHint = aaHint;
68             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
69         }
70
71         Color oldColor = g2d.getColor();
72
73         double scaleRecip = 1;
74         if (scaleStroke) {
75             double scale = GeometryUtils.getScale(g2d.getTransform());
76             scale = Math.max(10000, Math.min(scale, 50000));
77             scaleRecip = 1.0 / scale;
78         }
79         scaleRecip = scaleRecip * nodeSize;
80
81         // Translate lat and lon to X and Y
82         Point2D res = calculatePoint2D(vertex);
83
84         Rectangle2D toDraw;
85         if (hover) {
86             toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
87         } else {
88             toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
89         }
90
91         if (NodeUtil.isSelected(this, 1)) {
92             g2d.setColor(SELECTION_COLOR);
93             BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) scaleRecip*2);
94             g2d.setStroke(ss);
95             g2d.draw(toDraw);
96         }
97
98         // render
99         g2d.setColor(dynamicColor != null ? dynamicColor : color);
100         g2d.fill(toDraw);
101
102         // Reset settings
103         g2d.setColor(oldColor);
104         if (oaaHint != null)
105            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
106         if (ot != null)
107             g2d.setTransform(ot);
108     }
109
110     @Override
111     public Rectangle2D getBounds() {
112         return super.getBounds();
113     }
114
115     @Override
116     public Rectangle2D getBoundsInLocal() {
117         return bounds;
118     }
119
120     private void updateBounds() {
121         Rectangle2D oldBounds = bounds;
122         if (oldBounds == null)
123             oldBounds = new Rectangle2D.Double();
124         bounds = calculateBounds(oldBounds);
125     }
126
127     @Override
128     public void setTransform(AffineTransform transform) {
129         super.setTransform(transform);
130         // Update bounds
131         updateBounds();
132     }
133
134     @Override
135     public AffineTransform getTransform() {
136         return super.getTransform();
137     }
138
139     private Rectangle2D calculateBounds(Rectangle2D rect) {
140         Point2D calcPoint = calculatePoint2D(vertex);
141         AffineTransform at = getTransform();
142         return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), width / at.getScaleX(), height / at.getScaleY()).getBounds2D();
143     }
144
145     private static Point2D calculatePoint2D(DistrictNetworkVertex vertex) {
146         Point2D point= vertex.getPoint();
147         double x = ModelledCRS.longitudeToX(point.getX());
148         double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
149
150         // Apply the scaling
151         Point2D res = new Point2D.Double(x, y);
152         return res;
153     }
154
155     public void setVertex(DistrictNetworkVertex vertex) {
156         this.vertex = vertex;
157         updateBounds();
158     }
159
160     public boolean hover(boolean hover) {
161 //        if (hover && LOGGER.isDebugEnabled())
162 //            LOGGER.debug("Hovering " + this);
163         boolean changed = false;
164         if (this.hover != hover) {
165             this.hover = hover;
166             changed = true;
167         }
168         return changed;
169     }
170
171     public void setColor(Color color) {
172         this.color = color;
173     }
174
175     public Color getColor() {
176         return color;
177     }
178
179     @PropertySetter(value = "size")
180     public void setSize(Double size) {
181         boolean changed = false;
182         if (size != null) {
183             changed = size != this.nodeSize;
184             this.nodeSize = size;
185         } else {
186             changed = this.nodeSize != 1.0;
187             this.nodeSize = 1.0;
188         }
189         if (changed)
190             updateBounds();
191     }
192
193     @PropertySetter(value = "dynamicColor")
194     public void setDynamicColor(Color color) {
195         this.dynamicColor = color;
196     }
197
198 }