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.Main;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
14 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
15 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.HiDPISupport;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This is the style that defines how an area is filled.
|
---|
25 | */
|
---|
26 | public class AreaElement extends StyleElement {
|
---|
27 |
|
---|
28 | /**
|
---|
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 | /**
|
---|
34 | * If fillImage == null, color is the fill-color, otherwise
|
---|
35 | * an arbitrary color value sampled from the fillImage.
|
---|
36 | *
|
---|
37 | * The color may be fully transparent to indicate that the area should not be filled.
|
---|
38 | */
|
---|
39 | public Color color;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * An image to cover this area. May be null to disable this feature.
|
---|
43 | */
|
---|
44 | public MapImage fillImage;
|
---|
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 | */
|
---|
52 | public Float extent;
|
---|
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 | */
|
---|
60 | public Float extentThreshold;
|
---|
61 |
|
---|
62 | protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold) {
|
---|
63 | super(c, 1f);
|
---|
64 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
65 | this.color = color;
|
---|
66 | this.fillImage = fillImage;
|
---|
67 | this.extent = extent;
|
---|
68 | this.extentThreshold = extentThreshold;
|
---|
69 | }
|
---|
70 |
|
---|
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 | */
|
---|
76 | public static AreaElement create(final Environment env) {
|
---|
77 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
78 | MapImage fillImage = null;
|
---|
79 | Color color;
|
---|
80 |
|
---|
81 | IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
|
---|
82 | if (iconRef != null) {
|
---|
83 | fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
|
---|
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(
|
---|
89 | fillImage.getWidth() / 2, fillImage.getHeight() / 2)
|
---|
90 | );
|
---|
91 |
|
---|
92 | fillImage.alpha = Utils.clamp(Main.pref.getInt("mappaint.fill-image-alpha", 255), 0, 255);
|
---|
93 | Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
|
---|
94 | if (pAlpha != null) {
|
---|
95 | fillImage.alpha = pAlpha;
|
---|
96 | }
|
---|
97 | } else {
|
---|
98 | color = c.get(FILL_COLOR, null, Color.class);
|
---|
99 | if (color != null) {
|
---|
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);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (color != null) {
|
---|
107 | Float extent = c.get(FILL_EXTENT, null, float.class);
|
---|
108 | Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
|
---|
109 |
|
---|
110 | return new AreaElement(c, color, fillImage, extent, extentThreshold);
|
---|
111 | } else {
|
---|
112 | return null;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
118 | boolean selected, boolean outermember, boolean member) {
|
---|
119 | Color myColor = color;
|
---|
120 | if (osm instanceof Way) {
|
---|
121 | if (color != null) {
|
---|
122 | if (selected) {
|
---|
123 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
124 | } else if (outermember) {
|
---|
125 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
126 | }
|
---|
127 | }
|
---|
128 | painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
|
---|
129 | } else if (osm instanceof Relation) {
|
---|
130 | if (color != null && (selected || outermember)) {
|
---|
131 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
132 | }
|
---|
133 | painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public boolean equals(Object obj) {
|
---|
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) &&
|
---|
146 | Objects.equals(extentThreshold, that.extentThreshold);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public int hashCode() {
|
---|
151 | return Objects.hash(super.hashCode(), color, fillImage, extent, extentThreshold);
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public String toString() {
|
---|
156 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
157 | " fillImage=[" + fillImage + "] extent=[" + extent + "] extentThreshold=[" + extentThreshold + "]}";
|
---|
158 | }
|
---|
159 | }
|
---|