if (width == PixelLocation.REFERENCE_SIZE.x() && height == PixelLocation.REFERENCE_SIZE.y()) {
// The screen size is exactly what our reference pixels are based on, so
// we can use their coordinates directly
result = new HashMap<>();
for (PixelLocation pixelLocation : PixelLocation.values()) {
Coordinate coordinate = new Coordinate(pixelLocation.x(), pixelLocation.y());
log.debug("Stored position of {} as {}", pixelLocation, coordinate);
result.put(pixelLocation, coordinate);
}
} else {
// The screen size is different to our reference pixels, so coordinates
// need to be adjusted
float ratioX = (float) width / (float) PixelLocation.REFERENCE_SIZE.x();
float ratioY = (float) height / (float) PixelLocation.REFERENCE_SIZE.y();
// ratioY is normally the correct ratio to use, but occasionally ratioX is
// smaller (usually during screen resizing?)
float ratio = Math.min(ratioX, ratioY);
float screenRatio = (float) width / (float) height;
int xOffset;
if (screenRatio > 1.4) {
xOffset = (int) (((float) width - (ratio * PixelLocation.REFERENCE_SIZE.x())) / 2);
} else {
xOffset = 0;
}
log.debug("ratio={} screenRatio={}, xOffset={}", ratio, screenRatio, xOffset);
result = new HashMap<>();
for (PixelLocation pixelLocation : PixelLocation.values()) {
int x = (int) (pixelLocation.x() * ratio) + xOffset;
int y = (int) (pixelLocation.y() * ratio);
Coordinate coordinate = new Coordinate(x, y);
log.debug("Calculated position of {} as {}", pixelLocation, coordinate);
result.put(pixelLocation, coordinate);
}
}