Ignore:
Timestamp:
2011-02-09T19:13:04+01:00 (15 years ago)
Author:
bastiK
Message:

mapcss: improve shape & area style generation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.