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(); 048 if (stroke == null && fillColor == null) 049 throw new IllegalArgumentException(); 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, boolean selected, boolean member) { 244 if (primitive instanceof Node) { 245 Node n = (Node) primitive; 246 if (mapImage != null && painter.isShowIcons()) { 247 final Image nodeIcon; 248 if (painter.isInactiveMode() || n.isDisabled()) { 249 if (disabledNodeIcon == null || disabledNodeIconIsTemporary) { 250 disabledNodeIcon = mapImage.getDisplayedNodeIcon(true); 251 disabledNodeIconIsTemporary = mapImage.isTemporary(); 252 } 253 nodeIcon = disabledNodeIcon; 254 } else { 255 if (enabledNodeIcon == null || enabledNodeIconIsTemporary) { 256 enabledNodeIcon = mapImage.getDisplayedNodeIcon(false); 257 enabledNodeIconIsTemporary = mapImage.isTemporary(); 258 } 259 nodeIcon = enabledNodeIcon; 260 } 261 painter.drawNodeIcon(n, nodeIcon, Utils.color_int2float(mapImage.alpha), selected, member); 262 } else if (symbol != null) { 263 Color fillColor = symbol.fillColor; 264 if (fillColor != null) { 265 if (painter.isInactiveMode() || n.isDisabled()) { 266 fillColor = settings.getInactiveColor(); 267 } else if (selected) { 268 fillColor = settings.getSelectedColor(fillColor.getAlpha()); 269 } else if (member) { 270 fillColor = settings.getRelationSelectedColor(fillColor.getAlpha()); 271 } 272 } 273 Color strokeColor = symbol.strokeColor; 274 if (strokeColor != null) { 275 if (painter.isInactiveMode() || n.isDisabled()) { 276 strokeColor = settings.getInactiveColor(); 277 } else if (selected) { 278 strokeColor = settings.getSelectedColor(strokeColor.getAlpha()); 279 } else if (member) { 280 strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha()); 281 } 282 } 283 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor); 284 } else { 285 Color color; 286 boolean isConnection = n.isConnectionNode(); 287 288 if (painter.isInactiveMode() || n.isDisabled()) { 289 color = settings.getInactiveColor(); 290 } else if (selected) { 291 color = settings.getSelectedColor(); 292 } else if (member) { 293 color = settings.getRelationSelectedColor(); 294 } else if (isConnection) { 295 if (n.isTagged()) { 296 color = settings.getTaggedConnectionColor(); 297 } else { 298 color = settings.getConnectionColor(); 299 } 300 } else { 301 if (n.isTagged()) { 302 color = settings.getTaggedColor(); 303 } else { 304 color = settings.getNodeColor(); 305 } 306 } 307 308 final int size = Utils.max((selected ? settings.getSelectedNodeSize() : 0), 309 (n.isTagged() ? settings.getTaggedNodeSize() : 0), 310 (isConnection ? settings.getConnectionNodeSize() : 0), 311 settings.getUnselectedNodeSize()); 312 313 final boolean fill = (selected && settings.isFillSelectedNode()) || 314 (n.isTagged() && settings.isFillTaggedNode()) || 315 (isConnection && settings.isFillConnectionNode()) || 316 settings.isFillUnselectedNode(); 317 318 painter.drawNode(n, color, size, fill); 319 320 } 321 } else if (primitive instanceof Relation && mapImage != null) { 322 painter.drawRestriction((Relation) primitive, mapImage); 323 } 324 } 325 326 public BoxProvider getBoxProvider() { 327 if (mapImage != null) 328 return mapImage.getBoxProvider(); 329 else if (symbol != null) 330 return new SimpleBoxProvider(new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size)); 331 else { 332 // This is only executed once, so no performance concerns. 333 // However, it would be better, if the settings could be changed at runtime. 334 int size = Utils.max( 335 Main.pref.getInteger("mappaint.node.selected-size", 5), 336 Main.pref.getInteger("mappaint.node.unselected-size", 3), 337 Main.pref.getInteger("mappaint.node.connection-size", 5), 338 Main.pref.getInteger("mappaint.node.tagged-size", 3) 339 ); 340 return new SimpleBoxProvider(new Rectangle(-size/2, -size/2, size, size)); 341 } 342 } 343 344 @Override 345 public int hashCode() { 346 int hash = super.hashCode(); 347 hash = 17 * hash + (mapImage != null ? mapImage.hashCode() : 0); 348 hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0); 349 return hash; 350 } 351 352 @Override 353 public boolean equals(Object obj) { 354 if (obj == null || getClass() != obj.getClass()) 355 return false; 356 if (!super.equals(obj)) 357 return false; 358 359 final NodeElemStyle other = (NodeElemStyle) obj; 360 // we should get the same image object due to caching 361 if (!Objects.equals(mapImage, other.mapImage)) 362 return false; 363 if (!Objects.equals(symbol, other.symbol)) 364 return false; 365 return true; 366 } 367 368 @Override 369 public String toString() { 370 StringBuilder s = new StringBuilder("NodeElemStyle{"); 371 s.append(super.toString()); 372 if (mapImage != null) { 373 s.append(" icon=[" + mapImage + "]"); 374 } 375 if (symbol != null) { 376 s.append(" symbol=[" + symbol + "]"); 377 } 378 s.append('}'); 379 return s.toString(); 380 } 381}