Changeset 3860 in josm for trunk/src/org


Ignore:
Timestamp:
2011-02-06T10:19:47+01:00 (13 years ago)
Author:
bastiK
Message:

LineElemStyle refactoring (more caching and additional properties)

Location:
trunk/src/org/openstreetmap/josm
Files:
9 edited
1 moved

Legend:

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

    r3859 r3860  
    100100    }
    101101
    102     public void drawWay(Way way, Color color, float width, float dashed[], float dashesOffset, Color dashedColor, boolean showDirection,
     102    public void drawWay(Way way, Color color, BasicStroke line, BasicStroke dashes, Color dashedColor, boolean showDirection,
    103103            boolean reversedDirection, boolean showHeadArrowOnly) {
    104104
     
    156156            lastPoint = p;
    157157        }
    158         displaySegments(path, arrows, color, width, dashed, dashesOffset, dashedColor);
    159     }
    160 
    161     private void displaySegments(GeneralPath path, GeneralPath arrows, Color color, float width, float dashed[], float dashesOffset, Color dashedColor) {
     158        displaySegments(path, arrows, color, line, dashes, dashedColor);
     159    }
     160
     161    private void displaySegments(GeneralPath path, GeneralPath arrows, Color color, BasicStroke line, BasicStroke dashes, Color dashedColor) {
    162162        g.setColor(inactive ? inactiveColor : color);
    163163        if (useStrokes) {
    164             if (dashed != null && dashed.length > 0) {
    165                 g.setStroke(new BasicStroke(width,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0,dashed,dashesOffset));
    166             } else {
    167                 g.setStroke(new BasicStroke(width,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    168             }
     164            g.setStroke(line);
    169165        }
    170166        g.draw(path);
    171167        g.draw(arrows);
    172168
    173         if(!inactive && useStrokes && dashedColor != null) {
     169        if(!inactive && useStrokes && dashes != null) {
    174170            g.setColor(dashedColor);
    175             if (dashed != null && dashed.length > 0) {
    176                 float[] dashedOffset = new float[dashed.length];
    177                 System.arraycopy(dashed, 0, dashedOffset, 1, dashed.length - 1);
    178                 dashedOffset[0] = dashed[dashed.length-1];
    179                 float offset = dashedOffset[0] + dashesOffset;
    180                 g.setStroke(new BasicStroke(width,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0,dashedOffset, offset));
    181             } else {
    182                 g.setStroke(new BasicStroke(width,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    183             }
     171            g.setStroke(dashes);
    184172            g.draw(path);
    185173            g.draw(arrows);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java

    r3858 r3860  
    1919    protected Map<String, Object> prop = new HashMap<String, Object>();
    2020
     21    public <T> T get(String key, T def, Class<T> klass) {
     22        return get(key, def, klass, false);
     23    }
     24   
    2125    /**
    2226     * Get value for the given key
     
    2428     * @param def default value, can be null
    2529     * @param klass the same as T
     30     * @param suppressWarnings show or don't show a warning when some value is
     31     *      found, but cannot be converted to the requested type
    2632     * @return if a value with class klass has been mapped to key, returns this
    2733     *      value, def otherwise
    2834     */
    29     public <T> T get(String key, T def, Class<T> klass) {
     35    public <T> T get(String key, T def, Class<T> klass, boolean suppressWarnings) {
    3036        if (def != null && !klass.isInstance(def))
    3137            throw new IllegalArgumentException();
     
    3541        T res = convertTo(o, klass);
    3642        if (res == null) {
    37             System.err.println(String.format("Warning: unable to convert property %s to type %s: found %s of type %s!", key, klass, o, o.getClass()));
     43            if (!suppressWarnings) {
     44                System.err.println(String.format("Warning: unable to convert property %s to type %s: found %s of type %s!", key, klass, o, o.getClass()));
     45            }
    3846            return def;
    3947        } else
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java

    r3855 r3860  
    193193        List<ElemStyle> sl = new ArrayList<ElemStyle>();
    194194        MultiCascade mc = new MultiCascade();
     195        Environment env = new Environment(osm, mc, null, null);
    195196
    196197        for (StyleSource s : styleSources) {
     
    203204            if ("*".equals(e.getKey()))
    204205                continue;
     206            env.layer = e.getKey();
    205207            Cascade c = e.getValue();
    206208            if (osm instanceof Way) {
    207209                addIfNotNull(sl, AreaElemStyle.create(c));
    208                 addIfNotNull(sl, LineElemStyle.createLine(c));
    209                 addIfNotNull(sl, LineElemStyle.createCasing(c));
     210                addIfNotNull(sl, LineElemStyle.createLine(env));
     211                addIfNotNull(sl, LineElemStyle.createCasing(env));
    210212            } else if (osm instanceof Node) {
    211213                addIfNotNull(sl, NodeElemStyle.create(c));
     
    213215                if (((Relation)osm).isMultipolygon()) {
    214216                    addIfNotNull(sl, AreaElemStyle.create(c));
    215                     addIfNotNull(sl, LineElemStyle.createLine(c));
    216                     addIfNotNull(sl, LineElemStyle.createCasing(c));
     217                    addIfNotNull(sl, LineElemStyle.createLine(env));
     218                    addIfNotNull(sl, LineElemStyle.createCasing(env));
    217219                } else if ("restriction".equals(osm.get("type"))) {
    218220                    addIfNotNull(sl, NodeElemStyle.create(c));
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Environment.java

    r3858 r3860  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.gui.mappaint.mapcss;
     2package org.openstreetmap.josm.gui.mappaint;
    33
    44import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    99public class Environment {
    1010   
    11     OsmPrimitive osm;
    12     MultiCascade mc;
    13     String layer;
    14     StyleSource source;
     11    public OsmPrimitive osm;
     12    public MultiCascade mc;
     13    public String layer;
     14    public StyleSource source;
    1515
    1616    public Environment(OsmPrimitive osm, MultiCascade mc, String layer, StyleSource source) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r3859 r3860  
    22package org.openstreetmap.josm.gui.mappaint;
    33
     4import static org.openstreetmap.josm.tools.Utils.equal;
     5
     6import java.awt.BasicStroke;
    47import java.awt.Color;
    58import java.util.Arrays;
     
    1114import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
    1215import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
     16import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
    1317import org.openstreetmap.josm.tools.Utils;
    1418
     
    1620
    1721    public static LineElemStyle createSimpleLineStyle(Color color) {
    18         return new LineElemStyle(Cascade.EMPTY_CASCADE, -1f, 0f, color != null ? color : PaintColors.UNTAGGED.get(), null, 0f, null);
     22        Cascade c = new Cascade();
     23        c.put("width", -1f);
     24        c.put("color", color != null ? color : PaintColors.UNTAGGED.get());
     25        MultiCascade mc = new MultiCascade();
     26        mc.put("default", c);
     27        return createLine(new Environment(null, mc, "default", null));
    1928    }
    2029    public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null);
    2130
    22     private float width;
    2331    public float realWidth; // the real width of this line in meter
    2432    public Color color;
    25     private float[] dashed;
    26     private float dashesOffset;
    27     public Color dashedColor;
    28 
    29     protected LineElemStyle(Cascade c, float width, float realWidth, Color color, float[] dashed, float dashesOffset, Color dashedColor) {
     33    public Color dashesBackground;
     34
     35    private BasicStroke line;
     36    private BasicStroke dashesLine;
     37
     38    protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, float realWidth) {
    3039        super(c);
    31         setWidth(width);
     40        this.line = line;
     41        this.color = color;
     42        this.dashesLine = dashesLine;
     43        this.dashesBackground = dashesBackground;
    3244        this.realWidth = realWidth;
    33         this.color = color;
    34         this.dashed = dashed;
    35         this.dashesOffset = dashesOffset;
    36         this.dashedColor = dashedColor;
    37     }
    38 
    39     public static LineElemStyle createLine(Cascade c) {
    40         return createImpl(c, "");
    41     }
    42 
    43     public static LineElemStyle createCasing(Cascade c) {
    44         LineElemStyle casing =  createImpl(c, "casing-");
     45    }
     46
     47    public static LineElemStyle createLine(Environment env) {
     48        return createImpl(env, "");
     49    }
     50
     51    public static LineElemStyle createCasing(Environment env) {
     52        LineElemStyle casing =  createImpl(env, "casing-");
    4553        if (casing != null) {
    4654            casing.object_z_index = -1;
     
    4957    }
    5058
    51     private static LineElemStyle createImpl(Cascade c, String prefix) {
    52         Float width = c.get(prefix + "width", null, Float.class);
    53         if (width == null)
    54             return null;
     59    private static LineElemStyle createImpl(Environment env, String prefix) {
     60        Cascade c = env.getCascade();
     61        Float width = c.get(prefix + "width", null, Float.class, true);
     62        if (width != null) {
     63            if (width == -1f) {
     64                width = (float) MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
     65            }
     66            if (width <= 0)
     67                return null;
     68        } else {
     69            String width_key = c.get(prefix + "width", null, String.class, true);
     70            if (equal(width_key, "thinnest")) {
     71                width = 0f;
     72            } else if (! equal(env.layer, "default")) {
     73                RelativeFloat width_rel = c.get(prefix + "width", null, RelativeFloat.class, true);
     74                if (width_rel != null) {
     75                    width = env.mc.getCascade("default").get("width", 0f, Float.class) + width_rel.val;
     76                } else
     77                    return null;
     78            } else
     79                return null;
     80        }
    5581
    5682        float realWidth = c.get(prefix + "real-width", 0f, Float.class);
     83        if (realWidth > 0 && MapPaintSettings.INSTANCE.isUseRealWidth()) {
     84
     85            /* if we have a "width" tag, try use it */
     86            String widthTag = env.osm.get("width");
     87            if(widthTag == null) {
     88                widthTag = env.osm.get("est_width");
     89            }
     90            if(widthTag != null) {
     91                try {
     92                    realWidth = new Float(Integer.parseInt(widthTag));
     93                }
     94                catch(NumberFormatException nfe) {
     95                }
     96            }
     97        }
     98
    5799        Color color = c.get(prefix + "color", null, Color.class);
    58100        if (color == null) {
     
    82124                }
    83125            }
    84             if (!hasPositive) {
     126            if (!hasPositive || dashes.length == 0) {
    85127                dashes = null;
    86128            }
     
    97139        }
    98140
    99         return new LineElemStyle(c, width, realWidth, color, dashes, dashesOffset, dashesBackground);
     141        int cap;
     142        String capStr = c.get(prefix + "linecap", null, String.class);
     143        if (equal(capStr, "none")) {
     144            cap = BasicStroke.CAP_BUTT;
     145        } else if (equal(capStr, "round")) {
     146            cap = BasicStroke.CAP_ROUND;
     147        } else if (equal(capStr, "square")) {
     148            cap = BasicStroke.CAP_SQUARE;
     149        } else {
     150            cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
     151        }
     152
     153        int join;
     154        String joinStr = c.get(prefix + "linejoin", null, String.class);
     155        if (equal(joinStr, "round")) {
     156            join = BasicStroke.JOIN_ROUND;
     157        } else if (equal(joinStr, "miter")) {
     158            join = BasicStroke.JOIN_MITER;
     159        } else if (equal(joinStr, "bevel")) {
     160            join = BasicStroke.JOIN_BEVEL;
     161        } else {
     162            join = BasicStroke.JOIN_ROUND;
     163        }
     164
     165        float miterlimit = c.get(prefix + "miterlimit", 10f, Float.class);
     166
     167        BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
     168        BasicStroke dashesLine = null;
     169
     170        if (dashes != null && dashesBackground != null) {
     171            float[] dashes2 = new float[dashes.length];
     172            System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
     173            dashes2[0] = dashes[dashes.length-1];
     174            dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
     175        }
     176
     177        return new LineElemStyle(c, line, color, dashesLine, dashesBackground, realWidth);
    100178    }
    101179
     
    114192        Node lastN;
    115193
    116         Color myDashedColor = dashedColor;
    117         float myWidth = getWidth();
    118 
     194        Color myDashedColor = dashesBackground;
     195        BasicStroke myLine = line, myDashLine = dashesLine;
    119196        if (realWidth > 0 && paintSettings.isUseRealWidth() && !showDirection) {
    120 
    121             /* if we have a "width" tag, try use it */
    122             /* (this might be slow and could be improved by caching the value in the Way, on the other hand only used if "real width" is enabled) */
    123             String widthTag = w.get("width");
    124             if(widthTag == null) {
    125                 widthTag = w.get("est_width");
    126             }
    127             if(widthTag != null) {
    128                 try {
    129                     realWidth = new Float(Integer.parseInt(widthTag));
    130                 }
    131                 catch(NumberFormatException nfe) {
    132                 }
    133             }
    134 
    135             myWidth = (int) (100 /  (float) (painter.getCircum() / realWidth));
    136             if (myWidth < getWidth()) {
    137                 myWidth = getWidth();
     197            float myWidth = (int) (100 /  (float) (painter.getCircum() / realWidth));
     198            if (myWidth < line.getLineWidth()) {
     199                myWidth = line.getLineWidth();
     200            }
     201            myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
     202                    line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
     203            if (dashesLine != null) {
     204                myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
     205                        dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
    138206            }
    139207        }
     
    151219        }
    152220
    153         painter.drawWay(w, markColor != null ? markColor : color, myWidth, dashed,
    154                 dashesOffset, myDashedColor, showDirection,
     221        painter.drawWay(w, markColor != null ? markColor : color, myLine, myDashLine, myDashedColor, showDirection,
    155222                selected ? false : reversedDirection, showOnlyHeadArrowOnly);
    156223
     
    168235    }
    169236
    170     public float getWidth() {
    171         if (width == -1f)
    172             return MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
    173         return width;
    174     }
    175 
    176     public void setWidth(float width) {
    177         this.width = width;
    178     }
    179 
    180237    @Override
    181238    public boolean equals(Object obj) {
     
    185242            return false;
    186243        final LineElemStyle other = (LineElemStyle) obj;
    187         return width == other.width &&
    188                 realWidth == other.realWidth &&
    189                 Utils.equal(color, other.color) &&
    190                 Arrays.equals(dashed, other.dashed) &&
    191                 dashesOffset == other.dashesOffset &&
    192                 Utils.equal(dashedColor, other.dashedColor);
     244        return  equal(line, other.line) &&
     245                equal(color, other.color) &&
     246                equal(dashesLine, other.dashesLine) &&
     247                equal(dashesBackground, other.dashesBackground) &&
     248                realWidth == other.realWidth;
    193249    }
    194250
     
    196252    public int hashCode() {
    197253        int hash = super.hashCode();
    198         hash = 29 * hash + Float.floatToIntBits(width);
     254        hash = 29 * hash + line.hashCode();
     255        hash = 29 * hash + color.hashCode();
     256        hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
     257        hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
    199258        hash = 29 * hash + Float.floatToIntBits(realWidth);
    200         hash = 29 * hash + color.hashCode();
    201         hash = 29 * hash + Arrays.hashCode(dashed);
    202         hash = 29 * hash + Float.floatToIntBits(dashesOffset);
    203         hash = 29 * hash + (dashedColor != null ? dashedColor.hashCode() : 0);
    204259        return hash;
    205260    }
     
    207262    @Override
    208263    public String toString() {
    209         return "LineElemStyle{" + super.toString() + "width=" + width +
     264        return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
    210265                " realWidth=" + realWidth + " color=" + Utils.toString(color) +
    211                 " dashed=" + Arrays.toString(dashed) +
    212                 (dashesOffset == 0f ? "" : " dashesOffses=" + dashesOffset) +
    213                 " dashedColor=" + Utils.toString(dashedColor) + '}';
     266                " dashed=" + Arrays.toString(line.getDashArray()) +
     267                (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) +
     268                " dashedColor=" + Utils.toString(dashesBackground) + '}';
    214269    }
    215270}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    r3856 r3860  
    44import org.openstreetmap.josm.data.osm.Relation;
    55import org.openstreetmap.josm.data.osm.Way;
     6import org.openstreetmap.josm.gui.mappaint.Environment;
    67import org.openstreetmap.josm.tools.Utils;
    78
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Expression.java

    r3856 r3860  
    1515import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
    1616import org.openstreetmap.josm.gui.mappaint.Cascade;
     17import org.openstreetmap.josm.gui.mappaint.Environment;
    1718import org.openstreetmap.josm.tools.Utils;
    1819
     
    137138                }
    138139                return c.containsKey(key);
     140            }
     141
     142            public String get_tag_value(String key) {
     143                return env.osm.get(key);
     144            }
     145
     146            public boolean has_tag_key(String key) {
     147                return env.osm.hasKey(key);
    139148            }
    140149
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java

    r3858 r3860  
    44import java.util.Arrays;
    55
     6import org.openstreetmap.josm.gui.mappaint.Environment;
    67import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    78
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r3858 r3860  
    1313import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1414import org.openstreetmap.josm.gui.mappaint.Cascade;
     15import org.openstreetmap.josm.gui.mappaint.Environment;
    1516import org.openstreetmap.josm.gui.mappaint.MultiCascade;
    1617import org.openstreetmap.josm.gui.mappaint.Range;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r3856 r3860  
    88import org.openstreetmap.josm.data.osm.Relation;
    99import org.openstreetmap.josm.data.osm.Way;
     10import org.openstreetmap.josm.gui.mappaint.Environment;
    1011import org.openstreetmap.josm.gui.mappaint.Range;
    1112import org.openstreetmap.josm.tools.Pair;
Note: See TracChangeset for help on using the changeset viewer.