float[] innerTL = getInnerObjectFromObjectPosition(0, 0);
float[] innerTR = getInnerObjectFromObjectPosition(getWidth(), 0);
float[] innerBR = getInnerObjectFromObjectPosition(getWidth(), getHeight());
float[] innerBL = getInnerObjectFromObjectPosition(0, getHeight());
Coordinate coordTL = getCoordinateFromInnerPosition(innerTL[0], innerTL[1]).zoomTo(zoomLevel);
Coordinate coordTR = getCoordinateFromInnerPosition(innerTR[0], innerTR[1]).zoomTo(zoomLevel);
Coordinate coordBR = getCoordinateFromInnerPosition(innerBR[0], innerBR[1]).zoomTo(zoomLevel);
Coordinate coordBL = getCoordinateFromInnerPosition(innerBL[0], innerBL[1]).zoomTo(zoomLevel);
minCol = (int) PApplet.min(new float[] { coordTL.column, coordTR.column, coordBR.column, coordBL.column });
maxCol = (int) PApplet.max(new float[] { coordTL.column, coordTR.column, coordBR.column, coordBL.column });
minRow = (int) PApplet.min(new float[] { coordTL.row, coordTR.row, coordBR.row, coordBL.row });
maxRow = (int) PApplet.max(new float[] { coordTL.row, coordTR.row, coordBR.row, coordBL.row });
}
// Add tile padding (to pre-load, and because we might be zooming out between zoom levels)
minCol -= grid_padding;
minRow -= grid_padding;
maxCol += grid_padding;
maxRow += grid_padding;
// log.debug("getVisibleKeys: " + minCol + "," + maxCol + "; " + minRow + "," + maxRow);
// PApplet.println("getVisibleKeys: " + minCol + "," + maxCol + "; " + minRow + "," + maxRow);
// we don't wrap around the world yet, so:
int numberTiles = (int) UnfoldingMap.getScaleFromZoom(zoomLevel);
minCol = PApplet.constrain(minCol, 0, numberTiles);
maxCol = PApplet.constrain(maxCol, 0, numberTiles);
minRow = PApplet.constrain(minRow, 0, numberTiles);
maxRow = PApplet.constrain(maxRow, 0, numberTiles);
// grab coords for visible tiles
for (int col = minCol; col <= maxCol; col++) {
for (int row = minRow; row <= maxRow; row++) {
// source coordinate wraps around the world:
Coordinate coord = provider.sourceCoordinate(new Coordinate(row, col, zoomLevel));
// make sure we still have ints:
coord.roundValues();
// keep this for later:
visibleKeys.add(coord);
if (!images.containsKey(coord)) {
// fetch it if we don't have it
grabTile(coord);
// see if we have a parent coord for this tile?
boolean gotParent = false;
for (int i = (int) coord.zoom; i > 0; i--) {
Coordinate zoomed = coord.zoomTo(i).container();
zoomed.roundValues();
if (images.containsKey(zoomed)) {
visibleKeys.add(zoomed);
gotParent = true;
break;
}
}
// or if we have any of the children
if (!gotParent) {
Coordinate zoomed = coord.zoomBy(1).container();
Coordinate[] kids = { zoomed, zoomed.right(), zoomed.down(), zoomed.right().down() };
for (int i = 0; i < kids.length; i++) {
zoomed = kids[i];
// make sure we still have ints:
zoomed.row = PApplet.round(zoomed.row);
zoomed.column = PApplet.round(zoomed.column);
zoomed.zoom = PApplet.round(zoomed.zoom);
if (images.containsKey(zoomed)) {
visibleKeys.add(zoomed);
}
}
}
}
} // rows
} // columns
// sort by zoom so we draw small zoom levels (big tiles) first:
Collections.sort(visibleKeys, zoomComparator);
// stop fetching things we can't see:
// (visibleKeys also has the parents and children, if needed, but that
// shouldn't matter)
queue.retainAll(visibleKeys);
// sort what's left by distance from center:
queueSorter.setCenter(new Coordinate((minRow + maxRow) / 2.0f, (minCol + maxCol) / 2.0f, zoomLevel));
Collections.sort(queue, queueSorter);
// load up to 4 more things:
processQueue();