Search in sources :

Example 96 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class DialogDrawables method remove.

@Override
public boolean remove() {
    scrollPosition = scrollPane.getScrollY();
    desktopWorker.removeFilesDroppedListener(filesDroppedListener);
    try {
        if (!atlasData.atlasCurrent) {
            FileHandle defaultsFile = Main.appFolder.child("texturepacker/atlas-internal-settings.json");
            atlasData.writeAtlas(defaultsFile);
            atlasData.atlasCurrent = true;
        }
    } catch (Exception e) {
        Gdx.app.error(getClass().getName(), "Error creating atlas upon drawable dialog exit", e);
        dialogFactory.showDialogError("Atlas Error...", "Error creating atlas upon drawable dialog exit.\n\nOpen log?");
    }
    fire(new DialogEvent(DialogEvent.Type.CLOSE));
    return super.remove();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle)

Example 97 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class JsonData method read.

@Override
public void read(Json json, JsonValue jsonData) {
    try {
        colors = json.readValue("colors", Array.class, jsonData);
        fonts = json.readValue("fonts", Array.class, jsonData);
        freeTypeFonts = json.readValue("freeTypeFonts", Array.class, new Array<FreeTypeFontData>(), jsonData);
        FileHandle previewFontsPath = Main.appFolder.child("preview fonts");
        var fontsList = previewFontsPath.list();
        for (var freeTypeFont : freeTypeFonts) {
            if (freeTypeFont.previewTTF != null) {
                boolean foundMatch = false;
                for (var previewFile : fontsList) {
                    if (freeTypeFont.previewTTF.equals(previewFile.nameWithoutExtension())) {
                        foundMatch = true;
                        break;
                    }
                }
                if (!foundMatch) {
                    freeTypeFont.previewTTF = previewFontsPath.list()[0].nameWithoutExtension();
                }
            }
        }
        classStyleMap = new OrderedMap<>();
        for (JsonValue data : jsonData.get("classStyleMap").iterator()) {
            classStyleMap.put(ClassReflection.forName(data.name), json.readValue(Array.class, data));
        }
        for (Array<StyleData> styleDatas : classStyleMap.values()) {
            for (StyleData styleData : styleDatas) {
                styleData.jsonData = this;
            }
        }
        customClasses = json.readValue("customClasses", Array.class, CustomClass.class, new Array<>(), jsonData);
    } catch (ReflectionException e) {
        Gdx.app.log(getClass().getName(), "Error parsing json data during file read", e);
        dialogFactory.showDialogError("Error while reading file...", "Error while attempting to read save file.\nPlease ensure that file is not corrupted.\n\nOpen error log?");
    }
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 98 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class JsonData method translateFontDrawables.

/**
 * Moves font drawables to the appropriate list when reading from legacy save files.
 */
public void translateFontDrawables(AtlasData atlasData) {
    for (var font : fonts) {
        if (font.file.exists()) {
            var bitmapFontData = new BitmapFontData(font.file, false);
            for (String path : bitmapFontData.imagePaths) {
                FileHandle file = new FileHandle(path);
                var drawable = atlasData.getDrawable(file.nameWithoutExtension());
                if (drawable != null) {
                    atlasData.getDrawables().removeValue(drawable, false);
                    atlasData.getFontDrawables().add(drawable);
                    drawable.type = DrawableType.FONT;
                }
            }
        }
    }
}
Also used : BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 99 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class FreeTypeFontData method writeFontToFile.

public void writeFontToFile(Main main, FileHandle target) {
    if (bitmapFont != null) {
        bitmapFont.dispose();
        bitmapFont = null;
    }
    if (!useCustomSerializer) {
        if (previewTTF == null)
            return;
        FreeTypeFontParameter parameter = new FreeTypeFontParameter();
        parameter.color = Color.BLACK;
        FileHandle previewFontPath = appFolder.child("preview fonts/" + previewTTF + ".ttf");
        if (previewFontPath.exists()) {
            FreeTypeFontGenerator generator = new FreeTypeFontGenerator(previewFontPath);
            generator.setMaxTextureSize(FreeTypeFontGenerator.NO_MAXIMUM);
            bitmapFont = generator.generateFont(parameter);
            generator.dispose();
        }
    } else {
        if (file == null)
            return;
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(file);
        FreeTypeFontParameter parameter = new FreeTypeFontParameter();
        if (borderColor != null)
            for (ColorData colorData : jsonData.getColors()) {
                if (colorData.getName().equals(borderColor)) {
                    parameter.borderColor = colorData.color;
                    break;
                }
            }
        parameter.borderGamma = borderGamma;
        parameter.borderStraight = borderStraight;
        parameter.borderWidth = borderWidth;
        parameter.characters = characters.equals("") ? DEFAULT_CHARS : characters;
        if (color != null)
            for (ColorData colorData : jsonData.getColors()) {
                if (colorData.getName().equals(color)) {
                    parameter.color = colorData.color;
                    break;
                }
            }
        parameter.flip = flip;
        parameter.gamma = gamma;
        parameter.genMipMaps = genMipMaps;
        parameter.hinting = hinting == null ? Hinting.AutoMedium : Hinting.valueOf(hinting);
        parameter.incremental = false;
        parameter.kerning = kerning;
        parameter.magFilter = magFilter == null ? TextureFilter.Nearest : TextureFilter.valueOf(magFilter);
        parameter.minFilter = minFilter == null ? TextureFilter.Nearest : TextureFilter.valueOf(minFilter);
        parameter.mono = mono;
        parameter.renderCount = renderCount;
        if (shadowColor != null)
            for (ColorData colorData : jsonData.getColors()) {
                if (colorData.getName().equals(shadowColor)) {
                    parameter.shadowColor = colorData.color;
                    break;
                }
            }
        parameter.shadowOffsetX = shadowOffsetX;
        parameter.shadowOffsetY = shadowOffsetY;
        parameter.size = size;
        parameter.spaceX = spaceX;
        parameter.spaceY = spaceY;
        parameter.packer = new PixmapPacker(512, 512, Pixmap.Format.RGBA8888, 2, false, new PixmapPacker.SkylineStrategy());
        var data = generator.generateData(parameter);
        desktopWorker.writeFont(data, parameter.packer.getPages(), target);
        generator.dispose();
    }
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 100 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class AtlasData method produceAtlas.

/**
 * Writes a TextureAtlas based on drawables list. Creates drawables to be
 * displayed on screen
 * @return
 */
public boolean produceAtlas() {
    try {
        drawablePairs.clear();
        if (!atlasData.atlasCurrent) {
            if (atlas != null) {
                atlas.dispose();
                atlas = null;
            }
            FileHandle defaultsFile = Main.appFolder.child("texturepacker/atlas-internal-settings.json");
            atlasData.writeAtlas(defaultsFile);
            atlasData.atlasCurrent = true;
            // clear all regions in any tenPatchData
            for (var data : atlasData.getDrawables()) {
                if (data.tenPatchData != null) {
                    data.tenPatchData.regions = null;
                }
            }
        }
        atlas = atlasData.getAtlas();
        var combined = new Array<>(getDrawables());
        combined.addAll(getFontDrawables());
        for (DrawableData data : combined) {
            Drawable drawable;
            if (data.type == DrawableType.CUSTOM) {
                drawable = skin.getDrawable("custom");
            } else if (data.type == DrawableType.PIXEL) {
                drawable = ((TextureRegionDrawable) skin.getDrawable("white")).tint(jsonData.getColorByName(data.tintName).color);
                drawable.setMinWidth(data.minWidth);
                drawable.setMinHeight(data.minHeight);
            } else if (data.type == DrawableType.TENPATCH) {
                var region = atlas.findRegion(DrawableData.proper(data.file.name()));
                drawable = new TenPatchDrawable(data.tenPatchData.horizontalStretchAreas.toArray(), data.tenPatchData.verticalStretchAreas.toArray(), data.tenPatchData.tile, region);
                if (((TenPatchDrawable) drawable).horizontalStretchAreas.length == 0) {
                    ((TenPatchDrawable) drawable).horizontalStretchAreas = new int[] { 0, region.getRegionWidth() - 1 };
                }
                if (((TenPatchDrawable) drawable).verticalStretchAreas.length == 0) {
                    ((TenPatchDrawable) drawable).verticalStretchAreas = new int[] { 0, region.getRegionHeight() - 1 };
                }
                drawable.setLeftWidth(data.tenPatchData.contentLeft);
                drawable.setRightWidth(data.tenPatchData.contentRight);
                drawable.setTopHeight(data.tenPatchData.contentTop);
                drawable.setBottomHeight(data.tenPatchData.contentBottom);
                if (!MathUtils.isEqual(data.minWidth, -1))
                    drawable.setMinWidth(data.minWidth);
                if (!MathUtils.isEqual(data.minHeight, -1))
                    drawable.setMinHeight(data.minHeight);
                if (data.tenPatchData.colorName != null)
                    ((TenPatchDrawable) drawable).setColor(jsonData.getColorByName(data.tenPatchData.colorName).color);
                if (data.tenPatchData.color1Name != null)
                    ((TenPatchDrawable) drawable).setColor1(jsonData.getColorByName(data.tenPatchData.color1Name).color);
                if (data.tenPatchData.color2Name != null)
                    ((TenPatchDrawable) drawable).setColor2(jsonData.getColorByName(data.tenPatchData.color2Name).color);
                if (data.tenPatchData.color3Name != null)
                    ((TenPatchDrawable) drawable).setColor3(jsonData.getColorByName(data.tenPatchData.color3Name).color);
                if (data.tenPatchData.color4Name != null)
                    ((TenPatchDrawable) drawable).setColor4(jsonData.getColorByName(data.tenPatchData.color4Name).color);
                ((TenPatchDrawable) drawable).setOffsetX(data.tenPatchData.offsetX);
                ((TenPatchDrawable) drawable).setOffsetY(data.tenPatchData.offsetY);
                ((TenPatchDrawable) drawable).setOffsetXspeed(data.tenPatchData.offsetXspeed);
                ((TenPatchDrawable) drawable).setOffsetYspeed(data.tenPatchData.offsetYspeed);
                ((TenPatchDrawable) drawable).setFrameDuration(data.tenPatchData.frameDuration);
                ((TenPatchDrawable) drawable).setPlayMode(data.tenPatchData.playMode);
                ((TenPatchDrawable) drawable).crushMode = data.tenPatchData.crushMode;
                if (data.tenPatchData.regions == null) {
                    data.tenPatchData.regions = new Array<>();
                    for (var name : data.tenPatchData.regionNames) {
                        data.tenPatchData.regions.add(atlas.findRegion(name));
                    }
                }
                ((TenPatchDrawable) drawable).setRegions(data.tenPatchData.regions);
            } else if (data.type == DrawableType.TILED) {
                String name = data.file.name();
                name = DrawableData.proper(name);
                drawable = new TiledDrawable(atlas.findRegion(name));
                drawable.setMinWidth(data.minWidth);
                drawable.setMinHeight(data.minHeight);
                ((TiledDrawable) drawable).getColor().set(jsonData.getColorByName(data.tintName).color);
            } else if (data.type == DrawableType.NINE_PATCH) {
                String name = data.file.name();
                name = DrawableData.proper(name);
                drawable = new NinePatchDrawable(atlas.createPatch(name));
                if (data.tint != null) {
                    drawable = ((NinePatchDrawable) drawable).tint(data.tint);
                } else if (data.tintName != null) {
                    drawable = ((NinePatchDrawable) drawable).tint(jsonData.getColorByName(data.tintName).color);
                }
                if (!MathUtils.isEqual(data.minWidth, -1))
                    drawable.setMinWidth(data.minWidth);
                if (!MathUtils.isEqual(data.minHeight, -1))
                    drawable.setMinHeight(data.minHeight);
            } else if (data.type == DrawableType.TINTED) {
                String name = DrawableData.proper(data.file.name());
                if (Utils.isNinePatch(data.file.name())) {
                    NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(atlas.createPatch(name));
                    drawable = ninePatchDrawable.tint(data.tint);
                } else {
                    SpriteDrawable spriteDrawable = new SpriteDrawable(atlas.createSprite(name));
                    drawable = spriteDrawable.tint(data.tint);
                }
                if (!MathUtils.isEqual(data.minWidth, -1))
                    drawable.setMinWidth(data.minWidth);
                if (!MathUtils.isEqual(data.minHeight, -1))
                    drawable.setMinHeight(data.minHeight);
            } else if (data.type == DrawableType.TINTED_FROM_COLOR_DATA) {
                String name = DrawableData.proper(data.file.name());
                if (Utils.isNinePatch(data.file.name())) {
                    NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(atlas.createPatch(name));
                    drawable = ninePatchDrawable.tint(jsonData.getColorByName(data.tintName).color);
                } else {
                    SpriteDrawable spriteDrawable = new SpriteDrawable(atlas.createSprite(name));
                    drawable = spriteDrawable.tint(jsonData.getColorByName(data.tintName).color);
                }
                if (!MathUtils.isEqual(data.minWidth, -1))
                    drawable.setMinWidth(data.minWidth);
                if (!MathUtils.isEqual(data.minHeight, -1))
                    drawable.setMinHeight(data.minHeight);
            } else {
                String name = data.file.name();
                name = DrawableData.proper(name);
                drawable = new SpriteDrawable(atlas.createSprite(name));
                if (!MathUtils.isEqual(data.minWidth, -1))
                    drawable.setMinWidth(data.minWidth);
                if (!MathUtils.isEqual(data.minHeight, -1))
                    drawable.setMinHeight(data.minHeight);
            }
            drawablePairs.put(data, drawable);
        }
        return true;
    } catch (Exception e) {
        Gdx.app.error(getClass().getName(), "Error while attempting to generate drawables.", e);
        dialogFactory.showDialogError("Atlas Error...", "Error while attempting to generate drawables.\n\nOpen log?");
        return false;
    }
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) TenPatchDrawable(com.ray3k.tenpatch.TenPatchDrawable) TenPatchDrawable(com.ray3k.tenpatch.TenPatchDrawable) FileNotFoundException(java.io.FileNotFoundException) Array(com.badlogic.gdx.utils.Array)

Aggregations

FileHandle (com.badlogic.gdx.files.FileHandle)207 File (java.io.File)62 IOException (java.io.IOException)38 Array (com.badlogic.gdx.utils.Array)36 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)28 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)16 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)16 Texture (com.badlogic.gdx.graphics.Texture)15 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)13 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)12 ArrayList (java.util.ArrayList)12 Region (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region)11 Color (com.badlogic.gdx.graphics.Color)10 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)10 IntArray (com.badlogic.gdx.utils.IntArray)10 Pixmap (com.badlogic.gdx.graphics.Pixmap)9 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)9 Element (com.badlogic.gdx.utils.XmlReader.Element)9 Locale (java.util.Locale)9 FreeTypeFontGenerator (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator)8