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

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

extend image sanitize options

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