source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/MapImage.java@ 6182

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

fix #8849 - allow map icons up to 24x24 pixels without rescaling

  • 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;
3
4import static org.openstreetmap.josm.tools.Utils.equal;
5
6import java.awt.Image;
7import java.awt.Rectangle;
8import java.awt.image.BufferedImage;
9
10import javax.swing.ImageIcon;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.BoxProvider;
14import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.BoxProviderResult;
15import org.openstreetmap.josm.gui.util.GuiHelper;
16import org.openstreetmap.josm.tools.ImageProvider;
17import org.openstreetmap.josm.tools.ImageProvider.ImageCallback;
18import org.openstreetmap.josm.tools.Utils;
19
20public class MapImage {
21 /**
22 * ImageIcon can change while the image is loading.
23 */
24 private BufferedImage img;
25
26 /**
27 * The 5 following fields are only used to check for equality.
28 */
29 public int alpha = 255;
30 public String name;
31 public StyleSource source;
32 public int width = -1;
33 public int height = -1;
34
35 private boolean temporary;
36 private Image disabledImg;
37
38 public MapImage(String name, StyleSource source) {
39 this.name = name;
40 this.source = source;
41 }
42
43 public Image getDisabled() {
44 if (disabledImg != null)
45 return disabledImg;
46 if (img == null)
47 getImage(); // fix #7498 ?
48 disabledImg = GuiHelper.getDisabledImage(img);
49 return disabledImg;
50 }
51
52 public BufferedImage getImage() {
53 if (img != null)
54 return img;
55 temporary = false;
56 new ImageProvider(name)
57 .setDirs(MapPaintStyles.getIconSourceDirs(source))
58 .setId("mappaint."+source.getPrefName())
59 .setArchive(source.zipIcons)
60 .setInArchiveDir(source.getZipEntryDirName())
61 .setWidth(width)
62 .setHeight(height)
63 .setOptional(true)
64 .getInBackground(new ImageCallback() {
65 @Override
66 public void finished(ImageIcon result) {
67 synchronized (MapImage.this) {
68 if (result == null) {
69 ImageIcon noIcon = MapPaintStyles.getNoIcon_Icon(source);
70 img = noIcon == null ? null : (BufferedImage) noIcon.getImage();
71 } else {
72 img = (BufferedImage) result.getImage();
73 }
74 if (temporary) {
75 Main.map.mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed
76 Main.map.mapView.repaint();
77 }
78 temporary = false;
79 }
80 }
81 }
82 );
83 synchronized (this) {
84 if (img == null) {
85 img = (BufferedImage) ImageProvider.get("clock").getImage();
86 temporary = true;
87 }
88 }
89 return img;
90 }
91
92 public int getWidth() {
93 return getImage().getWidth(null);
94 }
95
96 public int getHeight() {
97 return getImage().getHeight(null);
98 }
99
100 public float getAlphaFloat() {
101 return Utils.color_int2float(alpha);
102 }
103
104 /**
105 * Returns true, if image is not completely loaded and getImage() returns a temporary image.
106 */
107 public boolean isTemporary() {
108 return temporary;
109 }
110
111 protected class MapImageBoxProvider implements BoxProvider {
112 @Override
113 public BoxProviderResult get() {
114 return new BoxProviderResult(box(), temporary);
115 }
116
117 private Rectangle box() {
118 int w = getWidth(), h = getHeight();
119 return new Rectangle(-w/2, -h/2, w, h);
120 }
121
122 private MapImage getParent() {
123 return MapImage.this;
124 }
125
126 @Override
127 public int hashCode() {
128 return MapImage.this.hashCode();
129 }
130
131 @Override
132 public boolean equals(Object obj) {
133 if (!(obj instanceof BoxProvider))
134 return false;
135 if (obj instanceof MapImageBoxProvider) {
136 MapImageBoxProvider other = (MapImageBoxProvider) obj;
137 return MapImage.this.equals(other.getParent());
138 } else if (temporary) {
139 return false;
140 } else {
141 final BoxProvider other = (BoxProvider) obj;
142 BoxProviderResult resultOther = other.get();
143 if (resultOther.isTemporary()) return false;
144 return box().equals(resultOther.getBox());
145 }
146 }
147 }
148
149 public BoxProvider getBoxProvider() {
150 return new MapImageBoxProvider();
151 }
152
153 /**
154 * Returns the really displayed node icon for this {@code MapImage}.
155 * @param disabled {@code} true to request disabled version, {@code false} for the standard version
156 * @return The scaled down version to 16x16 pixels if the image size exceeds 24x24 pixels and no size has been explicitely specified
157 * @since 6174
158 */
159 public Image getDisplayedNodeIcon(boolean disabled) {
160 final int maxSize = 24;
161 final Image image = disabled ? getDisabled() : getImage();
162 // Scale down large (.svg) images to 16x16 pixels if no size is explicitely specified
163 if ((width == -1 && image.getWidth(null) > maxSize)
164 || (height == -1 && image.getHeight(null) > maxSize)) {
165 return ImageProvider.createBoundedImage(image, 16);
166 } else {
167 return image;
168 }
169 }
170
171 @Override
172 public boolean equals(Object obj) {
173 if (obj == null || getClass() != obj.getClass())
174 return false;
175 final MapImage other = (MapImage) obj;
176 // img changes when image is fully loaded and can't be used for equality check.
177 return alpha == other.alpha &&
178 equal(name, other.name) &&
179 equal(source, other.source) &&
180 width == other.width &&
181 height == other.height;
182 }
183
184 @Override
185 public int hashCode() {
186 int hash = 7;
187 hash = 67 * hash + alpha;
188 hash = 67 * hash + name.hashCode();
189 hash = 67 * hash + source.hashCode();
190 hash = 67 * hash + width;
191 hash = 67 * hash + height;
192 return hash;
193 }
194
195 @Override
196 public String toString() {
197 return name;
198 }
199}
Note: See TracBrowser for help on using the repository browser.