v0.8.1: added functionality to center or right align text blocks

This commit is contained in:
Evan Debenham 2020-05-21 16:14:40 -04:00
parent 5987a43150
commit a1e35263d3
2 changed files with 38 additions and 1 deletions

View File

@ -48,6 +48,11 @@ public class RenderedTextBlock extends Component {
private int hightlightColor = Window.TITLE_COLOR; private int hightlightColor = Window.TITLE_COLOR;
private boolean highlightingEnabled = true; 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){ public RenderedTextBlock(int size){
this.size = size; this.size = size;
} }
@ -175,6 +180,11 @@ public class RenderedTextBlock extends Component {
} }
} }
public synchronized void align(int align){
alignment = align;
layout();
}
@Override @Override
protected synchronized void layout() { protected synchronized void layout() {
super.layout(); super.layout();
@ -183,6 +193,10 @@ public class RenderedTextBlock extends Component {
float height = 0; float height = 0;
nLines = 1; nLines = 1;
ArrayList<ArrayList<RenderedText>> lines = new ArrayList<>();
ArrayList<RenderedText> curLine = new ArrayList<>();
lines.add(curLine);
width = 0; width = 0;
for (RenderedText word : words){ for (RenderedText word : words){
if (word == SPACE){ if (word == SPACE){
@ -192,6 +206,8 @@ public class RenderedTextBlock extends Component {
y += height+2f; y += height+2f;
x = this.x; x = this.x;
nLines++; nLines++;
curLine = new ArrayList<>();
lines.add(curLine);
} else { } else {
if (word.height() > height) height = word.height(); if (word.height() > height) height = word.height();
@ -199,12 +215,15 @@ public class RenderedTextBlock extends Component {
y += height+2f; y += height+2f;
x = this.x; x = this.x;
nLines++; nLines++;
curLine = new ArrayList<>();
lines.add(curLine);
} }
word.x = x; word.x = x;
word.y = y; word.y = y;
PixelScene.align(word); PixelScene.align(word);
x += word.width(); x += word.width();
curLine.add(word);
if ((x - this.x) > width) width = (x - this.x); if ((x - this.x) > width) width = (x - this.x);
@ -215,5 +234,23 @@ public class RenderedTextBlock extends Component {
} }
} }
this.height = (y - this.y) + height; this.height = (y - this.y) + height;
if (alignment != LEFT_ALIGN){
for (ArrayList<RenderedText> 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);
}
}
}
}
} }
} }

View File

@ -41,9 +41,9 @@ public class WndBadge extends Window {
icon.scale.set( 2 ); icon.scale.set( 2 );
add( icon ); add( icon );
//TODO: this used to be centered, should probably figure that out.
RenderedTextBlock info = PixelScene.renderTextBlock( badge.desc(), 8 ); RenderedTextBlock info = PixelScene.renderTextBlock( badge.desc(), 8 );
info.maxWidth(WIDTH - MARGIN * 2); info.maxWidth(WIDTH - MARGIN * 2);
info.align(RenderedTextBlock.CENTER_ALIGN);
PixelScene.align(info); PixelScene.align(info);
add(info); add(info);