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

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

Add a new style element for area icon styles. Use the same placement algorithm we use for texts. Fixes #10176

  • 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 * @deprecated Use {@link TextElement} instead.
46 */
47 @Deprecated
48 public TextLabel text = null;
49
50 /**
51 * Fill the area only partially from the borders
52 * <p>
53 * Public access is discouraged.
54 * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
55 */
56 public Float extent;
57
58 /**
59 * Areas smaller than this are filled no matter what value {@link #extent} has.
60 * <p>
61 * Public access is discouraged.
62 * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
63 */
64 public Float extentThreshold;
65
66 protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold) {
67 super(c, 1f);
68 CheckParameterUtil.ensureParameterNotNull(color);
69 this.color = color;
70 this.fillImage = fillImage;
71 this.extent = extent;
72 this.extentThreshold = extentThreshold;
73 }
74
75 /**
76 * Create a new {@link AreaElement}
77 * @param env The current style definitions
78 * @return The area element or <code>null</code> if the area should not be filled.
79 */
80 public static AreaElement create(final Environment env) {
81 final Cascade c = env.mc.getCascade(env.layer);
82 MapImage fillImage = null;
83 Color color;
84
85 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
86 if (iconRef != null) {
87 fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
88
89 color = new Color(fillImage.getImage(false).getRGB(
90 fillImage.getWidth() / 2, fillImage.getHeight() / 2)
91 );
92
93 fillImage.alpha = Utils.clamp(Main.pref.getInteger("mappaint.fill-image-alpha", 255), 0, 255);
94 Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
95 if (pAlpha != null) {
96 fillImage.alpha = pAlpha;
97 }
98 } else {
99 color = c.get(FILL_COLOR, null, Color.class);
100 if (color != null) {
101 float defaultOpacity = Utils.colorInt2float(DEFAULT_FILL_ALPHA.get());
102 float opacity = c.get(FILL_OPACITY, defaultOpacity, Float.class);
103 color = Utils.alphaMultiply(color, opacity);
104 }
105 }
106
107 if (color != null) {
108 Float extent = c.get(FILL_EXTENT, null, float.class);
109 Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
110
111 return new AreaElement(c, color, fillImage, extent, extentThreshold);
112 } else {
113 return null;
114 }
115 }
116
117 @Override
118 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
119 boolean selected, boolean outermember, boolean member) {
120 Color myColor = color;
121 if (osm instanceof Way) {
122 if (color != null) {
123 if (selected) {
124 myColor = paintSettings.getSelectedColor(color.getAlpha());
125 } else if (outermember) {
126 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
127 }
128 }
129 painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
130 } else if (osm instanceof Relation) {
131 if (color != null && (selected || outermember)) {
132 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
133 }
134 painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
135 }
136 }
137
138 @Override
139 public boolean equals(Object obj) {
140 if (this == obj) return true;
141 if (obj == null || getClass() != obj.getClass()) return false;
142 if (!super.equals(obj)) return false;
143 AreaElement that = (AreaElement) obj;
144 return Objects.equals(color, that.color) &&
145 Objects.equals(fillImage, that.fillImage) &&
146 Objects.equals(extent, that.extent) &&
147 Objects.equals(extentThreshold, that.extentThreshold);
148 }
149
150 @Override
151 public int hashCode() {
152 return Objects.hash(super.hashCode(), color, fillImage, extent, extentThreshold);
153 }
154
155 @Override
156 public String toString() {
157 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
158 " fillImage=[" + fillImage + "] extent=[" + extent + "] extentThreshold=[" + extentThreshold + "]}";
159 }
160}
Note: See TracBrowser for help on using the repository browser.