source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/AreaElement.java@ 13662

Last change on this file since 13662 was 13662, checked in by Don-vip, 7 years ago

use of IPrimitive interface

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import java.awt.Color;
5import java.awt.Image;
6import java.awt.image.BufferedImage;
7import java.util.Objects;
8
9import org.openstreetmap.josm.data.osm.IPrimitive;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.Way;
12import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
13import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
14import org.openstreetmap.josm.data.preferences.IntegerProperty;
15import org.openstreetmap.josm.gui.mappaint.Cascade;
16import org.openstreetmap.josm.gui.mappaint.Environment;
17import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
18import org.openstreetmap.josm.spi.preferences.Config;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20import org.openstreetmap.josm.tools.HiDPISupport;
21import org.openstreetmap.josm.tools.Utils;
22import org.openstreetmap.josm.tools.bugreport.BugReport;
23
24/**
25 * This is the style that defines how an area is filled.
26 */
27public class AreaElement extends StyleElement {
28
29 /**
30 * The default opacity for the fill. For historical reasons in range 0.255.
31 */
32 private static final IntegerProperty DEFAULT_FILL_ALPHA = new IntegerProperty("mappaint.fillalpha", 50);
33
34 /**
35 * If fillImage == null, color is the fill-color, otherwise
36 * an arbitrary color value sampled from the fillImage.
37 *
38 * The color may be fully transparent to indicate that the area should not be filled.
39 */
40 public Color color;
41
42 /**
43 * An image to cover this area. May be null to disable this feature.
44 */
45 public MapImage fillImage;
46
47 /**
48 * Fill the area only partially from the borders
49 * <p>
50 * Public access is discouraged.
51 * @see StyledMapRenderer#drawArea
52 */
53 public Float extent;
54
55 /**
56 * Areas smaller than this are filled no matter what value {@link #extent} has.
57 * <p>
58 * Public access is discouraged.
59 * @see StyledMapRenderer#drawArea
60 */
61 public Float extentThreshold;
62
63 protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold) {
64 super(c, 1f);
65 CheckParameterUtil.ensureParameterNotNull(color);
66 this.color = color;
67 this.fillImage = fillImage;
68 this.extent = extent;
69 this.extentThreshold = extentThreshold;
70 }
71
72 /**
73 * Create a new {@link AreaElement}
74 * @param env The current style definitions
75 * @return The area element or <code>null</code> if the area should not be filled.
76 */
77 public static AreaElement create(final Environment env) {
78 final Cascade c = env.mc.getCascade(env.layer);
79 MapImage fillImage = null;
80 Color color;
81
82 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
83 if (iconRef != null) {
84 fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
85 Image img = fillImage.getImage(false);
86 // get base image from possible multi-resolution image, so we can
87 // cast to BufferedImage and get pixel value at the center of the image
88 img = HiDPISupport.getBaseImage(img);
89 try {
90 color = new Color(((BufferedImage) img).getRGB(
91 img.getWidth(null) / 2, img.getHeight(null) / 2)
92 );
93 } catch (ArrayIndexOutOfBoundsException e) {
94 throw BugReport.intercept(e).put("env.osm", env.osm).put("iconRef", iconRef).put("fillImage", fillImage).put("img", img);
95 }
96
97 fillImage.alpha = Utils.clamp(Config.getPref().getInt("mappaint.fill-image-alpha", 255), 0, 255);
98 Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
99 if (pAlpha != null) {
100 fillImage.alpha = pAlpha;
101 }
102 } else {
103 color = c.get(FILL_COLOR, null, Color.class);
104 if (color != null) {
105 float defaultOpacity = Utils.colorInt2float(DEFAULT_FILL_ALPHA.get());
106 float opacity = c.get(FILL_OPACITY, defaultOpacity, Float.class);
107 color = Utils.alphaMultiply(color, opacity);
108 }
109 }
110
111 if (color != null) {
112 Float extent = c.get(FILL_EXTENT, null, float.class);
113 Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
114
115 return new AreaElement(c, color, fillImage, extent, extentThreshold);
116 } else {
117 return null;
118 }
119 }
120
121 @Override
122 public void paintPrimitive(IPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
123 boolean selected, boolean outermember, boolean member) {
124 Color myColor = color;
125 if (osm instanceof Way) {
126 if (color != null) {
127 if (selected) {
128 myColor = paintSettings.getSelectedColor(color.getAlpha());
129 } else if (outermember) {
130 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
131 }
132 }
133 painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
134 } else if (osm instanceof Relation) {
135 if (color != null && (selected || outermember)) {
136 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
137 }
138 painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled());
139 }
140 }
141
142 @Override
143 public boolean equals(Object obj) {
144 if (this == obj) return true;
145 if (obj == null || getClass() != obj.getClass()) return false;
146 if (!super.equals(obj)) return false;
147 AreaElement that = (AreaElement) obj;
148 return Objects.equals(color, that.color) &&
149 Objects.equals(fillImage, that.fillImage) &&
150 Objects.equals(extent, that.extent) &&
151 Objects.equals(extentThreshold, that.extentThreshold);
152 }
153
154 @Override
155 public int hashCode() {
156 return Objects.hash(super.hashCode(), color, fillImage, extent, extentThreshold);
157 }
158
159 @Override
160 public String toString() {
161 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
162 " fillImage=[" + fillImage + "] extent=[" + extent + "] extentThreshold=[" + extentThreshold + "]}";
163 }
164}
Note: See TracBrowser for help on using the repository browser.