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();
}
});
}
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);
}
}
}
}
}
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;
}
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);
}
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);
}
}
Aggregations