diff --git a/src/org/openstreetmap/josm/tools/ImageProvider.java b/src/org/openstreetmap/josm/tools/ImageProvider.java
index c07e0c88a..138ed46f9 100644
a
|
b
|
public class ImageProvider {
|
1328 | 1328 | return Toolkit.getDefaultToolkit().createCustomCursor(image, hotSpot, name); |
1329 | 1329 | } |
1330 | 1330 | |
| 1331 | static final int CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO = 32; // the following cursor hotspot constants are relative to this cursor size |
| 1332 | private static final Point DEFAULT_HOTSPOT = new Point(3, 2); // FIXME: define better hotspot for rotate.png |
| 1333 | private static final Point CROSSHAIR_HOTSPOT = new Point(10, 10); |
| 1334 | |
1331 | 1335 | /** |
1332 | 1336 | * Load a cursor image with a given file name, optionally decorated with an overlay image |
1333 | 1337 | * |
… |
… |
public class ImageProvider {
|
1344 | 1348 | .addOverlay(new ImageOverlay(new ImageProvider("cursor/modifier/" + overlay) |
1345 | 1349 | .setMaxSize(ImageSizes.CURSOROVERLAY))); |
1346 | 1350 | } |
1347 | | hotSpot.setLocation("crosshair".equals(name) ? new Point(10, 10) : new Point(3, 2)); |
| 1351 | hotSpot.setLocation("crosshair".equals(name) ? CROSSHAIR_HOTSPOT : DEFAULT_HOTSPOT); |
1348 | 1352 | ImageIcon imageIcon = imageProvider.get(); |
1349 | 1353 | Image image = imageIcon.getImage(); |
1350 | 1354 | int width = image.getWidth(null); |
… |
… |
public class ImageProvider {
|
1360 | 1364 | if (bestCursorSize.width != image.getWidth(null) || bestCursorSize.height != image.getHeight(null)) { |
1361 | 1365 | image = image.getScaledInstance(bestCursorSize.width, bestCursorSize.height, Image.SCALE_DEFAULT); |
1362 | 1366 | } |
1363 | | |
1364 | | hotSpot.x = hotSpot.x * bestCursorSize.width / width; |
1365 | | hotSpot.y = hotSpot.y * bestCursorSize.height / height; |
1366 | 1367 | } |
1367 | 1368 | |
| 1369 | hotSpot.x = hotSpot.x * image.getWidth(null) / CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO; |
| 1370 | hotSpot.y = hotSpot.y * image.getHeight(null) / CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO; |
| 1371 | |
1368 | 1372 | return image; |
1369 | 1373 | } |
1370 | 1374 | |
diff --git a/test/unit/org/openstreetmap/josm/tools/ImageProviderTest.java b/test/unit/org/openstreetmap/josm/tools/ImageProviderTest.java
index bdf0ae0e8..6e53122bd 100644
a
|
b
|
public class ImageProviderTest {
|
193 | 193 | } |
194 | 194 | } |
195 | 195 | |
196 | | public static final int ORIGINAL_CURSOR_SIZE = 32; |
197 | | |
198 | 196 | /** |
199 | 197 | * Test getting an image for a crosshair cursor. |
200 | 198 | */ |
… |
… |
public class ImageProviderTest {
|
214 | 212 | */ |
215 | 213 | @Test |
216 | 214 | public void testGetCursorImageWithOverlay() { |
| 215 | testCursorImageWithOverlay(1.0f); // normal case |
| 216 | testCursorImageWithOverlay(1.5f); // user has configured a GUI scale of 1.5 in the JOSM advanced preferences |
| 217 | } |
| 218 | |
| 219 | private void testCursorImageWithOverlay(float guiScale) { |
217 | 220 | if (GraphicsEnvironment.isHeadless()) { |
218 | 221 | // TODO mock Toolkit.getDefaultToolkit().getBestCursorSize() |
219 | 222 | return; |
220 | 223 | } |
| 224 | GuiSizesHelper.setPixelDensity(guiScale); |
221 | 225 | Point hotSpot = new Point(); |
222 | 226 | Image image = ImageProvider.getCursorImage("normal", "selection", hotSpot); |
223 | 227 | assertCursorDimensionsCorrect(new Point.Double(3.0, 2.0), image, hotSpot); |
… |
… |
public class ImageProviderTest {
|
236 | 240 | } |
237 | 241 | |
238 | 242 | private void assertCursorDimensionsCorrect(Point.Double originalHotspot, Image image, Point hotSpot) { |
239 | | Dimension bestCursorSize = Toolkit.getDefaultToolkit().getBestCursorSize(ORIGINAL_CURSOR_SIZE, ORIGINAL_CURSOR_SIZE); |
| 243 | int originalCursorSize = ImageProvider.CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO; |
| 244 | Dimension bestCursorSize = Toolkit.getDefaultToolkit().getBestCursorSize(originalCursorSize, originalCursorSize); |
240 | 245 | Image bestCursorImage = HiDPISupport.getResolutionVariant(image, bestCursorSize.width, bestCursorSize.height); |
241 | 246 | int bestCursorImageWidth = bestCursorImage.getWidth(null); |
242 | 247 | assertEquals((int) Math.round(bestCursorSize.getWidth()), bestCursorImageWidth); |
243 | 248 | int bestCursorImageHeight = bestCursorImage.getHeight(null); |
244 | 249 | assertEquals((int) Math.round(bestCursorSize.getHeight()), bestCursorImageHeight); |
245 | | assertEquals(originalHotspot.x / ORIGINAL_CURSOR_SIZE * bestCursorImageWidth, hotSpot.x, 1 /* at worst one pixel off */); |
246 | | assertEquals(originalHotspot.y / ORIGINAL_CURSOR_SIZE * bestCursorImageHeight, hotSpot.y, 1 /* at worst one pixel off */); |
| 250 | assertEquals(originalHotspot.x / originalCursorSize * bestCursorImageWidth, hotSpot.x, 1 /* at worst one pixel off */); |
| 251 | assertEquals(originalHotspot.y / originalCursorSize * bestCursorImageHeight, hotSpot.y, 1 /* at worst one pixel off */); |
247 | 252 | } |
248 | 253 | |
249 | 254 | |