Support auto-shrinking font to fit

This commit is contained in:
drdev
2014-03-15 20:44:49 +00:00
parent b06d7bc3cc
commit eee31f1fd4
3 changed files with 25 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
@@ -571,25 +572,35 @@ public class Forge implements ApplicationListener {
}
public void drawText(String text, FSkinFont skinFont, FSkinColor skinColor, float x, float y, float w, float h, boolean wrap, HAlignment horzAlignment, boolean centerVertically) {
drawText(text, skinFont.getFont(), skinColor.getColor(), x, y, w, h, wrap, horzAlignment, centerVertically);
drawText(text, skinFont, skinColor.getColor(), x, y, w, h, wrap, horzAlignment, centerVertically);
}
public void drawText(String text, FSkinFont skinFont, Color color, float x, float y, float w, float h, boolean wrap, HAlignment horzAlignment, boolean centerVertically) {
drawText(text, skinFont.getFont(), color, x, y, w, h, wrap, horzAlignment, centerVertically);
}
public void drawText(String text, BitmapFont font, Color color, float x, float y, float w, float h, boolean wrap, HAlignment horzAlignment, boolean centerVertically) {
font.setColor(color);
int fontSize = skinFont.getSize();
BitmapFont font = skinFont.getFont();
if (wrap) {
float textHeight = font.getWrappedBounds(text, w).height;
TextBounds bounds = font.getWrappedBounds(text, w);
while ((bounds.width > w || bounds.height > h) && fontSize > FSkinFont.MIN_FONT_SIZE) {
font = FSkinFont.get(--fontSize).getFont(); //shrink font to fit if possible
bounds = font.getWrappedBounds(text, w);
}
float textHeight = bounds.height;
if (h > textHeight && centerVertically) {
y += (h - textHeight) / 2;
}
font.setColor(color);
font.drawWrapped(batch, text, adjustX(x), adjustY(y, 0), w, horzAlignment);
}
else {
float textHeight = font.getMultiLineBounds(text).height;
TextBounds bounds = font.getMultiLineBounds(text);
while ((bounds.width > w || bounds.height > h) && fontSize > FSkinFont.MIN_FONT_SIZE) {
font = FSkinFont.get(--fontSize).getFont(); //shrink font to fit if possible
bounds = font.getMultiLineBounds(text);
}
float textHeight = bounds.height;
if (h > textHeight && centerVertically) {
y += (h - textHeight) / 2;
}
font.setColor(color);
font.drawMultiLine(batch, text, adjustX(x), adjustY(y, 0), w, horzAlignment);
}
}

View File

@@ -9,6 +9,8 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
public class FSkinFont {
public static final int MIN_FONT_SIZE = 8;
private static final String TTF_FILE = "font1.ttf";
private static final int defaultFontSize = 12;
private static final Map<Integer, FSkinFont> fonts = new HashMap<Integer, FSkinFont>();

View File

@@ -152,9 +152,10 @@ public class VPlayerPanel extends FContainer {
y = height - VAvatar.HEIGHT;
avatar.setPosition(0, y);
float lifeLabelWidth = LIFE_FONT.getFont().getBounds("99").width * 1.2f; //make just wide enough for 2-digit life totals
float infoLabelHeight = VAvatar.HEIGHT - VPhaseIndicator.HEIGHT;
lblLife.setBounds(x, y, lblLife.getWidth(), infoLabelHeight);
x += lblLife.getWidth();
lblLife.setBounds(x, y, lifeLabelWidth, infoLabelHeight);
x += lifeLabelWidth;
float infoTabWidth = (getWidth() - x) / tabs.size();
for (InfoTab tab : tabs) {
@@ -192,7 +193,6 @@ public class VPlayerPanel extends FContainer {
private String life = "20";
private LifeLabel() {
setWidth(LIFE_FONT.getFont().getBounds("99").width * 1.2f); //make just wide enough for 2-digit life totals
}
private void update() {
@@ -268,8 +268,8 @@ public class VPlayerPanel extends FContainer {
y = (getHeight() - h) / 2;
g.drawImage(icon, x, y, w, h);
x += w * 1.05f;
g.drawText(value, INFO_FONT, INFO_FORE_COLOR, x, 0, getWidth() - x, getHeight(), false, HAlignment.LEFT, true);
x += w * 1.1f;
g.drawText(value, INFO_FONT, INFO_FORE_COLOR, x, 0, getWidth() - x + 1, getHeight(), false, HAlignment.LEFT, true);
}
}
}