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