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

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

Introduction

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

Prototype

public TextBounds getBounds(CharSequence str) 

Source Link

Document

Returns the bounds of the specified text.

Usage

From source file:com.game.libgdx.roguelikeengine.PopupInfoText.java

License:Open Source License

public void drawScreen(SpriteBatch batch, BitmapFont font, String text, float fadein, int linedist, Color color,
        boolean resize) {
    float scaleX = font.getScaleX();
    float scaleY = font.getScaleY();

    update_x((int) (Gdx.graphics.getWidth() * 0.5f - this.width * 0.5f));
    update_y((int) (Gdx.graphics.getHeight() * 0.5f - this.height * 0.5f));

    mouseOverElement = "";
    if (!lastMessage.equals(text) || resize) {
        this.clearRenderWords();

        lineCount = 0;/*from  w w  w.java2 s  .  c  om*/

        int linepos = 0;
        int currentWidth = textoffsetx;
        int nextWidth = 0;
        int maxWidth = (int) (width - textoffsetx);

        String[] words = null;
        for (String line : text.split("\n")) {
            lineCount = lineCount + 1;

            currentWidth = textoffsetx;

            words = line.split(" ");
            for (String word : words) {
                if (word.contains("\t")) {
                    word = word.replace("\t", "    ");
                }

                Color wordColor = color;
                if (word.length() > 7) {
                    String hex = word.substring(0, 7);

                    if (Pattern.matches(colorPattern, hex)) {
                        wordColor = this.hex2Rgb(hex);
                        word = word.replace(hex, "");
                    }
                }

                nextWidth = (int) (currentWidth + font.getBounds(word + " ").width);
                if (nextWidth > maxWidth) {
                    currentWidth = textoffsetx;
                    nextWidth = (int) (currentWidth + font.getBounds(word + " ").width);
                    linepos = linepos + 1;
                    lineCount = lineCount + 1;
                }

                int wordx = currentWidth;
                int wordy = height - ((textoffsety) + (linepos * linedist));

                this.addWordToRender(word,
                        new WordRectangle(wordx, wordy, font.getBounds(word + " ").width, font.getLineHeight())
                                .withColor(wordColor));

                currentWidth = nextWidth;
            }

            linepos++;
            currentWidth = x + textoffsetx;
        }
    }

    this.background.draw(batch, x, y, width, height);

    while (((lineCount * font.getLineHeight() + lineCount * linedist) * font.getScaleY()) > height
            - textoffsety) {
        font.setScale(font.getScaleY() - 0.1f);
    }

    if (font.getScaleY() != scaleY) {
        drawScreen(batch, font, text, fadein, linedist, color, true);
        font.setScale(scaleX, scaleY);
        return;
    }

    boolean anyClickable = false;
    Set<Entry<String, LinkedList<WordRectangle>>> keys = renderWords.entrySet();
    for (Entry<String, LinkedList<WordRectangle>> entry : keys) {
        for (WordRectangle rect : entry.getValue()) {
            font.setColor(rect.color.r, rect.color.g, rect.color.b, fadein);

            if (wordClickListeners.containsKey(entry.getKey())) {
                if (mouseOverWord(rect, font)) {
                    font.setColor(Color.BLUE.r, Color.BLUE.g, Color.BLUE.b, fadein);
                    mouseOverElement = entry.getKey();
                } else {
                    font.setColor(Color.CYAN);
                    font.setColor(Color.CYAN.r, Color.CYAN.g, Color.CYAN.b, fadein);
                }

                anyClickable = true;
            }

            font.draw(batch, entry.getKey().replace("_", " "), rect.x + x, rect.y + y);
        }
    }

    if (!anyClickable) {
        Color c = font.getColor();
        font.setColor(instructionsColor.r, instructionsColor.g, instructionsColor.b, fadein);
        font.draw(batch, instructions, x + (width * 0.5f) - (font.getBounds(instructions).width * 0.5f),
                y + font.getBounds(instructions).height + Gdx.graphics.getHeight() * 0.05f);
        font.setColor(c);
    }

    lastMessage = text;
    font.setScale(scaleX, scaleY);
}

From source file:com.haxtastic.haxmasher.entity.hud.MasherHUD.java

License:Apache License

