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.util.Objects;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.Main;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.Way;
|
---|
11 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
18 | import org.openstreetmap.josm.gui.util.RotationAngle;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This is the style that defines how an area is filled.
|
---|
24 | */
|
---|
25 | public class AreaElement extends StyleElement {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * If fillImage == null, color is the fill-color, otherwise
|
---|
29 | * an arbitrary color value sampled from the fillImage
|
---|
30 | */
|
---|
31 | public Color color;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * An image to cover this area. May be null to disable this feature.
|
---|
35 | */
|
---|
36 | public MapImage fillImage;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The text that should be written on this area.
|
---|
40 | */
|
---|
41 | public TextLabel text;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Fill the area only partially from the borders
|
---|
45 | * <p>
|
---|
46 | * Public access is discouraged.
|
---|
47 | * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
|
---|
48 | */
|
---|
49 | public Float extent;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Areas smaller than this are filled no matter what value {@link #extent} has.
|
---|
53 | * <p>
|
---|
54 | * Public access is discouraged.
|
---|
55 | * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
|
---|
56 | */
|
---|
57 | public Float extentThreshold;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The icon that is displayed on the center of the area.
|
---|
61 | */
|
---|
62 | private final MapImage iconImage;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The rotation of the {@link #iconImageAngle}
|
---|
66 | */
|
---|
67 | private final RotationAngle iconImageAngle;
|
---|
68 |
|
---|
69 | protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent,
|
---|
70 | Float extentThreshold, TextLabel text, MapImage iconImage, RotationAngle iconImageAngle) {
|
---|
71 | super(c, 1f);
|
---|
72 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
73 | this.color = color;
|
---|
74 | this.fillImage = fillImage;
|
---|
75 | this.extent = extent;
|
---|
76 | this.extentThreshold = extentThreshold;
|
---|
77 | this.text = text;
|
---|
78 | this.iconImage = iconImage;
|
---|
79 | this.iconImageAngle = iconImageAngle;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Create a new {@link AreaElement}
|
---|
84 | * @param env The current style definitions
|
---|
85 | * @return The area element or <code>null</code> if the area should not be filled.
|
---|
86 | */
|
---|
87 | public static AreaElement create(final Environment env) {
|
---|
88 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
89 | MapImage fillImage = null;
|
---|
90 | Color color;
|
---|
91 |
|
---|
92 | IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
|
---|
93 | if (iconRef != null) {
|
---|
94 | fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
|
---|
95 |
|
---|
96 | color = new Color(fillImage.getImage(false).getRGB(
|
---|
97 | fillImage.getWidth() / 2, fillImage.getHeight() / 2)
|
---|
98 | );
|
---|
99 |
|
---|
100 | fillImage.alpha = Math.min(255, Math.max(0, Main.pref.getInteger("mappaint.fill-image-alpha", 255)));
|
---|
101 | Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
|
---|
102 | if (pAlpha != null) {
|
---|
103 | fillImage.alpha = pAlpha;
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 | color = c.get(FILL_COLOR, null, Color.class);
|
---|
107 | if (color != null) {
|
---|
108 | int alpha = color.getAlpha();
|
---|
109 | if (alpha == 255) {
|
---|
110 | // Assume alpha value has not been specified by the user if
|
---|
111 | // is set to fully opaque. Use default value in this case.
|
---|
112 | // It is not an ideal solution, but a little tricky to get this
|
---|
113 | // right, especially as named map colors can be changed in
|
---|
114 | // the preference GUI and written to the preferences file.
|
---|
115 | alpha = Math.min(255, Math.max(0, Main.pref.getInteger("mappaint.fillalpha", 50)));
|
---|
116 | }
|
---|
117 | Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
|
---|
118 | if (pAlpha != null) {
|
---|
119 | alpha = pAlpha;
|
---|
120 | }
|
---|
121 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (color != null) {
|
---|
126 |
|
---|
127 | TextLabel text = null;
|
---|
128 | Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
|
---|
129 | if (textPos == null || "center".equals(textPos.val)) {
|
---|
130 | text = TextLabel.create(env, PaintColors.AREA_TEXT.get(), true);
|
---|
131 | }
|
---|
132 |
|
---|
133 | Float extent = c.get(FILL_EXTENT, null, float.class);
|
---|
134 | Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
|
---|
135 |
|
---|
136 | MapImage iconImage = NodeElement.createIcon(env);
|
---|
137 | RotationAngle rotationAngle = NodeElement.createRotationAngle(env);
|
---|
138 |
|
---|
139 | return new AreaElement(c, color, fillImage, extent, extentThreshold, text, iconImage, rotationAngle);
|
---|
140 | } else {
|
---|
141 | return null;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
147 | boolean selected, boolean outermember, boolean member) {
|
---|
148 | Color myColor = color;
|
---|
149 | if (osm instanceof Way) {
|
---|
150 | if (color != null) {
|
---|
151 | if (selected) {
|
---|
152 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
153 | } else if (outermember) {
|
---|
154 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
155 | }
|
---|
156 | }
|
---|
157 | painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
|
---|
158 | } else if (osm instanceof Relation) {
|
---|
159 | if (color != null && (selected || outermember)) {
|
---|
160 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
161 | }
|
---|
162 | painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (iconImage != null && painter.isShowIcons()) {
|
---|
166 | painter.drawAreaIcon(osm, iconImage, painter.isInactiveMode() || osm.isDisabled(), selected, member,
|
---|
167 | iconImageAngle == null ? 0.0 : iconImageAngle.getRotationAngle(osm));
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public boolean equals(Object obj) {
|
---|
173 | if (this == obj) return true;
|
---|
174 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
175 | if (!super.equals(obj)) return false;
|
---|
176 | AreaElement that = (AreaElement) obj;
|
---|
177 | return Objects.equals(color, that.color) &&
|
---|
178 | Objects.equals(fillImage, that.fillImage) &&
|
---|
179 | Objects.equals(text, that.text) &&
|
---|
180 | Objects.equals(extent, that.extent) &&
|
---|
181 | Objects.equals(extentThreshold, that.extentThreshold) &&
|
---|
182 | Objects.equals(iconImage, that.iconImage) &&
|
---|
183 | Objects.equals(iconImageAngle, that.iconImageAngle);
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public int hashCode() {
|
---|
188 | return Objects.hash(super.hashCode(), color, fillImage, text, extent, extentThreshold, iconImage, iconImageAngle);
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | public String toString() {
|
---|
193 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
194 | " fillImage=[" + fillImage + "] iconImage=[" + iconImage + "] iconImageAngle=[" + iconImageAngle + "]}";
|
---|
195 | }
|
---|
196 | }
|
---|