Search in sources :

Example 6 with BitmapFontData

use of com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData in project skin-composer by raeleus.

the class DialogFonts method produceAtlas.

private boolean produceAtlas() {
    try {
        if (atlas != null) {
            atlas.dispose();
            atlas = null;
        }
        if (!main.getAtlasData().atlasCurrent) {
            main.getAtlasData().writeAtlas();
            main.getAtlasData().atlasCurrent = true;
        }
        atlas = main.getAtlasData().getAtlas();
        for (FontData font : fonts) {
            BitmapFontData fontData = new BitmapFontData(font.file, false);
            Array<TextureRegion> regions = new Array<>();
            for (String path : fontData.imagePaths) {
                FileHandle file = new FileHandle(path);
                if (!file.exists()) {
                    file = fontData.fontFile.sibling(fontData.fontFile.nameWithoutExtension() + ".png");
                }
                TextureRegion region = atlas.findRegion(file.nameWithoutExtension());
                if (region != null) {
                    regions.add(region);
                }
            }
            fontMap.put(font, new BitmapFont(fontData, regions, true));
        }
        return true;
    } catch (Exception e) {
        Gdx.app.error(getClass().getName(), "Error while attempting to generate drawables.", e);
        main.getDialogFactory().showDialogError("Drawables Error...", "Error while attempting to generate drawables. Open log?");
        return false;
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) FileHandle(com.badlogic.gdx.files.FileHandle) FreeTypeFontData(com.ray3k.skincomposer.data.FreeTypeFontData) FontData(com.ray3k.skincomposer.data.FontData) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 7 with BitmapFontData

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

the class UnicodeFont method drawBitmap.

private void drawBitmap(String text, int startIndex, int endIndex) {
    BitmapFontData data = bitmapFont.getData();
    int padY = paddingTop + paddingBottom + paddingAdvanceY;
    data.setLineHeight(data.lineHeight + padY);
    layout.setText(bitmapFont, text);
    data.setLineHeight(data.lineHeight - padY);
    for (GlyphRun run : layout.runs) for (int i = 0, n = run.xAdvances.size; i < n; i++) run.xAdvances.incr(i, paddingAdvanceX + paddingLeft + paddingRight);
    cache.setText(layout, paddingLeft, paddingRight);
    Array<TextureRegion> regions = bitmapFont.getRegions();
    for (int i = 0, n = regions.size; i < n; i++) {
        regions.get(i).getTexture().bind();
        GL11.glBegin(GL11.GL_QUADS);
        float[] vertices = cache.getVertices(i);
        for (int ii = 0, nn = vertices.length; ii < nn; ii += 20) {
            GL11.glTexCoord2f(vertices[ii + U], vertices[ii + V]);
            GL11.glVertex3f(vertices[ii + X], vertices[ii + Y], 0);
            GL11.glTexCoord2f(vertices[ii + U], vertices[ii + V2]);
            GL11.glVertex3f(vertices[ii + X], vertices[ii + Y2], 0);
            GL11.glTexCoord2f(vertices[ii + U2], vertices[ii + V2]);
            GL11.glVertex3f(vertices[ii + X2], vertices[ii + Y2], 0);
            GL11.glTexCoord2f(vertices[ii + U2], vertices[ii + V]);
            GL11.glVertex3f(vertices[ii + X2], vertices[ii + Y], 0);
        }
        GL11.glEnd();
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun)

Example 8 with BitmapFontData

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

the class BitmapFontLoader method getDependencies.

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, BitmapFontParameter parameter) {
    Array<AssetDescriptor> deps = new Array();
    if (parameter != null && parameter.bitmapFontData != null) {
        data = parameter.bitmapFontData;
        return deps;
    }
    data = new BitmapFontData(file, parameter != null ? parameter.flip : false);
    if (parameter != null && parameter.atlasName != null) {
        deps.add(new AssetDescriptor(parameter.atlasName, TextureAtlas.class));
    } else {
        for (int i = 0; i < data.getImagePaths().length; i++) {
            String path = data.getImagePath(i);
            FileHandle resolved = resolve(path);
            TextureLoader.TextureParameter textureParams = new TextureLoader.TextureParameter();
            if (parameter != null) {
                textureParams.genMipMaps = parameter.genMipMaps;
                textureParams.minFilter = parameter.minFilter;
                textureParams.magFilter = parameter.magFilter;
            }
            AssetDescriptor descriptor = new AssetDescriptor(resolved, Texture.class, textureParams);
            deps.add(descriptor);
        }
    }
    return deps;
}
Also used : Array(com.badlogic.gdx.utils.Array) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 9 with BitmapFontData

use of com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData 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)

Example 10 with BitmapFontData

use of com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData in project skin-composer by raeleus.

the class DialogFonts method fontNameDialog.

