More wrapping fixes

This commit is contained in:
drdev
2014-05-24 19:05:43 +00:00
parent 1ebd0628e9
commit 354ebfa7d9
2 changed files with 77 additions and 25 deletions

View File

@@ -109,6 +109,8 @@ public class TextRenderer {
atReminderTextEnd = false; atReminderTextEnd = false;
ch = fullText.charAt(i); ch = fullText.charAt(i);
switch (ch) { switch (ch) {
case '\r':
continue; //skip '\r' character
case '\n': case '\n':
if (inSymbolCount > 0) { if (inSymbolCount > 0) {
inSymbolCount = 0; inSymbolCount = 0;
@@ -257,26 +259,18 @@ public class TextRenderer {
if (inSymbolCount == 0) { if (inSymbolCount == 0) {
pieceWidth = bitmapFont.getBounds(text).width; pieceWidth = bitmapFont.getBounds(text).width;
if (x + pieceWidth > width) { //wrap or shrink if needed if (x + pieceWidth > width) { //wrap or shrink if needed
if (wrap && (lastSpaceIdx >= 0 || consecutiveSymbols > 0 || inReminderTextCount > 0)) { if (wrap && (lastSpaceIdx >= 0 || consecutiveSymbols > 0)) {
if (lastSpaceIdx < 0) { if (lastSpaceIdx < 0) {
if (consecutiveSymbols > 0) { //no space between symbols and end of line, wrap those symbols along with text
//no space between symbols and end of line, wrap those symbols along with text x = 0;
x = 0; int startSymbolIdx = pieces.size() - consecutiveSymbols;
int startSymbolIdx = pieces.size() - consecutiveSymbols; lineWidths.add(pieces.get(startSymbolIdx).x);
lineWidths.add(pieces.get(startSymbolIdx).x); for (int j = startSymbolIdx; j < pieces.size(); j++) {
for (int j = startSymbolIdx; j < pieces.size(); j++) { Piece piece = pieces.get(j);
Piece piece = pieces.get(j); piece.x = x;
piece.x = x; piece.y += lineHeight;
piece.y += lineHeight; piece.lineNum++;
piece.lineNum++; x += piece.w;
x += piece.w;
}
}
else {
//no space between start of reminder text and end of line, wrap all reminder text thus far
Piece lastPiece = pieces.get(pieces.size() - 1);
lineWidths.add(lastPiece.x + lastPiece.w);
x = 0;
} }
} }
else { else {
@@ -307,12 +301,70 @@ public class TextRenderer {
needClip = true; needClip = true;
} }
} }
else if (font.getSize() > FSkinFont.MIN_FONT_SIZE) { else if (x > 0 && pieceWidth <= width) {
//try next font size down if out of space //if current piece starting past beginning of line and no spaces found,
updatePieces(FSkinFont.get(font.getSize() - 1)); //wrap current piece being built up along with part of previous pieces as needed
return; int lastPieceIdx;
for (lastPieceIdx = pieces.size() - 1; lastPieceIdx >= 0; lastPieceIdx--) {
Piece lastPiece = pieces.get(lastPieceIdx);
if (lastPiece.lineNum < lineNum) {
lastPieceIdx = pieces.size() - 1; //don't re-wrap anything if reached previous line
break;
}
if (lastPiece instanceof TextPiece) {
TextPiece textPiece = (TextPiece)lastPiece;
int index = textPiece.text.lastIndexOf(' ');
if (index != -1) {
if (index == 0) {
textPiece.text = textPiece.text.substring(1);
textPiece.w = bitmapFont.getBounds(textPiece.text).width;
lastPieceIdx--;
}
else if (index == textPiece.text.length() - 1) {
textPiece.text = textPiece.text.substring(0, textPiece.text.length() - 1);
textPiece.w = bitmapFont.getBounds(textPiece.text).width;
}
else {
TextPiece splitPiece = new TextPiece(textPiece.text.substring(index + 1), textPiece.inReminderText);
textPiece.text = textPiece.text.substring(0, index);
textPiece.w = bitmapFont.getBounds(textPiece.text).width;
splitPiece.x = textPiece.x + textPiece.w;
splitPiece.y = textPiece.y;
splitPiece.w = bitmapFont.getBounds(splitPiece.text).width;
splitPiece.h = textPiece.h;
}
break;
}
}
}
Piece lastPiece = pieces.get(lastPieceIdx);
lineWidths.add(lastPiece.x + lastPiece.w);
x = 0;
for (int j = lastPieceIdx + 1; j < pieces.size(); j++) {
Piece piece = pieces.get(j);
piece.x = x;
piece.y += lineHeight;
piece.lineNum++;
x += piece.w;
}
y += lineHeight;
totalHeight += lineHeight;
lineNum++;
if (totalHeight > height) {
//try next font size down if out of space
if (font.getSize() > FSkinFont.MIN_FONT_SIZE) {
updatePieces(FSkinFont.get(font.getSize() - 1));
return;
}
needClip = true;
}
} }
else { else {
if (font.getSize() > FSkinFont.MIN_FONT_SIZE) {
//try next font size down if out of space
updatePieces(FSkinFont.get(font.getSize() - 1));
return;
}
needClip = true; needClip = true;
} }
} }
@@ -440,7 +492,7 @@ public class TextRenderer {
} }
private class TextPiece extends Piece { private class TextPiece extends Piece {
private final String text; private String text;
private TextPiece(String text0, boolean inReminderText0) { private TextPiece(String text0, boolean inReminderText0) {
super(inReminderText0); super(inReminderText0);

View File

@@ -39,7 +39,7 @@ public class ViewWinLose extends FOverlay {
super(FSkinColor.get(Colors.CLR_OVERLAY).alphaColor(0.75f)); super(FSkinColor.get(Colors.CLR_OVERLAY).alphaColor(0.75f));
game = game0; game = game0;
lblTitle = add(new FLabel.Builder().fontSize(30).align(HAlignment.CENTER).build()); lblTitle = add(new FLabel.Builder().fontSize(30).align(HAlignment.CENTER).build());
lblStats = add(new FLabel.Builder().fontSize(26).align(HAlignment.CENTER).build()); lblStats = add(new FLabel.Builder().fontSize(26).align(HAlignment.CENTER).build());
pnlOutcomes = add(new OutcomesPanel()); pnlOutcomes = add(new OutcomesPanel());