Changeset 2667 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2009-12-20T17:55:34+01:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/osm/visitor/paint
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPaintVisitor.java
r2666 r2667 6 6 import static org.openstreetmap.josm.tools.I18n.tr; 7 7 8 import java.awt.BasicStroke;9 8 import java.awt.Color; 10 import java.awt.Font;11 import java.awt.FontMetrics;12 import java.awt.Image;13 9 import java.awt.Point; 14 10 import java.awt.Polygon; 15 import java.awt.Rectangle;16 import java.awt.geom.GeneralPath;17 11 import java.awt.geom.Point2D; 18 import java.awt.geom.Rectangle2D;19 12 import java.util.ArrayList; 20 13 import java.util.Arrays; … … 22 15 import java.util.Collections; 23 16 import java.util.Comparator; 24 import java.util.Iterator;25 17 import java.util.LinkedList; 26 18 import java.util.List; 27 28 import javax.swing.ImageIcon;29 19 30 20 import org.openstreetmap.josm.Main; … … 48 38 import org.openstreetmap.josm.gui.mappaint.LineElemStyle; 49 39 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 50 import org.openstreetmap.josm.tools.ImageProvider;51 40 import org.openstreetmap.josm.tools.LanguageInfo; 52 41 … … 64 53 protected Color textColor; 65 54 protected Color areaTextColor; 66 protected Font orderFont;67 55 protected ElemStyles.StyleSet styles; 68 56 protected double circum; 69 57 protected double dist; 70 protected Collection<String> regionalNameOrder;71 58 protected boolean useStyleCache; 72 59 private static int paintid = 0; 73 60 private EastNorth minEN; 74 61 private EastNorth maxEN; 62 private MapPainter painter; 63 protected Collection<String> regionalNameOrder; 75 64 76 65 protected boolean isZoomOk(ElemStyle e) { … … 138 127 139 128 if (nodeStyle != null && isZoomOk(nodeStyle) && showIcons > dist) { 140 if (inactive || n.isDisabled()) { 141 drawNode(n, nodeStyle.getDisabledIcon(), nodeStyle.annotate, data.isSelected(n)); 142 } else { 143 drawNode(n, nodeStyle.icon, nodeStyle.annotate, data.isSelected(n)); 144 } 145 } else if (n.highlighted) { 146 drawNode(n, highlightColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode); 147 } else if (data.isSelected(n)) { 148 drawNode(n, selectedColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode); 149 } else if (n.isTagged()) { 150 drawNode(n, nodeColor, taggedNodeSize, taggedNodeRadius, fillUnselectedNode); 151 } else if (inactive || n.isDisabled()) { 152 drawNode(n, inactiveColor, unselectedNodeSize, unselectedNodeRadius, fillUnselectedNode); 129 painter.drawNodeIcon(n, (inactive || n.isDisabled())?nodeStyle.getDisabledIcon():nodeStyle.icon, 130 nodeStyle.annotate, data.isSelected(n), getNodeName(n)); 153 131 } else { 154 drawNode(n, nodeColor, unselectedNodeSize, unselectedNodeRadius, fillUnselectedNode); 132 if (isZoomOk(null)) { 133 if (n.highlighted) { 134 painter.drawNode(n, highlightColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode, 135 getNodeName(n)); 136 } else if (data.isSelected(n)) { 137 painter.drawNode(n, selectedColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode, 138 getNodeName(n)); 139 } else if (n.isTagged()) { 140 painter.drawNode(n, nodeColor, taggedNodeSize, taggedNodeRadius, fillUnselectedNode, 141 getNodeName(n)); 142 } else if (inactive || n.isDisabled()) { 143 painter.drawNode(n, inactiveColor, unselectedNodeSize, unselectedNodeRadius, fillUnselectedNode, 144 getNodeName(n)); 145 } else { 146 painter.drawNode(n, nodeColor, unselectedNodeSize, unselectedNodeRadius, fillUnselectedNode, 147 getNodeName(n)); 148 } 149 } 155 150 } 156 151 } … … 218 213 if (fillAreas > dist) 219 214 { 220 drawArea(w, data.isSelected(w) ? selectedColor : areaStyle.color);215 painter.drawArea(getPolygon(w), (data.isSelected(w) ? selectedColor : areaStyle.color), getWayName(w)); 221 216 if(!w.isClosed()) { 222 217 putError(w, tr("Area style way is not closed."), true); … … 242 237 Node lastN; 243 238 244 if(l != null) 245 { 239 if(l != null) { 246 240 if (l.color != null) { 247 241 color = l.color; … … 255 249 color = selectedColor; 256 250 } 257 if (realWidth > 0 && useRealWidth && !showDirection) 258 { 251 if (realWidth > 0 && useRealWidth && !showDirection) { 259 252 int tmpWidth = (int) (100 / (float) (circum / realWidth)); 260 253 if (tmpWidth > width) { … … 289 282 for(LineElemStyle s : l.overlays) { 290 283 if(!s.over) { 291 drawWay(w, s.color != null && !data.isSelected(w) ? s.color : color, s.getWidth(width),284 painter.drawWay(w, s.color != null && !data.isSelected(w) ? s.color : color, s.getWidth(width), 292 285 s.getDashed(), s.dashedColor, false, false); 293 286 } … … 296 289 297 290 /* draw the way */ 298 drawWay(w, color, width, dashed, dashedColor, showDirection, showOnlyHeadArrowOnly);291 painter.drawWay(w, color, width, dashed, dashedColor, showDirection, showOnlyHeadArrowOnly); 299 292 300 293 /* draw overlays above the way */ … … 302 295 for(LineElemStyle s : l.overlays) { 303 296 if(s.over) { 304 drawWay(w, s.color != null && !data.isSelected(w) ? s.color : color, s.getWidth(width),297 painter.drawWay(w, s.color != null && !data.isSelected(w) ? s.color : color, s.getWidth(width), 305 298 s.getDashed(), s.dashedColor, false, false); 306 299 } … … 308 301 } 309 302 310 if(showOrderNumber) 311 { 303 if(showOrderNumber) { 312 304 int orderNumber = 0; 313 305 lastN = null; 314 for(Node n : w.getNodes()) 315 { 316 if(lastN != null) 317 { 306 for(Node n : w.getNodes()) { 307 if(lastN != null) { 318 308 orderNumber++; 319 309 drawOrderNumber(lastN, n, orderNumber); … … 321 311 lastN = n; 322 312 } 323 }324 }325 326 public void drawWay(Way way, Color color, int width, float dashed[], Color dashedColor, boolean showDirection,327 boolean showHeadArrowOnly) {328 329 GeneralPath path = new GeneralPath();330 331 Node lastN = null;332 Iterator<Node> it = way.getNodes().iterator();333 while (it.hasNext()) {334 Node n = it.next();335 if(lastN != null) {336 drawSegment(path, nc.getPoint(lastN), nc.getPoint(n), (showHeadArrowOnly ? !it.hasNext() : showDirection));337 }338 lastN = n;339 }340 displaySegments(path, color, width, dashed, dashedColor);341 }342 343 private void displaySegments(GeneralPath path, Color color, int width, float dashed[], Color dashedColor) {344 g.setColor(inactive ? inactiveColor : color);345 if (useStrokes > dist) {346 if (dashed.length > 0) {347 g.setStroke(new BasicStroke(width,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0, dashed,0));348 } else {349 g.setStroke(new BasicStroke(width,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));350 }351 }352 g.draw(path);353 354 if(!inactive && useStrokes > dist && dashedColor != null) {355 g.setColor(dashedColor);356 if (dashed.length > 0) {357 float[] dashedOffset = new float[dashed.length];358 System.arraycopy(dashed, 1, dashedOffset, 0, dashed.length - 1);359 dashedOffset[dashed.length-1] = dashed[0];360 float offset = dashedOffset[0];361 g.setStroke(new BasicStroke(width,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0,dashedOffset,offset));362 } else {363 g.setStroke(new BasicStroke(width,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));364 }365 g.draw(path);366 }367 368 if(useStrokes > dist) {369 g.setStroke(new BasicStroke());370 313 } 371 314 } … … 488 431 drawWay(way, areaStyle.line, selectedColor, true); 489 432 if(area) { 490 drawArea(way, areaselected ? selectedColor : areaStyle.color);433 painter.drawArea(getPolygon(way), (areaselected ? selectedColor : areaStyle.color), getWayName(way)); 491 434 } 492 435 } … … 499 442 { 500 443 if(style != null && isZoomOk(style)) { 501 drawNode((Node)osm, ((IconElemStyle)style).icon, 502 ((IconElemStyle)style).annotate, true); 503 } else { 504 drawNode((Node)osm, selectedColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode); 444 painter.drawNodeIcon((Node)osm, ((IconElemStyle)style).icon, 445 ((IconElemStyle)style).annotate, true, getNodeName((Node)osm)); 446 } else if (isZoomOk(null)) { 447 painter.drawNode((Node)osm, selectedColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode, 448 getNodeName((Node)osm)); 505 449 } 506 450 } … … 751 695 } 752 696 753 /* rotate icon with direction last node in from to */ 754 ImageIcon rotatedIcon = ImageProvider.createRotatedImage(null /*icon2*/, inactive || r.isDisabled() ? 755 nodeStyle.getDisabledIcon() : 756 nodeStyle.icon, iconAngle); 757 758 /* scale down icon to 16*16 pixels */ 759 ImageIcon smallIcon = new ImageIcon(rotatedIcon.getImage().getScaledInstance(16 , 16, Image.SCALE_SMOOTH)); 760 int w = smallIcon.getIconWidth(), h=smallIcon.getIconHeight(); 761 smallIcon.paintIcon ( Main.map.mapView, g, (int)(pVia.x+vx+vx2)-w/2, (int)(pVia.y+vy+vy2)-h/2 ); 762 763 if (data.isSelected(r)) 764 { 765 g.setColor ( selectedColor ); 766 g.drawRect ((int)(pVia.x+vx+vx2)-w/2-2,(int)(pVia.y+vy+vy2)-h/2-2, w+4, h+4); 767 } 697 painter.drawRestriction(inactive || r.isDisabled() ? nodeStyle.getDisabledIcon() : nodeStyle.icon, 698 pVia, vx, vx2, vy, vy2, iconAngle, data.isSelected(r)); 768 699 } 769 700 … … 972 903 973 904 boolean selected = pd.selected || data.isSelected(pd.way) || data.isSelected(r); 974 drawAreaPolygon(p, selected ? selectedColor : areaStyle.color);905 painter.drawArea(p, selected ? selectedColor : areaStyle.color, null); 975 906 visible = true; 976 907 } … … 1090 1021 } 1091 1022 1092 protected void drawArea(Way w, Color color) 1093 { 1094 Polygon polygon = getPolygon(w); 1095 1096 /* set the opacity (alpha) level of the filled polygon */ 1097 g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), fillAlpha)); 1098 g.fillPolygon(polygon); 1099 1023 protected String getNodeName(Node n) { 1100 1024 if (showNames > dist) { 1101 String name = getWayName(w); 1102 if (name!=null /* && annotate */) { 1103 Rectangle pb = polygon.getBounds(); 1104 FontMetrics fontMetrics = g.getFontMetrics(orderFont); // if slow, use cache 1105 Rectangle2D nb = fontMetrics.getStringBounds(name, g); // if slow, approximate by strlen()*maxcharbounds(font) 1106 1107 // Point2D c = getCentroid(polygon); 1108 // Using the Centroid is Nicer for buildings like: +--------+ 1109 // but this needs to be fast. As most houses are | 42 | 1110 // boxes anyway, the center of the bounding box +---++---+ 1111 // will have to do. ++ 1112 // Centroids are not optimal either, just imagine a U-shaped house. 1113 // Point2D c = new Point2D.Double(pb.x + pb.width / 2.0, pb.y + pb.height / 2.0); 1114 // Rectangle2D.Double centeredNBounds = 1115 // new Rectangle2D.Double(c.getX() - nb.getWidth()/2, 1116 // c.getY() - nb.getHeight()/2, 1117 // nb.getWidth(), 1118 // nb.getHeight()); 1119 1120 Rectangle centeredNBounds = new Rectangle(pb.x + (int)((pb.width - nb.getWidth())/2.0), 1121 pb.y + (int)((pb.height - nb.getHeight())/2.0), 1122 (int)nb.getWidth(), 1123 (int)nb.getHeight()); 1124 1125 if ((pb.width >= nb.getWidth() && pb.height >= nb.getHeight()) && // quick check 1126 polygon.contains(centeredNBounds) // slow but nice 1127 ) { 1128 g.setColor(areaTextColor); 1129 Font defaultFont = g.getFont(); 1130 g.setFont (orderFont); 1131 g.drawString (name, 1132 (int)(centeredNBounds.getMinX() - nb.getMinX()), 1133 (int)(centeredNBounds.getMinY() - nb.getMinY())); 1134 g.setFont(defaultFont); 1135 } 1136 } 1137 } 1025 String name = null; 1026 if (n.hasKeys()) { 1027 for (String rn : regionalNameOrder) { 1028 name = n.get(rn); 1029 if (name != null) { 1030 break; 1031 } 1032 } 1033 } 1034 return name; 1035 } else 1036 return null; 1138 1037 } 1139 1038 1140 1039 protected String getWayName(Way w) { 1141 String name = null; 1142 if (w.hasKeys()) { 1143 for (String rn : regionalNameOrder) { 1144 name = w.get(rn); 1145 if (name != null) { 1146 break; 1147 } 1148 } 1149 } 1150 return name; 1151 } 1152 1153 protected void drawAreaPolygon(Polygon polygon, Color color) 1154 { 1155 /* set the opacity (alpha) level of the filled polygon */ 1156 g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), fillAlpha)); 1157 g.fillPolygon(polygon); 1158 } 1159 1160 protected void drawNode(Node n, ImageIcon icon, boolean annotate, boolean selected) { 1161 Point p = nc.getPoint(n); 1162 if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return; 1163 1164 int w = icon.getIconWidth(), h=icon.getIconHeight(); 1165 icon.paintIcon ( Main.map.mapView, g, p.x-w/2, p.y-h/2 ); 1166 if(showNames > dist && annotate) { 1167 String name = getNodeName(n); 1168 if (name!=null) { 1169 if (inactive || n.isDisabled()) { 1170 g.setColor(inactiveColor); 1171 } else { 1172 g.setColor(textColor); 1173 } 1174 Font defaultFont = g.getFont(); 1175 g.setFont (orderFont); 1176 g.drawString (name, p.x+w/2+2, p.y+h/2+2); 1177 g.setFont(defaultFont); 1178 } 1179 } 1180 if (selected) 1181 { 1182 g.setColor ( selectedColor ); 1183 g.drawRect (p.x-w/2-2, p.y-h/2-2, w+4, h+4); 1184 } 1185 } 1186 1187 protected String getNodeName(Node n) { 1188 String name = null; 1189 if (n.hasKeys()) { 1190 for (String rn : regionalNameOrder) { 1191 name = n.get(rn); 1192 if (name != null) { 1193 break; 1194 } 1195 } 1196 } 1197 return name; 1198 } 1199 1200 1201 /** 1202 * Draw the node as small rectangle with the given color. 1203 * 1204 * @param n The node to draw. 1205 * @param color The color of the node. 1206 */ 1207 @Override 1208 public void drawNode(Node n, Color color, int size, int radius, boolean fill) { 1209 if (isZoomOk(null) && size > 1) { 1210 Point p = nc.getPoint(n); 1211 if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) 1212 || (p.y > nc.getHeight())) 1213 return; 1214 1215 if (inactive || n.isDisabled()) { 1216 g.setColor(inactiveColor); 1217 } else { 1218 g.setColor(color); 1219 } 1220 if (fill) { 1221 g.fillRect(p.x - radius, p.y - radius, size, size); 1222 g.drawRect(p.x - radius, p.y - radius, size, size); 1223 } else { 1224 g.drawRect(p.x - radius, p.y - radius, size, size); 1225 } 1226 1227 if(showNames > dist) 1228 { 1229 String name = getNodeName(n); 1230 if (name!=null /* && annotate */) 1231 { 1232 if (inactive || n.isDisabled()) { 1233 g.setColor(inactiveColor); 1234 } else { 1235 g.setColor(textColor); 1236 } 1237 Font defaultFont = g.getFont(); 1238 g.setFont (orderFont); 1239 g.drawString (name, p.x+radius+2, p.y+radius+2); 1240 g.setFont(defaultFont); 1241 } 1242 } 1243 } 1040 if (showNames > dist) { 1041 String name = null; 1042 if (w.hasKeys()) { 1043 for (String rn : regionalNameOrder) { 1044 name = w.get(rn); 1045 if (name != null) { 1046 break; 1047 } 1048 } 1049 } 1050 return name; 1051 } else 1052 return null; 1244 1053 } 1245 1054 … … 1296 1105 drawRestriction = Main.pref.getBoolean("mappaint.restriction", true); 1297 1106 leftHandTraffic = Main.pref.getBoolean("mappaint.lefthandtraffic", false); 1298 orderFont = new Font(Main.pref.get("mappaint.font", "Helvetica"), Font.PLAIN, Main.pref.getInteger("mappaint.fontsize", 8));1299 1107 String[] names = {"name:" + LanguageInfo.getJOSMLocaleCode(), "name", "int_name", "ref", "operator", "brand", "addr:housenumber"}; 1300 regionalNameOrder = Main.pref.getCollection("mappaint.nameOrder", Arrays.asList(names));1301 1108 minEN = nc.getEastNorth(0, nc.getHeight() - 1); 1302 1109 maxEN = nc.getEastNorth(nc.getWidth() - 1, 0); 1110 regionalNameOrder = Main.pref.getCollection("mappaint.nameOrder", Arrays.asList(names)); 1111 1112 this.painter = new MapPainter(g, inactive, nc, useStrokes > dist); 1303 1113 1304 1114 data.clearErrors();
Note:
See TracChangeset
for help on using the changeset viewer.