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

Last change on this file since 8085 was 8085, checked in by bastiK, 9 years ago

fixed #10216 Offsetting node icon in MapCSS

also: rework of rendering of disabled layers and objects

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