source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/AreaElement.java@ 11720

Last change on this file since 11720 was 11720, checked in by stoecker, 7 years ago

typo

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import java.awt.Color;
5import java.util.Objects;
6
7import org.openstreetmap.josm.Main;
8import org.openstreetmap.josm.data.osm.OsmPrimitive;
9import org.openstreetmap.josm.data.osm.Relation;
10import org.openstreetmap.josm.data.osm.Way;
11import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
12import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
13import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
14import org.openstreetmap.josm.data.preferences.IntegerProperty;
15import org.openstreetmap.josm.gui.mappaint.Cascade;
16import org.openstreetmap.josm.gui.mappaint.Environment;
17import org.openstreetmap.josm.gui.mappaint.Keyword;
18import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
19import org.openstreetmap.josm.gui.util.RotationAngle;
20import org.openstreetmap.josm.tools.CheckParameterUtil;
21import org.openstreetmap.josm.tools.Utils;
22
23/**
24 * This is the style that defines how an area is filled.
25 */
26public 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 * The text that should be written on this area.
48 */
49 public TextLabel text;
50
51 /**
52 * Fill the area only partially from the borders
53 * <p>
54 * Public access is discouraged.
55 * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
56 */
57 public Float extent;
58
59 /**
60 * Areas smaller than this are filled no matter what value {@link #extent} has.
61 * <p>
62 * Public access is discouraged.
63 * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
64 */
65 public Float extentThreshold;
66
67 /**
68 * The icon that is displayed on the center of the area.
69 */
70 private final MapImage iconImage;
71
72 /**
73 * The rotation of the {@link #iconImageAngle}
74 */
75 private final RotationAngle iconImageAngle;
76
77 protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent,
78 Float extentThreshold, TextLabel text, MapImage iconImage, RotationAngle iconImageAngle) {
79 super(c, 1f);
80 CheckParameterUtil.ensureParameterNotNull(color);
81 this.color = color;
82 this.fillImage = fillImage;
83 this.extent = extent;
84 this.extentThreshold = extentThreshold;
85 this.text = text;
86 this.iconImage = iconImage;
87 this.iconImageAngle = iconImageAngle;
88 }
89
90 /**
91 * Create a new {@link AreaElement}
92 * @param env The current style definitions
93 * @return The area element or <code>null</code> if the area should not be filled.
94 */
95 public static AreaElement create(final Environment env) {
96 final Cascade c = env.mc.getCascade(env.layer);
97 MapImage fillImage = null;
98 Color color;
99
100 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
101 if (iconRef != null) {
102 fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
103
104 color = new Color(fillImage.getImage(false).getRGB(
105 fillImage.getWidth() / 2, fillImage.getHeight() / 2)
106 );
107
108 fillImage.alpha = Utils.clamp(Main.pref.getInteger("mappaint.fill-image-alpha", 255), 0, 255);
109 Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
110 if (pAlpha != null) {
111 fillImage.alpha = pAlpha;
112 }
113 } else {
114 color = c.get(FILL_COLOR, null, Color.class);
115 if (color != null) {
116 float defaultOpacity = Utils.colorInt2float(DEFAULT_FILL_ALPHA.get());
117 float opacity = c.get(FILL_OPACITY, defaultOpacity, Float.class);
118 color = Utils.alphaMultiply(color, opacity);
119 }
120 }
121
122 TextLabel text = null;
123 Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
124 if (textPos == null || "center".equals(textPos.val)) {
125 text = TextLabel.create(env, PaintColors.AREA_TEXT.get(), true);
126 }
127 MapImage iconImage = NodeElement.createIcon(env);
128 RotationAngle rotationAngle = NodeElement.createRotationAngle(env);
129
130 if (iconImage != null || text != null) {
131 // fake a transparent color.
132 color = new Color(0, 0, 0, 0);
133 }
134
135 if (color != null) {
136 Float extent = c.get(FILL_EXTENT, null, float.class);
137 Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
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}
Note: See TracBrowser for help on using the repository browser.