Ignore:
Timestamp:
2012-01-19T19:58:08+01:00 (12 years ago)
Author:
bastiK
Message:

make identity of map icons depend on the name and not the image data (see #6797)

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

    r4820 r4822  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
     3
     4import static org.openstreetmap.josm.tools.Utils.equal;
    35
    46import java.awt.Color;
     
    2628     */
    2729    public Color color;
    28     public BufferedImage fillImage;
    29     public float fillImageAlpha;
     30    public MapImage<BufferedImage> fillImage;
    3031    public TextElement text;
    3132
    32     protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage, float fillImageAlpha, TextElement text) {
     33    protected AreaElemStyle(Cascade c, Color color, MapImage<BufferedImage> fillImage, TextElement text) {
    3334        super(c, -1000f);
    3435        CheckParameterUtil.ensureParameterNotNull(color);
    3536        this.color = color;
    3637        this.fillImage = fillImage;
    37         this.fillImageAlpha = fillImageAlpha;
    3838        this.text = text;
    3939    }
    4040
    4141    public static AreaElemStyle create(Cascade c) {
    42         BufferedImage fillImage = null;
     42        MapImage<BufferedImage> fillImage = null;
    4343        Color color = null;
    44         float fillImageAlpha = 1f;
    4544
    4645        IconReference iconRef = c.get("fill-image", null, IconReference.class);
     
    5049                if (!(icon.getImage() instanceof BufferedImage))
    5150                    throw new RuntimeException();
    52                 fillImage = (BufferedImage) icon.getImage();
     51                fillImage = new MapImage<BufferedImage>(iconRef.iconName, iconRef.source);
     52                fillImage.img = (BufferedImage) icon.getImage();
    5353
    54                 color = new Color(fillImage.getRGB(fillImage.getWidth() / 2, fillImage.getHeight() / 2));
     54                color = new Color(fillImage.img.getRGB(
     55                        fillImage.img.getWidth() / 2, fillImage.img.getHeight() / 2)
     56                );
    5557
    56                 fillImageAlpha = Utils.color_int2float(Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255)))));
    57                 Float pAlpha = c.get("fill-opacity", null, Float.class);
     58                fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
     59                Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
    5860                if (pAlpha != null) {
    59                     if (pAlpha < 0f || pAlpha > 1f) {
    60                         pAlpha= 1f;
    61                     }
    62                     fillImageAlpha = pAlpha;
     61                    fillImage.alpha = pAlpha;
    6362                }
    6463            }
     
    8281       
    8382        if (color != null)
    84             return new AreaElemStyle(c, color, fillImage, fillImageAlpha, text);
     83            return new AreaElemStyle(c, color, fillImage, text);
    8584        else
    8685            return null;
     
    9796                }
    9897            }
    99             painter.drawArea((Way) osm, myColor, fillImage, fillImageAlpha, text);
     98            painter.drawArea((Way) osm, myColor, fillImage, text);
    10099        } else if (osm instanceof Relation)
    101100        {
     
    106105                }
    107106            }
    108             painter.drawArea((Relation) osm, myColor, fillImage, fillImageAlpha, text);
     107            painter.drawArea((Relation) osm, myColor, fillImage, text);
    109108        }
    110109    }
     
    118117        AreaElemStyle other = (AreaElemStyle) obj;
    119118        // we should get the same image object due to caching
    120         if (fillImage != other.fillImage)
     119        if (!equal(fillImage, other.fillImage))
    121120            return false;
    122         if (!Utils.equal(color, other.color))
     121        if (!equal(color, other.color))
    123122            return false;
    124         if (fillImageAlpha != other.fillImageAlpha)
    125             return false;
    126         if (!Utils.equal(text, other.text))
     123        if (!equal(text, other.text))
    127124            return false;
    128125        return true;
     
    134131        hash = 61 * hash + color.hashCode();
    135132        hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
    136         hash = 61 * hash + Float.floatToIntBits(fillImageAlpha);
    137133        hash = 61 * hash + (text != null ? text.hashCode() : 0);
    138134        return hash;
     
    142138    public String toString() {
    143139        return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
    144                 " fillImageAlpha=" + fillImageAlpha + " fillImage=[" + fillImage + "]}";
     140                " fillImage=[" + fillImage + "]}";
    145141    }
    146142}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LinePatternElemStyle.java

    r4820 r4822  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
     3
     4import java.awt.Image;
    35
    46import javax.swing.ImageIcon;
     
    1517public class LinePatternElemStyle extends ElemStyle {
    1618
    17     public ImageIcon pattern;
     19    public MapImage<Image> pattern;
    1820
    19     public LinePatternElemStyle(Cascade c, ImageIcon pattern) {
     21    public LinePatternElemStyle(Cascade c, MapImage<Image> pattern) {
    2022        super(c, -1f);
    2123        this.pattern = pattern;
     
    3133        if (icon == null)
    3234            return null;
    33         return new LinePatternElemStyle(c, icon);
     35        MapImage<Image> pattern = new MapImage<Image>(iconRef.iconName, iconRef.source);
     36        pattern.img = icon.getImage();
     37        return new LinePatternElemStyle(c, pattern);
    3438    }
    3539
     
    3741    public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
    3842        Way w = (Way)primitive;
    39         painter.drawLinePattern(w, pattern);
     43        painter.drawLinePattern(w, pattern.img);
    4044    }
    4145
     
    5256            return false;
    5357        final LinePatternElemStyle other = (LinePatternElemStyle) obj;
    54         return pattern.getImage() == other.pattern.getImage();
     58        return pattern.equals(other.pattern);
    5559    }
    5660
     
    6266    @Override
    6367    public String toString() {
    64         return "LinePatternElemStyle{" + super.toString() + "pattern=" + pattern + '}';
     68        return "LinePatternElemStyle{" + super.toString() + "pattern=[" + pattern + "]}";
    6569    }
    6670}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r4820 r4822  
    66import java.awt.BasicStroke;
    77import java.awt.Color;
     8import java.awt.Image;
    89import java.awt.Rectangle;
    910import java.awt.Stroke;
    1011
    11 import javax.swing.GrayFilter;
    1212import javax.swing.ImageIcon;
    1313
     
    2727 */
    2828public class NodeElemStyle extends ElemStyle {
    29     public ImageIcon icon;
    30     public int iconAlpha;
     29    public MapImage<Image> mapImage;
    3130    public Symbol symbol;
    3231
     
    9695    public static final StyleList DEFAULT_NODE_STYLELIST_TEXT = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE);
    9796
    98     protected NodeElemStyle(Cascade c, ImageIcon icon, Integer iconAlpha, Symbol symbol) {
     97    protected NodeElemStyle(Cascade c, MapImage<Image> mapImage, Symbol symbol) {
    9998        super(c, 1000f);
    100         this.icon = icon;
    101         this.iconAlpha = iconAlpha == null ? 0 : iconAlpha;
     99        this.mapImage = mapImage;
    102100        this.symbol = symbol;
    103101    }
     
    110108        Cascade c = env.mc.getCascade(env.layer);
    111109
    112         Pair<ImageIcon, Integer> icon = createIcon(env);
     110        MapImage<Image> mapImage = createIcon(env);
    113111        Symbol symbol = null;
    114         if (icon == null) {
     112        if (mapImage == null) {
    115113            symbol = createSymbol(env);
    116114        }
    117115
    118         // optimization: if we neither have a symbol, nor an icon
     116        // optimization: if we neither have a symbol, nor a mapImage
    119117        // we don't have to check for the remaining style properties and we don't
    120118        // have to allocate a node element style.
    121         if (!allowDefault && symbol == null && icon == null) return null;
    122 
    123         return new NodeElemStyle(c,
    124                 icon == null ? null : icon.a,
    125                 icon == null ? null : icon.b,
    126                 symbol);
    127     }
    128 
    129     private static Pair<ImageIcon, Integer> createIcon(Environment env) {
     119        if (!allowDefault && symbol == null && mapImage == null) return null;
     120
     121        return new NodeElemStyle(c, mapImage, symbol);
     122    }
     123
     124    private static MapImage<Image> createIcon(Environment env) {
    130125        Cascade c = env.mc.getCascade(env.layer);
    131126        Cascade c_def = env.mc.getCascade("default");
     
    150145        int height = heightF == null ? -1 : Math.round(heightF);
    151146
     147        MapImage<Image> mapImage = new MapImage<Image>(iconRef.iconName, iconRef.source);
     148
    152149        ImageIcon icon = MapPaintStyles.getIcon(iconRef, width, height);
    153         if (icon == null)
    154             return new Pair<ImageIcon, Integer>(MapPaintStyles.getNoIcon_Icon(iconRef.source), 255);
    155         int iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
    156         Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
    157         if (pAlpha != null) {
    158             iconAlpha = pAlpha;
    159         }
    160 
    161         return new Pair<ImageIcon, Integer>(icon, iconAlpha);
     150        if (icon == null) {
     151            mapImage.img = MapPaintStyles.getNoIcon_Icon(iconRef.source).getImage();
     152        } else {
     153            mapImage.img = icon.getImage();
     154            mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
     155            Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
     156            if (pAlpha != null) {
     157                mapImage.alpha = pAlpha;
     158            }
     159            mapImage.width = width;
     160            mapImage.height = height;
     161        }
     162        return mapImage;
    162163    }
    163164
     
    241242        if (primitive instanceof Node) {
    242243            Node n = (Node) primitive;
    243             if (icon != null && painter.isShowIcons()) {
    244                 painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? getDisabledIcon() : icon,
    245                         Utils.color_int2float(iconAlpha), selected, member);
     244            if (mapImage != null && painter.isShowIcons()) {
     245                painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? mapImage.getDisabled() : mapImage.img,
     246                        Utils.color_int2float(mapImage.alpha), selected, member);
    246247            } else if (symbol != null) {
    247248                Color fillColor = symbol.fillColor;
     
    303304
    304305            }
    305         } else if (primitive instanceof Relation && icon != null) {
    306             painter.drawRestriction((Relation) primitive, this);
    307         }
    308     }
    309 
    310     public ImageIcon getDisabledIcon() {
    311         if (disabledIcon != null)
    312             return disabledIcon;
    313         if (icon == null)
    314             return null;
    315         return disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
     306        } else if (primitive instanceof Relation && mapImage != null) {
     307            painter.drawRestriction((Relation) primitive, mapImage);
     308        }
    316309    }
    317310
    318311    public Rectangle getBox() {
    319         if (icon != null) {
    320             int w = icon.getIconWidth(), h=icon.getIconHeight();
     312        if (mapImage != null) {
     313            int w = mapImage.img.getWidth(null), h = mapImage.img.getHeight(null);
    321314            return new Rectangle(-w/2, -h/2, w, h);
    322315        } else if (symbol != null) {
     
    338331    public int hashCode() {
    339332        int hash = super.hashCode();
    340         hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0);
    341         hash = 17 * hash + iconAlpha;
     333        hash = 17 * hash + (mapImage != null ? mapImage.hashCode() : 0);
    342334        hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
    343335        return hash;
     
    353345        final NodeElemStyle other = (NodeElemStyle) obj;
    354346        // we should get the same image object due to caching
    355         if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage()))
    356             return false;
    357         if (this.iconAlpha != other.iconAlpha)
     347        if (!equal(mapImage, other.mapImage))
    358348            return false;
    359349        if (!equal(symbol, other.symbol))
     
    362352    }
    363353
    364 
    365354    @Override
    366355    public String toString() {
    367356        StringBuilder s = new StringBuilder("NodeElemStyle{");
    368357        s.append(super.toString());
    369         if (icon != null) {
    370             s.append(" icon=" + icon + " iconAlpha=" + iconAlpha);
     358        if (mapImage != null) {
     359            s.append(" icon=[" + mapImage + "]");
    371360        }
    372361        if (symbol != null) {
Note: See TracChangeset for help on using the changeset viewer.