source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java@ 9099

Last change on this file since 9099 was 9099, checked in by bastiK, 8 years ago

mapcss partial fill: move threshold parameter ([9063]) into the mapcss style
(new property fill-extent-threshold)

  • add support for this parameter when area is unclosed
  • smaller extent for unclosed areas

(see #12104)

  • 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;
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.gui.mappaint.MapPaintStyles.IconReference;
15import org.openstreetmap.josm.tools.CheckParameterUtil;
16import org.openstreetmap.josm.tools.Utils;
17
18public class AreaElemStyle extends ElemStyle {
19
20 /**
21 * If fillImage == null, color is the fill-color, otherwise
22 * an arbitrary color value sampled from the fillImage
23 */
24 public Color color;
25 public MapImage fillImage;
26 public TextElement text;
27 public Float extent;
28 public Float extentThreshold;
29
30 protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold, TextElement text) {
31 super(c, 1f);
32 CheckParameterUtil.ensureParameterNotNull(color);
33 this.color = color;
34 this.fillImage = fillImage;
35 this.extent = extent;
36 this.extentThreshold = extentThreshold;
37 this.text = text;
38 }
39
40 public static AreaElemStyle create(final Environment env) {
41 final Cascade c = env.mc.getCascade(env.layer);
42 MapImage fillImage = null;
43 Color color;
44
45 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
46 if (iconRef != null) {
47 fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
48
49 color = new Color(fillImage.getImage(false).getRGB(
50 fillImage.getWidth() / 2, fillImage.getHeight() / 2)
51 );
52
53 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
54 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
55 if (pAlpha != null) {
56 fillImage.alpha = pAlpha;
57 }
58 } else {
59 color = c.get(FILL_COLOR, null, Color.class);
60 if (color != null) {
61 int alpha = color.getAlpha();
62 if (alpha == 255) {
63 // Assume alpha value has not been specified by the user if
64 // is set to fully opaque. Use default value in this case.
65 // It is not an ideal solution, but a little tricky to get this
66 // right, especially as named map colors can be changed in
67 // the preference GUI and written to the preferences file.
68 alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
69 }
70 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
71 if (pAlpha != null) {
72 alpha = pAlpha;
73 }
74 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
75 }
76 }
77
78 TextElement text = null;
79 Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
80 if (textPos == null || "center".equals(textPos.val)) {
81 text = TextElement.create(env, PaintColors.AREA_TEXT.get(), true);
82 }
83
84 Float extent = c.get(FILL_EXTENT, null, float.class);
85 Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
86
87 if (color != null)
88 return new AreaElemStyle(c, color, fillImage, extent, extentThreshold, text);
89 else
90 return null;
91 }
92
93 @Override
94 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
95 boolean selected, boolean outermember, boolean member) {
96 Color myColor = color;
97 if (osm instanceof Way) {
98 if (color != null) {
99 if (selected) {
100 myColor = paintSettings.getSelectedColor(color.getAlpha());
101 } else if (outermember) {
102 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
103 }
104 }
105 painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
106 } else if (osm instanceof Relation) {
107 if (color != null && (selected || outermember)) {
108 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
109 }
110 painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
111 }
112 }
113
114 @Override
115 public boolean equals(Object obj) {
116 if (obj == null || getClass() != obj.getClass())
117 return false;
118 if (!super.equals(obj))
119 return false;
120 AreaElemStyle other = (AreaElemStyle) obj;
121 // we should get the same image object due to caching
122 if (!Objects.equals(fillImage, other.fillImage))
123 return false;
124 if (!Objects.equals(color, other.color))
125 return false;
126 if (extent != other.extent)
127 return false;
128 if (extentThreshold != other.extentThreshold)
129 return false;
130 if (!Objects.equals(text, other.text))
131 return false;
132 return true;
133 }
134
135 @Override
136 public int hashCode() {
137 int hash = 3;
138 hash = 61 * hash + color.hashCode();
139 hash = 61 * hash + (extent != null ? Float.floatToIntBits(extent) : 0);
140 hash = 61 * hash + (extentThreshold != null ? Float.floatToIntBits(extent) : 0);
141 hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
142 hash = 61 * hash + (text != null ? text.hashCode() : 0);
143 return hash;
144 }
145
146 @Override
147 public String toString() {
148 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
149 " fillImage=[" + fillImage + "]}";
150 }
151}
Note: See TracBrowser for help on using the repository browser.