Ignore:
Timestamp:
2013-03-24T12:58:45+01:00 (11 years ago)
Author:
bastiK
Message:

mapcss: new map element 'repeat-image' similar to 'pattern-image', but more flexible

File:
1 edited

Legend:

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

    r5705 r5801  
    5757import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
    5858import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.Symbol;
     59import org.openstreetmap.josm.gui.mappaint.RepeatImageElemStyle.LineImageAlignment;
    5960import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
    6061import org.openstreetmap.josm.gui.mappaint.TextElement;
     
    595596    }
    596597
     598    @Deprecated
    597599    public void drawLinePattern(Way way, Image pattern) {
    598         final int width = pattern.getWidth(null);
    599         final int height = pattern.getHeight(null);
     600        drawRepeatImage(way, pattern, 0f, 0f, LineImageAlignment.TOP);
     601    }
     602
     603    /**
     604     * Draw an image along a way repeatedly.
     605     *
     606     * @param way the way
     607     * @param pattern the image
     608     * @param offset offset from the way
     609     * @param spacing spacing between two images
     610     * @param align alignment of the image. The top, center or bottom edge
     611     * can be aligned with the way.
     612     */
     613    public void drawRepeatImage(Way way, Image pattern, float offset, float spacing, LineImageAlignment align) {
     614        final int imgWidth = pattern.getWidth(null);
     615        final double repeat = imgWidth + spacing;
     616        final int imgHeight = pattern.getHeight(null);
    600617
    601618        Point lastP = null;
    602         double wayLength = 0;
    603 
    604         Iterator<Node> it = way.getNodes().iterator();
     619        double currentWayLength = 0;
     620
     621        int dy1, dy2;
     622        switch (align) {
     623            case TOP:
     624                dy1 = 0;
     625                dy2 = imgHeight;
     626                break;
     627            case CENTER:
     628                dy1 = - imgHeight / 2;
     629                dy2 = imgHeight + dy1;
     630                break;
     631            case BOTTOM:
     632                dy1 = -imgHeight;
     633                dy2 = 0;
     634                break;
     635            default:
     636                throw new AssertionError();
     637        }
     638
     639        OffsetIterator it = new OffsetIterator(way.getNodes(), offset);
    605640        while (it.hasNext()) {
    606             Node n = it.next();
    607             Point thisP = nc.getPoint(n);
     641            Point thisP = it.next();
    608642
    609643            if (lastP != null) {
     
    613647                final double dy = thisP.y - lastP.y;
    614648
    615                 double dist = wayLength == 0 ? 0 : width - (wayLength % width);
     649                double pos = currentWayLength == 0 ? 0 : repeat - (currentWayLength % repeat);
    616650
    617651                AffineTransform saveTransform = g.getTransform();
     
    619653                g.rotate(Math.atan2(dy, dx));
    620654
    621                 if (dist > 0) {
    622                     g.drawImage(pattern, 0, 0, (int) dist, height,
    623                             width - (int) dist, 0, width, height, null);
    624                 }
    625                 while (dist < segmentLength) {
    626                     if (dist + width > segmentLength) {
    627                         g.drawImage(pattern, (int) dist, 0, (int) segmentLength, height,
    628                                 0, 0, (int) segmentLength - (int) dist, height, null);
     655                // draw the rest of the image from the last segment in case it
     656                // is cut off
     657                if (pos > spacing) {
     658                    g.drawImage(pattern, 0, dy1, (int) (pos - spacing), dy2,
     659                            (int) (imgWidth + spacing - pos), 0, imgWidth, imgHeight, null);
     660                }
     661                // draw remaining images for this segment
     662                while (pos < segmentLength) {
     663                    // cut off at the end?
     664                    if (pos + imgWidth > segmentLength) {
     665                        g.drawImage(pattern, (int) pos, dy1, (int) segmentLength, dy2,
     666                                0, 0, (int) segmentLength - (int) pos, imgHeight, null);
    629667                    } else {
    630                         g.drawImage(pattern, (int) dist, 0, nc);
     668                        g.drawImage(pattern, (int) pos, dy1, nc);
    631669                    }
    632                     dist += width;
     670                    pos += repeat;
    633671                }
    634672                g.setTransform(saveTransform);
    635673
    636                 wayLength += segmentLength;
     674                currentWayLength += segmentLength;
    637675            }
    638676            lastP = thisP;
    639677        }
    640678    }
    641    
     679
    642680    @Override
    643681    public void drawNode(Node n, Color color, int size, boolean fill) {
Note: See TracChangeset for help on using the changeset viewer.