Changeset 3879 in josm


Ignore:
Timestamp:
Feb 9, 2011 7:13:04 PM (2 years ago)
Author:
bastiK
Message:

mapcss: improve shape & area style generation

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

Legend:

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

    r3871 r3879  
    211211    } 
    212212 
    213     public void drawNodeSymbol(Node n, Symbol s, boolean selected, boolean member, TextElement text) { 
     213    public void drawNodeSymbol(Node n, Symbol s, Color fillColor, Color strokeColor, TextElement text) { 
    214214        Point p = nc.getPoint(n); 
    215215        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return; 
    216216        int radius = s.size / 2; 
    217217 
    218         if (s.fillColor != null) { 
    219             if (inactive || n.isDisabled()) { 
    220                 g.setColor(inactiveColor); 
    221             } else { 
    222                 g.setColor(s.fillColor); 
    223             } 
     218        if (fillColor != null) { 
     219            g.setColor(fillColor); 
    224220            switch (s.symbol) { 
    225221                case SQUARE: 
     
    235231        if (s.stroke != null) { 
    236232            g.setStroke(s.stroke); 
    237             if (inactive || n.isDisabled()) { 
    238                 g.setColor(inactiveColor); 
    239             } else { 
    240                 g.setColor(s.strokeColor); 
    241             } 
     233            g.setColor(strokeColor); 
    242234            switch (s.symbol) { 
    243235                case SQUARE: 
     
    353345    } 
    354346 
    355     public void drawArea(Way w, Color color, BufferedImage fillImage, String name) { 
     347    public void drawArea(Way w, Color color, BufferedImage fillImage, float fillImageAlpha, String name) { 
    356348        Polygon polygon = getPolygon(w); 
    357         drawArea(polygon, color, fillImage, name); 
    358     } 
    359  
    360     protected void drawArea(Polygon polygon, Color color, BufferedImage fillImage, String name) { 
     349        drawArea(polygon, color, fillImage, fillImageAlpha, name); 
     350    } 
     351 
     352    protected void drawArea(Polygon polygon, Color color, BufferedImage fillImage, float fillImageAlpha, String name) { 
    361353 
    362354        if (fillImage == null) { 
     
    367359                    new Rectangle(polygon.xpoints[0], polygon.ypoints[0], fillImage.getWidth(), fillImage.getHeight())); 
    368360            g.setPaint(texture); 
    369             if (color.getAlpha() != 255) { 
    370                 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Utils.color_int2float(color.getAlpha()))); 
     361            if (fillImageAlpha != 1f) { 
     362                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillImageAlpha)); 
    371363            } 
    372364            g.fill(polygon); 
     
    411403    } 
    412404 
    413     public void drawArea(Relation r, Color color, BufferedImage fillImage, String name) { 
     405    public void drawArea(Relation r, Color color, BufferedImage fillImage, float fillImageAlpha, String name) { 
    414406        Multipolygon multipolygon = new Multipolygon(nc); 
    415407        multipolygon.load(r); 
     
    420412                    continue; 
    421413                } 
    422                 drawArea(p, color, fillImage, getAreaName(r)); 
     414                drawArea(p, color, fillImage, fillImageAlpha, getAreaName(r)); 
    423415            } 
    424416        } 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

    r3865 r3879  
    1414import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter; 
    1515import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 
     16import org.openstreetmap.josm.tools.CheckParameterUtil; 
    1617import org.openstreetmap.josm.tools.Utils; 
    1718 
    1819public class AreaElemStyle extends ElemStyle 
    1920{ 
     21    /** 
     22     * If fillImage == null, color is the fill-color, otherwise 
     23     * an arbitrary color value sampled from the fillImage 
     24     */ 
    2025    public Color color; 
    2126    public BufferedImage fillImage; 
     27    public float fillImageAlpha; 
    2228 
    23     protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage) { 
     29    protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage, float fillImageAlpha) { 
    2430        super(c); 
     31        CheckParameterUtil.ensureParameterNotNull(color); 
    2532        this.color = color; 
    2633        this.fillImage = fillImage; 
     34        this.fillImageAlpha = fillImageAlpha; 
    2735    } 
    2836 
    2937    public static AreaElemStyle create(Cascade c) { 
    3038        BufferedImage fillImage = null; 
     39        Color color = null; 
     40        float fillImageAlpha = 1f; 
     41 
    3142        IconReference iconRef = c.get("fill-image", null, IconReference.class); 
    32         Integer fillImageAlpha = null; 
    33  
    3443        if (iconRef != null) { 
    3544            ImageIcon icon = MapPaintStyles.getIcon(iconRef, false); 
     
    4251                fillImage = (BufferedImage) icon.getImage(); 
    4352 
    44                 fillImageAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255)))); 
    45                 Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class)); 
     53                color = new Color(fillImage.getRGB(fillImage.getWidth() / 2, fillImage.getHeight() / 2)); 
     54 
     55                fillImageAlpha = Utils.color_int2float(Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))))); 
     56                Float pAlpha = c.get("fill-opacity", null, Float.class); 
    4657                if (pAlpha != null) { 
     58                    if (pAlpha < 0f || pAlpha > 1f) { 
     59                        pAlpha= 1f; 
     60                    } 
    4761                    fillImageAlpha = pAlpha; 
    4862                } 
    4963            } 
    50         } 
    51  
    52         Color color = c.get("fill-color", null, Color.class); 
    53         if (color != null) { 
    54  
    55             int alpha; 
    56             if (fillImageAlpha != null) { 
    57                 alpha = fillImageAlpha; 
    58             } else { 
    59                 alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50)))); 
     64        } else { 
     65            color = c.get("fill-color", null, Color.class); 
     66            if (color != null) { 
     67                int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50)))); 
    6068                Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class)); 
    6169                if (pAlpha != null) { 
    6270                    alpha = pAlpha; 
    6371                } 
     72                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 
    6473            } 
    65             color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 
    6674        } 
    6775         
    68         if (fillImage != null || color != null) { 
    69             if (color == null) { 
    70                 color = new Color(0, 0, 0, fillImageAlpha); 
    71             } 
    72             return new AreaElemStyle(c, color, fillImage); 
    73         } 
     76        if (color != null) 
     77            return new AreaElemStyle(c, color, fillImage, fillImageAlpha); 
    7478        else 
    7579            return null; 
     
    8690                } 
    8791            } 
    88             painter.drawArea((Way) osm, myColor, fillImage, 
     92            painter.drawArea((Way) osm, myColor, fillImage, fillImageAlpha, 
    8993                    painter.isShowNames() ? painter.getAreaName(osm) : null); 
    9094        } else if (osm instanceof Relation) 
     
    96100                } 
    97101            } 
    98             painter.drawArea((Relation) osm, myColor, fillImage, 
     102            painter.drawArea((Relation) osm, myColor, fillImage, fillImageAlpha, 
    99103                    painter.getAreaName(osm)); 
    100104        } 
     
    109113        AreaElemStyle other = (AreaElemStyle) obj; 
    110114        // we should get the same image object due to caching 
    111         if (fillImage != other.fillImage && (fillImage == null || other.fillImage == null || fillImage != other.fillImage)) 
     115        if (fillImage != other.fillImage) 
    112116            return false; 
    113117        if (!Utils.equal(color, other.color)) 
     118            return false; 
     119        if (fillImageAlpha != other.fillImageAlpha) 
    114120            return false; 
    115121        return true; 
     
    119125    public int hashCode() { 
    120126        int hash = 3; 
    121         hash = 61 * hash + (this.color != null ? this.color.hashCode() : 0); 
    122         hash = 61 * hash + (this.fillImage != null ? this.fillImage.hashCode() : 0); 
     127        hash = 61 * hash + color.hashCode(); 
     128        hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0); 
     129        hash = 61 * hash + Float.floatToIntBits(fillImageAlpha); 
    123130        return hash; 
    124131    } 
     
    126133    @Override 
    127134    public String toString() { 
    128         return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) + '}'; 
     135        return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) + 
     136                " fillImageAlpha=" + fillImageAlpha + " fillImage=[" + fillImage + "]}"; 
    129137    } 
    130138} 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3871 r3879  
    240240            return null; 
    241241 
    242         Float size = c.get("symbol-size", null, Float.class); 
    243         if (size == null || size <= 0) 
     242        float size = c.get("symbol-size", 10f, Float.class); 
     243        if (size <= 0) 
    244244            return null; 
    245245 
    246246        Float strokeWidth = c.get("symbol-stroke-width", null, Float.class); 
     247        if (strokeWidth != null && strokeWidth <= 0) { 
     248            strokeWidth = null; 
     249        } 
    247250        Color strokeColor = c.get("symbol-stroke-color", null, Color.class); 
     251 
     252        if (strokeWidth == null && strokeColor != null) { 
     253            strokeWidth = 1f; 
     254        } else if (strokeWidth != null && strokeColor == null) { 
     255            strokeColor = Color.ORANGE; 
     256        } 
     257 
     258        Stroke stroke = null; 
    248259        if (strokeColor != null) { 
    249260            float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class); 
    250261            strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(), 
    251262                    strokeColor.getBlue(), Utils.color_float2int(strokeAlpha)); 
    252         } 
    253         Stroke stroke = null; 
    254         if (strokeWidth != null && strokeWidth > 0 && strokeColor != null) { 
    255263            stroke = new BasicStroke(strokeWidth); 
    256264        } 
    257265 
    258266        Color fillColor = c.get("symbol-fill-color", null, Color.class); 
     267        if (stroke == null && fillColor == null) 
     268            fillColor = Color.BLUE; 
     269 
    259270        if (fillColor != null) { 
    260271            float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class); 
     
    263274        } 
    264275 
    265         if ((stroke == null || strokeColor == null) && fillColor == null) 
    266             return null; 
    267  
    268         return new Symbol(shape, size.intValue(), stroke, strokeColor, fillColor); 
     276        return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor); 
    269277    } 
    270278 
     
    277285                        Utils.color_int2float(iconAlpha), selected, member, text); 
    278286            } else if (symbol != null) { 
    279                 painter.drawNodeSymbol(n, symbol, selected, member, text); 
     287                Color fillColor = symbol.fillColor; 
     288                if (fillColor != null) { 
     289                    if (n.isHighlighted()) { 
     290                        fillColor = settings.getHighlightColor(); 
     291                    } else { 
     292                        if (painter.isInactive() || n.isDisabled()) { 
     293                            fillColor = settings.getInactiveColor(); 
     294                        } else if (selected) { 
     295                            fillColor = settings.getSelectedColor(fillColor.getAlpha()); 
     296                        } else if (member) { 
     297                            fillColor = settings.getRelationSelectedColor(fillColor.getAlpha()); 
     298                        } 
     299                    } 
     300                } 
     301                Color strokeColor = symbol.strokeColor; 
     302                if (strokeColor != null) { 
     303                    if (n.isHighlighted()) { 
     304                        strokeColor = settings.getHighlightColor(); 
     305                    } else { 
     306                        if (painter.isInactive() || n.isDisabled()) { 
     307                            strokeColor = settings.getInactiveColor(); 
     308                        } else if (selected) { 
     309                            strokeColor = settings.getSelectedColor(strokeColor.getAlpha()); 
     310                        } else if (member) { 
     311                            strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha()); 
     312                        } 
     313                    } 
     314                } 
     315                painter.drawNodeSymbol(n, symbol, fillColor, strokeColor, text); 
    280316            } else { 
    281317                if (n.isHighlighted()) { 
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r3869 r3879  
    138138     * convert float range 0 <= x <= 1 to integer range 0..255 
    139139     * when dealing with colors and color alpha value 
     140     * @return null if val is null, the corresponding int if val is in the 
     141     *         range 0...1. If val is outside that range, return 255 
    140142     */ 
    141143    public static Integer color_float2int(Float val) { 
Note: See TracChangeset for help on using the changeset viewer.