This patch adds a #toColorCode(ColorRGBA) and #fromColorCode(String) methods to convert to and from colors to display in the BitmapText object. I forget when they added it but BitmapText’s can be different colors like a tooltip in your favorite game.
[java]Index: src/tonegod/gui/core/utils/BitmapTextUtil.java
— src/tonegod/gui/core/utils/BitmapTextUtil.java (revision 448)
+++ src/tonegod/gui/core/utils/BitmapTextUtil.java (working copy)
@@ -2,11 +2,13 @@
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package tonegod.gui.core.utils;
import com.jme3.font.BitmapText;
import com.jme3.font.LineWrapMode;
import com.jme3.font.Rectangle;
+import com.jme3.math.ColorRGBA;
import tonegod.gui.core.Element;
/**
@@ -13,70 +15,129 @@
*
-
@author t0neg0d
*/
-public class BitmapTextUtil {
- /**
-
- Returns the width of the provided text
-
- @param ref Element The element the text will be added to (reference for font settings)
-
- @param text String the text to be evaluated
-
- @return float The width
- */
- public static float getTextWidth(Element ref, String text) {
-
BitmapText eval = new BitmapText(ref.getFont());
-
eval.setSize(ref.getFontSize());
-
eval.setLineWrapMode(LineWrapMode.NoWrap);
-
eval.setBox(null);
-
eval.setText(text);
-
return eval.getLineWidth();
+public class BitmapTextUtil
+{
+
- /**
-
* Returns the width of the provided text
-
* <p/>
-
* @param ref Element The element the text will be added to (reference for font settings)
-
* @param text String the text to be evaluated
-
* @return float The width
-
*/
- public static float getTextWidth(Element ref, String text)
- {
- BitmapText eval = new BitmapText(ref.getFont());
- eval.setSize(ref.getFontSize());
- eval.setLineWrapMode(LineWrapMode.NoWrap);
- eval.setBox(null);
- eval.setText(text);
- return eval.getLineWidth();
- }
- /**
-
* Returns the width of the provided text or the maxwidth, which ever is less
-
* <p/>
-
* @param ref Element The element the text will be added to (reference for font settings)
-
* @param text String the text to be evaluated
-
* @param maxWidth The maximum width considered a valid return value
-
* @return float The width
-
*/
- public static float getTextWidth(Element ref, String text, float maxWidth)
- {
- BitmapText eval = new BitmapText(ref.getFont());
- eval.setSize(ref.getFontSize());
- eval.setText(“Xg”);
- eval.setText(text);
- return (eval.getLineWidth() < maxWidth) ? eval.getLineWidth() : maxWidth;
- }
- /**
-
* Returns the height value of a single line of text
-
* <p/>
-
* @param ref Element The element the text will be added to (reference for font settings)
-
* @param text String the text to be evaluated
-
* @return float
-
*/
- public static float getTextLineHeight(Element ref, String text)
- {
- BitmapText eval = new BitmapText(ref.getFont());
- eval.setSize(ref.getFontSize());
- eval.setLineWrapMode(LineWrapMode.NoWrap);
- eval.setBox(null);
- eval.setText(text);
- return eval.getLineHeight();
- }
- /**
-
* Returns the total height of a wrapped text string
-
* <p/>
-
* @param ref Element The element the text will be added to (reference for font settings)
-
* @param text String the text to be evaluated
-
* @param maxWidth The maximum width considered a valid return value
-
* @return float
-
*/
- public static float getTextTotalHeight(Element ref, String text, float maxWidth)
- {
- BitmapText eval = new BitmapText(ref.getFont());
- eval.setSize(ref.getFontSize());
- eval.setText(“Xg”);
- eval.setBox(new Rectangle(0, 0, maxWidth, eval.getLineHeight()));
- eval.setText(text);
- return eval.getLineWidth() * eval.getLineCount();
- }
- /**
-
* Example usage:
-
* <br />
-
* {@code
-
* element.setText("Hi! " + toColorCode(ColorRGBA.Blue) + "PC. " + "Do you like " + toColorCode(ColorRGBA.Pink) + "Pink?");}
-
* <p/>
-
* @param color
-
* @return
-
*/
- public static String toColorCode(ColorRGBA color)
- {
- return String.format("\#%02x%02x%02x#", (int) color.r * 255, (int) color.g * 255, (int) color.b * 255);
- }
- /**
-
* Example usage:
-
* <br />
-
* {@code
-
* element.setText("Hi! " + toColorText(ColorRGBA.Blue) + "PC. " + "Do you like " + toColorText(ColorRGBA.Pink) + "Pink?");}
-
* <p/>
-
* @param color
-
* @return
-
*/
- public static ColorRGBA fromColorCode(String color)
- {
- int idxOf = color.indexOf(’#’), lastIdxOf = color.lastIndexOf(’#’);
- if (idxOf == -1 || lastIdxOf == -1)
- {
-
}return ColorRGBA.Pink; // :evil grin:
- /**
-
- Returns the width of the provided text or the maxwidth, which ever is less
-
- @param ref Element The element the text will be added to (reference for font settings)
-
- @param text String the text to be evaluated
-
- @param maxWidth The maximum width considered a valid return value
-
- @return float The width
- */
- public static float getTextWidth(Element ref, String text, float maxWidth) {
-
BitmapText eval = new BitmapText(ref.getFont());
-
eval.setSize(ref.getFontSize());
-
eval.setText("Xg");
-
eval.setText(text);
-
return (eval.getLineWidth() < maxWidth) ? eval.getLineWidth() : maxWidth;
- String colorText = color.substring(idxOf, lastIdxOf);
- if (colorText != null)
- {
-
try
-
{
-
float r = Float.parseFloat(colorText.substring(0, 2));
-
float g = Float.parseFloat(colorText.substring(2, 4));
-
float b = Float.parseFloat(colorText.substring(4, 6));
-
return new ColorRGBA(r, g, b, 1f);
-
} catch (Exception ex)
-
{
-
return ColorRGBA.Pink; // :evil grin:
-
}
- } else
- {
-
}return ColorRGBA.Pink; // :evil grin:
- /**
-
- Returns the height value of a single line of text
-
- @param ref Element The element the text will be added to (reference for font settings)
-
- @param text String the text to be evaluated
-
- @return float
- */
- public static float getTextLineHeight(Element ref, String text) {
-
BitmapText eval = new BitmapText(ref.getFont());
-
eval.setSize(ref.getFontSize());
-
eval.setLineWrapMode(LineWrapMode.NoWrap);
-
eval.setBox(null);
-
eval.setText(text);
-
return eval.getLineHeight();
- }
- /**
-
- Returns the total height of a wrapped text string
-
- @param ref Element The element the text will be added to (reference for font settings)
-
- @param text String the text to be evaluated
-
- @param maxWidth The maximum width considered a valid return value
-
- @return float
- */
- public static float getTextTotalHeight(Element ref, String text, float maxWidth) {
-
BitmapText eval = new BitmapText(ref.getFont());
-
eval.setSize(ref.getFontSize());
-
eval.setText("Xg");
-
eval.setBox(new Rectangle(0,0,maxWidth, eval.getLineHeight()));
-
eval.setText(text);
-
return eval.getLineWidth()*eval.getLineCount();
- }
- }
}
[/java]