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