From eee31f1fd43d62b1cbc2c94e068fa2663a43ea1a Mon Sep 17 00:00:00 2001 From: drdev Date: Sat, 15 Mar 2014 20:44:49 +0000 Subject: [PATCH] Support auto-shrinking font to fit --- forge-m-base/src/forge/Forge.java | 25 +++++++++++++------ forge-m-base/src/forge/assets/FSkinFont.java | 2 ++ .../screens/match/views/VPlayerPanel.java | 10 ++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/forge-m-base/src/forge/Forge.java b/forge-m-base/src/forge/Forge.java index 86f90fcdbc2..969f94ec5ba 100644 --- a/forge-m-base/src/forge/Forge.java +++ b/forge-m-base/src/forge/Forge.java @@ -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); } } diff --git a/forge-m-base/src/forge/assets/FSkinFont.java b/forge-m-base/src/forge/assets/FSkinFont.java index 89607eb4953..2f7912237a9 100644 --- a/forge-m-base/src/forge/assets/FSkinFont.java +++ b/forge-m-base/src/forge/assets/FSkinFont.java @@ -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 fonts = new HashMap(); diff --git a/forge-m-base/src/forge/screens/match/views/VPlayerPanel.java b/forge-m-base/src/forge/screens/match/views/VPlayerPanel.java index 63aff8898ed..0c9ab78c93d 100644 --- a/forge-m-base/src/forge/screens/match/views/VPlayerPanel.java +++ b/forge-m-base/src/forge/screens/match/views/VPlayerPanel.java @@ -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); } } }