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

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

Don't use null to indicate a rotation of 0 degrees, simply set the rotation to 0.

  • Property svn:eol-style set to native
File size: 6.1 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.tools.CheckParameterUtil;
18import org.openstreetmap.josm.tools.Utils;
19
20/**
21 * This is the style that defines how an area is filled.
22 */
23public class AreaElement extends StyleElement {
24
25 /**
26 * The default opacity for the fill. For historical reasons in range 0.255.
27 */
28 private static final IntegerProperty DEFAULT_FILL_ALPHA = new IntegerProperty("mappaint.fillalpha", 50);
29
30 /**
31 * If fillImage == null, color is the fill-color, otherwise
32 * an arbitrary color value sampled from the fillImage.
33 *
34 * The color may be fully transparent to indicate that the area should not be filled.
35 */
36 public Color color;
37
38 /**
39 * An image to cover this area. May be null to disable this feature.
40 */
41 public MapImage fillImage;
42
43 /**
44 * The text that should be written on this area.
45 */
46 public TextLabel text;
47
48 /**
49 * Fill the area only partially from the borders
50 * <p>
51 * Public access is discouraged.
52 * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
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(Way, Color, MapImage, Float, Float, boolean, TextLabel)
61 */
62 public Float extentThreshold;
63
64 protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold, TextLabel text) {
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 this.text = text;
72 }
73
74 /**
75 * Create a new {@link AreaElement}
76 * @param env The current style definitions
77 * @return The area element or <code>null</code> if the area should not be filled.
78 */
79 public static AreaElement create(final Environment env) {
80 final Cascade c = env.mc.getCascade(env.layer);
81 MapImage fillImage = null;
82 Color color;
83
84 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
85 if (iconRef != null) {
86 fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
87
88 color = new Color(fillImage.getImage(false).getRGB(
89 fillImage.getWidth() / 2, fillImage.getHeight() / 2)
90 );
91
92 fillImage.alpha = Utils.clamp(Main.pref.getInteger("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 TextLabel text = null; // <- text is handled by TextElement
107
108 if (color != null) {
109 Float extent = c.get(FILL_EXTENT, null, float.class);
110 Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
111
112 return new AreaElement(c, color, fillImage, extent, extentThreshold, text);
113 } else {
114 return null;
115 }
116 }
117
118 @Override
119 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
120 boolean selected, boolean outermember, boolean member) {
121 Color myColor = color;
122 if (osm instanceof Way) {
123 if (color != null) {
124 if (selected) {
125 myColor = paintSettings.getSelectedColor(color.getAlpha());
126 } else if (outermember) {
127 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
128 }
129 }
130 painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
131 } else if (osm instanceof Relation) {
132 if (color != null && (selected || outermember)) {
133 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
134 }
135 painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
136 }
137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (this == obj) return true;
142 if (obj == null || getClass() != obj.getClass()) return false;
143 if (!super.equals(obj)) return false;
144 AreaElement that = (AreaElement) obj;
145 return Objects.equals(color, that.color) &&
146 Objects.equals(fillImage, that.fillImage) &&
147 Objects.equals(text, that.text) &&
148 Objects.equals(extent, that.extent) &&
149 Objects.equals(extentThreshold, that.extentThreshold);
150 }
151
152 @Override
153 public int hashCode() {
154 return Objects.hash(super.hashCode(), color, fillImage, text, extent, extentThreshold);
155 }
156
157 @Override
158 public String toString() {
159 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
160 " fillImage=[" + fillImage + "]}";
161 }
162}
Note: See TracBrowser for help on using the repository browser.