Refactor TextRenderer a bit.

This commit is contained in:
Manuel Friedli 2020-09-30 23:31:53 +02:00
parent 57d9d8c56b
commit cbada5bd2c

View file

@ -11,8 +11,11 @@ public class TextRenderer {
private final Labyrinth labyrinth; private final Labyrinth labyrinth;
private final int width; private final int width;
private final int height; private final int height;
// column counter
private int x = 0; private int x = 0;
// row counter
private int y = 0; private int y = 0;
// line counter (top-, center- or bottom line of a row)
private int line = 0; private int line = 0;
private TextRenderer(@NonNull final Labyrinth labyrinth, final int width, final int height) { private TextRenderer(@NonNull final Labyrinth labyrinth, final int width, final int height) {
@ -23,10 +26,10 @@ public class TextRenderer {
public static String render(@NonNull final Labyrinth labyrinth) { public static String render(@NonNull final Labyrinth labyrinth) {
final int width = labyrinth.getWidth(); final int width = labyrinth.getWidth();
if (width == 0) { final int height = labyrinth.getHeight();
if (width == 0 || height == 0) {
return ""; return "";
} }
final int height = labyrinth.getHeight();
final TextRenderer renderer = new TextRenderer(labyrinth, width, height); final TextRenderer renderer = new TextRenderer(labyrinth, width, height);
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
@ -40,13 +43,6 @@ public class TextRenderer {
return this.y < this.height; return this.y < this.height;
} }
private Tile getTileOrNull(final int x, final int y) {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
return null;
}
return this.labyrinth.getTileAt(x, y);
}
private String next() { private String next() {
final Tile currentTile = this.labyrinth.getTileAt(this.x, this.y); final Tile currentTile = this.labyrinth.getTileAt(this.x, this.y);
final Tile leftTile = this.getTileOrNull(this.x - 1, this.y); final Tile leftTile = this.getTileOrNull(this.x - 1, this.y);
@ -66,21 +62,38 @@ public class TextRenderer {
s = ""; s = "";
break; break;
} }
this.prepareNextStep();
return s;
}
private void prepareNextStep() {
// do some magic ... // do some magic ...
this.x++; this.x++;
if (this.x == this.width) { if (this.x == this.width) {
// Reached the end of the row?
// On to the next line then!
this.x = 0; this.x = 0;
this.line++; this.line++;
} }
if (this.line == 2 && this.y < this.height - 1) { if (this.line == 2 && this.y < this.height - 1) {
// Finished rendering the center line, and more rows available?
// On to the next row then!
this.line = 0; this.line = 0;
this.y++; this.y++;
} }
if (this.line == 3) { if (this.line == 3) {
// Finished rendering the bottom line (of the last row)?
// Increment row counter one more time => this is the exit condition.
this.line = 0; this.line = 0;
this.y++; this.y++;
} }
return s; }
private Tile getTileOrNull(final int x, final int y) {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
return null;
}
return this.labyrinth.getTileAt(x, y);
} }
private String renderTopLine(@NonNull final Tile currentTile, @Nullable final Tile leftTile, @Nullable final Tile topTile) { private String renderTopLine(@NonNull final Tile currentTile, @Nullable final Tile leftTile, @Nullable final Tile topTile) {