1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
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.Cascade;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
19 | import org.openstreetmap.josm.tools.Utils;
|
---|
20 |
|
---|
21 | public class AreaElement extends StyleElement {
|
---|
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 MapImage fillImage;
|
---|
29 | public TextLabel text;
|
---|
30 | public Float extent;
|
---|
31 | public Float extentThreshold;
|
---|
32 |
|
---|
33 | protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold, TextLabel text) {
|
---|
34 | super(c, 1f);
|
---|
35 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
36 | this.color = color;
|
---|
37 | this.fillImage = fillImage;
|
---|
38 | this.extent = extent;
|
---|
39 | this.extentThreshold = extentThreshold;
|
---|
40 | this.text = text;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static AreaElement create(final Environment env) {
|
---|
44 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
45 | MapImage fillImage = null;
|
---|
46 | Color color;
|
---|
47 |
|
---|
48 | IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
|
---|
49 | if (iconRef != null) {
|
---|
50 | fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
|
---|
51 |
|
---|
52 | color = new Color(fillImage.getImage(false).getRGB(
|
---|
53 | fillImage.getWidth() / 2, fillImage.getHeight() / 2)
|
---|
54 | );
|
---|
55 |
|
---|
56 | fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
|
---|
57 | Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
|
---|
58 | if (pAlpha != null) {
|
---|
59 | fillImage.alpha = pAlpha;
|
---|
60 | }
|
---|
61 | } else {
|
---|
62 | color = c.get(FILL_COLOR, null, Color.class);
|
---|
63 | if (color != null) {
|
---|
64 | int alpha = color.getAlpha();
|
---|
65 | if (alpha == 255) {
|
---|
66 | // Assume alpha value has not been specified by the user if
|
---|
67 | // is set to fully opaque. Use default value in this case.
|
---|
68 | // It is not an ideal solution, but a little tricky to get this
|
---|
69 | // right, especially as named map colors can be changed in
|
---|
70 | // the preference GUI and written to the preferences file.
|
---|
71 | alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
|
---|
72 | }
|
---|
73 | Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
|
---|
74 | if (pAlpha != null) {
|
---|
75 | alpha = pAlpha;
|
---|
76 | }
|
---|
77 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | TextLabel text = null;
|
---|
82 | Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
|
---|
83 | if (textPos == null || "center".equals(textPos.val)) {
|
---|
84 | text = TextLabel.create(env, PaintColors.AREA_TEXT.get(), true);
|
---|
85 | }
|
---|
86 |
|
---|
87 | Float extent = c.get(FILL_EXTENT, null, float.class);
|
---|
88 | Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
|
---|
89 |
|
---|
90 | if (color != null)
|
---|
91 | return new AreaElement(c, color, fillImage, extent, extentThreshold, text);
|
---|
92 | else
|
---|
93 | return null;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
98 | boolean selected, boolean outermember, boolean member) {
|
---|
99 | Color myColor = color;
|
---|
100 | if (osm instanceof Way) {
|
---|
101 | if (color != null) {
|
---|
102 | if (selected) {
|
---|
103 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
104 | } else if (outermember) {
|
---|
105 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
106 | }
|
---|
107 | }
|
---|
108 | painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
|
---|
109 | } else if (osm instanceof Relation) {
|
---|
110 | if (color != null && (selected || outermember)) {
|
---|
111 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
112 | }
|
---|
113 | painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public boolean equals(Object obj) {
|
---|
119 | if (obj == null || getClass() != obj.getClass())
|
---|
120 | return false;
|
---|
121 | if (!super.equals(obj))
|
---|
122 | return false;
|
---|
123 | AreaElement other = (AreaElement) obj;
|
---|
124 | // we should get the same image object due to caching
|
---|
125 | if (!Objects.equals(fillImage, other.fillImage))
|
---|
126 | return false;
|
---|
127 | if (!Objects.equals(color, other.color))
|
---|
128 | return false;
|
---|
129 | if (!Objects.equals(extent, other.extent))
|
---|
130 | return false;
|
---|
131 | if (!Objects.equals(extentThreshold, other.extentThreshold))
|
---|
132 | return false;
|
---|
133 | if (!Objects.equals(text, other.text))
|
---|
134 | return false;
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public int hashCode() {
|
---|
140 | int hash = super.hashCode();
|
---|
141 | hash = 61 * hash + color.hashCode();
|
---|
142 | hash = 61 * hash + (extent != null ? Float.floatToIntBits(extent) : 0);
|
---|
143 | hash = 61 * hash + (extentThreshold != null ? Float.floatToIntBits(extentThreshold) : 0);
|
---|
144 | hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
|
---|
145 | hash = 61 * hash + (text != null ? text.hashCode() : 0);
|
---|
146 | return hash;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public String toString() {
|
---|
151 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
152 | " fillImage=[" + fillImage + "]}";
|
---|
153 | }
|
---|
154 | }
|
---|