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

Last change on this file since 11722 was 11722, checked in by michael2402, 7 years ago

Do not include text label in area style - use a separate class and start merging code for on-line and in-area drawing.

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