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.CheckParameterUtil; 026import org.openstreetmap.josm.tools.Utils; 027 028/** 029 * applies for Nodes and turn restriction relations 030 */ 031public class NodeElement extends StyleElement { 032 public final MapImage mapImage; 033 public final RotationAngle mapImageAngle; 034 public final Symbol symbol; 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 return Objects.hash(symbol, size, stroke, strokeColor, fillColor); 072 } 073 074 @Override 075 public String toString() { 076 return "symbol=" + symbol + " size=" + size + 077 (stroke != null ? " stroke=" + stroke + " strokeColor=" + strokeColor : "") + 078 (fillColor != null ? " fillColor=" + fillColor : ""); 079 } 080 } 081 082 private static final String[] ICON_KEYS = {ICON_IMAGE, ICON_WIDTH, ICON_HEIGHT, ICON_OPACITY, ICON_OFFSET_X, ICON_OFFSET_Y}; 083 084 public static final NodeElement SIMPLE_NODE_ELEMSTYLE; 085 public static final BoxProvider SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER; 086 static { 087 MultiCascade mc = new MultiCascade(); 088 mc.getOrCreateCascade("default"); 089 SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), 4.1f, true); 090 if (SIMPLE_NODE_ELEMSTYLE == null) throw new AssertionError(); 091 SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER = SIMPLE_NODE_ELEMSTYLE.getBoxProvider(); 092 } 093 094 public static final StyleElementList DEFAULT_NODE_STYLELIST = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE); 095 public static final StyleElementList DEFAULT_NODE_STYLELIST_TEXT = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE, 096 BoxTextElement.SIMPLE_NODE_TEXT_ELEMSTYLE); 097 098 protected NodeElement(Cascade c, MapImage mapImage, Symbol symbol, float defaultMajorZindex, RotationAngle rotationAngle) { 099 super(c, defaultMajorZindex); 100 this.mapImage = mapImage; 101 this.symbol = symbol; 102 this.mapImageAngle = rotationAngle; 103 } 104 105 public static NodeElement create(Environment env) { 106 return create(env, 4f, false); 107 } 108 109 private static NodeElement create(Environment env, float defaultMajorZindex, 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 RotationAngle rotationAngle = null; 118 final Float angle = c.get(ICON_ROTATION, null, Float.class, true); 119 if (angle != null) { 120 rotationAngle = RotationAngle.buildStaticRotation(angle); 121 } else { 122 final Keyword rotationKW = c.get(ICON_ROTATION, null, Keyword.class); 123 if (rotationKW != null) { 124 if ("way".equals(rotationKW.val)) { 125 rotationAngle = RotationAngle.buildWayDirectionRotation(); 126 } else { 127 try { 128 rotationAngle = RotationAngle.buildStaticRotation(rotationKW.val); 129 } catch (IllegalArgumentException ignore) { 130 if (Main.isTraceEnabled()) { 131 Main.trace(ignore.getMessage()); 132 } 133 } 134 } 135 } 136 } 137 138 // optimization: if we neither have a symbol, nor a mapImage 139 // we don't have to check for the remaining style properties and we don't 140 // have to allocate a node element style. 141 if (!allowDefault && symbol == null && mapImage == null) return null; 142 143 return new NodeElement(c, mapImage, symbol, defaultMajorZindex, rotationAngle); 144 } 145 146 public static MapImage createIcon(final Environment env, final String[] keys) { 147 CheckParameterUtil.ensureParameterNotNull(env, "env"); 148 CheckParameterUtil.ensureParameterNotNull(keys, "keys"); 149 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 cDef = env.mc.getCascade("default"); 157 158 Float widthOnDefault = cDef.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 = cDef.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 cDef = 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 = cDef.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(cDef, "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 && strokeWidth != 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 (defaultSelectedHandling && 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 (defaultSelectedHandling && 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 return Objects.hash(super.hashCode(), mapImage, mapImageAngle, symbol); 367 } 368 369 @Override 370 public boolean equals(Object obj) { 371 if (this == obj) return true; 372 if (obj == null || getClass() != obj.getClass()) return false; 373 if (!super.equals(obj)) return false; 374 NodeElement that = (NodeElement) obj; 375 return Objects.equals(mapImage, that.mapImage) && 376 Objects.equals(mapImageAngle, that.mapImageAngle) && 377 Objects.equals(symbol, that.symbol); 378 } 379 380 @Override 381 public String toString() { 382 StringBuilder s = new StringBuilder(64).append("NodeElemStyle{").append(super.toString()); 383 if (mapImage != null) { 384 s.append(" icon=[" + mapImage + ']'); 385 } 386 if (symbol != null) { 387 s.append(" symbol=[" + symbol + ']'); 388 } 389 if (mapImageAngle != null) { 390 s.append(" mapImageAngle=[" + mapImageAngle + ']'); 391 } 392 s.append('}'); 393 return s.toString(); 394 } 395}