Changeset 11670 in josm for trunk/src


Ignore:
Timestamp:
2017-03-04T22:29:24+01:00 (7 years ago)
Author:
michael2402
Message:

See #10176: Make icon-image work for areas. Icon is displayed at center of lat/lon bounds.

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

Legend:

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

    r11553 r11670  
    3939import java.util.concurrent.ForkJoinTask;
    4040import java.util.concurrent.RecursiveTask;
     41import java.util.function.BiConsumer;
    4142import java.util.function.Supplier;
    4243import java.util.stream.Collectors;
     
    932933        }
    933934
     935        drawIcon(p, img, disabled, selected, member, theta, (g, r) -> {
     936            Color color = getSelectionHintColor(disabled, selected);
     937            g.setColor(color);
     938            g.draw(r);
     939        });
     940    }
     941
     942
     943    /**
     944     * Draw the icon for a given area. The icon is drawn around the lat/lon center of the area.
     945     * @param primitive The node
     946     * @param img The icon to draw at the node position
     947     * @param disabled {@code} true to render disabled version, {@code false} for the standard version
     948     * @param selected {@code} true to render it as selected, {@code false} otherwise
     949     * @param member {@code} true to render it as a relation member, {@code false} otherwise
     950     * @param theta the angle of rotation in radians
     951     */
     952    public void drawAreaIcon(OsmPrimitive primitive, MapImage img, boolean disabled, boolean selected, boolean member, double theta) {
     953        BBox bbox = null;
     954        if (primitive instanceof Way) {
     955            bbox = primitive.getBBox();
     956        } else if (primitive instanceof Relation) {
     957            Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, (Relation) primitive);
     958            if (multipolygon != null) {
     959                BBox collect = new BBox();
     960                multipolygon.getOuterPolygons().forEach(p -> p.getNodes().forEach(n -> collect.add(n.getCoor())));
     961                bbox = collect;
     962            }
     963        }
     964
     965        if (bbox != null && bbox.isValid()) {
     966            MapViewPoint p = mapState.getPointFor(bbox.getCenter());
     967            drawIcon(p, img, disabled, selected, member, theta, (g, r) -> {
     968                // only draw a minor highlighting, so that users do not confuse this for a point.
     969                Color color = getSelectionHintColor(disabled, selected);
     970                g.setColor(color);
     971                g.draw(r);
     972            });
     973        }
     974    }
     975
     976    private void drawIcon(MapViewPoint p, MapImage img, boolean disabled, boolean selected, boolean member, double theta,
     977            BiConsumer<Graphics2D, Rectangle2D> selectionDrawer) {
    934978        float alpha = img.getAlphaFloat();
    935979
     
    943987        temporaryGraphics.translate(x, y);
    944988        temporaryGraphics.rotate(theta);
    945         int drawX = -w/2 + img.offsetX;
    946         int drawY = -h/2 + img.offsetY;
     989        int drawX = -img.getWidth() / 2 + img.offsetX;
     990        int drawY = -img.getHeight() / 2 + img.offsetY;
    947991        temporaryGraphics.drawImage(img.getImage(disabled), drawX, drawY, nc);
    948992        if (selected || member) {
    949             Color color;
    950             if (disabled) {
    951                 color = inactiveColor;
    952             } else if (selected) {
    953                 color = selectedColor;
    954             } else {
    955                 color = relationSelectedColor;
    956             }
    957             temporaryGraphics.setColor(color);
    958             temporaryGraphics.draw(new Rectangle2D.Double(drawX - 2, drawY - 2, w + 4, h + 4));
    959         }
     993            selectionDrawer.accept(temporaryGraphics, new Rectangle2D.Double(drawX - 2, drawY - 2, img.getWidth() + 4, img.getHeight() + 4));
     994        }
     995    }
     996
     997    private Color getSelectionHintColor(boolean disabled, boolean selected) {
     998        Color color;
     999        if (disabled) {
     1000            color = inactiveColor;
     1001        } else if (selected) {
     1002            color = selectedColor;
     1003        } else {
     1004            color = relationSelectedColor;
     1005        }
     1006        return color;
    9601007    }
    9611008
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/AreaElement.java

    r11285 r11670  
    1616import org.openstreetmap.josm.gui.mappaint.Keyword;
    1717import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
     18import org.openstreetmap.josm.gui.util.RotationAngle;
    1819import org.openstreetmap.josm.tools.CheckParameterUtil;
    1920import org.openstreetmap.josm.tools.Utils;
     
    5657    public Float extentThreshold;
    5758
    58     protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold, TextLabel text) {
     59    /**
     60     * The icon that is displayed on the center of the area.
     61     */
     62    private final MapImage iconImage;
     63
     64    private final RotationAngle iconImageAngle;
     65
     66    protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold, TextLabel text, MapImage iconImage, RotationAngle iconImageAngle) {
    5967        super(c, 1f);
    6068        CheckParameterUtil.ensureParameterNotNull(color);
     
    6472        this.extentThreshold = extentThreshold;
    6573        this.text = text;
     74        this.iconImage = iconImage;
     75        this.iconImageAngle = iconImageAngle;
    6676    }
    6777
     
    109119        }
    110120
    111         TextLabel text = null;
    112         Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
    113         if (textPos == null || "center".equals(textPos.val)) {
    114             text = TextLabel.create(env, PaintColors.AREA_TEXT.get(), true);
     121        if (color != null) {
     122
     123            TextLabel text = null;
     124            Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
     125            if (textPos == null || "center".equals(textPos.val)) {
     126                text = TextLabel.create(env, PaintColors.AREA_TEXT.get(), true);
     127            }
     128
     129            Float extent = c.get(FILL_EXTENT, null, float.class);
     130            Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
     131
     132            MapImage iconImage = NodeElement.createIcon(env);
     133            RotationAngle rotationAngle = NodeElement.createRotationAngle(env);
     134
     135            return new AreaElement(c, color, fillImage, extent, extentThreshold, text, iconImage, rotationAngle);
     136        } else {
     137            return null;
    115138        }
    116 
    117         Float extent = c.get(FILL_EXTENT, null, float.class);
    118         Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
    119 
    120         if (color != null)
    121             return new AreaElement(c, color, fillImage, extent, extentThreshold, text);
    122         else
    123             return null;
    124139    }
    125140
     
    142157            }
    143158            painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
     159        }
     160
     161        if (iconImage != null && painter.isShowIcons()) {
     162            painter.drawAreaIcon(osm, iconImage, painter.isInactiveMode() || osm.isDisabled(), selected, member,
     163                    iconImageAngle == null ? 0.0 : iconImageAngle.getRotationAngle(osm));
    144164        }
    145165    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java

    r11553 r11670  
    7070        Cascade c = env.mc.getCascade(env.layer);
    7171
    72         MapImage mapImage = createIcon(env, ICON_KEYS);
     72        MapImage mapImage = createIcon(env);
    7373        Symbol symbol = null;
    7474        if (mapImage == null) {
    7575            symbol = createSymbol(env);
    7676        }
     77
     78        RotationAngle rotationAngle = createRotationAngle(env);
     79
     80        // optimization: if we neither have a symbol, nor a mapImage
     81        // we don't have to check for the remaining style properties and we don't
     82        // have to allocate a node element style.
     83        if (!allowDefault && symbol == null && mapImage == null) return null;
     84
     85        return new NodeElement(c, mapImage, symbol, defaultMajorZindex, rotationAngle);
     86    }
     87
     88    /**
     89     * Reads the icon-rotation property and creates a rotation angle from it.
     90     * @param env The environment
     91     * @return The angle
     92     */
     93    public static RotationAngle createRotationAngle(Environment env) {
     94        Cascade c = env.mc.getCascade(env.layer);
     95
    7796        RotationAngle rotationAngle = null;
    7897        final Float angle = c.get(ICON_ROTATION, null, Float.class, true);
     
    93112            }
    94113        }
    95 
    96         // optimization: if we neither have a symbol, nor a mapImage
    97         // we don't have to check for the remaining style properties and we don't
    98         // have to allocate a node element style.
    99         if (!allowDefault && symbol == null && mapImage == null) return null;
    100 
    101         return new NodeElement(c, mapImage, symbol, defaultMajorZindex, rotationAngle);
     114        return rotationAngle;
     115    }
     116
     117    public static MapImage createIcon(final Environment env) {
     118        return createIcon(env, ICON_KEYS);
    102119    }
    103120
Note: See TracChangeset for help on using the changeset viewer.