1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.Image;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.awt.image.BufferedImage;
|
---|
8 | import java.util.Objects;
|
---|
9 |
|
---|
10 | import javax.swing.ImageIcon;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.BoxProvider;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.BoxProviderResult;
|
---|
15 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider.ImageCallback;
|
---|
18 | import org.openstreetmap.josm.tools.Utils;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * An image that will be displayed on the map.
|
---|
22 | */
|
---|
23 | public class MapImage {
|
---|
24 |
|
---|
25 | private static final int MAX_SIZE = 48;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * ImageIcon can change while the image is loading.
|
---|
29 | */
|
---|
30 | private BufferedImage img;
|
---|
31 |
|
---|
32 | public int alpha = 255;
|
---|
33 | public String name;
|
---|
34 | public StyleSource source;
|
---|
35 | public boolean autoRescale;
|
---|
36 | public int width = -1;
|
---|
37 | public int height = -1;
|
---|
38 | public int offsetX;
|
---|
39 | public int offsetY;
|
---|
40 |
|
---|
41 | private boolean temporary;
|
---|
42 | private BufferedImage disabledImgCache;
|
---|
43 |
|
---|
44 | public MapImage(String name, StyleSource source) {
|
---|
45 | this(name, source, true);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public MapImage(String name, StyleSource source, boolean autoRescale) {
|
---|
49 | this.name = name;
|
---|
50 | this.source = source;
|
---|
51 | this.autoRescale = autoRescale;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Get the image associated with this MapImage object.
|
---|
56 | *
|
---|
57 | * @param disabled {@code} true to request disabled version, {@code false} for the standard version
|
---|
58 | * @return the image
|
---|
59 | */
|
---|
60 | public BufferedImage getImage(boolean disabled) {
|
---|
61 | if (disabled) {
|
---|
62 | return getDisabled();
|
---|
63 | } else {
|
---|
64 | return getImage();
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | private BufferedImage getDisabled() {
|
---|
69 | if (disabledImgCache != null)
|
---|
70 | return disabledImgCache;
|
---|
71 | if (img == null)
|
---|
72 | getImage(); // fix #7498 ?
|
---|
73 | Image disImg = GuiHelper.getDisabledImage(img);
|
---|
74 | if (disImg instanceof BufferedImage) {
|
---|
75 | disabledImgCache = (BufferedImage) disImg;
|
---|
76 | } else {
|
---|
77 | disabledImgCache = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
|
---|
78 | Graphics g = disabledImgCache.getGraphics();
|
---|
79 | g.drawImage(disImg, 0, 0, null);
|
---|
80 | g.dispose();
|
---|
81 | }
|
---|
82 | return disabledImgCache;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private BufferedImage getImage() {
|
---|
86 | if (img != null)
|
---|
87 | return img;
|
---|
88 | temporary = false;
|
---|
89 | new ImageProvider(name)
|
---|
90 | .setDirs(MapPaintStyles.getIconSourceDirs(source))
|
---|
91 | .setId("mappaint."+source.getPrefName())
|
---|
92 | .setArchive(source.zipIcons)
|
---|
93 | .setInArchiveDir(source.getZipEntryDirName())
|
---|
94 | .setWidth(width)
|
---|
95 | .setHeight(height)
|
---|
96 | .setOptional(true)
|
---|
97 | .getInBackground(new ImageCallback() {
|
---|
98 | @Override
|
---|
99 | public void finished(ImageIcon result) {
|
---|
100 | synchronized (MapImage.this) {
|
---|
101 | if (result == null) {
|
---|
102 | ImageIcon noIcon = MapPaintStyles.getNoIcon_Icon(source);
|
---|
103 | img = noIcon == null ? null : (BufferedImage) noIcon.getImage();
|
---|
104 | } else {
|
---|
105 | img = (BufferedImage) rescale(result.getImage());
|
---|
106 | }
|
---|
107 | if (temporary) {
|
---|
108 | disabledImgCache = null;
|
---|
109 | Main.map.mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed
|
---|
110 | Main.map.mapView.repaint();
|
---|
111 | }
|
---|
112 | temporary = false;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | );
|
---|
117 | synchronized (this) {
|
---|
118 | if (img == null) {
|
---|
119 | img = (BufferedImage) ImageProvider.get("clock").getImage();
|
---|
120 | temporary = true;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return img;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public int getWidth() {
|
---|
127 | return getImage().getWidth(null);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public int getHeight() {
|
---|
131 | return getImage().getHeight(null);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public float getAlphaFloat() {
|
---|
135 | return Utils.color_int2float(alpha);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Returns true, if image is not completely loaded and getImage() returns a temporary image.
|
---|
140 | */
|
---|
141 | public boolean isTemporary() {
|
---|
142 | return temporary;
|
---|
143 | }
|
---|
144 |
|
---|
145 | protected class MapImageBoxProvider implements BoxProvider {
|
---|
146 | @Override
|
---|
147 | public BoxProviderResult get() {
|
---|
148 | return new BoxProviderResult(box(), temporary);
|
---|
149 | }
|
---|
150 |
|
---|
151 | private Rectangle box() {
|
---|
152 | int w = getWidth(), h = getHeight();
|
---|
153 | if (mustRescale(getImage())) {
|
---|
154 | w = 16;
|
---|
155 | h = 16;
|
---|
156 | }
|
---|
157 | return new Rectangle(-w/2, -h/2, w, h);
|
---|
158 | }
|
---|
159 |
|
---|
160 | private MapImage getParent() {
|
---|
161 | return MapImage.this;
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public int hashCode() {
|
---|
166 | return MapImage.this.hashCode();
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | public boolean equals(Object obj) {
|
---|
171 | if (!(obj instanceof BoxProvider))
|
---|
172 | return false;
|
---|
173 | if (obj instanceof MapImageBoxProvider) {
|
---|
174 | MapImageBoxProvider other = (MapImageBoxProvider) obj;
|
---|
175 | return MapImage.this.equals(other.getParent());
|
---|
176 | } else if (temporary) {
|
---|
177 | return false;
|
---|
178 | } else {
|
---|
179 | final BoxProvider other = (BoxProvider) obj;
|
---|
180 | BoxProviderResult resultOther = other.get();
|
---|
181 | if (resultOther.isTemporary()) return false;
|
---|
182 | return box().equals(resultOther.getBox());
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | public BoxProvider getBoxProvider() {
|
---|
188 | return new MapImageBoxProvider();
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Rescale excessively large images.
|
---|
193 | * @param image the unscaled image
|
---|
194 | * @return The scaled down version to 16x16 pixels if the image height and width exceeds 48 pixels and no size has been explicitely specified
|
---|
195 | */
|
---|
196 | private Image rescale(Image image) {
|
---|
197 | if (image == null) return null;
|
---|
198 | // Scale down large (.svg) images to 16x16 pixels if no size is explicitely specified
|
---|
199 | if (mustRescale(image)) {
|
---|
200 | return ImageProvider.createBoundedImage(image, 16);
|
---|
201 | } else {
|
---|
202 | return image;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | private boolean mustRescale(Image image) {
|
---|
207 | return autoRescale && width == -1 && image.getWidth(null) > MAX_SIZE
|
---|
208 | && height == -1 && image.getHeight(null) > MAX_SIZE;
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public boolean equals(Object obj) {
|
---|
213 | if (obj == null || getClass() != obj.getClass())
|
---|
214 | return false;
|
---|
215 | final MapImage other = (MapImage) obj;
|
---|
216 | // img changes when image is fully loaded and can't be used for equality check.
|
---|
217 | return alpha == other.alpha &&
|
---|
218 | Objects.equals(name, other.name) &&
|
---|
219 | Objects.equals(source, other.source) &&
|
---|
220 | autoRescale == other.autoRescale &&
|
---|
221 | width == other.width &&
|
---|
222 | height == other.height;
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public int hashCode() {
|
---|
227 | int hash = 7;
|
---|
228 | hash = 67 * hash + alpha;
|
---|
229 | hash = 67 * hash + name.hashCode();
|
---|
230 | hash = 67 * hash + source.hashCode();
|
---|
231 | hash = 67 * hash + (autoRescale ? 1 : 0);
|
---|
232 | hash = 67 * hash + width;
|
---|
233 | hash = 67 * hash + height;
|
---|
234 | return hash;
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Override
|
---|
238 | public String toString() {
|
---|
239 | return name;
|
---|
240 | }
|
---|
241 | }
|
---|