Changeset 3862 in josm


Ignore:
Timestamp:
Feb 6, 2011 3:48:58 PM (2 years ago)
Author:
bastiK
Message:

mapcss: fill-image

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

Legend:

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

    r3858 r3862  
    8181 
    8282        outlineOnly = Main.pref.getBoolean("draw.data.area_outline_only", false); 
    83  
     83         
    8484    } 
    8585 
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPaintVisitor.java

    r3836 r3862  
    101101        styles = MapPaintStyles.getStyles(); 
    102102 
     103        this.paintSettings = MapPaintSettings.INSTANCE; 
     104 
    103105        circum = nc.getDist100Pixel(); 
    104         boolean drawArea = circum <= Main.pref.getInteger("mappaint.fillareas", 10000000); 
     106        boolean drawArea = circum <= Main.pref.getInteger("mappaint.fillareas", 10000000) && !paintSettings.isOutlineOnly(); 
    105107        boolean drawMultipolygon = drawArea && Main.pref.getBoolean("mappaint.multipolygon", true); 
    106108        styles.setDrawMultipolygon(drawMultipolygon); 
     
    112114                        RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF); 
    113115 
    114         this.paintSettings = MapPaintSettings.INSTANCE; 
    115116        this.painter = new MapPainter(paintSettings, g, inactive, nc, virtual, circum, leftHandTraffic); 
    116117 
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java

    r3860 r3862  
    1111import java.awt.Polygon; 
    1212import java.awt.Rectangle; 
     13import java.awt.TexturePaint; 
    1314import java.awt.geom.GeneralPath; 
    1415import java.awt.geom.Rectangle2D; 
     16import java.awt.image.BufferedImage; 
    1517import java.util.Arrays; 
    1618import java.util.Collection; 
     
    4244    private final boolean showNames; 
    4345    private final boolean showIcons; 
    44     private final boolean outlineOnly; 
    4546 
    4647    private final Color inactiveColor; 
     
    7879        this.showNames = settings.getShowNamesDistance() > circum; 
    7980        this.showIcons = settings.getShowIconsDistance() > circum; 
    80         this.outlineOnly = settings.isOutlineOnly(); 
    8181 
    8282        this.inactiveColor = PaintColors.INACTIVE.get(); 
     
    258258    } 
    259259 
    260     public void drawArea(Way w, Color color, String name) { 
     260    public void drawArea(Way w, Color color, BufferedImage fillImage, String name) { 
    261261        Polygon polygon = getPolygon(w); 
    262         drawArea(polygon, color, name); 
    263     } 
    264  
    265     protected void drawArea(Polygon polygon, Color color, String name) { 
    266  
    267         g.setColor(color); 
    268  
    269         if (outlineOnly) { 
    270             g.drawPolygon(polygon); 
     262        drawArea(polygon, color, fillImage, name); 
     263    } 
     264 
     265    protected void drawArea(Polygon polygon, Color color, BufferedImage fillImage, String name) { 
     266 
     267        if (fillImage == null) { 
     268            g.setColor(color); 
     269            g.fillPolygon(polygon); 
    271270        } else { 
    272             g.fillPolygon(polygon); 
    273         } 
    274  
     271            TexturePaint texture = new TexturePaint(fillImage,  
     272                    new Rectangle(polygon.xpoints[0], polygon.ypoints[0], fillImage.getWidth(), fillImage.getHeight())); 
     273 
     274            g.setPaint(texture); 
     275            g.fill(polygon); 
     276        } 
    275277 
    276278        if (name != null) { 
     
    311313    } 
    312314 
    313     public void drawArea(Relation r, Color color, String name) { 
     315    public void drawArea(Relation r, Color color, BufferedImage fillImage, String name) { 
    314316        Multipolygon multipolygon = new Multipolygon(nc); 
    315317        multipolygon.load(r); 
     
    320322                    continue; 
    321323                } 
    322                 drawArea(p, color, getAreaName(r)); 
     324                drawArea(p, color, fillImage, getAreaName(r)); 
    323325            } 
    324326        } 
  • trunk/src/org/openstreetmap/josm/gui/dialogs/InspectPrimitiveDialog.java

    r3848 r3862  
    77import java.awt.Font; 
    88import java.awt.GridBagLayout; 
     9import java.util.ArrayList; 
    910import java.util.Collection; 
    1011import java.util.List; 
     
    3435import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 
    3536import org.openstreetmap.josm.gui.mappaint.MultiCascade; 
     37import org.openstreetmap.josm.gui.mappaint.StyleCache; 
    3638import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList; 
    3739import org.openstreetmap.josm.gui.mappaint.StyleSource; 
     
    300302            txtMappaint.append("\n\n"); 
    301303        } 
     304 
     305        if (sel.size() == 2) { 
     306            List<OsmPrimitive> selList = new ArrayList<OsmPrimitive>(sel); 
     307            StyleCache sc1 = selList.get(0).mappaintStyle; 
     308            StyleCache sc2 = selList.get(1).mappaintStyle; 
     309            if (sc1 == sc2) { 
     310                txtMappaint.append("The 2 selected Objects have identical style caches."); 
     311            } 
     312            if (!sc1.equals(sc2)) { 
     313                txtMappaint.append("The 2 selected Objects have different style caches."); 
     314            } 
     315            if (sc1.equals(sc2) && sc1 != sc2) { 
     316                txtMappaint.append("Warning: The 2 selected Objects have equal, but not identical style caches."); 
     317            } 
     318        } 
    302319    } 
    303320 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

    r3859 r3862  
    33 
    44import java.awt.Color; 
     5import java.awt.Rectangle; 
     6import java.awt.TexturePaint; 
     7import java.awt.image.BufferedImage; 
     8 
     9import javax.swing.ImageIcon; 
    510 
    611import org.openstreetmap.josm.Main; 
     
    1015import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings; 
    1116import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter; 
     17import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 
    1218import org.openstreetmap.josm.tools.Utils; 
     19 
    1320 
    1421public class AreaElemStyle extends ElemStyle 
    1522{ 
    1623    public Color color; 
     24    public BufferedImage fillImage; 
    1725 
    18     protected AreaElemStyle(Cascade c, Color color) { 
     26    protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage) { 
    1927        super(c); 
    2028        this.color = color; 
     29        this.fillImage = fillImage; 
    2130    } 
    2231 
    2332    public static AreaElemStyle create(Cascade c) { 
     33        BufferedImage fillImage = null; 
     34        IconReference iconRef = c.get("fill-image", null, IconReference.class); 
     35        if (iconRef != null) { 
     36            ImageIcon icon = MapPaintStyles.getIcon(iconRef, false); 
     37            if (icon != null) { 
     38                if (!(icon.getImage() instanceof BufferedImage)) { 
     39                    icon = MapPaintStyles.getIcon(iconRef, true); 
     40                } 
     41                if (!(icon.getImage() instanceof BufferedImage)) 
     42                    throw new RuntimeException(); 
     43                fillImage = (BufferedImage) icon.getImage(); 
     44            } 
     45        } 
     46 
    2447        Color color = c.get("fill-color", null, Color.class); 
    25         if (color == null) 
     48        if (color != null) { 
     49 
     50            int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50)))); 
     51            Integer pAlpha = color_float2int(c.get("fill-opacity", null, float.class)); 
     52            if (pAlpha != null) { 
     53                alpha = pAlpha; 
     54            } 
     55            color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 
     56        } 
     57         
     58        if (fillImage != null || color != null) 
     59            return new AreaElemStyle(c, color, fillImage); 
     60        else 
    2661            return null; 
    27         int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50)))); 
    28         Integer pAlpha = color_float2int(c.get("fill-opacity", null, float.class)); 
    29         if (pAlpha != null) { 
    30             alpha = pAlpha; 
    31         } 
    32         color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 
    33         return new AreaElemStyle(c, color); 
    3462    } 
    3563 
     
    3866        if (osm instanceof Way) 
    3967        { 
    40             painter.drawArea((Way) osm, 
    41                     osm.isSelected() ? paintSettings.getSelectedColor(color.getAlpha()) : color, 
     68            Color myColor = color; 
     69            if (color != null) { 
     70                if (osm.isSelected()) { 
     71                    myColor = paintSettings.getSelectedColor(color.getAlpha()); 
     72                } 
     73            } 
     74            painter.drawArea((Way) osm, myColor, fillImage, 
    4275                    painter.isShowNames() ? painter.getAreaName(osm) : null); 
    4376        } else if (osm instanceof Relation) 
    4477        { 
    45             painter.drawArea((Relation) osm, 
    46                     selected ? paintSettings.getRelationSelectedColor(color.getAlpha()) : color, 
     78            Color myColor = color; 
     79            if (color != null) { 
     80                if (selected) { 
     81                    myColor = paintSettings.getRelationSelectedColor(color.getAlpha()); 
     82                } 
     83            } 
     84            painter.drawArea((Relation) osm, myColor, fillImage, 
    4785                    painter.getAreaName(osm)); 
    4886        } 
     
    5593        if (!super.equals(obj)) 
    5694            return false; 
    57         return Utils.equal(color, ((AreaElemStyle) obj).color); 
     95        AreaElemStyle other = (AreaElemStyle) obj; 
     96        // we should get the same image object due to caching 
     97        if (fillImage != other.fillImage && (fillImage == null || other.fillImage == null || fillImage != other.fillImage)) 
     98            return false; 
     99        if (!Utils.equal(color, other.color)) 
     100            return false; 
     101        return true; 
    58102    } 
    59103 
    60104    @Override 
    61105    public int hashCode() { 
    62         return 11 * super.hashCode() + color.hashCode(); 
     106        int hash = 3; 
     107        hash = 61 * hash + (this.color != null ? this.color.hashCode() : 0); 
     108        hash = 61 * hash + (this.fillImage != null ? this.fillImage.hashCode() : 0); 
     109        return hash; 
    63110    } 
    64111 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r3858 r3862  
    6060    } 
    6161 
    62     public static ImageIcon getIcon(IconReference ref) 
     62    public static ImageIcon getIcon(IconReference ref, boolean sanitize) 
    6363    { 
    6464        String styleName = ref.source.getPrefName(); 
     
    7878            } 
    7979        } 
    80         ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, ref.iconName, ref.source.zipIcons); 
     80        ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, ref.iconName, ref.source.zipIcons, sanitize); 
    8181        if(i == null) 
    8282        { 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3855 r3862  
    3737        if (iconRef == null) 
    3838            return null; 
    39  
    40         ImageIcon icon = MapPaintStyles.getIcon(iconRef); 
     39        ImageIcon icon = MapPaintStyles.getIcon(iconRef, false); 
     40         
    4141        String text = c.get("text", null, String.class); 
    4242 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java

    r3860 r3862  
    3636        public void execute(Environment env) { 
    3737            Object value = (val instanceof Expression) ? ((Expression) val).evaluate(env) : val; 
    38             if (key.equals("icon-image")) { 
     38            if (key.equals("icon-image") || key.equals("fill-image")) { 
    3939                if (value instanceof String) { 
    4040                    value = new IconReference((String) value, env.source); 
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r3512 r3862  
    5252 
    5353    /** 
     54     * remember whether the image has been sanitized 
     55     */ 
     56    private static class ImageWrapper { 
     57        Image img; 
     58        boolean sanitized; 
     59 
     60        public ImageWrapper(Image img, boolean sanitized) { 
     61            this.img = img; 
     62            this.sanitized = sanitized; 
     63        } 
     64    } 
     65 
     66    /** 
    5467     * The icon cache 
    5568     */ 
    56     private static Map<String, Image> cache = new HashMap<String, Image>(); 
     69    private static Map<String, ImageWrapper> cache = new HashMap<String, ImageWrapper>(); 
    5770 
    5871    /** 
     
    8497    } 
    8598 
    86     public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) { 
     99    public static ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) { 
    87100        return getIfAvailable(Arrays.asList(dirs), id, subdir, name); 
    88101    } 
     
    95108    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) { 
    96109        return getIfAvailable(dirs, id, subdir, name, null); 
     110    } 
     111 
     112    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) { 
     113        return getIfAvailable(dirs, id, subdir, name, archive, false); 
    97114    } 
    98115 
     
    106123     * @param name      The name of the image. If it contains no '.', a png extension is added. 
    107124     * @param archive   A zip file where the image is located. 
    108      */ 
    109     public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) { 
     125     * @param sanitize  If the image should be repainted to a new BufferedImage to work 
     126     *                  around certain issues. 
     127     */ 
     128    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) { 
     129        ImageWrapper iw = getIfAvailableImpl(dirs, id, subdir, name, archive); 
     130        if (iw == null) 
     131            return null; 
     132        if (sanitize && !iw.sanitized) { 
     133            iw.img = sanitize(iw.img); 
     134            iw.sanitized = true; 
     135        } 
     136        return new ImageIcon(iw.img); 
     137    } 
     138 
     139    private static ImageWrapper getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 
    110140        if (name == null) 
    111141            return null; 
    112142        if (name.startsWith("http://")) { 
    113             Image img = cache.get(name); 
    114             if (img == null) { 
     143            ImageWrapper iw = cache.get(name); 
     144            if (iw == null) { 
    115145                try { 
    116146                    MirroredInputStream is = new MirroredInputStream(name, new File(Main.pref.getPreferencesDir(), 
    117147                    "images").toString()); 
    118                     img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 
    119                     cache.put(name, img); 
     148                    Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 
     149                    iw = new ImageWrapper(img, false); 
     150                    cache.put(name, iw); 
    120151                } catch (IOException e) { 
    121152                } 
    122153            } 
    123             return img == null ? null : new ImageIcon(img); 
     154            return iw; 
    124155        } 
    125156        if (subdir == null) { 
     
    139170        } 
    140171 
    141         Image img = cache.get(cache_name); 
    142         if (img == null) { 
     172        ImageWrapper iw = cache.get(cache_name); 
     173        if (iw == null) { 
    143174            if(archive != null) 
    144175            { 
     
    162193                                size -= l; 
    163194                            } 
    164                             img = Toolkit.getDefaultToolkit().createImage(buf); 
     195                            Image img = Toolkit.getDefaultToolkit().createImage(buf); 
     196                            iw = new ImageWrapper(img, false); 
    165197                        } finally { 
    166198                            if (is != null) { 
     
    185217            // and don't bother to create a URL unless we're actually 
    186218            // creating the image. 
    187             if(img == null) 
     219            if(iw == null) 
    188220            { 
    189221                URL path = getImageUrl(full_name, dirs); 
    190222                if (path == null) 
    191223                    return null; 
    192                 img = Toolkit.getDefaultToolkit().createImage(path); 
    193             } 
    194             cache.put(cache_name, img); 
    195         } 
    196  
    197         return new ImageIcon(img); 
     224                Image img = Toolkit.getDefaultToolkit().createImage(path); 
     225                iw = new ImageWrapper(img, false); 
     226            } 
     227            cache.put(cache_name, iw); 
     228        } 
     229 
     230        return iw; 
    198231    } 
    199232 
     
    405438        return get("data", type.getAPIName()); 
    406439    } 
     440 
     441    public static BufferedImage sanitize(Image img) { 
     442        (new ImageIcon(img)).getImage(); // load competely 
     443        int width = img.getWidth(null); 
     444        int height = img.getHeight(null); 
     445        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
     446        result.getGraphics().drawImage(img, 0, 0, null); 
     447        return result; 
     448    } 
    407449} 
Note: See TracChangeset for help on using the changeset viewer.