@Override
public void draw(SpriteBatch batch, BitmapFont font) {
    //Draw hpframe
    AtlasRegion spriteRegion = Art.regions.get(name);
    if (width == 0)
        width = spriteRegion.getRegionWidth();
    if (height == 0)
        height = spriteRegion.getRegionHeight();
    if (width < 0)
        width = -width;//w w w  . j  a v a2 s. c  o m
    float posX = x * Constants.PIXELS_PER_METER_X;
    float posY = y * Constants.PIXELS_PER_METER_Y;
    batch.draw(spriteRegion, posX, posY, width * Constants.PIXELS_PER_METER_X,
            height * Constants.PIXELS_PER_METER_Y);

    //Draw health
    spriteRegion = Art.regions.get(hpslice.name);
    if (hpslice.width == 0)
        hpslice.width = spriteRegion.getRegionWidth();
    if (hpslice.height == 0)
        hpslice.height = spriteRegion.getRegionHeight();
    if (hpslice.width < 0)
        hpslice.width = -hpslice.width;
    /*for(float i = 0; i < bars*100; i++) {
       posX = (bar.x + (0.04f*i)) * Constants.PIXELS_PER_METER_X;
       posY = bar.y * Constants.PIXELS_PER_METER_Y;
       batch.draw(spriteRegion, posX, posY, bar.width * Constants.PIXELS_PER_METER_X, bar.height * Constants.PIXELS_PER_METER_Y);
    }*/
    posX = hpslice.x * Constants.PIXELS_PER_METER_X;
    posY = hpslice.y * Constants.PIXELS_PER_METER_Y;
    float tempwidth = hpslice.width * (prevbars * 100);
    batch.draw(spriteRegion, posX, posY, tempwidth * Constants.PIXELS_PER_METER_X,
            hpslice.height * Constants.PIXELS_PER_METER_Y);

    //Draw text
    font.setColor(0, 0, 0, 1);
    String msg = (int) masher.health + "/" + (int) masher.maxhealth;
    posX = (x + (width / 2)) * Constants.PIXELS_PER_METER_X - (font.getBounds(msg).width / 2);
    posY = (y + (height / 2)) * Constants.PIXELS_PER_METER_Y + (font.getBounds(msg).height / 2);
    font.draw(batch, msg, posX, posY);

    posX = statsX;
    posY = statsY;
    msg = "Name: " + masher.nick;
    font.draw(batch, msg, posX, posY);

    posY -= (font.getBounds(msg).height * space);
    msg = "Level: " + masher.level;
    font.draw(batch, msg, posX, posY);

    msg = "Armor: " + (int) masher.armor + "("
            + (int) ((masher.armor / (Constants.Stats.armorReduction + masher.armor)) * 100) + "%)";
    posY -= (font.getBounds(msg).height * space);
    font.draw(batch, msg, posX, posY);
    msg = "Damage: " + (int) (masher.getDamage() * Constants.Stats.minDamage) + " - "
            + (int) (masher.getDamage() * Constants.Stats.maxDamage) + "(" + masher.crit + "%)";
    posY -= (font.getBounds(msg).height * space);
    font.draw(batch, msg, posX, posY);
    if (masher.getClass().equals(Player.class)) {
        int exp = masher.getExp();
        int nxp = masher.expForLevel(masher.getLevel() + 1);
        msg = "exp: " + exp + "/" + nxp + " (" + (nxp - exp) + " left)";
        posY -= (font.getBounds(msg).height * space);
        font.draw(batch, msg, posX, posY);
    } else if (masher.getClass().equals(Guy.class)) {
        msg = "experience worth: " + masher.getExp();
        posY -= (font.getBounds(msg).height * space);
        font.draw(batch, msg, posX, posY);
    }
    //font.setScale(1f, 1f);
}

From source file:com.haxtastic.haxmasher.entity.ui.Button.java

License:Apache License

