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

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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.