1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.image.BufferedImage;
|
---|
8 |
|
---|
9 | import javax.swing.ImageIcon;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.data.osm.Way;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
---|
17 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | public 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 | }
|
---|