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 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
10 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
---|
14 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
18 |
|
---|
19 | public class AreaElemStyle extends ElemStyle
|
---|
20 | {
|
---|
21 | /**
|
---|
22 | * If fillImage == null, color is the fill-color, otherwise
|
---|
23 | * an arbitrary color value sampled from the fillImage
|
---|
24 | */
|
---|
25 | public Color color;
|
---|
26 | public MapImage fillImage;
|
---|
27 | public TextElement text;
|
---|
28 |
|
---|
29 | protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) {
|
---|
30 | super(c, -1000f);
|
---|
31 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
32 | this.color = color;
|
---|
33 | this.fillImage = fillImage;
|
---|
34 | this.text = text;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static AreaElemStyle create(Cascade c) {
|
---|
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 | fillImage.getImage();
|
---|
45 |
|
---|
46 | color = new Color(fillImage.getImage().getRGB(
|
---|
47 | fillImage.getWidth() / 2, fillImage.getHeight() / 2)
|
---|
48 | );
|
---|
49 |
|
---|
50 | fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
|
---|
51 | Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
|
---|
52 | if (pAlpha != null) {
|
---|
53 | fillImage.alpha = pAlpha;
|
---|
54 | }
|
---|
55 | } else {
|
---|
56 | color = c.get("fill-color", null, Color.class);
|
---|
57 | if (color != null) {
|
---|
58 | int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
|
---|
59 | Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
|
---|
60 | if (pAlpha != null) {
|
---|
61 | alpha = pAlpha;
|
---|
62 | }
|
---|
63 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | TextElement text = null;
|
---|
68 | Keyword textPos = c.get("text-position", null, Keyword.class);
|
---|
69 | if (textPos == null || Utils.equal(textPos.val, "center")) {
|
---|
70 | text = TextElement.create(c, PaintColors.AREA_TEXT.get(), true);
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (color != null)
|
---|
74 | return new AreaElemStyle(c, color, fillImage, text);
|
---|
75 | else
|
---|
76 | return null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
|
---|
81 | if (osm instanceof Way)
|
---|
82 | {
|
---|
83 | Color myColor = color;
|
---|
84 | if (color != null) {
|
---|
85 | if (osm.isSelected()) {
|
---|
86 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
87 | }
|
---|
88 | }
|
---|
89 | painter.drawArea((Way) osm, myColor, fillImage, text);
|
---|
90 | } else if (osm instanceof Relation)
|
---|
91 | {
|
---|
92 | Color myColor = color;
|
---|
93 | if (color != null) {
|
---|
94 | if (selected) {
|
---|
95 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
96 | }
|
---|
97 | }
|
---|
98 | painter.drawArea((Relation) osm, myColor, fillImage, text);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public boolean equals(Object obj) {
|
---|
104 | if (obj == null || getClass() != obj.getClass())
|
---|
105 | return false;
|
---|
106 | if (!super.equals(obj))
|
---|
107 | return false;
|
---|
108 | AreaElemStyle other = (AreaElemStyle) obj;
|
---|
109 | // we should get the same image object due to caching
|
---|
110 | if (!equal(fillImage, other.fillImage))
|
---|
111 | return false;
|
---|
112 | if (!equal(color, other.color))
|
---|
113 | return false;
|
---|
114 | if (!equal(text, other.text))
|
---|
115 | return false;
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public int hashCode() {
|
---|
121 | int hash = 3;
|
---|
122 | hash = 61 * hash + color.hashCode();
|
---|
123 | hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
|
---|
124 | hash = 61 * hash + (text != null ? text.hashCode() : 0);
|
---|
125 | return hash;
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public String toString() {
|
---|
130 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
131 | " fillImage=[" + fillImage + "]}";
|
---|
132 | }
|
---|
133 | }
|
---|