diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/RenderedTextBlock.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/RenderedTextBlock.java index f7bf4ea3c..dfa524f41 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/RenderedTextBlock.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/RenderedTextBlock.java @@ -47,6 +47,11 @@ public class RenderedTextBlock extends Component { private int hightlightColor = Window.TITLE_COLOR; private boolean highlightingEnabled = true; + + public static final int LEFT_ALIGN = 1; + public static final int CENTER_ALIGN = 2; + public static final int RIGHT_ALIGN = 3; + private int alignment = LEFT_ALIGN; public RenderedTextBlock(int size){ this.size = size; @@ -175,6 +180,11 @@ public class RenderedTextBlock extends Component { } } + public synchronized void align(int align){ + alignment = align; + layout(); + } + @Override protected synchronized void layout() { super.layout(); @@ -183,6 +193,10 @@ public class RenderedTextBlock extends Component { float height = 0; nLines = 1; + ArrayList> lines = new ArrayList<>(); + ArrayList curLine = new ArrayList<>(); + lines.add(curLine); + width = 0; for (RenderedText word : words){ if (word == SPACE){ @@ -192,6 +206,8 @@ public class RenderedTextBlock extends Component { y += height+2f; x = this.x; nLines++; + curLine = new ArrayList<>(); + lines.add(curLine); } else { if (word.height() > height) height = word.height(); @@ -199,12 +215,15 @@ public class RenderedTextBlock extends Component { y += height+2f; x = this.x; nLines++; + curLine = new ArrayList<>(); + lines.add(curLine); } word.x = x; word.y = y; PixelScene.align(word); x += word.width(); + curLine.add(word); if ((x - this.x) > width) width = (x - this.x); @@ -215,5 +234,23 @@ public class RenderedTextBlock extends Component { } } this.height = (y - this.y) + height; + + if (alignment != LEFT_ALIGN){ + for (ArrayList line : lines){ + if (line.size() == 0) continue; + float lineWidth = line.get(line.size()-1).width() + line.get(line.size()-1).x - this.x; + if (alignment == CENTER_ALIGN){ + for (RenderedText text : line){ + text.x += (width() - lineWidth)/2f; + PixelScene.align(text); + } + } else if (alignment == RIGHT_ALIGN) { + for (RenderedText text : line){ + text.x += width() - lineWidth; + PixelScene.align(text); + } + } + } + } } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBadge.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBadge.java index a21c081eb..26d5d20bb 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBadge.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBadge.java @@ -41,9 +41,9 @@ public class WndBadge extends Window { icon.scale.set( 2 ); add( icon ); - //TODO: this used to be centered, should probably figure that out. RenderedTextBlock info = PixelScene.renderTextBlock( badge.desc(), 8 ); info.maxWidth(WIDTH - MARGIN * 2); + info.align(RenderedTextBlock.CENTER_ALIGN); PixelScene.align(info); add(info);