@Override
public void draw(SpriteBatch batch, BitmapFont font) {
    AtlasRegion spriteRegion = null;/*from  w w w  .ja  va  2  s  .c o  m*/
    switch (getState()) {
    case normal:
        spriteRegion = Art.regions.get(States.states.get(States.state.normal));
        break;
    case hover:
        spriteRegion = Art.regions.get(States.states.get(States.state.hover));
        break;
    case clicked:
    case clicking:
        spriteRegion = Art.regions.get(States.states.get(States.state.clicked));
        break;
    case blocked:
        spriteRegion = Art.regions.get(States.states.get(States.state.blocked));
        break;
    default:
        spriteRegion = Art.regions.get(States.states.get(States.state.normal));
        break;
    }
    if (width == 0)
        width = spriteRegion.getRegionWidth();
    if (height == 0)
        height = spriteRegion.getRegionHeight();
    if (width < 0)
        width = -width;
    float posX = x * Constants.PIXELS_PER_METER_X;
    float posY = y * Constants.PIXELS_PER_METER_Y;
    batch.draw(spriteRegion, posX, posY, width * Constants.PIXELS_PER_METER_X,
            height * Constants.PIXELS_PER_METER_Y);
    //Draw text
    font.setColor(0, 0, 0, 1);
    String msg = text;
    posX = (x + (width / 2)) * Constants.PIXELS_PER_METER_X - (font.getBounds(msg).width / 2);
    posY = (y + (height / 2)) * Constants.PIXELS_PER_METER_Y + (font.getBounds(msg).height / 2);
    font.draw(batch, msg, posX, posY);
}

From source file:com.haxtastic.haxmasher.entity.ui.Console.java

License:Apache License

@Override
public void draw(SpriteBatch batch, BitmapFont font) {
    AtlasRegion spriteRegion = Art.regions.get(name);
    if (width == 0)
        width = spriteRegion.getRegionWidth();
    if (height == 0)
        height = spriteRegion.getRegionHeight();
    if (width < 0)
        width = -width;//from   w w w .java  2  s  .  c om
    float posX = x * Constants.PIXELS_PER_METER_X;
    float posY = y * Constants.PIXELS_PER_METER_Y;
    batch.draw(spriteRegion, posX, posY, width * Constants.PIXELS_PER_METER_X,
            height * Constants.PIXELS_PER_METER_Y);
    //Draw text
    font.setColor(0, 0, 0, 1);
    float sX = font.getScaleX();
    float sY = font.getScaleY();
    font.setScale(1.1f, 1.1f);
    String msg = null;
    int i = 0;
    ListIterator<String> iter = lines.listIterator(lines.size());
    while (iter.hasPrevious()) {
        msg = iter.previous();
        posX = linex * Constants.PIXELS_PER_METER_X;
        posY = (liney + (space * i)) * Constants.PIXELS_PER_METER_Y + (font.getBounds(msg).height * i);
        font.draw(batch, msg, posX, posY);
        i++;
    }
    font.setScale(sX, sY);
}

From source file:com.haxtastic.haxmasher.scene.Stats.java

License:Apache License

@Override
public void draw(float dt, SpriteBatch batch, BitmapFont font) {
    if (parent != null)
        parent.draw(dt, batch, font);/*from  w ww .  java2  s  .co m*/
    Iterator<Actor> iter = getActors().iterator();
    while (iter.hasNext()) {
        Actor actor = iter.next();
        actor.draw(batch, font);
    }
    font.setColor(0, 0, 0, 1);
    float sX = font.getScaleX();
    float sY = font.getScaleY();
    font.setScale(1.575f, 1.725f);

    String msg = "You have gained " + level;
    msg += (level > 1 ? " levels." : " level.");
    float posX = (Constants.Positions.pointsTextX) * Constants.PIXELS_PER_METER_X
            + (font.getBounds(" to spend").width / 2);
    float posY = (Constants.Positions.pointsTextY + 0.1f) * Constants.PIXELS_PER_METER_Y
            + (font.getBounds(msg).height);
    font.draw(batch, msg, posX, posY);

    msg = "You have " + points + " skill points to spend.";
    posX = Constants.Positions.pointsTextX * Constants.PIXELS_PER_METER_X;
    posY = Constants.Positions.pointsTextY * Constants.PIXELS_PER_METER_Y;
    font.draw(batch, msg, posX, posY);

    msg = Constants.Stats.healthWeight + " * " + (PlayerStats.pHealth + PlayerStats.level);
    posX = Constants.Buttons.healthTextX * Constants.PIXELS_PER_METER_X - (font.getBounds(msg).width / 2);
    posY = Constants.Buttons.pointsY * Constants.PIXELS_PER_METER_Y + (font.getBounds(msg).height);
    font.draw(batch, msg, posX, posY);

    msg = Constants.Stats.damageWeight + " * " + (PlayerStats.pDamage + PlayerStats.level);
    posX = Constants.Buttons.damageTextX * Constants.PIXELS_PER_METER_X - (font.getBounds(msg).width / 2);
    posY = Constants.Buttons.pointsY * Constants.PIXELS_PER_METER_Y + (font.getBounds(msg).height);
    font.draw(batch, msg, posX, posY);

    msg = Constants.Stats.armorWeight + " * " + (PlayerStats.pArmor + PlayerStats.level);
    posX = Constants.Buttons.armorTextX * Constants.PIXELS_PER_METER_X - (font.getBounds(msg).width / 2);
    posY = Constants.Buttons.pointsY * Constants.PIXELS_PER_METER_Y + (font.getBounds(msg).height);
    font.draw(batch, msg, posX, posY);

    msg = Constants.Stats.critWeight + " * " + (PlayerStats.pCrit + PlayerStats.level);
    posX = Constants.Buttons.critTextX * Constants.PIXELS_PER_METER_X - (font.getBounds(msg).width / 2);
    posY = Constants.Buttons.pointsY * Constants.PIXELS_PER_METER_Y + (font.getBounds(msg).height);
    font.draw(batch, msg, posX, posY);
    font.setScale(sX, sY);
}

