Fix screen text align

- Better calculation of center with horizontal
  and vertical positions.
- Fixes #853
This commit is contained in:
Janne Valkealahti
2023-08-19 08:19:07 +03:00
parent 7391d0d24f
commit 8363c953ed
2 changed files with 11 additions and 4 deletions

View File

@@ -338,7 +338,7 @@ public class DefaultScreen implements Screen, DisplayLines {
public void text(String text, Rectangle rect, HorizontalAlign hAlign, VerticalAlign vAlign) {
int x = rect.x();
if (hAlign == HorizontalAlign.CENTER) {
x = (x + rect.width()) / 2;
x = x + rect.width() / 2;
x = x - text.length() / 2;
}
else if (hAlign == HorizontalAlign.RIGHT) {
@@ -346,7 +346,7 @@ public class DefaultScreen implements Screen, DisplayLines {
}
int y = rect.y();
if (vAlign == VerticalAlign.CENTER) {
y = (y + rect.height()) / 2;
y = y + rect.height() / 2;
}
else if (vAlign == VerticalAlign.BOTTOM) {
y = y + rect.height() - 1;

View File

@@ -76,14 +76,21 @@ class ScreenTests extends AbstractViewTests {
void printsTextAlign() {
Rectangle rect = new Rectangle(1, 1, 10, 10);
screen24x80.writerBuilder().build().text("text", rect, HorizontalAlign.CENTER, VerticalAlign.CENTER);
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 3, 5, 4);
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 4, 6, 4);
}
@Test
void printsTextAlignInOneRowRect() {
Rectangle rect = new Rectangle(1, 1, 10, 1);
screen24x80.writerBuilder().build().text("text", rect, HorizontalAlign.CENTER, VerticalAlign.CENTER);
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 3, 1, 4);
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 4, 1, 4);
}
@Test
void printsTextAlignInSmallRect() {
Rectangle rect = new Rectangle(10, 14, 10, 3);
screen24x80.writerBuilder().build().text("text", rect, HorizontalAlign.CENTER, VerticalAlign.CENTER);
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 13, 15, 4);
}
@Test