1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.util.Objects;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.Main;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.Way;
|
---|
11 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
15 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 |
|
---|
18 | public class AreaElemStyle extends ElemStyle {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * If fillImage == null, color is the fill-color, otherwise
|
---|
22 | * an arbitrary color value sampled from the fillImage
|
---|
23 | */
|
---|
24 | public Color color;
|
---|
25 | public MapImage fillImage;
|
---|
26 | public TextElement text;
|
---|
27 |
|
---|
28 | protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) {
|
---|
29 | super(c, 1f);
|
---|
30 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
31 | this.color = color;
|
---|
32 | this.fillImage = fillImage;
|
---|
33 | this.text = text;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static AreaElemStyle create(final Environment env) {
|
---|
37 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
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 = color.getAlpha();
|
---|
59 | if (alpha == 255) {
|
---|
60 | // Assume alpha value has not been specified by the user if
|
---|
61 | // is set to fully opaque. Use default value in this case.
|
---|
62 | // It is not an ideal solution, but a little tricky to get this
|
---|
63 | // right, especially as named map colors can be changed in
|
---|
64 | // the preference GUI and written to the preferences file.
|
---|
65 | alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
|
---|
66 | }
|
---|
67 | Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
|
---|
68 | if (pAlpha != null) {
|
---|
69 | alpha = pAlpha;
|
---|
70 | }
|
---|
71 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | TextElement text = null;
|
---|
76 | Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
|
---|
77 | if (textPos == null || "center".equals(textPos.val)) {
|
---|
78 | text = TextElement.create(env, PaintColors.AREA_TEXT.get(), true);
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (color != null)
|
---|
82 | return new AreaElemStyle(c, color, fillImage, text);
|
---|
83 | else
|
---|
84 | return null;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
89 | boolean selected, boolean outermember, boolean member) {
|
---|
90 | Color myColor = color;
|
---|
91 | if (osm instanceof Way) {
|
---|
92 | if (color != null) {
|
---|
93 | if (selected) {
|
---|
94 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
95 | } else if (outermember) {
|
---|
96 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
97 | }
|
---|
98 | }
|
---|
99 | painter.drawArea((Way) osm, myColor, fillImage, text);
|
---|
100 | } else if (osm instanceof Relation) {
|
---|
101 | if (color != null && (selected || outermember)) {
|
---|
102 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
103 | }
|
---|
104 | painter.drawArea((Relation) osm, myColor, fillImage, text);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public boolean equals(Object obj) {
|
---|
110 | if (obj == null || getClass() != obj.getClass())
|
---|
111 | return false;
|
---|
112 | if (!super.equals(obj))
|
---|
113 | return false;
|
---|
114 | AreaElemStyle other = (AreaElemStyle) obj;
|
---|
115 | // we should get the same image object due to caching
|
---|
116 | if (!Objects.equals(fillImage, other.fillImage))
|
---|
117 | return false;
|
---|
118 | if (!Objects.equals(color, other.color))
|
---|
119 | return false;
|
---|
120 | if (!Objects.equals(text, other.text))
|
---|
121 | return false;
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public int hashCode() {
|
---|
127 | int hash = 3;
|
---|
128 | hash = 61 * hash + color.hashCode();
|
---|
129 | hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
|
---|
130 | hash = 61 * hash + (text != null ? text.hashCode() : 0);
|
---|
131 | return hash;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public String toString() {
|
---|
136 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
137 | " fillImage=[" + fillImage + "]}";
|
---|
138 | }
|
---|
139 | }
|
---|