[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[9278] | 2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
[3824] | 3 |
|
---|
[626] | 4 | import java.awt.Color;
|
---|
[12722] | 5 | import java.awt.Image;
|
---|
| 6 | import java.awt.image.BufferedImage;
|
---|
[7083] | 7 | import java.util.Objects;
|
---|
[626] | 8 |
|
---|
[3858] | 9 | import org.openstreetmap.josm.Main;
|
---|
[2675] | 10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[3836] | 11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
[3822] | 12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[2675] | 13 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
[5571] | 14 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
[11700] | 15 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
[9278] | 16 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
| 17 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
[3862] | 18 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
[3879] | 19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[12722] | 20 | import org.openstreetmap.josm.tools.HiDPISupport;
|
---|
[3824] | 21 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2675] | 22 |
|
---|
[11285] | 23 | /**
|
---|
| 24 | * This is the style that defines how an area is filled.
|
---|
| 25 | */
|
---|
[9278] | 26 | public class AreaElement extends StyleElement {
|
---|
[6883] | 27 |
|
---|
[3879] | 28 | /**
|
---|
[11700] | 29 | * The default opacity for the fill. For historical reasons in range 0.255.
|
---|
| 30 | */
|
---|
| 31 | private static final IntegerProperty DEFAULT_FILL_ALPHA = new IntegerProperty("mappaint.fillalpha", 50);
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[3879] | 34 | * If fillImage == null, color is the fill-color, otherwise
|
---|
[11718] | 35 | * an arbitrary color value sampled from the fillImage.
|
---|
| 36 | *
|
---|
[11720] | 37 | * The color may be fully transparent to indicate that the area should not be filled.
|
---|
[3879] | 38 | */
|
---|
[1169] | 39 | public Color color;
|
---|
[11285] | 40 |
|
---|
| 41 | /**
|
---|
| 42 | * An image to cover this area. May be null to disable this feature.
|
---|
| 43 | */
|
---|
[5054] | 44 | public MapImage fillImage;
|
---|
[11285] | 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Fill the area only partially from the borders
|
---|
| 48 | * <p>
|
---|
| 49 | * Public access is discouraged.
|
---|
| 50 | * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
|
---|
| 51 | */
|
---|
[9005] | 52 | public Float extent;
|
---|
[11285] | 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Areas smaller than this are filled no matter what value {@link #extent} has.
|
---|
| 56 | * <p>
|
---|
| 57 | * Public access is discouraged.
|
---|
| 58 | * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
|
---|
| 59 | */
|
---|
[9099] | 60 | public Float extentThreshold;
|
---|
[626] | 61 |
|
---|
[11730] | 62 | protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold) {
|
---|
[5217] | 63 | super(c, 1f);
|
---|
[3879] | 64 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
[3824] | 65 | this.color = color;
|
---|
[9099] | 66 | this.fillImage = fillImage;
|
---|
[9005] | 67 | this.extent = extent;
|
---|
[9099] | 68 | this.extentThreshold = extentThreshold;
|
---|
[1169] | 69 | }
|
---|
[626] | 70 |
|
---|
[11285] | 71 | /**
|
---|
| 72 | * Create a new {@link AreaElement}
|
---|
| 73 | * @param env The current style definitions
|
---|
| 74 | * @return The area element or <code>null</code> if the area should not be filled.
|
---|
| 75 | */
|
---|
[9278] | 76 | public static AreaElement create(final Environment env) {
|
---|
[7383] | 77 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
[5054] | 78 | MapImage fillImage = null;
|
---|
[9099] | 79 | Color color;
|
---|
[3879] | 80 |
|
---|
[5342] | 81 | IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
|
---|
[3862] | 82 | if (iconRef != null) {
|
---|
[8289] | 83 | fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
|
---|
[12722] | 84 | Image img = fillImage.getImage(false);
|
---|
| 85 | // get base image from possible multi-resolution image, so we can
|
---|
| 86 | // cast to BufferedImage and get pixel value at the center of the image
|
---|
| 87 | img = HiDPISupport.getBaseImage(img);
|
---|
| 88 | color = new Color(((BufferedImage) img).getRGB(
|
---|
[5054] | 89 | fillImage.getWidth() / 2, fillImage.getHeight() / 2)
|
---|
| 90 | );
|
---|
[3879] | 91 |
|
---|
[12841] | 92 | fillImage.alpha = Utils.clamp(Main.pref.getInt("mappaint.fill-image-alpha", 255), 0, 255);
|
---|
[10748] | 93 | Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
|
---|
[5054] | 94 | if (pAlpha != null) {
|
---|
| 95 | fillImage.alpha = pAlpha;
|
---|
[3862] | 96 | }
|
---|
[3879] | 97 | } else {
|
---|
[5342] | 98 | color = c.get(FILL_COLOR, null, Color.class);
|
---|
[3879] | 99 | if (color != null) {
|
---|
[11700] | 100 | float defaultOpacity = Utils.colorInt2float(DEFAULT_FILL_ALPHA.get());
|
---|
| 101 | float opacity = c.get(FILL_OPACITY, defaultOpacity, Float.class);
|
---|
| 102 | color = Utils.alphaMultiply(color, opacity);
|
---|
[3862] | 103 | }
|
---|
| 104 | }
|
---|
[3880] | 105 |
|
---|
[11718] | 106 | if (color != null) {
|
---|
[11670] | 107 | Float extent = c.get(FILL_EXTENT, null, float.class);
|
---|
| 108 | Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
|
---|
| 109 |
|
---|
[11730] | 110 | return new AreaElement(c, color, fillImage, extent, extentThreshold);
|
---|
[11670] | 111 | } else {
|
---|
[3836] | 112 | return null;
|
---|
[11670] | 113 | }
|
---|
[3836] | 114 | }
|
---|
| 115 |
|
---|
[2675] | 116 | @Override
|
---|
[7621] | 117 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
| 118 | boolean selected, boolean outermember, boolean member) {
|
---|
[7555] | 119 | Color myColor = color;
|
---|
[6883] | 120 | if (osm instanceof Way) {
|
---|
[7555] | 121 | if (color != null) {
|
---|
| 122 | if (selected) {
|
---|
| 123 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
[7621] | 124 | } else if (outermember) {
|
---|
[7555] | 125 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
| 126 | }
|
---|
[3862] | 127 | }
|
---|
[12285] | 128 | painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
|
---|
[6883] | 129 | } else if (osm instanceof Relation) {
|
---|
[7621] | 130 | if (color != null && (selected || outermember)) {
|
---|
[6883] | 131 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
[3862] | 132 | }
|
---|
[12285] | 133 | painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
|
---|
[3822] | 134 | }
|
---|
[2675] | 135 | }
|
---|
[3824] | 136 |
|
---|
| 137 | @Override
|
---|
| 138 | public boolean equals(Object obj) {
|
---|
[9371] | 139 | if (this == obj) return true;
|
---|
| 140 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 141 | if (!super.equals(obj)) return false;
|
---|
| 142 | AreaElement that = (AreaElement) obj;
|
---|
| 143 | return Objects.equals(color, that.color) &&
|
---|
| 144 | Objects.equals(fillImage, that.fillImage) &&
|
---|
| 145 | Objects.equals(extent, that.extent) &&
|
---|
[11730] | 146 | Objects.equals(extentThreshold, that.extentThreshold);
|
---|
[3824] | 147 | }
|
---|
| 148 |
|
---|
| 149 | @Override
|
---|
| 150 | public int hashCode() {
|
---|
[11730] | 151 | return Objects.hash(super.hashCode(), color, fillImage, extent, extentThreshold);
|
---|
[3824] | 152 | }
|
---|
| 153 |
|
---|
| 154 | @Override
|
---|
| 155 | public String toString() {
|
---|
[3879] | 156 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
[11730] | 157 | " fillImage=[" + fillImage + "] extent=[" + extent + "] extentThreshold=[" + extentThreshold + "]}";
|
---|
[3824] | 158 | }
|
---|
[626] | 159 | }
|
---|