Example usage for com.badlogic.gdx.graphics.g2d BitmapFont getMultiLineBounds

List of usage examples for com.badlogic.gdx.graphics.g2d BitmapFont getMultiLineBounds

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d BitmapFont getMultiLineBounds.

Prototype

public TextBounds getMultiLineBounds(CharSequence str) 

Source Link

Document

Returns the bounds of the specified text, which may contain newlines.

Usage

From source file:edu.lehigh.cse.lol.Display.java

License:Open Source License

/**
 * A helper method to draw text nicely. In GDX, we draw everything by giving
 * the bottom left corner, except text, which takes the top left corner.
 * This function handles the conversion, so that we can use bottom-left.
 *
 * @param x       The X coordinate of the bottom left corner (in pixels)
 * @param y       The Y coordinate of the bottom left corner (in pixels)
 * @param message The text to display/*from  w  w w  .j  a  v  a  2s. co m*/
 * @param bf      The BitmapFont object to use for the text's font
 * @param sb      The SpriteBatch used to render the text
 */
static void drawTextTransposed(int x, int y, String message, BitmapFont bf, SpriteBatch sb) {
    bf.drawMultiLine(sb, message, x, y + bf.getMultiLineBounds(message).height);
}

From source file:edu.lehigh.cse.lol.Util.java

License:Open Source License

/**
 * Create a Renderable that consists of some text to draw
 *
 * @param x        The X coordinate of the bottom left corner, in pixels
 * @param y        The Y coordinate of the bottom left corner, in pixels
 * @param message  The text to display... note that it can't change on the fly
 * @param red      The red component of the font color (0-255)
 * @param green    The green component of the font color (0-255)
 * @param blue     The blue component of the font color (0-255)
 * @param fontName The font to use/*w  ww .j av a  2s.co  m*/
 * @param size     The font size
 * @return A Renderable of the text
 */
public static Renderable makeText(final int x, final int y, final String message, final int red,
        final int green, final int blue, String fontName, int size) {
    final BitmapFont bf = Media.getFont(fontName, size);
    return new Renderable() {
        @Override
        public void render(SpriteBatch sb, float elapsed) {
            bf.setColor(((float) red) / 256, ((float) green) / 256, ((float) blue) / 256, 1);
            bf.drawMultiLine(sb, message, x, y + bf.getMultiLineBounds(message).height);
        }
    };
}

From source file:edu.lehigh.cse.lol.Util.java

License:Open Source License

/**
 * Create a Renderable that consists of some text to draw. The text will be
 * centered vertically and horizontally on the screen
 *
 * @param message  The text to display... note that it can't change on the fly
 * @param red      The red component of the font color (0-255)
 * @param green    The green component of the font color (0-255)
 * @param blue     The blue component of the font color (0-255)
 * @param fontName The font to use//  w w  w .j a  v a  2  s.c o m
 * @param size     The font size
 * @return A Renderable of the text
 */
public static Renderable makeText(final String message, final int red, final int green, final int blue,
        String fontName, int size) {
    final BitmapFont bf = Media.getFont(fontName, size);
    final float x = Lol.sGame.mWidth / 2 - bf.getMultiLineBounds(message).width / 2;
    final float y = Lol.sGame.mHeight / 2 + bf.getMultiLineBounds(message).height / 2;
    return new Renderable() {
        @Override
        public void render(SpriteBatch sb, float elapsed) {
            bf.setColor(((float) red) / 256, ((float) green) / 256, ((float) blue) / 256, 1);
            bf.drawMultiLine(sb, message, x, y);
        }
    };
}

From source file:edu.lehigh.cse.lol.Util.java

License:Open Source License

/**
 * Draw some text on the current level/*from w w w  .java  2s.  c  om*/
 *
 * Note: the order in which this is called relative to other actors will
 * determine whether they go under or over this text.
 *
 * @param x        X coordinate of bottom left corner of the text
 * @param y        Y coordinate of bottom left corner of the text
 * @param text     The text to display
 * @param red      The red component of the color (0-255)
 * @param green    The green component of the color (0-255)
 * @param blue     The blue component of the color (0-255)
 * @param fontName The name of the font file to use
 * @param size     The font size to use
 * @param zIndex   The z index of the image. There are 5 planes: -2, -2, 0, 1,
 *                 and 2. By default, everything goes to plane 0
 */
