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

Last change on this file since 4822 was 4822, checked in by bastiK, 12 years ago

make identity of map icons depend on the name and not the image data (see #6797)

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.openstreetmap.josm.tools.Utils.equal;
5
6import java.awt.Color;
7import java.awt.image.BufferedImage;
8
9import javax.swing.ImageIcon;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Relation;
14import org.openstreetmap.josm.data.osm.Way;
15import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
16import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
17import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
18import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
21import org.openstreetmap.josm.tools.Utils;
22
23public class AreaElemStyle extends ElemStyle
24{
25 /**
26 * If fillImage == null, color is the fill-color, otherwise
27 * an arbitrary color value sampled from the fillImage
28 */
29 public Color color;
30 public MapImage<BufferedImage> fillImage;
31 public TextElement text;
32
33 protected AreaElemStyle(Cascade c, Color color, MapImage<BufferedImage> fillImage, TextElement text) {
34 super(c, -1000f);
35 CheckParameterUtil.ensureParameterNotNull(color);
36 this.color = color;
37 this.fillImage = fillImage;
38 this.text = text;
39 }
40
41 public static AreaElemStyle create(Cascade c) {
42 MapImage<BufferedImage> fillImage = null;
43 Color color = null;
44
45 IconReference iconRef = c.get("fill-image", null, IconReference.class);
46 if (iconRef != null) {
47 ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1, SanitizeMode.MAKE_BUFFEREDIMAGE);
48 if (icon != null) {
49 if (!(icon.getImage() instanceof BufferedImage))
50 throw new RuntimeException();
51 fillImage = new MapImage<BufferedImage>(iconRef.iconName, iconRef.source);
52 fillImage.img = (BufferedImage) icon.getImage();
53
54 color = new Color(fillImage.img.getRGB(
55 fillImage.img.getWidth() / 2, fillImage.img.getHeight() / 2)
56 );
57
58 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
59 Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
60 if (pAlpha != null) {
61 fillImage.alpha = pAlpha;
62 }
63 }
64 } else {
65 color = c.get("fill-color", null, Color.class);
66 if (color != null) {
67 int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
68 Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
69 if (pAlpha != null) {
70 alpha = pAlpha;
71 }
72 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
73 }
74 }
75
76 TextElement text = null;
77 Keyword textPos = c.get("text-position", null, Keyword.class);
78 if (textPos == null || Utils.equal(textPos.val, "center")) {
79 text = TextElement.create(c, PaintColors.AREA_TEXT.get(), true);
80 }
81
82 if (color != null)
83 return new AreaElemStyle(c, color, fillImage, text);
84 else
85 return null;
86 }
87
88 @Override
89 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
90 if (osm instanceof Way)
91 {
92 Color myColor = color;
93 if (color != null) {
94 if (osm.isSelected()) {
95 myColor = paintSettings.getSelectedColor(color.getAlpha());
96 }
97 }
98 painter.drawArea((Way) osm, myColor, fillImage, text);
99 } else if (osm instanceof Relation)
100 {
101 Color myColor = color;
102 if (color != null) {
103 if (selected) {
104 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
105 }
106 }
107 painter.drawArea((Relation) osm, myColor, fillImage, 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 (!equal(fillImage, other.fillImage))
120 return false;
121 if (!equal(color, other.color))
122 return false;
123 if (!equal(text, other.text))
124 return false;
125 return true;
126 }
127
128 @Override
129 public int hashCode() {
130 int hash = 3;
131 hash = 61 * hash + color.hashCode();
132 hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
133 hash = 61 * hash + (text != null ? text.hashCode() : 0);
134 return hash;
135 }
136
137 @Override
138 public String toString() {
139 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
140 " fillImage=[" + fillImage + "]}";
141 }
142}
Note: See TracBrowser for help on using the repository browser.