Search in sources :

Example 61 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class FreeTypeAtlasTest method createFonts.

protected int createFonts() {
    // This test uses a less efficient but more convenient way to pack multiple generated fonts into a single
    // texture atlas.
    //
    // 1. Create a new PixmapPacker big enough to fit all your desired glyphs
    // 2. Create a new FreeTypeFontGenerator for each TTF file (i.e. font styles/families)
    // 3. For each size and style, call generator.generateFont() with the packer set on the parameter
    // 4. Generate the texture atlas using packer.generateTextureAtlas or packer.updateTextureAtlas.
    // 5. Dispose of the atlas upon application exit or when you are done using the fonts
    // //////////////////////////////////////////////////////////////////////////////////////////////////////
    // create the pixmap packer
    packer = new PixmapPacker(FONT_ATLAS_WIDTH, FONT_ATLAS_HEIGHT, Format.RGBA8888, 2, false);
    fontMap = new FontMap<BitmapFont>();
    int fontCount = 0;
    // for each style...
    for (FontStyle style : FontStyle.values()) {
        // get the file for this style
        FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal(style.path));
        // For each size...
        for (FontSize size : FontSize.values()) {
            // pack the glyphs into the atlas using the default chars
            FreeTypeFontGenerator.FreeTypeFontParameter fontParameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
            fontParameter.size = size.size;
            fontParameter.packer = packer;
            fontParameter.characters = CHARACTERS;
            BitmapFont bmFont = gen.generateFont(fontParameter);
            fontMap.get(style).put(size, bmFont);
            fontCount++;
        }
        // dispose of the generator once we're finished with this family
        gen.dispose();
    }
    // for the demo, show how many glyphs we loaded
    return fontCount * CHARACTERS.length();
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 62 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class DebugDrawer method draw3dText.

@Override
public void draw3dText(Vector3 location, String textString) {
    if (spriteBatch == null) {
        spriteBatch = new SpriteBatch();
    }
    if (font == null) {
        font = new BitmapFont();
    }
    // this check is necessary to avoid "mirrored" instances of the text
    if (camera.frustum.pointInFrustum(location)) {
        if (viewport != null) {
            camera.project(location, viewport.getScreenX(), viewport.getScreenY(), viewport.getScreenWidth(), viewport.getScreenHeight());
        } else {
            camera.project(location);
        }
        shapeRenderer.end();
        spriteBatch.begin();
        // the text will be centered on the position
        font.draw(spriteBatch, textString, location.x, location.y, 0, textString.length(), 0, Align.center, false);
        spriteBatch.end();
        shapeRenderer.begin(ShapeType.Line);
    }
}
Also used : BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 63 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class Label method scaleAndComputePrefSize.

private void scaleAndComputePrefSize() {
    BitmapFont font = cache.getFont();
    float oldScaleX = font.getScaleX();
    float oldScaleY = font.getScaleY();
    if (fontScaleChanged)
        font.getData().setScale(fontScaleX, fontScaleY);
    computePrefSize();
    if (fontScaleChanged)
        font.getData().setScale(oldScaleX, oldScaleY);
}
Also used : BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 64 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class List method layout.

public void layout() {
    final BitmapFont font = style.font;
    final Drawable selectedDrawable = style.selection;
    itemHeight = font.getCapHeight() - font.getDescent() * 2;
    itemHeight += selectedDrawable.getTopHeight() + selectedDrawable.getBottomHeight();
    textOffsetX = selectedDrawable.getLeftWidth();
    textOffsetY = selectedDrawable.getTopHeight() - font.getDescent();
    prefWidth = 0;
    Pool<GlyphLayout> layoutPool = Pools.get(GlyphLayout.class);
    GlyphLayout layout = layoutPool.obtain();
    for (int i = 0; i < items.size; i++) {
        layout.setText(font, toString(items.get(i)));
        prefWidth = Math.max(layout.width, prefWidth);
    }
    layoutPool.free(layout);
    prefWidth += selectedDrawable.getLeftWidth() + selectedDrawable.getRightWidth();
    prefHeight = items.size * itemHeight;
    Drawable background = style.background;
    if (background != null) {
        prefWidth += background.getLeftWidth() + background.getRightWidth();
        prefHeight += background.getTopHeight() + background.getBottomHeight();
    }
}
Also used : Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) GlyphLayout(com.badlogic.gdx.graphics.g2d.GlyphLayout) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 65 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class TextField method updateDisplayText.

void updateDisplayText() {
    BitmapFont font = style.font;
    BitmapFontData data = font.getData();
    String text = this.text;
    int textLength = text.length();
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < textLength; i++) {
        char c = text.charAt(i);
        buffer.append(data.hasGlyph(c) ? c : ' ');
    }
    String newDisplayText = buffer.toString();
    if (passwordMode && data.hasGlyph(passwordCharacter)) {
        if (passwordBuffer == null)
            passwordBuffer = new StringBuilder(newDisplayText.length());
        if (passwordBuffer.length() > textLength)
            passwordBuffer.setLength(textLength);
        else {
            for (int i = passwordBuffer.length(); i < textLength; i++) passwordBuffer.append(passwordCharacter);
        }
        displayText = passwordBuffer;
    } else
        displayText = newDisplayText;
    layout.setText(font, displayText);
    glyphPositions.clear();
    float x = 0;
    if (layout.runs.size > 0) {
        GlyphRun run = layout.runs.first();
        FloatArray xAdvances = run.xAdvances;
        fontOffset = xAdvances.first();
        for (int i = 1, n = xAdvances.size; i < n; i++) {
            glyphPositions.add(x);
            x += xAdvances.get(i);
        }
    } else
        fontOffset = 0;
    glyphPositions.add(x);
    if (selectionStart > newDisplayText.length())
        selectionStart = textLength;
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Aggregations

BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)104 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)64 Texture (com.badlogic.gdx.graphics.Texture)31 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)21 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)18 Stage (com.badlogic.gdx.scenes.scene2d.Stage)16 OrthoCamController (com.badlogic.gdx.tests.utils.OrthoCamController)13 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)12 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)11 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)10 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)10 AssetManager (com.badlogic.gdx.assets.AssetManager)9 Color (com.badlogic.gdx.graphics.Color)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 InputAdapter (com.badlogic.gdx.InputAdapter)7 Actor (com.badlogic.gdx.scenes.scene2d.Actor)7 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)7 FileHandle (com.badlogic.gdx.files.FileHandle)6 Pixmap (com.badlogic.gdx.graphics.Pixmap)6 GlyphLayout (com.badlogic.gdx.graphics.g2d.GlyphLayout)6