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

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

see #12104 - mapcss: add option for partial filling of areas (inspired by iD)

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