Refactored map clearing code to make things more readable.

This commit is contained in:
badvision 2016-12-24 15:24:13 -06:00
parent fc00a1febe
commit 707bfd15cd

View File

@ -305,12 +305,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
boolean reset = tiles == null || tiles.length <= cols || tiles[0] == null || tiles[0].length <= rows; boolean reset = tiles == null || tiles.length <= cols || tiles[0] == null || tiles[0].length <= rows;
if (forceReset || reset) { if (forceReset || reset) {
tiles = new String[cols + 1][rows + 1]; tiles = new String[cols + 1][rows + 1];
for (int x = 0; x < drawCanvas.getWidth(); x += 10) { fillEmpty(0, 0, drawCanvas.getWidth(), drawCanvas.getHeight());
for (int y = 0; y < drawCanvas.getHeight(); y += 10) {
drawCanvas.getGraphicsContext2D().setFill(getFillPattern(x, y));
drawCanvas.getGraphicsContext2D().fillRect(x, y, 10, 10);
}
}
} }
} }
@ -326,16 +321,20 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
if (tile != null) { if (tile != null) {
drawCanvas.getGraphicsContext2D().drawImage(TileUtils.getImage(tile, getCurrentPlatform()), xx, yy, tileWidth, tileHeight); drawCanvas.getGraphicsContext2D().drawImage(TileUtils.getImage(tile, getCurrentPlatform()), xx, yy, tileWidth, tileHeight);
} else { } else {
for (double x1 = -(xx % 10); x1 < tileWidth; x1 += 10) { fillEmpty(xx, yy, xx + tileWidth, yy + tileHeight);
for (double y1 = (-yy % 10); y1 < tileHeight; y1 += 10) {
double boxX = Math.max(x1, 0);
double boxY = Math.max(y1, 0);
double width = Math.min(10, tileWidth - x1);
double height = Math.min(10, tileHeight - y1);
drawCanvas.getGraphicsContext2D().setFill(getFillPattern(boxX + xx, boxY + yy));
drawCanvas.getGraphicsContext2D().fillRect(boxX + xx, boxY + yy, width, height);
} }
} }
int patternSize = 10;
private void fillEmpty(double startX, double startY, double endX, double endY) {
for (double x = startX; x < endX; x = x - (x % patternSize) + patternSize) {
for (double y = startY; y < endY; y = y - (y % patternSize) + patternSize) {
double width = Math.min(patternSize, endX - x);
double height = Math.min(patternSize, endY - y);
drawCanvas.getGraphicsContext2D().setFill(getFillPattern(x, y));
drawCanvas.getGraphicsContext2D().fillRect(x, y, width, height);
}
} }
} }