source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java@ 11797

Last change on this file since 11797 was 11797, checked in by bastiK, 7 years ago

see #14495 - fix immutable class non-constant field (by removing it)

  • Property svn:eol-style set to native
File size: 15.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import java.awt.BasicStroke;
5import java.awt.Color;
6import java.awt.Rectangle;
7import java.awt.Stroke;
8import java.util.Objects;
9import java.util.Optional;
10import java.util.stream.IntStream;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
17import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
18import org.openstreetmap.josm.gui.draw.SymbolShape;
19import org.openstreetmap.josm.gui.mappaint.Cascade;
20import org.openstreetmap.josm.gui.mappaint.Environment;
21import org.openstreetmap.josm.gui.mappaint.Keyword;
22import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
23import org.openstreetmap.josm.gui.mappaint.MultiCascade;
24import org.openstreetmap.josm.gui.mappaint.StyleElementList;
25import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProvider;
26import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.SimpleBoxProvider;
27import org.openstreetmap.josm.gui.util.RotationAngle;
28import org.openstreetmap.josm.tools.CheckParameterUtil;
29import org.openstreetmap.josm.tools.Utils;
30
31/**
32 * applies for Nodes and turn restriction relations
33 */
34public class NodeElement extends StyleElement {
35 /**
36 * The image that is used to display this node. May be <code>null</code>
37 */
38 public final MapImage mapImage;
39 /**
40 * The angle that is used to rotate {@link #mapImage}. May be <code>null</code> to indicate no rotation.
41 */
42 public final RotationAngle mapImageAngle;
43 /**
44 * The symbol that should be used for drawing this node.
45 */
46 public final Symbol symbol;
47
48 private static final String[] ICON_KEYS = {ICON_IMAGE, ICON_WIDTH, ICON_HEIGHT, ICON_OPACITY, ICON_OFFSET_X, ICON_OFFSET_Y};
49
50 public static final NodeElement SIMPLE_NODE_ELEMSTYLE;
51 public static final BoxProvider SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER;
52 static {
53 MultiCascade mc = new MultiCascade();
54 mc.getOrCreateCascade("default");
55 SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), 4.1f, true);
56 if (SIMPLE_NODE_ELEMSTYLE == null) throw new AssertionError();
57 SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER = SIMPLE_NODE_ELEMSTYLE.getBoxProvider();
58 }
59
60 public static final StyleElementList DEFAULT_NODE_STYLELIST = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE);
61 public static final StyleElementList DEFAULT_NODE_STYLELIST_TEXT = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE,
62 BoxTextElement.SIMPLE_NODE_TEXT_ELEMSTYLE);
63
64 protected NodeElement(Cascade c, MapImage mapImage, Symbol symbol, float defaultMajorZindex, RotationAngle rotationAngle) {
65 super(c, defaultMajorZindex);
66 this.mapImage = mapImage;
67 this.symbol = symbol;
68 this.mapImageAngle = Objects.requireNonNull(rotationAngle, "rotationAngle");
69 }
70
71 /**
72 * Creates a new node element for the given Environment
73 * @param env The environment
74 * @return The node element style or <code>null</code> if the node should not be painted.
75 */
76 public static NodeElement create(Environment env) {
77 return create(env, 4f, false);
78 }
79
80 private static NodeElement create(Environment env, float defaultMajorZindex, boolean allowDefault) {
81 Cascade c = env.mc.getCascade(env.layer);
82
83 MapImage mapImage = createIcon(env);
84 Symbol symbol = null;
85 if (mapImage == null) {
86 symbol = createSymbol(env);
87 }
88
89 RotationAngle rotationAngle = createRotationAngle(env);
90
91 // optimization: if we neither have a symbol, nor a mapImage
92 // we don't have to check for the remaining style properties and we don't
93 // have to allocate a node element style.
94 if (!allowDefault && symbol == null && mapImage == null) return null;
95
96 return new NodeElement(c, mapImage, symbol, defaultMajorZindex, rotationAngle);
97 }
98
99 /**
100 * Reads the icon-rotation property and creates a rotation angle from it.
101 * @param env The environment
102 * @return The angle
103 * @since 11670
104 */
105 public static RotationAngle createRotationAngle(Environment env) {
106 Cascade c = env.mc.getCascade(env.layer);
107
108 RotationAngle rotationAngle = RotationAngle.NO_ROTATION;
109 final Float angle = c.get(ICON_ROTATION, null, Float.class, true);
110 if (angle != null) {
111 rotationAngle = RotationAngle.buildStaticRotation(angle);
112 } else {
113 final Keyword rotationKW = c.get(ICON_ROTATION, null, Keyword.class);
114 if (rotationKW != null) {
115 if ("way".equals(rotationKW.val)) {
116 rotationAngle = RotationAngle.buildWayDirectionRotation();
117 } else {
118 try {
119 rotationAngle = RotationAngle.buildStaticRotation(rotationKW.val);
120 } catch (IllegalArgumentException ignore) {
121 Main.trace(ignore);
122 }
123 }
124 }
125 }
126 return rotationAngle;
127 }
128
129 /**
130 * Create a map icon for the environment using the default keys.
131 * @param env The environment to read the icon form
132 * @return The icon or <code>null</code> if no icon is defined
133 * @since 11670
134 */
135 public static MapImage createIcon(final Environment env) {
136 return createIcon(env, ICON_KEYS);
137 }
138
139 /**
140 * Create a map icon for the environment.
141 * @param env The environment to read the icon form
142 * @param keys The keys, indexed by the ICON_..._IDX constants.
143 * @return The icon or <code>null</code> if no icon is defined
144 */
145 public static MapImage createIcon(final Environment env, final String... keys) {
146 CheckParameterUtil.ensureParameterNotNull(env, "env");
147 CheckParameterUtil.ensureParameterNotNull(keys, "keys");
148
149 Cascade c = env.mc.getCascade(env.layer);
150
151 final IconReference iconRef = c.get(keys[ICON_IMAGE_IDX], null, IconReference.class, true);
152 if (iconRef == null)
153 return null;
154
155 Cascade cDef = env.mc.getCascade("default");
156
157 Float widthOnDefault = cDef.get(keys[ICON_WIDTH_IDX], null, Float.class);
158 if (widthOnDefault != null && widthOnDefault <= 0) {
159 widthOnDefault = null;
160 }
161 Float widthF = getWidth(c, keys[ICON_WIDTH_IDX], widthOnDefault);
162
163 Float heightOnDefault = cDef.get(keys[ICON_HEIGHT_IDX], null, Float.class);
164 if (heightOnDefault != null && heightOnDefault <= 0) {
165 heightOnDefault = null;
166 }
167 Float heightF = getWidth(c, keys[ICON_HEIGHT_IDX], heightOnDefault);
168
169 int width = widthF == null ? -1 : Math.round(widthF);
170 int height = heightF == null ? -1 : Math.round(heightF);
171
172 float offsetXF = 0f;
173 float offsetYF = 0f;
174 if (keys[ICON_OFFSET_X_IDX] != null) {
175 offsetXF = c.get(keys[ICON_OFFSET_X_IDX], 0f, Float.class);
176 offsetYF = c.get(keys[ICON_OFFSET_Y_IDX], 0f, Float.class);
177 }
178
179 final MapImage mapImage = new MapImage(iconRef.iconName, iconRef.source);
180
181 mapImage.width = width;
182 mapImage.height = height;
183 mapImage.offsetX = Math.round(offsetXF);
184 mapImage.offsetY = Math.round(offsetYF);
185
186 mapImage.alpha = Utils.clamp(Main.pref.getInteger("mappaint.icon-image-alpha", 255), 0, 255);
187 Integer pAlpha = Utils.colorFloat2int(c.get(keys[ICON_OPACITY_IDX], null, float.class));
188 if (pAlpha != null) {
189 mapImage.alpha = pAlpha;
190 }
191 return mapImage;
192 }
193
194 /**
195 * Create a symbol for the environment
196 * @param env The environment to read the icon form
197 * @return The symbol.
198 */
199 private static Symbol createSymbol(Environment env) {
200 Cascade c = env.mc.getCascade(env.layer);
201
202 Keyword shapeKW = c.get("symbol-shape", null, Keyword.class);
203 if (shapeKW == null)
204 return null;
205 Optional<SymbolShape> shape = SymbolShape.forName(shapeKW.val);
206 if (!shape.isPresent()) {
207 return null;
208 }
209
210 Cascade cDef = env.mc.getCascade("default");
211 Float sizeOnDefault = cDef.get("symbol-size", null, Float.class);
212 if (sizeOnDefault != null && sizeOnDefault <= 0) {
213 sizeOnDefault = null;
214 }
215 Float size = Optional.ofNullable(getWidth(c, "symbol-size", sizeOnDefault)).orElse(10f);
216 if (size <= 0)
217 return null;
218
219 Float strokeWidthOnDefault = getWidth(cDef, "symbol-stroke-width", null);
220 Float strokeWidth = getWidth(c, "symbol-stroke-width", strokeWidthOnDefault);
221
222 Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
223
224 if (strokeWidth == null && strokeColor != null) {
225 strokeWidth = 1f;
226 } else if (strokeWidth != null && strokeColor == null) {
227 strokeColor = Color.ORANGE;
228 }
229
230 Stroke stroke = null;
231 if (strokeColor != null && strokeWidth != null) {
232 Integer strokeAlpha = Utils.colorFloat2int(c.get("symbol-stroke-opacity", null, Float.class));
233 if (strokeAlpha != null) {
234 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
235 strokeColor.getBlue(), strokeAlpha);
236 }
237 stroke = new BasicStroke(strokeWidth);
238 }
239
240 Color fillColor = c.get("symbol-fill-color", null, Color.class);
241 if (stroke == null && fillColor == null) {
242 fillColor = Color.BLUE;
243 }
244
245 if (fillColor != null) {
246 Integer fillAlpha = Utils.colorFloat2int(c.get("symbol-fill-opacity", null, Float.class));
247 if (fillAlpha != null) {
248 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
249 fillColor.getBlue(), fillAlpha);
250 }
251 }
252
253 return new Symbol(shape.get(), Math.round(size), stroke, strokeColor, fillColor);
254 }
255
256 @Override
257 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, StyledMapRenderer painter,
258 boolean selected, boolean outermember, boolean member) {
259 if (primitive instanceof Node) {
260 Node n = (Node) primitive;
261 if (mapImage != null && painter.isShowIcons()) {
262 painter.drawNodeIcon(n, mapImage, painter.isInactiveMode() || n.isDisabled(), selected, member,
263 mapImageAngle == null ? 0.0 : mapImageAngle.getRotationAngle(primitive));
264 } else if (symbol != null) {
265 paintWithSymbol(settings, painter, selected, member, n);
266 } else {
267 Color color;
268 boolean isConnection = n.isConnectionNode();
269
270 if (painter.isInactiveMode() || n.isDisabled()) {
271 color = settings.getInactiveColor();
272 } else if (selected) {
273 color = settings.getSelectedColor();
274 } else if (member) {
275 color = settings.getRelationSelectedColor();
276 } else if (isConnection) {
277 if (n.isTagged()) {
278 color = settings.getTaggedConnectionColor();
279 } else {
280 color = settings.getConnectionColor();
281 }
282 } else {
283 if (n.isTagged()) {
284 color = settings.getTaggedColor();
285 } else {
286 color = settings.getNodeColor();
287 }
288 }
289
290 final int size = max(
291 selected ? settings.getSelectedNodeSize() : 0,
292 n.isTagged() ? settings.getTaggedNodeSize() : 0,
293 isConnection ? settings.getConnectionNodeSize() : 0,
294 settings.getUnselectedNodeSize());
295
296 final boolean fill = (selected && settings.isFillSelectedNode()) ||
297 (n.isTagged() && settings.isFillTaggedNode()) ||
298 (isConnection && settings.isFillConnectionNode()) ||
299 settings.isFillUnselectedNode();
300
301 painter.drawNode(n, color, size, fill);
302
303 }
304 } else if (primitive instanceof Relation && mapImage != null) {
305 painter.drawRestriction((Relation) primitive, mapImage, painter.isInactiveMode() || primitive.isDisabled());
306 }
307 }
308
309 private void paintWithSymbol(MapPaintSettings settings, StyledMapRenderer painter, boolean selected, boolean member,
310 Node n) {
311 Color fillColor = symbol.fillColor;
312 if (fillColor != null) {
313 if (painter.isInactiveMode() || n.isDisabled()) {
314 fillColor = settings.getInactiveColor();
315 } else if (defaultSelectedHandling && selected) {
316 fillColor = settings.getSelectedColor(fillColor.getAlpha());
317 } else if (member) {
318 fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
319 }
320 }
321 Color strokeColor = symbol.strokeColor;
322 if (strokeColor != null) {
323 if (painter.isInactiveMode() || n.isDisabled()) {
324 strokeColor = settings.getInactiveColor();
325 } else if (defaultSelectedHandling && selected) {
326 strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
327 } else if (member) {
328 strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
329 }
330 }
331 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor);
332 }
333
334 /**
335 * Gets the selection box for this element.
336 * @return The selection box as {@link BoxProvider} object.
337 */
338 public BoxProvider getBoxProvider() {
339 if (mapImage != null)
340 return mapImage.getBoxProvider();
341 else if (symbol != null)
342 return new SimpleBoxProvider(new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size));
343 else {
344 // This is only executed once, so no performance concerns.
345 // However, it would be better, if the settings could be changed at runtime.
346 int size = max(
347 Main.pref.getInteger("mappaint.node.selected-size", 5),
348 Main.pref.getInteger("mappaint.node.unselected-size", 3),
349 Main.pref.getInteger("mappaint.node.connection-size", 5),
350 Main.pref.getInteger("mappaint.node.tagged-size", 3)
351 );
352 return new SimpleBoxProvider(new Rectangle(-size/2, -size/2, size, size));
353 }
354 }
355
356 private static int max(int... elements) {
357 return IntStream.of(elements).max().orElseThrow(IllegalStateException::new);
358 }
359
360 @Override
361 public int hashCode() {
362 return Objects.hash(super.hashCode(), mapImage, mapImageAngle, symbol);
363 }
364
365 @Override
366 public boolean equals(Object obj) {
367 if (this == obj) return true;
368 if (obj == null || getClass() != obj.getClass()) return false;
369 if (!super.equals(obj)) return false;
370 NodeElement that = (NodeElement) obj;
371 return Objects.equals(mapImage, that.mapImage) &&
372 Objects.equals(mapImageAngle, that.mapImageAngle) &&
373 Objects.equals(symbol, that.symbol);
374 }
375
376 @Override
377 public String toString() {
378 StringBuilder s = new StringBuilder(64).append("NodeElement{").append(super.toString());
379 if (mapImage != null) {
380 s.append(" icon=[" + mapImage + ']');
381 }
382 if (mapImage != null && mapImageAngle != null) {
383 s.append(" mapImageAngle=[" + mapImageAngle + ']');
384 }
385 if (symbol != null) {
386 s.append(" symbol=[" + symbol + ']');
387 }
388 s.append('}');
389 return s.toString();
390 }
391}
Note: See TracBrowser for help on using the repository browser.