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