private void fontNameDialog(Array<FileHandle> files, int index) {
    if (index < files.size) {
        try {
            final FileHandle fileHandle = files.get(index);
            final TextField textField = new TextField(FontData.filter(fileHandle.nameWithoutExtension()), getSkin());
            final Dialog nameDialog = new Dialog("Enter a name...", getSkin(), "bg") {

                @Override
                protected void result(Object object) {
                    if ((Boolean) object) {
                        String name = textField.getText();
                        addFont(name, fileHandle);
                    }
                }

                @Override
                public boolean remove() {
                    fontNameDialog(files, index + 1);
                    return super.remove();
                }
            };
            nameDialog.getTitleTable().padLeft(5.0f);
            nameDialog.button("OK", true);
            nameDialog.button("Cancel", false);
            nameDialog.getButtonTable().getCells().first().getActor().addListener(main.getHandListener());
            nameDialog.getButtonTable().getCells().get(1).getActor().addListener(main.getHandListener());
            final TextButton button = (TextButton) nameDialog.getButtonTable().getCells().first().getActor();
            nameDialog.getButtonTable().padBottom(15.0f);
            textField.setTextFieldListener((TextField textField1, char c) -> {
                if (c == '\n') {
                    if (!button.isDisabled()) {
                        String name1 = textField1.getText();
                        if (addFont(name1, fileHandle)) {
                            nameDialog.hide();
                        }
                    }
                    main.getStage().setKeyboardFocus(textField1);
                }
            });
            textField.addListener(main.getIbeamListener());
            nameDialog.getContentTable().defaults().padLeft(10.0f).padRight(10.0f).padTop(5.0f);
            nameDialog.text("Please enter a name for the new font: ");
            nameDialog.getContentTable().row();
            nameDialog.getContentTable().add(textField).growX();
            nameDialog.getContentTable().row();
            nameDialog.text("Preview:");
            nameDialog.getContentTable().row();
            LabelStyle previewStyle = new LabelStyle();
            previewStyle.font = new BitmapFont(fileHandle);
            Table table = new Table(getSkin());
            table.setBackground("white");
            BitmapFontData bitmapFontData = new BitmapFontData(fileHandle, false);
            if (Utils.brightness(Utils.averageEdgeColor(new FileHandle(bitmapFontData.imagePaths[0]))) > .5f) {
                table.setColor(Color.BLACK);
            } else {
                table.setColor(Color.WHITE);
            }
            table.add(new Label("Lorem Ipsum", previewStyle)).pad(5.0f);
            nameDialog.getContentTable().add(table);
            nameDialog.key(Keys.ESCAPE, false);
            button.setDisabled(!FontData.validate(textField.getText()));
            textField.addListener(new ChangeListener() {

                @Override
                public void changed(ChangeListener.ChangeEvent event, Actor actor) {
                    boolean disable = !FontData.validate(textField.getText());
                    if (!disable) {
                        for (FontData data : main.getJsonData().getFonts()) {
                            if (data.getName().equals(textField.getText())) {
                                disable = true;
                                break;
                            }
                        }
                    }
                    button.setDisabled(disable);
                }
            });
            nameDialog.setColor(1.0f, 1.0f, 1.0f, 0.0f);
            textField.setFocusTraversal(false);
            if (!Utils.doesImageFitBox(new FileHandle(bitmapFontData.imagePaths[0]), maxTextureWidth, maxTextureHeight)) {
                showAddFontSizeError(fileHandle.nameWithoutExtension());
            } else {
                nameDialog.show(getStage());
                getStage().setKeyboardFocus(textField);
                textField.selectAll();
            }
        } catch (Exception e) {
            Gdx.app.error(getClass().getName(), "Error creating preview font from file", e);
            main.getDialogFactory().showDialogError("Preview Error...", "Error creating preview font from file. Check file paths.\n\nOpen log?");
        }
    } else {
        // after all fonts are processed
        if (main.getProjectData().areResourcesRelative()) {
            main.getProjectData().makeResourcesRelative();
        }
    }
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Table(com.badlogic.gdx.scenes.scene2d.ui.Table) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) FileHandle(com.badlogic.gdx.files.FileHandle) FreeTypeFontData(com.ray3k.skincomposer.data.FreeTypeFontData) FontData(com.ray3k.skincomposer.data.FontData) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) LabelStyle(com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle) Dialog(com.badlogic.gdx.scenes.scene2d.ui.Dialog) Actor(com.badlogic.gdx.scenes.scene2d.Actor) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Aggregations

BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)13 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 FileHandle (com.badlogic.gdx.files.FileHandle)6 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 Array (com.badlogic.gdx.utils.Array)5 FontData (com.ray3k.skincomposer.data.FontData)4 FreeTypeFontData (com.ray3k.skincomposer.data.FreeTypeFontData)4 Color (com.badlogic.gdx.graphics.Color)2 GlyphRun (com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun)2 Actor (com.badlogic.gdx.scenes.scene2d.Actor)2 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)2 LabelStyle (com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle)2 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)2 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)2 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)2 DrawableData (com.ray3k.skincomposer.data.DrawableData)2 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 Glyph (com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)1 PixmapPacker (com.badlogic.gdx.graphics.g2d.PixmapPacker)1