| 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.image.BufferedImage;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.GrayFilter;
|
|---|
| 11 | import javax.swing.ImageIcon;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.Main;
|
|---|
| 14 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.BoxProvider;
|
|---|
| 15 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.BoxProviderResult;
|
|---|
| 16 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 17 | import org.openstreetmap.josm.tools.ImageProvider.ImageCallback;
|
|---|
| 18 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 19 |
|
|---|
| 20 | public 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 | return disabledImg = GrayFilter.createDisabledImage(img);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | public BufferedImage getImage() {
|
|---|
| 52 | if (img != null)
|
|---|
| 53 | return img;
|
|---|
| 54 | temporary = false;
|
|---|
| 55 | new ImageProvider(name)
|
|---|
| 56 | .setDirs(MapPaintStyles.getIconSourceDirs(source))
|
|---|
| 57 | .setId("mappaint."+source.getPrefName())
|
|---|
| 58 | .setArchive(source.zipIcons)
|
|---|
| 59 | .setWidth(width)
|
|---|
| 60 | .setHeight(height)
|
|---|
| 61 | .setOptional(true)
|
|---|
| 62 | .getInBackground(new ImageCallback() {
|
|---|
| 63 | @Override
|
|---|
| 64 | public void finished(ImageIcon result) {
|
|---|
| 65 | synchronized (MapImage.this) {
|
|---|
| 66 | if (result == null) {
|
|---|
| 67 | img = (BufferedImage) MapPaintStyles.getNoIcon_Icon(source).getImage();
|
|---|
| 68 | } else {
|
|---|
| 69 | img = (BufferedImage) result.getImage();
|
|---|
| 70 | }
|
|---|
| 71 | if (temporary) {
|
|---|
| 72 | Main.map.mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed
|
|---|
| 73 | Main.map.mapView.repaint();
|
|---|
| 74 | }
|
|---|
| 75 | temporary = false;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | );
|
|---|
| 80 | synchronized (this) {
|
|---|
| 81 | if (img == null) {
|
|---|
| 82 | img = (BufferedImage) ImageProvider.get("clock").getImage();
|
|---|
| 83 | temporary = true;
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | return img;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public int getWidth() {
|
|---|
| 90 | return getImage().getWidth(null);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public int getHeight() {
|
|---|
| 94 | return getImage().getHeight(null);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | public float getAlphaFloat() {
|
|---|
| 98 | return Utils.color_int2float(alpha);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Returns true, if image is not completely loaded and getImage() returns a temporary image.
|
|---|
| 103 | */
|
|---|
| 104 | public boolean isTemporary() {
|
|---|
| 105 | return temporary;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | protected class MapImageBoxProvider implements BoxProvider {
|
|---|
| 109 | @Override
|
|---|
| 110 | public BoxProviderResult get() {
|
|---|
| 111 | return new BoxProviderResult(box(), temporary);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | private Rectangle box() {
|
|---|
| 115 | int w = getWidth(), h = getHeight();
|
|---|
| 116 | return new Rectangle(-w/2, -h/2, w, h);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | private MapImage getParent() {
|
|---|
| 120 | return MapImage.this;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | @Override
|
|---|
| 124 | public int hashCode() {
|
|---|
| 125 | return MapImage.this.hashCode();
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | @Override
|
|---|
| 129 | public boolean equals(Object obj) {
|
|---|
| 130 | if (obj == null || !(obj instanceof BoxProvider))
|
|---|
| 131 | return false;
|
|---|
| 132 | if (obj instanceof MapImageBoxProvider) {
|
|---|
| 133 | MapImageBoxProvider other = (MapImageBoxProvider) obj;
|
|---|
| 134 | return MapImage.this.equals(other.getParent());
|
|---|
| 135 | } else if (temporary) {
|
|---|
| 136 | return false;
|
|---|
| 137 | } else {
|
|---|
| 138 | final BoxProvider other = (BoxProvider) obj;
|
|---|
| 139 | BoxProviderResult resultOther = other.get();
|
|---|
| 140 | if (resultOther.isTemporary()) return false;
|
|---|
| 141 | return box().equals(resultOther.getBox());
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | public BoxProvider getBoxProvider() {
|
|---|
| 147 | return new MapImageBoxProvider();
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | @Override
|
|---|
| 151 | public boolean equals(Object obj) {
|
|---|
| 152 | if (obj == null || getClass() != obj.getClass())
|
|---|
| 153 | return false;
|
|---|
| 154 | final MapImage other = (MapImage) obj;
|
|---|
| 155 | // img changes when image is fully loaded and can't be used for equality check.
|
|---|
| 156 | return alpha == other.alpha &&
|
|---|
| 157 | equal(name, other.name) &&
|
|---|
| 158 | equal(source, other.source) &&
|
|---|
| 159 | width == other.width &&
|
|---|
| 160 | height == other.height;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | @Override
|
|---|
| 164 | public int hashCode() {
|
|---|
| 165 | int hash = 7;
|
|---|
| 166 | hash = 67 * hash + alpha;
|
|---|
| 167 | hash = 67 * hash + name.hashCode();
|
|---|
| 168 | hash = 67 * hash + source.hashCode();
|
|---|
| 169 | hash = 67 * hash + width;
|
|---|
| 170 | hash = 67 * hash + height;
|
|---|
| 171 | return hash;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | @Override
|
|---|
| 175 | public String toString() {
|
|---|
| 176 | return name;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|