Fix screen text align
- Better calculation of center with horizontal and vertical positions. - Fixes #853
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user