Changeset 3871 in josm for trunk/src/org/openstreetmap/josm
- Timestamp:
- 2011-02-08T14:29:23+01:00 (14 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java
r3867 r3871 13 13 import java.awt.Rectangle; 14 14 import java.awt.TexturePaint; 15 import java.awt.font.FontRenderContext; 16 import java.awt.font.LineMetrics; 15 17 import java.awt.geom.GeneralPath; 16 18 import java.awt.geom.Rectangle2D; … … 33 35 import org.openstreetmap.josm.gui.NavigatableComponent; 34 36 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle; 37 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.HorizontalTextAlignment; 35 38 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.Symbol; 39 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.TextElement; 40 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.VerticalTextAlignment; 36 41 import org.openstreetmap.josm.tools.ImageProvider; 37 42 import org.openstreetmap.josm.tools.LanguageInfo; … … 188 193 } 189 194 190 public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member, String name) {195 public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member, TextElement text) { 191 196 Point p = nc.getPoint(n); 192 197 if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return; … … 198 203 icon.paintIcon ( nc, g, p.x-w/2, p.y-h/2 ); 199 204 g.setPaintMode(); 200 if(name != null) { 201 if (inactive || n.isDisabled()) { 202 g.setColor(inactiveColor); 203 } else { 204 g.setColor(textColor); 205 } 206 Font defaultFont = g.getFont(); 207 g.setFont (orderFont); 208 g.drawString (name, p.x+w/2+2, p.y+h/2+2); 209 g.setFont(defaultFont); 210 } 205 drawNodeText(n, text, p, w/2, h/2); 211 206 if (selected || member) 212 207 { … … 216 211 } 217 212 218 public void drawNodeSymbol(Node n, Symbol s, boolean selected, boolean member, String name) {213 public void drawNodeSymbol(Node n, Symbol s, boolean selected, boolean member, TextElement text) { 219 214 Point p = nc.getPoint(n); 220 215 if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return; 221 int radius = (int) (s.size / 2);216 int radius = s.size / 2; 222 217 223 218 if (s.fillColor != null) { 224 g.setColor(s.fillColor); 219 if (inactive || n.isDisabled()) { 220 g.setColor(inactiveColor); 221 } else { 222 g.setColor(s.fillColor); 223 } 225 224 switch (s.symbol) { 225 case SQUARE: 226 g.fillRect(p.x - radius, p.y - radius, s.size, s.size); 227 break; 226 228 case CIRCLE: 227 g.fillOval(p.x - radius, p.y - radius, (int) s.size, (int)s.size);229 g.fillOval(p.x - radius, p.y - radius, s.size, s.size); 228 230 break; 229 case SQUARE: 230 g.fillRect(p.x - radius, p.y - radius, (int) s.size, (int) s.size); 231 break; 231 default: 232 throw new AssertionError(); 232 233 } 233 234 } 234 235 if (s.stroke != null) { 235 236 g.setStroke(s.stroke); 236 g.setColor(s.strokeColor); 237 if (inactive || n.isDisabled()) { 238 g.setColor(inactiveColor); 239 } else { 240 g.setColor(s.strokeColor); 241 } 237 242 switch (s.symbol) { 243 case SQUARE: 244 g.drawRect(p.x - radius, p.y - radius, s.size - 1, s.size - 1); 245 break; 238 246 case CIRCLE: 239 g.drawOval(p.x - radius, p.y - radius, (int) s.size - 1, (int)s.size - 1);247 g.drawOval(p.x - radius, p.y - radius, s.size - 1, s.size - 1); 240 248 break; 241 case SQUARE: 242 g.drawRect(p.x - radius, p.y - radius, (int) s.size - 1, (int) s.size - 1); 243 break; 249 default: 250 throw new AssertionError(); 244 251 } 245 252 g.setStroke(new BasicStroke()); 246 253 } 254 drawNodeText(n, text, p, radius, radius); 247 255 } 248 256 … … 253 261 * @param color The color of the node. 254 262 */ 255 public void drawNode(Node n, Color color, int size, boolean fill, String name) {263 public void drawNode(Node n, Color color, int size, boolean fill, TextElement text) { 256 264 if (size > 1) { 257 265 Point p = nc.getPoint(n); … … 270 278 } 271 279 272 if(name != null) { 273 if (inactive || n.isDisabled()) { 274 g.setColor(inactiveColor); 275 } else { 276 g.setColor(textColor); 277 } 278 Font defaultFont = g.getFont(); 279 g.setFont (orderFont); 280 g.drawString (name, p.x+radius+2, p.y+radius+2); 281 g.setFont(defaultFont); 282 } 283 } 280 drawNodeText(n, text, p, radius, radius + 4); 281 } 282 } 283 284 private void drawNodeText(Node n, TextElement text, Point p, int w_half, int h_half) { 285 if (!isShowNames() || text == null) 286 return; 287 288 String s = text.textKey == null ? getNodeName(n) : n.get(text.textKey); 289 if (s == null) 290 return; 291 292 if (inactive || n.isDisabled()) { 293 g.setColor(inactiveColor); 294 } else { 295 g.setColor(text.color); 296 } 297 Font defaultFont = g.getFont(); 298 g.setFont(text.font); 299 300 int x = p.x + text.xOffset; 301 int y = p.y + text.yOffset; 302 /** 303 * 304 * left-above __center-above___ right-above 305 * left-top| |right-top 306 * | | 307 * left-center| center-center |right-center 308 * | | 309 * left-bottom|_________________|right-bottom 310 * left-below center-below right-below 311 * 312 */ 313 if (text.hAlign == HorizontalTextAlignment.RIGHT) { 314 x += w_half + 2; 315 } else { 316 FontRenderContext frc = g.getFontRenderContext(); 317 Rectangle2D bounds = text.font.getStringBounds(s, frc); 318 int textWidth = (int) bounds.getWidth(); 319 if (text.hAlign == HorizontalTextAlignment.CENTER) { 320 x -= textWidth / 2; 321 } else if (text.hAlign == HorizontalTextAlignment.LEFT) { 322 x -= w_half + 4 + textWidth; 323 } else throw new AssertionError(); 324 } 325 326 if (text.vAlign == VerticalTextAlignment.BOTTOM) { 327 y += h_half - 2; 328 } else { 329 FontRenderContext frc = g.getFontRenderContext(); 330 LineMetrics metrics = text.font.getLineMetrics(s, frc); 331 if (text.vAlign == VerticalTextAlignment.ABOVE) { 332 y -= h_half + metrics.getDescent(); 333 } else if (text.vAlign == VerticalTextAlignment.TOP) { 334 y -= h_half - metrics.getAscent(); 335 } else if (text.vAlign == VerticalTextAlignment.CENTER) { 336 y += (metrics.getAscent() - metrics.getDescent()) / 2; 337 } else if (text.vAlign == VerticalTextAlignment.BELOW) { 338 y += h_half + metrics.getAscent(); 339 } else throw new AssertionError(); 340 } 341 g.drawString(s, x, y); 342 g.setFont(defaultFont); 284 343 } 285 344 -
trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java
r3865 r3871 9 9 10 10 import org.openstreetmap.josm.gui.mappaint.mapcss.CSSColors; 11 import org.openstreetmap.josm.tools.Utils; 11 12 12 13 /** … … 183 184 res.append(Arrays.toString((float[]) val)); 184 185 } else if (val instanceof Color) { 185 res.append( String.format("#%x", ((Color) val).getRGB()));186 res.append(Utils.toString((Color)val)); 186 187 } else { 187 188 res.append(val+""); -
trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java
r3865 r3871 164 164 165 165 float miterlimit = c.get(prefix + "miterlimit", 10f, Float.class); 166 if (miterlimit < 1f) { 167 miterlimit = 10f; 168 } 166 169 167 170 BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset); -
trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java
r3865 r3871 6 6 import java.awt.BasicStroke; 7 7 import java.awt.Color; 8 import java.awt.Font; 8 9 import java.awt.Stroke; 9 10 … … 17 18 import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings; 18 19 import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter; 20 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; 19 21 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 22 import org.openstreetmap.josm.tools.CheckParameterUtil; 20 23 import org.openstreetmap.josm.tools.Utils; 21 24 … … 24 27 */ 25 28 public class NodeElemStyle extends ElemStyle { 26 public boolean annotate; 27 public String annotation_key; 29 28 30 public ImageIcon icon; 29 31 public int iconAlpha; 30 32 public Symbol symbol; 33 public TextElement text; 31 34 32 35 private ImageIcon disabledIcon; 36 37 public enum SymbolShape { SQUARE, CIRCLE } 38 public enum HorizontalTextAlignment { LEFT, CENTER, RIGHT } 39 public enum VerticalTextAlignment { ABOVE, TOP, CENTER, BOTTOM, BELOW } 33 40 34 41 public static class Symbol { 35 42 public SymbolShape symbol; 36 public float size;43 public int size; 37 44 public Stroke stroke; 38 45 public Color strokeColor; 39 46 public Color fillColor; 40 47 41 public Symbol(SymbolShape symbol, float size, Stroke stroke, Color strokeColor, Color fillColor) {48 public Symbol(SymbolShape symbol, int size, Stroke stroke, Color strokeColor, Color fillColor) { 42 49 if (stroke != null && strokeColor == null) 43 50 throw new IllegalArgumentException(); … … 67 74 int hash = 7; 68 75 hash = 67 * hash + symbol.hashCode(); 69 hash = 67 * hash + Float.floatToIntBits(size);76 hash = 67 * hash + size; 70 77 hash = 67 * hash + (stroke != null ? stroke.hashCode() : 0); 71 78 hash = 67 * hash + (strokeColor != null ? strokeColor.hashCode() : 0); … … 81 88 } 82 89 } 90 91 public static class TextElement { 92 public String textKey; 93 public HorizontalTextAlignment hAlign; 94 public VerticalTextAlignment vAlign; 95 public Font font; 96 public int xOffset; 97 public int yOffset; 98 public Color color; 99 100 public TextElement(String textKey, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign, Font font, int xOffset, int yOffset, Color color) { 101 CheckParameterUtil.ensureParameterNotNull(hAlign); 102 CheckParameterUtil.ensureParameterNotNull(vAlign); 103 CheckParameterUtil.ensureParameterNotNull(font); 104 CheckParameterUtil.ensureParameterNotNull(color); 105 this.textKey = textKey; 106 this.hAlign = hAlign; 107 this.vAlign = vAlign; 108 this.font = font; 109 this.xOffset = xOffset; 110 this.yOffset = yOffset; 111 this.color = color; 112 } 113 114 @Override 115 public boolean equals(Object obj) { 116 if (obj == null || getClass() != obj.getClass()) 117 return false; 118 final TextElement other = (TextElement) obj; 119 return equal(textKey, other.textKey) && 120 hAlign == other.hAlign && 121 vAlign == other.vAlign && 122 equal(font, other.font) && 123 xOffset == other.xOffset && 124 yOffset == other.yOffset && 125 equal(color, other.color); 126 } 127 128 @Override 129 public int hashCode() { 130 int hash = 3; 131 hash = 79 * hash + (textKey != null ? textKey.hashCode() : 0); 132 hash = 79 * hash + hAlign.hashCode(); 133 hash = 79 * hash + vAlign.hashCode(); 134 hash = 79 * hash + font.hashCode(); 135 hash = 79 * hash + xOffset; 136 hash = 79 * hash + yOffset; 137 hash = 79 * hash + color.hashCode(); 138 return hash; 139 } 140 } 83 141 84 public enum SymbolShape { SQUARE, CIRCLE } 85 86 public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE = new NodeElemStyle(Cascade.EMPTY_CASCADE, true, null, null, 0, null); 87 88 protected NodeElemStyle(Cascade c, boolean annotate, String annotation_key, ImageIcon icon, int iconAlpha, Symbol symbol) { 142 public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE; 143 static { 144 Cascade c = new Cascade(false); 145 c.put("text", "auto"); 146 SIMPLE_NODE_ELEMSTYLE = create(c, true); 147 } 148 149 protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, TextElement text) { 89 150 super(c); 90 this.annotate = annotate;91 this.annotation_key = annotation_key;92 151 this.icon = icon; 93 152 this.iconAlpha = iconAlpha; 94 153 this.symbol = symbol; 154 this.text = text; 95 155 } 96 156 97 157 public static NodeElemStyle create(Cascade c) { 158 return create(c, false); 159 } 160 161 private static NodeElemStyle create(Cascade c, boolean allowOnlyText) { 98 162 IconReference iconRef = c.get("icon-image", null, IconReference.class); 99 163 ImageIcon icon = null; … … 109 173 } 110 174 } else { 111 SymbolShape shape; 112 String shapeStr = c.get("symbol-shape", null, String.class); 113 if (equal(shapeStr, "square")) { 114 shape = SymbolShape.SQUARE; 115 } else if (equal(shapeStr, "circle")) { 116 shape = SymbolShape.CIRCLE; 117 } else 118 return null; 119 120 Float size = c.get("symbol-size", null, Float.class); 121 if (size == null || size <= 0) 122 return null; 123 124 Float strokeWidth = c.get("symbol-stroke-width", null, Float.class); 125 Color strokeColor = c.get("symbol-stroke-color", null, Color.class); 126 if (strokeColor != null) { 127 float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class); 128 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(), 129 strokeColor.getBlue(), Utils.color_float2int(strokeAlpha)); 130 } 131 Stroke stroke = null; 132 if (strokeWidth != null && strokeWidth > 0 && strokeColor != null) { 133 stroke = new BasicStroke(strokeWidth); 134 } 135 136 Color fillColor = c.get("symbol-fill-color", null, Color.class); 137 if (fillColor != null) { 138 float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class); 139 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(), 140 fillColor.getBlue(), Utils.color_float2int(fillAlpha)); 141 } 142 143 if ((stroke == null || strokeColor == null) && fillColor == null) 144 return null; 145 146 symbol = new Symbol(shape, size, stroke, strokeColor, fillColor); 147 } 148 149 String text = c.get("text", null, String.class); 150 151 boolean annotate = text != null; 152 String annotation_key = null; 153 154 if (annotate && !"auto".equalsIgnoreCase(text)) { 155 annotation_key = text; 156 } 157 return new NodeElemStyle(c, annotate, annotation_key, icon, iconAlpha, symbol); 175 symbol = createSymbol(c); 176 } 177 178 if (icon == null && symbol == null && !allowOnlyText) 179 return null; 180 181 TextElement text = null; 182 String textStr = c.get("text", null, String.class); 183 if (textStr != null) { 184 String textKey = null; 185 if (!"auto".equalsIgnoreCase(textStr)) { 186 textKey = textStr; 187 } 188 HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT; 189 String hAlignStr = c.get("text-anchor-horizontal", null, String.class); 190 if (equal(hAlignStr, "left")) { 191 hAlign = HorizontalTextAlignment.LEFT; 192 } else if (equal(hAlignStr, "center")) { 193 hAlign = HorizontalTextAlignment.CENTER; 194 } else if (equal(hAlignStr, "right")) { 195 hAlign = HorizontalTextAlignment.RIGHT; 196 } 197 VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM; 198 String vAlignStr = c.get("text-anchor-vertical", null, String.class); 199 if (equal(vAlignStr, "above")) { 200 vAlign = VerticalTextAlignment.ABOVE; 201 } else if (equal(vAlignStr, "top")) { 202 vAlign = VerticalTextAlignment.TOP; 203 } else if (equal(vAlignStr, "center")) { 204 vAlign = VerticalTextAlignment.CENTER; 205 } else if (equal(vAlignStr, "bottom")) { 206 vAlign = VerticalTextAlignment.BOTTOM; 207 } else if (equal(vAlignStr, "below")) { 208 vAlign = VerticalTextAlignment.BELOW; 209 } 210 String name = c.get("font-family", Main.pref.get("mappaint.font", "Helvetica"), String.class); 211 float size = c.get("font-size", (float) Main.pref.getInteger("mappaint.fontsize", 8), Float.class); 212 int weight = Font.PLAIN; 213 String weightStr = c.get("font-wheight", null, String.class); 214 if (equal(weightStr, "bold")) { 215 weight = Font.BOLD; 216 } 217 int style = Font.PLAIN; 218 String styleStr = c.get("font-style", null, String.class); 219 if (equal(styleStr, "italic")) { 220 style = Font.ITALIC; 221 } 222 Font font = new Font(name, weight | style, Math.round(size)); 223 int xOffset = c.get("text-offset-x", 0f, Float.class).intValue(); 224 int yOffset = c.get("text-offset-y", 0f, Float.class).intValue(); 225 Color color = c.get("text-color", PaintColors.TEXT.get(), Color.class); 226 text = new TextElement(textKey, hAlign, vAlign, font, xOffset, yOffset, color); 227 } 228 229 return new NodeElemStyle(c, icon, iconAlpha, symbol, text); 230 } 231 232 private static Symbol createSymbol(Cascade c) { 233 SymbolShape shape; 234 String shapeStr = c.get("symbol-shape", null, String.class); 235 if (equal(shapeStr, "square")) { 236 shape = SymbolShape.SQUARE; 237 } else if (equal(shapeStr, "circle")) { 238 shape = SymbolShape.CIRCLE; 239 } else 240 return null; 241 242 Float size = c.get("symbol-size", null, Float.class); 243 if (size == null || size <= 0) 244 return null; 245 246 Float strokeWidth = c.get("symbol-stroke-width", null, Float.class); 247 Color strokeColor = c.get("symbol-stroke-color", null, Color.class); 248 if (strokeColor != null) { 249 float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class); 250 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(), 251 strokeColor.getBlue(), Utils.color_float2int(strokeAlpha)); 252 } 253 Stroke stroke = null; 254 if (strokeWidth != null && strokeWidth > 0 && strokeColor != null) { 255 stroke = new BasicStroke(strokeWidth); 256 } 257 258 Color fillColor = c.get("symbol-fill-color", null, Color.class); 259 if (fillColor != null) { 260 float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class); 261 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(), 262 fillColor.getBlue(), Utils.color_float2int(fillAlpha)); 263 } 264 265 if ((stroke == null || strokeColor == null) && fillColor == null) 266 return null; 267 268 return new Symbol(shape, size.intValue(), stroke, strokeColor, fillColor); 158 269 } 159 270 … … 164 275 if (icon != null && painter.isShowIcons()) { 165 276 painter.drawNodeIcon(n, (painter.isInactive() || n.isDisabled()) ? getDisabledIcon() : icon, 166 Utils.color_int2float(iconAlpha), selected, member, getName(n, painter));277 Utils.color_int2float(iconAlpha), selected, member, text); 167 278 } else if (symbol != null) { 168 painter.drawNodeSymbol(n, symbol, selected, member, getName(n, painter));279 painter.drawNodeSymbol(n, symbol, selected, member, text); 169 280 } else { 170 281 if (n.isHighlighted()) { 171 painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), getName(n, painter));282 painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), text); 172 283 } else { 173 284 Color color; … … 204 315 settings.isFillUnselectedNode(); 205 316 206 painter.drawNode(n, color, size, fill, getName(n, painter));317 painter.drawNode(n, color, size, fill, text); 207 318 } 208 319 } … … 220 331 } 221 332 222 protected String getName(Node n, MapPainter painter) {223 if (painter.isShowNames() && annotate) {224 if (annotation_key != null) {225 return n.get(annotation_key);226 } else {227 return painter.getNodeName(n);228 }229 }230 return null;231 }232 233 333 @Override 234 334 public int hashCode() { 235 335 int hash = super.hashCode(); 236 hash = 17 * hash + (annotate ? 1 : 0);237 hash = 17 * hash + (annotation_key != null ? annotation_key.hashCode() : 0);238 336 hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0); 239 hash = 17 * hash + this.iconAlpha; 240 hash = 17 * hash + (this.symbol != null ? this.symbol.hashCode() : 0); 337 hash = 17 * hash + iconAlpha; 338 hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0); 339 hash = 17 * hash + (text != null ? text.hashCode() : 0); 241 340 return hash; 242 341 } … … 253 352 if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage())) 254 353 return false; 255 if (annotate != other.annotate)256 return false;257 if (!equal(annotation_key, annotation_key))258 return false;259 354 if (this.iconAlpha != other.iconAlpha) 260 355 return false; 261 356 if (!equal(symbol, other.symbol)) 357 return false; 358 if (!equal(text, other.text)) 262 359 return false; 263 360 return true; … … 267 364 @Override 268 365 public String toString() { 269 return "NodeElemStyle{" + super.toString() + "annotate=" + annotate + " annotation_key=" + annotation_key +270 (icon != null ? (" 366 return "NodeElemStyle{" + super.toString() + 367 (icon != null ? ("icon=" + icon + " iconAlpha=" + iconAlpha) : "") + 271 368 (symbol != null ? (" symbol=[" + symbol + "]") : "") + '}'; 272 369 } -
trunk/src/org/openstreetmap/josm/tools/CheckParameterUtil.java
r3083 r3871 33 33 34 34 /** 35 * can find line number in the stack trace, so parameter name is optional 36 */ 37 public static void ensureParameterNotNull(Object value) { 38 if (value == null) 39 throw new IllegalArgumentException("Parameter must not be null"); 40 } 41 42 /** 35 43 * Ensures that <code>id</code> is non-null primitive id of type {@see OsmPrimitiveType#NODE} 36 44 *
Note:
See TracChangeset
for help on using the changeset viewer.