Ignore:
Timestamp:
2015-10-18T23:57:31+02:00 (9 years ago)
Author:
Don-vip
Message:

fix #11968 - Show node icons from style in selection list and relation editor (patch by Augustus Kling)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r8846 r8903  
    1212import java.awt.Image;
    1313import java.awt.Point;
     14import java.awt.Rectangle;
    1415import java.awt.RenderingHints;
    1516import java.awt.Toolkit;
     
    5657
    5758import org.openstreetmap.josm.Main;
     59import org.openstreetmap.josm.data.osm.OsmPrimitive;
    5860import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     61import org.openstreetmap.josm.gui.mappaint.ElemStyle;
     62import org.openstreetmap.josm.gui.mappaint.MapImage;
     63import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
     64import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
     65import org.openstreetmap.josm.gui.mappaint.Range;
     66import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
    5967import org.openstreetmap.josm.io.CachedFile;
    6068import org.openstreetmap.josm.plugins.PluginHandler;
     
    12661274        CheckParameterUtil.ensureParameterNotNull(type, "type");
    12671275        return get("data", type.getAPIName());
     1276    }
     1277
     1278    /**
     1279     * @param primitive Object for which an icon shall be fetched. The icon is chosen based on tags.
     1280     * @param iconSize Target size of icon. Icon is padded if required.
     1281     * @return Icon for {@code primitive} that fits in cell.
     1282     * @since 8903
     1283     */
     1284    public static ImageIcon getPadded(OsmPrimitive primitive, Rectangle iconSize) {
     1285        // Check if the current styles have special icon for tagged nodes.
     1286        if (primitive instanceof org.openstreetmap.josm.data.osm.Node) {
     1287            Pair<StyleList, Range> nodeStyles = MapPaintStyles.getStyles().generateStyles(primitive, 100, false);
     1288            for (ElemStyle style : nodeStyles.a) {
     1289                if (style instanceof NodeElemStyle) {
     1290                    NodeElemStyle nodeStyle = (NodeElemStyle) style;
     1291                    MapImage icon = nodeStyle.mapImage;
     1292                    if (icon != null) {
     1293                        int backgroundWidth = iconSize.height;
     1294                        int backgroundHeight = iconSize.height;
     1295                        int iconWidth = icon.getWidth();
     1296                        int iconHeight = icon.getHeight();
     1297                        BufferedImage image = new BufferedImage(backgroundWidth, backgroundHeight,
     1298                                BufferedImage.TYPE_INT_ARGB);
     1299                        double scaleFactor = Math.min(backgroundWidth / (double) iconWidth, backgroundHeight
     1300                                / (double) iconHeight);
     1301                        BufferedImage iconImage = icon.getImage(false);
     1302                        Image scaledIcon;
     1303                        final int scaledWidth;
     1304                        final int scaledHeight;
     1305                        if (scaleFactor < 1) {
     1306                            // Scale icon such that it fits on background.
     1307                            scaledWidth = (int) (iconWidth * scaleFactor);
     1308                            scaledHeight = (int) (iconHeight * scaleFactor);
     1309                            scaledIcon = iconImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
     1310                        } else {
     1311                            // Use original size, don't upscale.
     1312                            scaledWidth = iconWidth;
     1313                            scaledHeight = iconHeight;
     1314                            scaledIcon = iconImage;
     1315                        }
     1316                        image.getGraphics().drawImage(scaledIcon, (backgroundWidth - scaledWidth) / 2,
     1317                                (backgroundHeight - scaledHeight) / 2, null);
     1318
     1319                        return new ImageIcon(image);
     1320                    }
     1321                }
     1322            }
     1323        }
     1324
     1325        // Use generic default icon.
     1326        return ImageProvider.get(primitive.getDisplayType());
    12681327    }
    12691328
Note: See TracChangeset for help on using the changeset viewer.