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