public static void drawText(final float x, final float y, final String text, final int red, final int green,
        final int blue, String fontName, int size, int zIndex) {
    final BitmapFont bf = Media.getFont(fontName, size);
    Renderable r = new Renderable() {
        @Override
        public void render(SpriteBatch sb, float elapsed) {
            bf.setColor(((float) red) / 256, ((float) green) / 256, ((float) blue) / 256, 1);
            bf.setScale(1 / Physics.PIXEL_METER_RATIO);
            bf.drawMultiLine(sb, text, x, y + bf.getMultiLineBounds(text).height);
            bf.setScale(1);
        }
    };
    Lol.sGame.mCurrentLevel.addActor(r, zIndex);
}

From source file:edu.lehigh.cse.lol.Util.java

License:Open Source License

/**
 * Draw some text on the current level, centered on a point.
 *
 * Note: the order in which this is called relative to other actors will
 * determine whether they go under or over this text.
 *
 * @param centerX  X coordinate of center of the text
 * @param centerY  Y coordinate of center of the text
 * @param text     The text to display/*from   w ww  .  j a  va  2s. co  m*/
 * @param red      The red component of the color (0-255)
 * @param green    The green component of the color (0-255)
 * @param blue     The blue component of the color (0-255)
 * @param fontName The name of the font file to use
 * @param size     The font size to use
 * @param zIndex   The z index of the image. There are 5 planes: -2, -2, 0, 1,
 *                 and 2. By default, everything goes to plane 0
 */
public static void drawTextCentered(final float centerX, final float centerY, final String text, final int red,
        final int green, final int blue, String fontName, int size, int zIndex) {
    final BitmapFont bf = Media.getFont(fontName, size);

    // figure out the image dimensions
    bf.setScale(1 / Physics.PIXEL_METER_RATIO);
    final float w = bf.getMultiLineBounds(text).width;
    final float h = bf.getMultiLineBounds(text).height;
    bf.setScale(1);

    // describe how to render it
    Renderable r = new Renderable() {
        @Override
        public void render(SpriteBatch sb, float elapsed) {
            bf.setColor(((float) red) / 256, ((float) green) / 256, ((float) blue) / 256, 1);
            bf.setScale(1 / Physics.PIXEL_METER_RATIO);
            bf.drawMultiLine(sb, text, centerX - w / 2, centerY + h / 2);
            bf.setScale(1);
        }
    };
    Lol.sGame.mCurrentLevel.addActor(r, zIndex);
}

From source file:org.bladecoder.bladeengine.util.TextUtils.java

License:Apache License

public static void drawCentered(SpriteBatch batch, BitmapFont font, CharSequence str, float x, float y) {
    float x2, y2;

    TextBounds b = font.getMultiLineBounds(str);

    x2 = x - b.width / 2;/*w  w  w  .  ja va2  s.c  o  m*/
    y2 = y - b.height / 2;

    font.drawMultiLine(batch, str, x2, y2, b.width, HAlignment.CENTER);
}

From source file:org.bladecoder.bladeengine.util.TextUtils.java

License:Apache License

public static void drawCenteredScreenY(SpriteBatch batch, BitmapFont font, CharSequence str, float x,
        int viewportHeight) {
    float y;// w  w w . j a  v  a 2s  . c  o m

    TextBounds b = font.getMultiLineBounds(str);

    y = (viewportHeight - b.height) / 2;

    font.drawMultiLine(batch, str, x, y, b.width, HAlignment.CENTER);
}

From source file:org.bladecoder.bladeengine.util.TextUtils.java

License:Apache License

public static void drawCenteredScreenX(SpriteBatch batch, BitmapFont font, CharSequence str, float y,
        int viewportWidth) {

    float x;/*  www  .  j  a v a2s . c  o m*/

    TextBounds b = font.getMultiLineBounds(str);

    x = (viewportWidth - b.width) / 2;

    font.drawMultiLine(batch, str, x, y, b.width, HAlignment.CENTER);
}

From source file:org.bladecoder.bladeengine.util.TextUtils.java

License:Apache License

public static float getCenterX(BitmapFont font, CharSequence str, int viewportWidth) {
    float x;/*from   ww w. ja  v a 2 s.  co m*/

    TextBounds b = font.getMultiLineBounds(str);

    x = (viewportWidth - b.width) / 2;

    return x;
}

From source file:org.bladecoder.bladeengine.util.TextUtils.java

License:Apache License

public static float getCenterY(BitmapFont font, CharSequence str, int viewportHeight) {

    float y;//from   w ww.ja  v a 2  s .co m

    TextBounds b = font.getMultiLineBounds(str);

    y = (viewportHeight + b.height) / 2;

    return y;
}