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

Last change on this file since 4272 was 4272, checked in by bastiK, 13 years ago

mapcss: proper support for scaled icons (fixes #6560)

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