Ignore:
Timestamp:
2017-03-13T20:11:23+01:00 (7 years ago)
Author:
michael2402
Message:

Add a new style element for area icon styles. Use the same placement algorithm we use for texts. Fixes #10176

File:
1 edited

Legend:

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

    r11722 r11730  
    3939import java.util.concurrent.RecursiveTask;
    4040import java.util.function.BiConsumer;
     41import java.util.function.Consumer;
    4142import java.util.function.Supplier;
    4243import java.util.stream.Collectors;
     
    740741
    741742    /**
    742      * Draw the icon for a given area. The icon is drawn around the lat/lon center of the area.
    743      * @param primitive The node
    744      * @param img The icon to draw at the node position
     743     * Draw the icon for a given area. Normally, the icon is drawn around the center of the area.
     744     * @param osm The primitive to draw the icon for
     745     * @param img The icon to draw
    745746     * @param disabled {@code} true to render disabled version, {@code false} for the standard version
    746747     * @param selected {@code} true to render it as selected, {@code false} otherwise
    747748     * @param member {@code} true to render it as a relation member, {@code false} otherwise
    748749     * @param theta the angle of rotation in radians
     750     * @param iconPosition Where to place the icon.
    749751     * @since 11670
    750752     */
    751     public void drawAreaIcon(OsmPrimitive primitive, MapImage img, boolean disabled, boolean selected, boolean member, double theta) {
    752         BBox bbox = null;
    753         if (primitive instanceof Way) {
    754             bbox = primitive.getBBox();
    755         } else if (primitive instanceof Relation) {
    756             Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, (Relation) primitive);
    757             if (multipolygon != null) {
    758                 BBox collect = new BBox();
    759                 multipolygon.getOuterPolygons().forEach(p -> p.getNodes().forEach(n -> collect.add(n.getCoor())));
    760                 bbox = collect;
    761             }
    762         }
    763 
    764         if (bbox != null && bbox.isValid()) {
    765             MapViewPoint p = mapState.getPointFor(bbox.getCenter());
     753    public void drawAreaIcon(OsmPrimitive osm, MapImage img, boolean disabled, boolean selected, boolean member, double theta, PositionForAreaStrategy iconPosition) {
     754        Rectangle2D.Double iconRect = new Rectangle2D.Double(-img.getWidth() / 2.0, -img.getHeight() / 2.0, img.getWidth(), img.getHeight());
     755
     756        forEachPolygon(osm, path -> {
     757            Shape area = path.createTransformedShape(mapState.getAffineTransform());
     758            Rectangle2D placement = iconPosition.findLabelPlacement(area, iconRect);
     759            if (placement == null) {
     760                return;
     761            }
     762            MapViewPoint p = mapState.getForView(placement.getCenterX(), placement.getCenterY());
    766763            drawIcon(p, img, disabled, selected, member, theta, (g, r) -> {
    767764                if (useStrokes) {
     
    774771                g.draw(r);
    775772            });
    776         }
     773        });
    777774    }
    778775
     
    11221119            }
    11231120        } else {
    1124             if (osm instanceof Way) {
    1125                 drawAreaText(osm, text, getPath((Way) osm));
    1126             } else if (osm instanceof Relation) {
    1127                 Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, (Relation) osm);
    1128                 if (!multipolygon.getOuterWays().isEmpty()) {
    1129                     for (PolyData pd : multipolygon.getCombinedPolygons()) {
    1130                         drawAreaText(osm, text, pd.get());
    1131                     }
     1121            forEachPolygon(osm, path -> drawAreaText(osm, text, path));
     1122        }
     1123    }
     1124
     1125    /**
     1126     * Calls a consumer for each path of the area shape-
     1127     * @param osm A way or a multipolygon
     1128     * @param consumer The consumer to call.
     1129     */
     1130    private void forEachPolygon(OsmPrimitive osm, Consumer<Path2D.Double> consumer) {
     1131        if (osm instanceof Way) {
     1132            consumer.accept(getPath((Way) osm));
     1133        } else if (osm instanceof Relation) {
     1134            Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, (Relation) osm);
     1135            if (!multipolygon.getOuterWays().isEmpty()) {
     1136                for (PolyData pd : multipolygon.getCombinedPolygons()) {
     1137                    consumer.accept(pd.get());
    11321138                }
    11331139            }
Note: See TracChangeset for help on using the changeset viewer.