Changeset 4006 in josm


Ignore:
Timestamp:
Mar 26, 2011 7:34:21 PM (2 years ago)
Author:
bastiK
Message:

mapcss: new LinePattern style element similar to mapnik's LinePatternSymbolizer; split off the text part of LineElemStyle, so it also works for LinePattern

Location:
trunk/src/org/openstreetmap/josm
Files:
2 added
5 edited

Legend:

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

    r4002 r4006  
    116116     */ 
    117117    public void drawWay(Way way, Color color, BasicStroke line, BasicStroke dashes, Color dashedColor, 
    118             TextElement text, boolean showOrientation, boolean showHeadArrowOnly, 
     118            boolean showOrientation, boolean showHeadArrowOnly, 
    119119            boolean showOneway, boolean onewayReversed) { 
    120120 
     
    204204        } 
    205205        displaySegments(path, orientationArrows, onewayArrows, onewayArrowsCasing, color, line, dashes, dashedColor); 
    206         drawTextOnPath(way, text); 
    207206    } 
    208207 
     
    247246    } 
    248247 
    249     private void drawTextOnPath(Way way, TextElement text) { 
     248    public void drawTextOnPath(Way way, TextElement text) { 
    250249        if (text == null) 
    251250            return; 
     
    348347        } 
    349348        return null; 
     349    } 
     350 
     351    public void drawLinePattern(Way way, ImageIcon pattern) { 
     352        final int width = pattern.getIconWidth(); 
     353        final int height = pattern.getIconHeight(); 
     354 
     355        Point lastP = null; 
     356        double wayLength = 0; 
     357 
     358        Iterator<Node> it = way.getNodes().iterator(); 
     359        while (it.hasNext()) { 
     360            Node n = it.next(); 
     361            Point thisP = nc.getPoint(n); 
     362 
     363            if (lastP != null) { 
     364                final double segmentLength = thisP.distance(lastP); 
     365 
     366                final double dx = thisP.x - lastP.x; 
     367                final double dy = thisP.y - lastP.y; 
     368 
     369                double dist = wayLength == 0 ? 0 : width - (wayLength % width); 
     370 
     371                AffineTransform saveTransform = g.getTransform(); 
     372                g.translate(lastP.x, lastP.y); 
     373                g.rotate(Math.atan2(dy, dx)); 
     374 
     375                if (dist > 0) { 
     376                    g.drawImage(pattern.getImage(), 0, 0, (int) dist, height, 
     377                            width - (int) dist, 0, width, height, null); 
     378                } 
     379                while (dist < segmentLength) { 
     380                    if (dist + width > segmentLength) { 
     381                        g.drawImage(pattern.getImage(), (int) dist, 0, (int) segmentLength, height, 
     382                                0, 0, (int) segmentLength - (int) dist, height, null); 
     383                    } else { 
     384                        pattern.paintIcon(nc, g, (int) dist, 0); 
     385                    } 
     386                    dist += width; 
     387                } 
     388                g.setTransform(saveTransform); 
     389 
     390                wayLength += segmentLength; 
     391            } 
     392            lastP = thisP; 
     393        } 
    350394    } 
    351395 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java

    r4005 r4006  
    3434 
    3535    public abstract void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member); 
     36 
     37    public boolean isProperLineStyle() { 
     38        return false; 
     39    } 
    3640 
    3741    /** 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java

    r3903 r4006  
    6464            } 
    6565        } else if (osm instanceof Way) { 
    66             boolean hasNonModifierLine = false; 
     66            boolean hasProperLineStyle = false; 
    6767            for (ElemStyle s : p.a) { 
    68                 if (s instanceof LineElemStyle && !s.isModifier) { 
    69                     hasNonModifierLine = true; 
     68                if (s.isProperLineStyle()) { 
     69                    hasProperLineStyle = true; 
    7070                    break; 
    7171                } 
    7272            } 
    73             if (!hasNonModifierLine) { 
     73            if (!hasProperLineStyle) { 
    7474                AreaElemStyle area = Utils.find(p.a, AreaElemStyle.class); 
    7575                LineElemStyle line = (area == null ? LineElemStyle.UNTAGGED_WAY : LineElemStyle.createSimpleLineStyle(area.color)); 
     
    9292 
    9393            boolean isOuterWayOfSomeMP = false; 
    94             boolean hasIndependentLineElemStyle = false; 
    9594            Color wayColor = null; 
    9695 
     
    104103 
    105104                if (multipolygon.getOuterWays().contains(osm)) { 
     105                    boolean hasIndependentLineStyle = false; 
    106106                    if (!isOuterWayOfSomeMP) { // do this only one time 
    107107                        List<ElemStyle> tmp = new ArrayList<ElemStyle>(p.a.size()); 
     
    111111                            } else { 
    112112                                tmp.add(s); 
     113                                if (s.isProperLineStyle()) { 
     114                                    hasIndependentLineStyle = true; 
     115                                } 
    113116                            } 
    114117                        } 
    115118                        p.a = new StyleList(tmp); 
    116119                        isOuterWayOfSomeMP = true; 
    117                         hasIndependentLineElemStyle = Utils.exists(p.a, LineElemStyle.class); 
    118                     } 
    119  
    120                     if (!hasIndependentLineElemStyle) { 
     120                    } 
     121 
     122                    if (!hasIndependentLineStyle) { 
    121123                        Pair<StyleList, Range> mpElemStyles = getStyleCacheWithRange(r, scale, nc); 
    122                         LineElemStyle mpLine = Utils.find(mpElemStyles.a, LineElemStyle.class); 
     124                        ElemStyle mpLine = null; 
     125                        for (ElemStyle s : mpElemStyles.a) { 
     126                            if (s.isProperLineStyle()) { 
     127                                mpLine = s; 
     128                                break; 
     129                            } 
     130                        } 
    123131                        if (mpLine != null) { 
    124                                 p.a = new StyleList(p.a, mpLine); 
    125                                 p.b = Range.cut(p.b, mpElemStyles.b); 
    126                                 break; 
     132                            p.a = new StyleList(p.a, mpLine); 
     133                            p.b = Range.cut(p.b, mpElemStyles.b); 
     134                            break; 
    127135                        } else if (wayColor == null) { 
    128136                            AreaElemStyle mpArea = Utils.find(mpElemStyles.a, AreaElemStyle.class); 
     
    136144            } 
    137145            if (isOuterWayOfSomeMP) { 
    138                 if (!Utils.exists(p.a, LineElemStyle.class)) { 
     146                boolean hasLineStyle = false; 
     147                for (ElemStyle s : p.a) { 
     148                    if (s.isProperLineStyle()) { 
     149                        hasLineStyle = true; 
     150                        break; 
     151                    } 
     152                } 
     153                if (!hasLineStyle) { 
    139154                    p.a = new StyleList(p.a, LineElemStyle.createSimpleLineStyle(wayColor)); 
    140155                } 
     
    155170                    boolean hasIndependentElemStyle = false; 
    156171                    for (ElemStyle s : p.a) { 
    157                         if (s instanceof LineElemStyle || s instanceof AreaElemStyle) { 
     172                        if (s.isProperLineStyle() || s instanceof AreaElemStyle) { 
    158173                            hasIndependentElemStyle = true; 
    159174                        } 
     
    226241            if (osm instanceof Way) { 
    227242                addIfNotNull(sl, AreaElemStyle.create(c)); 
     243                addIfNotNull(sl, LinePatternElemStyle.create(env)); 
    228244                addIfNotNull(sl, LineElemStyle.createLine(env)); 
    229245                addIfNotNull(sl, LineElemStyle.createCasing(env)); 
     246                addIfNotNull(sl, LineTextElemStyle.create(env)); 
    230247            } else if (osm instanceof Node) { 
    231248                addIfNotNull(sl, NodeElemStyle.create(env)); 
     
    233250                if (((Relation)osm).isMultipolygon()) { 
    234251                    addIfNotNull(sl, AreaElemStyle.create(c)); 
     252                    addIfNotNull(sl, LinePatternElemStyle.create(env)); 
    235253                    addIfNotNull(sl, LineElemStyle.createLine(env)); 
    236254                    addIfNotNull(sl, LineElemStyle.createCasing(env)); 
     255                    addIfNotNull(sl, LineTextElemStyle.create(env)); 
    237256                } else if ("restriction".equals(osm.get("type"))) { 
    238257                    addIfNotNull(sl, NodeElemStyle.create(env)); 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r4005 r4006  
    3030    public Color color; 
    3131    public Color dashesBackground; 
    32     public TextElement text; 
    3332    public float realWidth; // the real width of this line in meter 
    3433 
    3534    private BasicStroke dashesLine; 
    3635 
    37     protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, TextElement text, float realWidth) { 
     36    protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, float realWidth) { 
    3837        super(c, 0f); 
    3938        this.line = line; 
     
    4140        this.dashesLine = dashesLine; 
    4241        this.dashesBackground = dashesBackground; 
    43         this.text = text; 
    4442        this.realWidth = realWidth; 
    4543    } 
     
    181179        } 
    182180 
    183         TextElement text = null; 
    184         if (!casing) { 
    185             Keyword textPos = c.get("text-position", null, Keyword.class); 
    186             if (textPos == null || equal(textPos.val, "line")) { 
    187                 text = TextElement.create(c, PaintColors.TEXT.get(), false); 
    188             } 
    189         } 
    190  
    191         return new LineElemStyle(c, line, color, dashesLine, dashesBackground, text, realWidth); 
     181        return new LineElemStyle(c, line, color, dashesLine, dashesBackground, realWidth); 
    192182    } 
    193183 
     
    235225        } 
    236226 
    237         painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, text, showOrientation, 
     227        painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, showOrientation, 
    238228                showOnlyHeadArrowOnly, showOneway, onewayReversed); 
    239229 
     
    252242 
    253243    @Override 
     244    public boolean isProperLineStyle() { 
     245        return !isModifier; 
     246    } 
     247 
     248    @Override 
    254249    public boolean equals(Object obj) { 
    255250        if (obj == null || getClass() != obj.getClass()) 
     
    259254        final LineElemStyle other = (LineElemStyle) obj; 
    260255        return  equal(line, other.line) && 
    261         equal(color, other.color) && 
    262         equal(dashesLine, other.dashesLine) && 
    263         equal(dashesBackground, other.dashesBackground) && 
    264         equal(text, other.text) && 
    265         realWidth == other.realWidth; 
     256            equal(color, other.color) && 
     257            equal(dashesLine, other.dashesLine) && 
     258            equal(dashesBackground, other.dashesBackground) && 
     259            realWidth == other.realWidth; 
    266260    } 
    267261 
     
    273267        hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0); 
    274268        hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0); 
    275         hash = 29 * hash + (text != null ? text.hashCode() : 0); 
    276269        hash = 29 * hash + Float.floatToIntBits(realWidth); 
    277270        return hash; 
     
    281274    public String toString() { 
    282275        return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() + 
    283         " realWidth=" + realWidth + " color=" + Utils.toString(color) + 
    284         " dashed=" + Arrays.toString(line.getDashArray()) + 
    285         (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) + 
    286         " dashedColor=" + Utils.toString(dashesBackground) + '}'; 
     276            " realWidth=" + realWidth + " color=" + Utils.toString(color) + 
     277            " dashed=" + Arrays.toString(line.getDashArray()) + 
     278            (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) + 
     279            " dashedColor=" + Utils.toString(dashesBackground) + '}'; 
    287280    } 
    288281} 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java

    r3893 r4006  
    4040        public void execute(Environment env) { 
    4141            Object value = (val instanceof Expression) ? ((Expression) val).evaluate(env) : val; 
    42             if (key.equals("icon-image") || key.equals("fill-image")) { 
     42            if (key.equals("icon-image") || key.equals("fill-image") || key.equals("pattern-image")) { 
    4343                if (value instanceof String) { 
    4444                    value = new IconReference((String) value, env.source); 
Note: See TracChangeset for help on using the changeset viewer.