Search in sources :

Example 1 with Page

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page in project commons-gdx by gemserk.

the class LibgdxResourceBuilder method splitLoadingTextureAtlas.

public void splitLoadingTextureAtlas(final String id, final String file) {
    FileHandle packFile = Gdx.files.internal(file);
    final TextureAtlasData textureAtlasData = new TextureAtlasData(packFile, packFile.parent(), false);
    Array<Page> pages = textureAtlasData.getPages();
    final String pageTextureSuffix = "_generated_page_";
    for (int i = 0; i < pages.size; i++) {
        Page page = pages.get(i);
        FileHandle textureFile = page.textureFile;
        resource(id + pageTextureSuffix + i, texture2(textureFile).format(page.format).useMipMaps(page.useMipMaps).magFilter(page.magFilter).minFilter(page.minFilter));
    }
    resourceManager.add(id, new DataLoader<TextureAtlas>() {

        @Override
        public TextureAtlas load() {
            Array<Page> pages = textureAtlasData.getPages();
            for (int i = 0; i < pages.size; i++) {
                Page page = pages.get(i);
                try {
                    String textureResourceId = id + pageTextureSuffix + i;
                    page.texture = resourceManager.getResourceValue(textureResourceId);
                    if (page.texture == null)
                        throw new RuntimeException("The resource " + textureResourceId + " was not found");
                } catch (Exception e) {
                    throw new RuntimeException("Error while loading page for textureAtlas " + id + " - page: " + page.textureFile.path(), e);
                }
            }
            return new TextureAtlas(textureAtlasData);
        }

        @Override
        public void unload(TextureAtlas atlas) {
            atlas.dispose();
        }
    });
}
Also used : Array(com.badlogic.gdx.utils.Array) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 2 with Page

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page in project libgdx by libgdx.

the class TextureUnpacker method splitAtlas.

/** Splits an atlas into seperate image and ninepatch files. */
public void splitAtlas(TextureAtlasData atlas, String outputDir) {
    // create the output directory if it did not exist yet
    File outputDirFile = new File(outputDir);
    if (!outputDirFile.exists()) {
        outputDirFile.mkdirs();
        System.out.println(String.format("Creating directory: %s", outputDirFile.getPath()));
    }
    for (Page page : atlas.getPages()) {
        // load the image file belonging to this page as a Buffered Image
        BufferedImage img = null;
        try {
            img = ImageIO.read(page.textureFile.file());
        } catch (IOException e) {
            printExceptionAndExit(e);
        }
        for (Region region : atlas.getRegions()) {
            System.out.println(String.format("Processing image for %s: x[%s] y[%s] w[%s] h[%s], rotate[%s]", region.name, region.left, region.top, region.width, region.height, region.rotate));
            // check if the page this region is in is currently loaded in a Buffered Image
            if (region.page == page) {
                BufferedImage splitImage = null;
                String extension = null;
                // check if the region is a ninepatch or a normal image and delegate accordingly
                if (region.splits == null) {
                    splitImage = extractImage(img, region, outputDirFile, 0);
                    extension = OUTPUT_TYPE;
                } else {
                    splitImage = extractNinePatch(img, region, outputDirFile);
                    extension = String.format("9.%s", OUTPUT_TYPE);
                }
                // check if the parent directories of this image file exist and create them if not
                File imgOutput = new File(outputDirFile, String.format("%s.%s", region.index == -1 ? region.name : region.name + "_" + region.index, extension));
                File imgDir = imgOutput.getParentFile();
                if (!imgDir.exists()) {
                    System.out.println(String.format("Creating directory: %s", imgDir.getPath()));
                    imgDir.mkdirs();
                }
                // save the image
                try {
                    ImageIO.write(splitImage, OUTPUT_TYPE, imgOutput);
                } catch (IOException e) {
                    printExceptionAndExit(e);
                }
            }
        }
    }
}
Also used : Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 3 with Page

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page in project libgdx by libgdx.

the class TextureAtlasLoader method getDependencies.

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle atlasFile, TextureAtlasParameter parameter) {
    FileHandle imgDir = atlasFile.parent();
    if (parameter != null)
        data = new TextureAtlasData(atlasFile, imgDir, parameter.flip);
    else {
        data = new TextureAtlasData(atlasFile, imgDir, false);
    }
    Array<AssetDescriptor> dependencies = new Array();
    for (Page page : data.getPages()) {
        TextureParameter params = new TextureParameter();
        params.format = page.format;
        params.genMipMaps = page.useMipMaps;
        params.minFilter = page.minFilter;
        params.magFilter = page.magFilter;
        dependencies.add(new AssetDescriptor(page.textureFile, Texture.class, params));
    }
    return dependencies;
}
Also used : Array(com.badlogic.gdx.utils.Array) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) FileHandle(com.badlogic.gdx.files.FileHandle) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) TextureParameter(com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter) Texture(com.badlogic.gdx.graphics.Texture) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 4 with Page

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page in project libgdx by libgdx.

the class TextureAtlasLoader method load.

@Override
public TextureAtlas load(AssetManager assetManager, String fileName, FileHandle file, TextureAtlasParameter parameter) {
    for (Page page : data.getPages()) {
        Texture texture = assetManager.get(page.textureFile.path().replaceAll("\\\\", "/"), Texture.class);
        page.texture = texture;
    }
    return new TextureAtlas(data);
}
Also used : TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) Texture(com.badlogic.gdx.graphics.Texture)

Example 5 with Page

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page in project libgdx by libgdx.

the class TextureAtlas method load.

private void load(TextureAtlasData data) {
    ObjectMap<Page, Texture> pageToTexture = new ObjectMap<Page, Texture>();
    for (Page page : data.pages) {
        Texture texture = null;
        if (page.texture == null) {
            texture = new Texture(page.textureFile, page.format, page.useMipMaps);
            texture.setFilter(page.minFilter, page.magFilter);
            texture.setWrap(page.uWrap, page.vWrap);
        } else {
            texture = page.texture;
            texture.setFilter(page.minFilter, page.magFilter);
            texture.setWrap(page.uWrap, page.vWrap);
        }
        textures.add(texture);
        pageToTexture.put(page, texture);
    }
    for (Region region : data.regions) {
        int width = region.width;
        int height = region.height;
        AtlasRegion atlasRegion = new AtlasRegion(pageToTexture.get(region.page), region.left, region.top, region.rotate ? height : width, region.rotate ? width : height);
        atlasRegion.index = region.index;
        atlasRegion.name = region.name;
        atlasRegion.offsetX = region.offsetX;
        atlasRegion.offsetY = region.offsetY;
        atlasRegion.originalHeight = region.originalHeight;
        atlasRegion.originalWidth = region.originalWidth;
        atlasRegion.rotate = region.rotate;
        atlasRegion.splits = region.splits;
        atlasRegion.pads = region.pads;
        if (region.flip)
            atlasRegion.flip(false, true);
        regions.add(atlasRegion);
    }
}
Also used : ObjectMap(com.badlogic.gdx.utils.ObjectMap) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) Texture(com.badlogic.gdx.graphics.Texture)

Aggregations

Page (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page)5 Texture (com.badlogic.gdx.graphics.Texture)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)2 Region (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region)2 Array (com.badlogic.gdx.utils.Array)2 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)1 TextureParameter (com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter)1 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)1 ObjectMap (com.badlogic.gdx.utils.ObjectMap)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1