001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint.styleelement;
003
004import java.awt.BasicStroke;
005import java.awt.Color;
006import java.awt.Rectangle;
007import java.awt.Stroke;
008import java.util.Objects;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.data.osm.Node;
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013import org.openstreetmap.josm.data.osm.Relation;
014import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
015import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
016import org.openstreetmap.josm.gui.mappaint.Cascade;
017import org.openstreetmap.josm.gui.mappaint.Environment;
018import org.openstreetmap.josm.gui.mappaint.Keyword;
019import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
020import org.openstreetmap.josm.gui.mappaint.MultiCascade;
021import org.openstreetmap.josm.gui.mappaint.StyleElementList;
022import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProvider;
023import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.SimpleBoxProvider;
024import org.openstreetmap.josm.gui.util.RotationAngle;
025import org.openstreetmap.josm.tools.Utils;
026
027/**
028 * applies for Nodes and turn restriction relations
029 */
030public class NodeElement extends StyleElement {
031    public final MapImage mapImage;
032    public final RotationAngle mapImageAngle;
033    public final Symbol symbol;
034
035    public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON }
036
037    public static class Symbol {
038        public SymbolShape symbol;
039        public int size;
040        public Stroke stroke;
041        public Color strokeColor;
042        public Color fillColor;
043
044        public Symbol(SymbolShape symbol, int size, Stroke stroke, Color strokeColor, Color fillColor) {
045            if (stroke != null && strokeColor == null)
046                throw new IllegalArgumentException("Stroke given without color");
047            if (stroke == null && fillColor == null)
048                throw new IllegalArgumentException("Either a stroke or a fill color must be given");
049            this.symbol = symbol;
050            this.size = size;
051            this.stroke = stroke;
052            this.strokeColor = strokeColor;
053            this.fillColor = fillColor;
054        }
055
056        @Override
057        public boolean equals(Object obj) {
058            if (obj == null || getClass() != obj.getClass())
059                return false;
060            final Symbol other = (Symbol) obj;
061            return  symbol == other.symbol &&
062                    size == other.size &&
063                    Objects.equals(stroke, other.stroke) &&
064                    Objects.equals(strokeColor, other.strokeColor) &&
065                    Objects.equals(fillColor, other.fillColor);
066        }
067
068        @Override
069        public int hashCode() {
070            int hash = 7;
071            hash = 67 * hash + symbol.hashCode();
072            hash = 67 * hash + size;
073            hash = 67 * hash + (stroke != null ? stroke.hashCode() : 0);
074            hash = 67 * hash + (strokeColor != null ? strokeColor.hashCode() : 0);
075            hash = 67 * hash + (fillColor != null ? fillColor.hashCode() : 0);
076            return hash;
077        }
078
079        @Override
080        public String toString() {
081            return "symbol=" + symbol + " size=" + size +
082                    (stroke != null ? " stroke=" + stroke + " strokeColor=" + strokeColor : "") +
083                    (fillColor != null ? " fillColor=" + fillColor : "");
084        }
085    }
086
087    public static final NodeElement SIMPLE_NODE_ELEMSTYLE;
088    public static final BoxProvider SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER;
089    static {
090        MultiCascade mc = new MultiCascade();
091        mc.getOrCreateCascade("default");
092        SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), 4.1f, true);
093        if (SIMPLE_NODE_ELEMSTYLE == null) throw new AssertionError();
094        SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER = SIMPLE_NODE_ELEMSTYLE.getBoxProvider();
095    }
096
097    public static final StyleElementList DEFAULT_NODE_STYLELIST = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE);
098    public static final StyleElementList DEFAULT_NODE_STYLELIST_TEXT = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE,
099            BoxTextElement.SIMPLE_NODE_TEXT_ELEMSTYLE);
100
101    protected NodeElement(Cascade c, MapImage mapImage, Symbol symbol, float default_major_z_index, RotationAngle rotationAngle) {
102        super(c, default_major_z_index);
103        this.mapImage = mapImage;
104        this.symbol = symbol;
105        this.mapImageAngle = rotationAngle;
106    }
107
108    public static NodeElement create(Environment env) {
109        return create(env, 4f, false);
110    }
111
112    private static NodeElement create(Environment env, float default_major_z_index, boolean allowDefault) {
113        Cascade c = env.mc.getCascade(env.layer);
114
115        MapImage mapImage = createIcon(env, ICON_KEYS);
116        Symbol symbol = null;
117        if (mapImage == null) {
118            symbol = createSymbol(env);
119        }
120        RotationAngle rotationAngle = null;
121        final Float angle = c.get(ICON_ROTATION, null, Float.class, true);
122        if (angle != null) {
123            rotationAngle = RotationAngle.buildStaticRotation(angle);
124        } else {
125            final Keyword rotationKW = c.get(ICON_ROTATION, null, Keyword.class);
126            if (rotationKW != null) {
127                if ("way".equals(rotationKW.val)) {
128                    rotationAngle = RotationAngle.buildWayDirectionRotation();
129                } else {
130                    try {
131                        rotationAngle = RotationAngle.buildStaticRotation(rotationKW.val);
132                    } catch (IllegalArgumentException ignore) {
133                        if (Main.isTraceEnabled()) {
134                            Main.trace(ignore.getMessage());
135                        }
136                    }
137                }
138            }
139        }
140
141        // optimization: if we neither have a symbol, nor a mapImage
142        // we don't have to check for the remaining style properties and we don't
143        // have to allocate a node element style.
144        if (!allowDefault && symbol == null && mapImage == null) return null;
145
146        return new NodeElement(c, mapImage, symbol, default_major_z_index, rotationAngle);
147    }
148
149    public static MapImage createIcon(final Environment env, final String[] keys) {
150        Cascade c = env.mc.getCascade(env.layer);
151
152        final IconReference iconRef = c.get(keys[ICON_IMAGE_IDX], null, IconReference.class, true);
153        if (iconRef == null)
154            return null;
155
156        Cascade c_def = env.mc.getCascade("default");
157
158        Float widthOnDefault = c_def.get(keys[ICON_WIDTH_IDX], null, Float.class);
159        if (widthOnDefault != null && widthOnDefault <= 0) {
160            widthOnDefault = null;
161        }
162        Float widthF = getWidth(c, keys[ICON_WIDTH_IDX], widthOnDefault);
163
164        Float heightOnDefault = c_def.get(keys[ICON_HEIGHT_IDX], null, Float.class);
165        if (heightOnDefault != null && heightOnDefault <= 0) {
166            heightOnDefault = null;
167        }
168        Float heightF = getWidth(c, keys[ICON_HEIGHT_IDX], heightOnDefault);
169
170        int width = widthF == null ? -1 : Math.round(widthF);
171        int height = heightF == null ? -1 : Math.round(heightF);
172
173        float offsetXF = 0f;
174        float offsetYF = 0f;
175        if (keys[ICON_OFFSET_X_IDX] != null) {
176            offsetXF = c.get(keys[ICON_OFFSET_X_IDX], 0f, Float.class);
177            offsetYF = c.get(keys[ICON_OFFSET_Y_IDX], 0f, Float.class);
178        }
179
180        final MapImage mapImage = new MapImage(iconRef.iconName, iconRef.source);
181
182        mapImage.width = width;
183        mapImage.height = height;
184        mapImage.offsetX = Math.round(offsetXF);
185        mapImage.offsetY = Math.round(offsetYF);
186
187        mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
188        Integer pAlpha = Utils.color_float2int(c.get(keys[ICON_OPACITY_IDX], null, float.class));
189        if (pAlpha != null) {
190            mapImage.alpha = pAlpha;
191        }
192        return mapImage;
193    }
194
195    private static Symbol createSymbol(Environment env) {
196        Cascade c = env.mc.getCascade(env.layer);
197        Cascade c_def = env.mc.getCascade("default");
198
199        SymbolShape shape;
200        Keyword shapeKW = c.get("symbol-shape", null, Keyword.class);
201        if (shapeKW == null)
202            return null;
203        if ("square".equals(shapeKW.val)) {
204            shape = SymbolShape.SQUARE;
205        } else if ("circle".equals(shapeKW.val)) {
206            shape = SymbolShape.CIRCLE;
207        } else if ("triangle".equals(shapeKW.val)) {
208            shape = SymbolShape.TRIANGLE;
209        } else if ("pentagon".equals(shapeKW.val)) {
210            shape = SymbolShape.PENTAGON;
211        } else if ("hexagon".equals(shapeKW.val)) {
212            shape = SymbolShape.HEXAGON;
213        } else if ("heptagon".equals(shapeKW.val)) {
214            shape = SymbolShape.HEPTAGON;
215        } else if ("octagon".equals(shapeKW.val)) {
216            shape = SymbolShape.OCTAGON;
217        } else if ("nonagon".equals(shapeKW.val)) {
218            shape = SymbolShape.NONAGON;
219        } else if ("decagon".equals(shapeKW.val)) {
220            shape = SymbolShape.DECAGON;
221        } else
222            return null;
223
224        Float sizeOnDefault = c_def.get("symbol-size", null, Float.class);
225        if (sizeOnDefault != null && sizeOnDefault <= 0) {
226            sizeOnDefault = null;
227        }
228        Float size = getWidth(c, "symbol-size", sizeOnDefault);
229
230        if (size == null) {
231            size = 10f;
232        }
233
234        if (size <= 0)
235            return null;
236
237        Float strokeWidthOnDefault = getWidth(c_def, "symbol-stroke-width", null);
238        Float strokeWidth = getWidth(c, "symbol-stroke-width", strokeWidthOnDefault);
239
240        Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
241
242        if (strokeWidth == null && strokeColor != null) {
243            strokeWidth = 1f;
244        } else if (strokeWidth != null && strokeColor == null) {
245            strokeColor = Color.ORANGE;
246        }
247
248        Stroke stroke = null;
249        if (strokeColor != null) {
250            Integer strokeAlpha = Utils.color_float2int(c.get("symbol-stroke-opacity", null, Float.class));
251            if (strokeAlpha != null) {
252                strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
253                        strokeColor.getBlue(), strokeAlpha);
254            }
255            stroke = new BasicStroke(strokeWidth);
256        }
257
258        Color fillColor = c.get("symbol-fill-color", null, Color.class);
259        if (stroke == null && fillColor == null) {
260            fillColor = Color.BLUE;
261        }
262
263        if (fillColor != null) {
264            Integer fillAlpha = Utils.color_float2int(c.get("symbol-fill-opacity", null, Float.class));
265            if (fillAlpha != null) {
266                fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
267                        fillColor.getBlue(), fillAlpha);
268            }
269        }
270
271        return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor);
272    }
273
274    @Override
275    public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, StyledMapRenderer painter,
276            boolean selected, boolean outermember, boolean member) {
277        if (primitive instanceof Node) {
278            Node n = (Node) primitive;
279            if (mapImage != null && painter.isShowIcons()) {
280                painter.drawNodeIcon(n, mapImage, painter.isInactiveMode() || n.isDisabled(), selected, member,
281                        mapImageAngle == null ? 0.0 : mapImageAngle.getRotationAngle(primitive));
282            } else if (symbol != null) {
283                Color fillColor = symbol.fillColor;
284                if (fillColor != null) {
285                    if (painter.isInactiveMode() || n.isDisabled()) {
286                        fillColor = settings.getInactiveColor();
287                    } else if (selected) {
288                        fillColor = settings.getSelectedColor(fillColor.getAlpha());
289                    } else if (member) {
290                        fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
291                    }
292                }
293                Color strokeColor = symbol.strokeColor;
294                if (strokeColor != null) {
295                    if (painter.isInactiveMode() || n.isDisabled()) {
296                        strokeColor = settings.getInactiveColor();
297                    } else if (selected) {
298                        strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
299                    } else if (member) {
300                        strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
301                    }
302                }
303                painter.drawNodeSymbol(n, symbol, fillColor, strokeColor);
304            } else {
305                Color color;
306                boolean isConnection = n.isConnectionNode();
307
308                if (painter.isInactiveMode() || n.isDisabled()) {
309                    color = settings.getInactiveColor();
310                } else if (selected) {
311                    color = settings.getSelectedColor();
312                } else if (member) {
313                    color = settings.getRelationSelectedColor();
314                } else if (isConnection) {
315                    if (n.isTagged()) {
316                        color = settings.getTaggedConnectionColor();
317                    } else {
318                        color = settings.getConnectionColor();
319                    }
320                } else {
321                    if (n.isTagged()) {
322                        color = settings.getTaggedColor();
323                    } else {
324                        color = settings.getNodeColor();
325                    }
326                }
327
328                final int size = Utils.max(selected ? settings.getSelectedNodeSize() : 0,
329                        n.isTagged() ? settings.getTaggedNodeSize() : 0,
330                        isConnection ? settings.getConnectionNodeSize() : 0,
331                        settings.getUnselectedNodeSize());
332
333                final boolean fill = (selected && settings.isFillSelectedNode()) ||
334                (n.isTagged() && settings.isFillTaggedNode()) ||
335                (isConnection && settings.isFillConnectionNode()) ||
336                settings.isFillUnselectedNode();
337
338                painter.drawNode(n, color, size, fill);
339
340            }
341        } else if (primitive instanceof Relation && mapImage != null) {
342            painter.drawRestriction((Relation) primitive, mapImage, painter.isInactiveMode() || primitive.isDisabled());
343        }
344    }
345
346    public BoxProvider getBoxProvider() {
347        if (mapImage != null)
348            return mapImage.getBoxProvider();
349        else if (symbol != null)
350            return new SimpleBoxProvider(new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size));
351        else {
352            // This is only executed once, so no performance concerns.
353            // However, it would be better, if the settings could be changed at runtime.
354            int size = Utils.max(
355                    Main.pref.getInteger("mappaint.node.selected-size", 5),
356                    Main.pref.getInteger("mappaint.node.unselected-size", 3),
357                    Main.pref.getInteger("mappaint.node.connection-size", 5),
358                    Main.pref.getInteger("mappaint.node.tagged-size", 3)
359            );
360            return new SimpleBoxProvider(new Rectangle(-size/2, -size/2, size, size));
361        }
362    }
363
364    @Override
365    public int hashCode() {
366        int hash = super.hashCode();
367        hash = 17 * hash + (mapImage != null ? mapImage.hashCode() : 0);
368        hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
369        hash = 17 * hash + (mapImageAngle != null ? mapImageAngle.hashCode() : 0);
370        return hash;
371    }
372
373    @Override
374    public boolean equals(Object obj) {
375        if (obj == null || getClass() != obj.getClass())
376            return false;
377        if (!super.equals(obj))
378            return false;
379
380        final NodeElement other = (NodeElement) obj;
381        // we should get the same image object due to caching
382        if (!Objects.equals(mapImage, other.mapImage))
383            return false;
384        if (!Objects.equals(symbol, other.symbol))
385            return false;
386        if (!Objects.equals(mapImageAngle, other.mapImageAngle))
387            return false;
388        return true;
389    }
390
391    @Override
392    public String toString() {
393        StringBuilder s = new StringBuilder("NodeElemStyle{");
394        s.append(super.toString());
395        if (mapImage != null) {
396            s.append(" icon=[" + mapImage + ']');
397        }
398        if (symbol != null) {
399            s.append(" symbol=[" + symbol + ']');
400        }
401        if (mapImageAngle != null) {
402            s.append(" mapImageAngle=[" + mapImageAngle + ']');
403        }
404        s.append('}');
405        return s.toString();
406    }
407}