Changeset 2671 in josm


Ignore:
Timestamp:
2009-12-22T09:13:07+01:00 (14 years ago)
Author:
jttt
Message:

Make MapPaintVisitor independent of SimplePaintVisitor

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPaintVisitor.java

    r2668 r2671  
    77
    88import java.awt.Color;
     9import java.awt.Graphics2D;
    910import java.awt.Point;
    1011import java.awt.Polygon;
     12import java.awt.Rectangle;
    1113import java.awt.geom.Point2D;
    1214import java.util.ArrayList;
     
    3234import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
    3335import org.openstreetmap.josm.gui.DefaultNameFormatter;
     36import org.openstreetmap.josm.gui.NavigatableComponent;
    3437import org.openstreetmap.josm.gui.mappaint.AreaElemStyle;
    3538import org.openstreetmap.josm.gui.mappaint.ElemStyle;
     
    4043import org.openstreetmap.josm.tools.LanguageInfo;
    4144
    42 public class MapPaintVisitor extends SimplePaintVisitor {
     45public class MapPaintVisitor implements PaintVisitor {
     46
     47    protected Graphics2D g;
     48    protected NavigatableComponent nc;
     49
    4350    protected boolean useRealWidth;
    4451    protected boolean zoomLevelDisplay;
     
    6370    protected Collection<String> regionalNameOrder;
    6471
     72    public boolean inactive;
     73    protected boolean fillSelectedNode;
     74    protected boolean fillUnselectedNode;
     75    protected int defaultSegmentWidth;
     76    protected boolean showOrderNumber;
     77
     78    protected boolean showRelevantDirectionsOnly;
     79    protected boolean showHeadArrowOnly;
     80    protected boolean showDirectionArrow;
     81
     82    protected int selectedNodeRadius;
     83    protected int selectedNodeSize;
     84    protected int taggedNodeSize;
     85    protected int taggedNodeRadius;
     86    protected int unselectedNodeRadius;
     87    protected int unselectedNodeSize;
     88
     89    protected Color selectedColor;
     90    protected Color highlightColor;
     91    protected Color inactiveColor;
     92    protected Color nodeColor;
     93
    6594    protected boolean isZoomOk(ElemStyle e) {
    6695        if (!zoomLevelDisplay) /* show everything if the user wishes so */
     
    108137    }
    109138
    110     /**
    111      * Draw a small rectangle.
    112      * White if selected (as always) or red otherwise.
    113      *
    114      * @param n The node to draw.
    115      */
    116     @Override
    117     public void visit(Node n) {}
    118139    public void drawNode(Node n) {
    119140        /* check, if the node is visible at all */
     
    151172    }
    152173
    153     /**
    154      * Draw a line for all segments, according to tags.
    155      * @param w The way to draw.
    156      */
    157     @Override
    158     public void visit(Way w) {}
    159174    public void drawWay(Way w, int fillAreas) {
    160175        if(w.getNodesCount() < 2)
     
    453468    }
    454469
    455     @Override
    456     public void visit(Relation r) {}
    457470    public void paintUnselectedRelation(Relation r) {
    458471
     
    982995    }
    983996
     997    protected boolean isPolygonVisible(Polygon polygon) {
     998        Rectangle bounds = polygon.getBounds();
     999        if (bounds.width == 0 && bounds.height == 0) return false;
     1000        if (bounds.x > nc.getWidth()) return false;
     1001        if (bounds.y > nc.getHeight()) return false;
     1002        if (bounds.x + bounds.width < 0) return false;
     1003        if (bounds.y + bounds.height < 0) return false;
     1004        return true;
     1005    }
     1006
    9841007    protected Polygon getPolygon(Way w)
    9851008    {
     
    10541077    }
    10551078
    1056     @Override
    1057     public void getColors()
    1058     {
    1059         super.getColors();
     1079    public void getColors() {
     1080        selectedColor  = PaintColors.SELECTED.get();
     1081        highlightColor = PaintColors.HIGHLIGHT.get();
     1082        inactiveColor = PaintColors.INACTIVE.get();
     1083        nodeColor = PaintColors.NODE.get();
    10601084        untaggedColor = PaintColors.UNTAGGED.get();
    10611085        textColor = PaintColors.TEXT.get();
     
    10831107
    10841108    /* Shows areas before non-areas */
    1085     @Override
    10861109    public void visitAll(DataSet data, boolean virtual, Bounds bounds) {
    10871110        BBox bbox = new BBox(bounds);
     
    10981121        dist = ll1.greatCircleDistance(ll2);
    10991122
    1100         getSettings(virtual);
     1123        getColors();
     1124
     1125        showDirectionArrow = Main.pref.getBoolean("draw.segment.direction", true);
     1126        showRelevantDirectionsOnly = Main.pref.getBoolean("draw.segment.relevant_directions_only", true);
     1127        showHeadArrowOnly = Main.pref.getBoolean("draw.segment.head_only", false);
     1128        showOrderNumber = Main.pref.getBoolean("draw.segment.order_number", false);
     1129        selectedNodeRadius = Main.pref.getInteger("mappaint.node.selected-size", 5) / 2;
     1130        selectedNodeSize = selectedNodeRadius * 2;
     1131        unselectedNodeRadius = Main.pref.getInteger("mappaint.node.unselected-size", 3) / 2;
     1132        unselectedNodeSize = unselectedNodeRadius * 2;
     1133        taggedNodeRadius = Main.pref.getInteger("mappaint.node.tagged-size", 5) / 2;
     1134        taggedNodeSize = taggedNodeRadius * 2;
     1135        defaultSegmentWidth = Main.pref.getInteger("mappaint.segment.default-width", 2);
     1136        fillSelectedNode = Main.pref.getBoolean("mappaint.node.fill-selected", true);
     1137        fillUnselectedNode = Main.pref.getBoolean("mappaint.node.fill-unselected", false);
     1138
    11011139        useRealWidth = Main.pref.getBoolean("mappaint.useRealWidth", false);
    11021140        zoomLevelDisplay = Main.pref.getBoolean("mappaint.zoomLevelDisplay", false);
     
    11111149        regionalNameOrder = Main.pref.getCollection("mappaint.nameOrder", Arrays.asList(names));
    11121150
    1113         this.painter = new MapPainter(g, inactive, nc, useStrokes > dist);
     1151        this.painter = new MapPainter(g, inactive, nc, useStrokes > dist, virtual);
    11141152
    11151153        data.clearErrors();
     
    11941232        }
    11951233
    1196         drawVirtualNodes(data.searchWays(bbox));
     1234        painter.drawVirtualNodes(data.searchWays(bbox));
    11971235    }
    11981236
     
    12041242        Point p1 = nc.getPoint(n1);
    12051243        Point p2 = nc.getPoint(n2);
    1206         drawOrderNumber(p1, p2, orderNumber);
     1244        painter.drawOrderNumber(p1, p2, orderNumber);
    12071245    }
    12081246
     
    12111249        data.addError(p, isError ? tr("Error: {0}", text) : tr("Warning: {0}", text));
    12121250    }
     1251
     1252    public void setGraphics(Graphics2D g) {
     1253        this.g = g;
     1254    }
     1255
     1256    public void setInactive(boolean inactive) {
     1257        this.inactive = inactive;
     1258    }
     1259
     1260    public void setNavigatableComponent(NavigatableComponent nc) {
     1261        this.nc = nc;
     1262    }
    12131263}
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java

    r2667 r2671  
    1313import java.awt.geom.GeneralPath;
    1414import java.awt.geom.Rectangle2D;
     15import java.util.Collection;
    1516import java.util.Iterator;
    1617
     
    3536    private final Color selectedColor;
    3637    private final Color areaTextColor;
    37 
    38     private Font orderFont;
    39     private int fillAlpha;
    40 
    41     public MapPainter(Graphics2D g, boolean inactive, NavigatableComponent nc, boolean useStrokes) {
     38    private final Color nodeColor;
     39    private final Color backgroundColor;
     40
     41    private final Font orderFont;
     42    private final int fillAlpha;
     43    private final int virtualNodeSize;
     44    private final int virtualNodeSpace;
     45    private final int segmentNumberSpace;
     46
     47
     48    public MapPainter(Graphics2D g, boolean inactive, NavigatableComponent nc, boolean useStrokes, boolean virtual) {
    4249        this.g = g;
    4350        this.inactive = inactive;
     
    4956        this.selectedColor = PaintColors.SELECTED.get();
    5057        this.areaTextColor = PaintColors.AREA_TEXT.get();
     58        this.nodeColor = PaintColors.NODE.get();
     59        this.backgroundColor = PaintColors.BACKGROUND.get();
    5160
    5261        this.orderFont = new Font(Main.pref.get("mappaint.font", "Helvetica"), Font.PLAIN, Main.pref.getInteger("mappaint.fontsize", 8));
    5362        this.fillAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
     63        this.virtualNodeSize = virtual ? Main.pref.getInteger("mappaint.node.virtual-size", 8) / 2 : 0;
     64        this.virtualNodeSpace = Main.pref.getInteger("mappaint.node.virtual-space", 70);
     65        this.segmentNumberSpace = Main.pref.getInteger("mappaint.segmentnumber.space", 40);
    5466    }
    5567
     
    247259    }
    248260
     261    public void drawVirtualNodes(Collection<Way> ways) {
     262
     263        if (virtualNodeSize != 0) {
     264            GeneralPath path = new GeneralPath();
     265            for (Way osm: ways){
     266                if (osm.isUsable() && !osm.isFiltered()) {
     267                    visitVirtual(path, osm);
     268                }
     269            }
     270            g.setColor(nodeColor);
     271            g.draw(path);
     272        }
     273    }
     274
     275    public void visitVirtual(GeneralPath path, Way w) {
     276        Iterator<Node> it = w.getNodes().iterator();
     277        if (it.hasNext()) {
     278            Point lastP = nc.getPoint(it.next());
     279            while(it.hasNext())
     280            {
     281                Point p = nc.getPoint(it.next());
     282                if(isSegmentVisible(lastP, p) && isLargeSegment(lastP, p, virtualNodeSpace))
     283                {
     284                    int x = (p.x+lastP.x)/2;
     285                    int y = (p.y+lastP.y)/2;
     286                    path.moveTo(x-virtualNodeSize, y);
     287                    path.lineTo(x+virtualNodeSize, y);
     288                    path.moveTo(x, y-virtualNodeSize);
     289                    path.lineTo(x, y+virtualNodeSize);
     290                }
     291                lastP = p;
     292            }
     293        }
     294    }
     295
     296    private static boolean isLargeSegment(Point p1, Point p2, int space)  {
     297        int xd = p1.x-p2.x; if(xd < 0) {
     298            xd = -xd;
     299        }
     300        int yd = p1.y-p2.y; if(yd < 0) {
     301            yd = -yd;
     302        }
     303        return (xd+yd > space);
     304    }
     305
     306    /**
     307     * Draw an number of the order of the two consecutive nodes within the
     308     * parents way
     309     */
     310    protected void drawOrderNumber(Point p1, Point p2, int orderNumber) {
     311        if (isSegmentVisible(p1, p2) && isLargeSegment(p1, p2, segmentNumberSpace)) {
     312            String on = Integer.toString(orderNumber);
     313            int strlen = on.length();
     314            int x = (p1.x+p2.x)/2 - 4*strlen;
     315            int y = (p1.y+p2.y)/2 + 4;
     316
     317            if(virtualNodeSize != 0 && isLargeSegment(p1, p2, virtualNodeSpace))
     318            {
     319                y = (p1.y+p2.y)/2 - virtualNodeSize - 3;
     320            }
     321
     322            Color c = g.getColor();
     323            g.setColor(backgroundColor);
     324            g.fillRect(x-1, y-12, 8*strlen+1, 14);
     325            g.setColor(c);
     326            g.drawString(on, x, y);
     327        }
     328    }
     329
    249330}
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/SimplePaintVisitor.java

    r2666 r2671  
    3333 * @author imi
    3434 */
    35 public class SimplePaintVisitor extends AbstractVisitor {
     35public class SimplePaintVisitor extends AbstractVisitor implements PaintVisitor {
    3636    /**
    3737     * The environment to paint to.
     
    487487        }
    488488    }
     489
     490    public void setInactive(boolean inactive) {
     491        this.inactive = inactive;
     492    }
    489493}
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r2666 r2671  
    6060import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    6161import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintVisitor;
     62import org.openstreetmap.josm.data.osm.visitor.paint.PaintVisitor;
    6263import org.openstreetmap.josm.data.osm.visitor.paint.SimplePaintVisitor;
    6364import org.openstreetmap.josm.gui.HelpAwareOptionPane;
     
    238239        }
    239240
    240         SimplePaintVisitor painter;
     241        PaintVisitor painter;
    241242        if (Main.pref.getBoolean("draw.wireframe")) {
    242243            painter = new SimplePaintVisitor();
     
    246247        painter.setGraphics(g);
    247248        painter.setNavigatableComponent(mv);
    248         painter.inactive = inactive;
     249        painter.setInactive(inactive);
    249250        painter.visitAll(data, virtual, box);
    250251        Main.map.conflictDialog.paintConflicts(g, mv);
Note: See TracChangeset for help on using the changeset viewer.