Changeset 3999 in josm


Ignore:
Timestamp:
Mar 22, 2011 1:33:04 PM (2 years ago)
Author:
bastiK
Message:

mapcss: add icon-size property

File:
1 edited

Legend:

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

    r3992 r3999  
    66import java.awt.BasicStroke; 
    77import java.awt.Color; 
     8import java.awt.Image; 
    89import java.awt.Stroke; 
    910 
     
    2021import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 
    2122import org.openstreetmap.josm.tools.CheckParameterUtil; 
     23import org.openstreetmap.josm.tools.Pair; 
    2224import org.openstreetmap.josm.tools.Utils; 
    2325 
     
    130132    } 
    131133 
    132     protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, NodeTextElement text) { 
     134    protected NodeElemStyle(Cascade c, ImageIcon icon, Integer iconAlpha, Symbol symbol, NodeTextElement text) { 
    133135        super(c); 
    134136        this.icon = icon; 
    135         this.iconAlpha = iconAlpha; 
     137        this.iconAlpha = iconAlpha == null ? 0 : iconAlpha; 
    136138        this.symbol = symbol; 
    137139        this.text = text; 
     
    158160        Cascade c = env.mc.getCascade(env.layer); 
    159161 
    160         IconReference iconRef = c.get("icon-image", null, IconReference.class); 
    161         ImageIcon icon = null; 
    162         int iconAlpha = 0; 
     162        Pair<ImageIcon, Integer> icon = createIcon(env); 
    163163        Symbol symbol = null; 
    164  
    165         if (iconRef != null) { 
    166             icon = MapPaintStyles.getIcon(iconRef, false); 
    167             if (icon == null) { 
    168                 icon = MapPaintStyles.getNoIcon_Icon(iconRef.source, false); 
    169             } 
    170             iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255)))); 
    171             Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class)); 
    172             if (pAlpha != null) { 
    173                 iconAlpha = pAlpha; 
    174             } 
    175         } else { 
     164        if (icon == null) { 
    176165            symbol = createSymbol(env); 
    177166        } 
     
    210199        } 
    211200 
    212         return new NodeElemStyle(c, icon, iconAlpha, symbol, text); 
     201        return new NodeElemStyle(c,  
     202                icon == null ? null : icon.a, 
     203                icon == null ? null : icon.b, 
     204                symbol, 
     205                text); 
     206    } 
     207 
     208    private static Pair<ImageIcon, Integer> createIcon(Environment env) { 
     209        Cascade c = env.mc.getCascade(env.layer); 
     210        Cascade c_def = env.mc.getCascade("default"); 
     211 
     212        IconReference iconRef = c.get("icon-image", null, IconReference.class); 
     213        if (iconRef == null) 
     214            return null; 
     215 
     216        ImageIcon icon = null; 
     217        int iconAlpha = 0; 
     218 
     219        icon = MapPaintStyles.getIcon(iconRef, false); 
     220        if (icon == null) 
     221            return new Pair<ImageIcon, Integer>(MapPaintStyles.getNoIcon_Icon(iconRef.source, false), 255); 
     222        else { 
     223            Float sizeOnDefault = c_def.get("icon-size", null, Float.class); 
     224            if (sizeOnDefault != null && sizeOnDefault <= 0) { 
     225                sizeOnDefault = null; 
     226            } 
     227            Float sizeF = getWidth(c, "icon-size", sizeOnDefault); 
     228 
     229            if (sizeF != null) { 
     230                if (sizeF <= 0) 
     231                    return null; 
     232                int size = Math.round(sizeF); 
     233                int width = icon.getIconWidth(); 
     234                int height = icon.getIconHeight(); 
     235                if (Math.max(width, height) != size) { 
     236                    if (width >= height) { 
     237                        width = size; 
     238                        height = -1; 
     239                    } else { 
     240                        width = -1; 
     241                        height = size; 
     242                    } 
     243                    icon.setImage(icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH)); 
     244                } 
     245            } 
     246            iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255)))); 
     247            Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class)); 
     248            if (pAlpha != null) { 
     249                iconAlpha = pAlpha; 
     250            } 
     251 
     252            return new Pair<ImageIcon, Integer>(icon, iconAlpha); 
     253        } 
    213254    } 
    214255 
Note: See TracChangeset for help on using the changeset viewer.