From source file:com.mygdx.entities.text.TextEntity.java

public void render(BitmapFont font, SpriteBatch sb) {
    font.draw(sb, string, pos.x - font.getBounds(string).width / 2, pos.y);
}

From source file:com.turbogerm.helljump.game.items.ItemBase.java

License:Open Source License

public final void renderText(SpriteBatch batch, float visibleAreaPosition, BitmapFont itemFont) {
    if (mItemState == TEXT_STATE) {
        if (mIsPickedUpTextBoundsDirty) {
            TextBounds textBounds = itemFont.getBounds(mPickedUpText);
            mPickedUpTextBounds.set(textBounds.width, textBounds.height);
            mIsPickedUpTextBoundsDirty = false;
        }/*from ww  w. j  a  v  a 2 s.com*/
        float alpha = mTextCountdown / TEXT_COUNTDOWN_DURATION;
        Color c = itemFont.getColor();
        itemFont.setColor(c.r, c.g, c.b, alpha);
        float textX = (mPosition.x + mSize.x / 2.0f) * GameAreaUtils.METER_TO_PIXEL
                - mPickedUpTextBounds.x / 2.0f;
        textX = MathUtils.clamp(textX, 0.0f, HellJump.VIEWPORT_WIDTH - mPickedUpTextBounds.x);
        float textY = (mPosition.y + mSize.y / 2.0f - visibleAreaPosition) * GameAreaUtils.METER_TO_PIXEL
                + mPickedUpTextBounds.y / 2.0f;
        itemFont.draw(batch, mPickedUpText, textX, textY);
    }
}

From source file:io.github.deathsbreedgames.spacerun.screens.CreditsMenuScreen.java

private boolean mouseCollides(BitmapFont font, String text, float posX, float posY) {
    float mouseX = GlobalVars.getTouchX();
    float mouseY = (float) GlobalVars.height - GlobalVars.getTouchY();

    if (mouseX > posX && mouseX < font.getBounds(text).width + posX
            && mouseY > posY - font.getBounds(text).height && mouseY < posY) {

        return true;
    } else {/*from w w  w. j a  v  a 2  s  .c om*/
        return false;
    }
}

From source file:mobi.shad.s3lib.main.S3Gfx.java

License:Apache License

public static void drawCenterString(float posY, String string, BitmapFont font) {
    float width = font.getBounds(string).width;
    drawString((S3Screen.width - width) / 2.0f, posY, string, font);
}

From source file:name.herve.bastod.gui.components.MetalBar.java

License:Open Source License

@Override
public void drawText() {
    drawCentered(GUIResources.getInstance().getFont(player.getColor()), strMetal);

    BitmapFont font = GUIResources.getInstance().getFont(GUIResources.FONT_SMALL_WHITE);
    TextBounds b = font.getBounds(strDeltaA);
    draw(font, strDeltaA, getX() + getWidth() - b.width - 4, getY() + getHeight() - b.height - 4);
    b = font.getBounds(strDeltaR);//from www . j ava  2 s. c  om
    draw(font, strDeltaR, getX() + getWidth() - b.width - 4, getY() + 4);

    //draw(, strMetal, getX() + getWidth() / 2 - 20, getY() + halfHeight + halfHeight / 2 + 7);
    //draw(GUIResources.getInstance().getFont(GUIResources.FONT_STANDARD_WHITE), strDelta, getX() + getWidth() / 2 - 10, getY() + halfHeight / 2 + 7);
}