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.StyledMapRenderer;
|
---|
13 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
17 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
18 | import org.openstreetmap.josm.tools.Utils;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * This is the style that defines how an area is filled.
|
---|
22 | */
|
---|
23 | public class AreaElement extends StyleElement {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * The default opacity for the fill. For historical reasons in range 0.255.
|
---|
27 | */
|
---|
28 | private static final IntegerProperty DEFAULT_FILL_ALPHA = new IntegerProperty("mappaint.fillalpha", 50);
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * If fillImage == null, color is the fill-color, otherwise
|
---|
32 | * an arbitrary color value sampled from the fillImage.
|
---|
33 | *
|
---|
34 | * The color may be fully transparent to indicate that the area should not be filled.
|
---|
35 | */
|
---|
36 | public Color color;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * An image to cover this area. May be null to disable this feature.
|
---|
40 | */
|
---|
41 | public MapImage fillImage;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Fill the area only partially from the borders
|
---|
45 | * <p>
|
---|
46 | * Public access is discouraged.
|
---|
47 | * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
|
---|
48 | */
|
---|
49 | public Float extent;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Areas smaller than this are filled no matter what value {@link #extent} has.
|
---|
53 | * <p>
|
---|
54 | * Public access is discouraged.
|
---|
55 | * @see StyledMapRenderer#drawArea(Way, Color, MapImage, Float, Float, boolean, TextLabel)
|
---|
56 | */
|
---|
57 | public Float extentThreshold;
|
---|
58 |
|
---|
59 | protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold) {
|
---|
60 | super(c, 1f);
|
---|
61 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
62 | this.color = color;
|
---|
63 | this.fillImage = fillImage;
|
---|
64 | this.extent = extent;
|
---|
65 | this.extentThreshold = extentThreshold;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Create a new {@link AreaElement}
|
---|
70 | * @param env The current style definitions
|
---|
71 | * @return The area element or <code>null</code> if the area should not be filled.
|
---|
72 | */
|
---|
73 | public static AreaElement create(final Environment env) {
|
---|
74 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
75 | MapImage fillImage = null;
|
---|
76 | Color color;
|
---|
77 |
|
---|
78 | IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
|
---|
79 | if (iconRef != null) {
|
---|
80 | fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
|
---|
81 |
|
---|
82 | color = new Color(fillImage.getImage(false).getRGB(
|
---|
83 | fillImage.getWidth() / 2, fillImage.getHeight() / 2)
|
---|
84 | );
|
---|
85 |
|
---|
86 | fillImage.alpha = Utils.clamp(Main.pref.getInteger("mappaint.fill-image-alpha", 255), 0, 255);
|
---|
87 | Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
|
---|
88 | if (pAlpha != null) {
|
---|
89 | fillImage.alpha = pAlpha;
|
---|
90 | }
|
---|
91 | } else {
|
---|
92 | color = c.get(FILL_COLOR, null, Color.class);
|
---|
93 | if (color != null) {
|
---|
94 | float defaultOpacity = Utils.colorInt2float(DEFAULT_FILL_ALPHA.get());
|
---|
95 | float opacity = c.get(FILL_OPACITY, defaultOpacity, Float.class);
|
---|
96 | color = Utils.alphaMultiply(color, opacity);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (color != null) {
|
---|
101 | Float extent = c.get(FILL_EXTENT, null, float.class);
|
---|
102 | Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
|
---|
103 |
|
---|
104 | return new AreaElement(c, color, fillImage, extent, extentThreshold);
|
---|
105 | } else {
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
112 | boolean selected, boolean outermember, boolean member) {
|
---|
113 | Color myColor = color;
|
---|
114 | if (osm instanceof Way) {
|
---|
115 | if (color != null) {
|
---|
116 | if (selected) {
|
---|
117 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
118 | } else if (outermember) {
|
---|
119 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
120 | }
|
---|
121 | }
|
---|
122 | painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
|
---|
123 | } else if (osm instanceof Relation) {
|
---|
124 | if (color != null && (selected || outermember)) {
|
---|
125 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
126 | }
|
---|
127 | painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public boolean equals(Object obj) {
|
---|
133 | if (this == obj) return true;
|
---|
134 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
135 | if (!super.equals(obj)) return false;
|
---|
136 | AreaElement that = (AreaElement) obj;
|
---|
137 | return Objects.equals(color, that.color) &&
|
---|
138 | Objects.equals(fillImage, that.fillImage) &&
|
---|
139 | Objects.equals(extent, that.extent) &&
|
---|
140 | Objects.equals(extentThreshold, that.extentThreshold);
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public int hashCode() {
|
---|
145 | return Objects.hash(super.hashCode(), color, fillImage, extent, extentThreshold);
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public String toString() {
|
---|
150 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
151 | " fillImage=[" + fillImage + "] extent=[" + extent + "] extentThreshold=[" + extentThreshold + "]}";
|
---|
152 | }
|
---|
153 | }
|
---|