Ignore:
Timestamp:
2009-12-20T17:55:34+01:00 (14 years ago)
Author:
jttt
Message:

Split MapPaintVisitor into two classes - MapPainter do the actual drawing while MapPaintVisitor choose style

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  
    66import static org.openstreetmap.josm.tools.I18n.tr;
    77
    8 import java.awt.BasicStroke;
    98import java.awt.Color;
    10 import java.awt.Font;
    11 import java.awt.FontMetrics;
    12 import java.awt.Image;
    139import java.awt.Point;
    1410import java.awt.Polygon;
    15 import java.awt.Rectangle;
    16 import java.awt.geom.GeneralPath;
    1711import java.awt.geom.Point2D;
    18 import java.awt.geom.Rectangle2D;
    1912import java.util.ArrayList;
    2013import java.util.Arrays;
     
    2215import java.util.Collections;
    2316import java.util.Comparator;
    24 import java.util.Iterator;
    2517import java.util.LinkedList;
    2618import java.util.List;
    27 
    28 import javax.swing.ImageIcon;
    2919
    3020import org.openstreetmap.josm.Main;
     
    4838import org.openstreetmap.josm.gui.mappaint.LineElemStyle;
    4939import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    50 import org.openstreetmap.josm.tools.ImageProvider;
    5140import org.openstreetmap.josm.tools.LanguageInfo;
    5241
     
    6453    protected Color textColor;
    6554    protected Color areaTextColor;
    66     protected Font orderFont;
    6755    protected ElemStyles.StyleSet styles;
    6856    protected double circum;
    6957    protected double dist;
    70     protected Collection<String> regionalNameOrder;
    7158    protected boolean useStyleCache;
    7259    private static int paintid = 0;
    7360    private EastNorth minEN;
    7461    private EastNorth maxEN;
     62    private MapPainter painter;
     63    protected Collection<String> regionalNameOrder;
    7564
    7665    protected boolean isZoomOk(ElemStyle e) {
     
    138127
    139128        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));
    153131        } 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            }
    155150        }
    156151    }
     
    218213            if (fillAreas > dist)
    219214            {
    220                 drawArea(w, data.isSelected(w) ? selectedColor : areaStyle.color);
     215                painter.drawArea(getPolygon(w), (data.isSelected(w) ? selectedColor : areaStyle.color), getWayName(w));
    221216                if(!w.isClosed()) {
    222217                    putError(w, tr("Area style way is not closed."), true);
     
    242237        Node lastN;
    243238
    244         if(l != null)
    245         {
     239        if(l != null) {
    246240            if (l.color != null) {
    247241                color = l.color;
     
    255249            color = selectedColor;
    256250        }
    257         if (realWidth > 0 && useRealWidth && !showDirection)
    258         {
     251        if (realWidth > 0 && useRealWidth && !showDirection) {
    259252            int tmpWidth = (int) (100 /  (float) (circum / realWidth));
    260253            if (tmpWidth > width) {
     
    289282            for(LineElemStyle s : l.overlays) {
    290283                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),
    292285                            s.getDashed(), s.dashedColor, false, false);
    293286                }
     
    296289
    297290        /* draw the way */
    298         drawWay(w, color, width, dashed, dashedColor, showDirection, showOnlyHeadArrowOnly);
     291        painter.drawWay(w, color, width, dashed, dashedColor, showDirection, showOnlyHeadArrowOnly);
    299292
    300293        /* draw overlays above the way */
     
    302295            for(LineElemStyle s : l.overlays) {
    303296                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),
    305298                            s.getDashed(), s.dashedColor, false, false);
    306299                }
     
    308301        }
    309302
    310         if(showOrderNumber)
    311         {
     303        if(showOrderNumber) {
    312304            int orderNumber = 0;
    313305            lastN = null;
    314             for(Node n : w.getNodes())
    315             {
    316                 if(lastN != null)
    317                 {
     306            for(Node n : w.getNodes()) {
     307                if(lastN != null) {
    318308                    orderNumber++;
    319309                    drawOrderNumber(lastN, n, orderNumber);
     
    321311                lastN = n;
    322312            }
    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());
    370313        }
    371314    }
     
    488431                drawWay(way, areaStyle.line, selectedColor, true);
    489432                if(area) {
    490                     drawArea(way, areaselected ? selectedColor : areaStyle.color);
     433                    painter.drawArea(getPolygon(way), (areaselected ? selectedColor : areaStyle.color), getWayName(way));
    491434                }
    492435            }
     
    499442        {
    500443            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));
    505449            }
    506450        }
     
    751695        }
    752696
    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));
    768699    }
    769700
     
    972903
    973904                    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);
    975906                    visible = true;
    976907                }
     
    10901021    }
    10911022
    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) {
    11001024        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;
    11381037    }
    11391038
    11401039    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;
    12441053    }
    12451054
     
    12961105        drawRestriction = Main.pref.getBoolean("mappaint.restriction", true);
    12971106        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));
    12991107        String[] names = {"name:" + LanguageInfo.getJOSMLocaleCode(), "name", "int_name", "ref", "operator", "brand", "addr:housenumber"};
    1300         regionalNameOrder = Main.pref.getCollection("mappaint.nameOrder", Arrays.asList(names));
    13011108        minEN = nc.getEastNorth(0, nc.getHeight() - 1);
    13021109        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);
    13031113
    13041114        data.clearErrors();
Note: See TracChangeset for help on using the changeset viewer.