Search in sources :

Example 56 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class AtlasTmxMapLoader method getDependencies.

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle tmxFile, AtlasTiledMapLoaderParameters parameter) {
    Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
    try {
        root = xml.parse(tmxFile);
        Element properties = root.getChildByName("properties");
        if (properties != null) {
            for (Element property : properties.getChildrenByName("property")) {
                String name = property.getAttribute("name");
                String value = property.getAttribute("value");
                if (name.startsWith("atlas")) {
                    FileHandle atlasHandle = getRelativeFileHandle(tmxFile, value);
                    dependencies.add(new AssetDescriptor(atlasHandle, TextureAtlas.class));
                }
            }
        }
    } catch (IOException e) {
        throw new GdxRuntimeException("Unable to parse .tmx file.");
    }
    return dependencies;
}
Also used : Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) LongArray(com.badlogic.gdx.utils.LongArray) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) IOException(java.io.IOException) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 57 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class AtlasTmxMapLoader method loadTileset.

protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);
        int offsetX = 0;
        int offsetY = 0;
        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;
        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandle(tmxFile, imageSource);
            }
        }
        String atlasFilePath = map.getProperties().get("atlas", String.class);
        if (atlasFilePath == null) {
            FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
            if (atlasFile.exists())
                atlasFilePath = atlasFile.name();
        }
        if (atlasFilePath == null) {
            throw new GdxRuntimeException("The map is missing the 'atlas' property");
        }
        // get the TextureAtlas for this tileset
        FileHandle atlasHandle = getRelativeFileHandle(tmxFile, atlasFilePath);
        atlasHandle = resolve(atlasHandle.path());
        TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
        String regionsName = name;
        for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        MapProperties props = tileset.getProperties();
        tileset.setName(name);
        props.put("firstgid", firstgid);
        props.put("imagesource", imageSource);
        props.put("imagewidth", imageWidth);
        props.put("imageheight", imageHeight);
        props.put("tilewidth", tilewidth);
        props.put("tileheight", tileheight);
        props.put("margin", margin);
        props.put("spacing", spacing);
        if (imageSource != null && imageSource.length() > 0) {
            int lastgid = firstgid + ((imageWidth / tilewidth) * (imageHeight / tileheight)) - 1;
            for (AtlasRegion region : atlas.findRegions(regionsName)) {
                // handle unused tile ids
                if (region != null) {
                    int tileid = region.index + 1;
                    if (tileid >= firstgid && tileid <= lastgid) {
                        StaticTiledMapTile tile = new StaticTiledMapTile(region);
                        tile.setId(tileid);
                        tile.setOffsetX(offsetX);
                        tile.setOffsetY(flipY ? -offsetY : offsetY);
                        tileset.putTile(tileid, tile);
                    }
                }
            }
        }
        for (Element tileElement : element.getChildrenByName("tile")) {
            int tileid = firstgid + tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(tileid);
            if (tile == null) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    // Is a tilemap with individual images.
                    String regionName = imageElement.getAttribute("source");
                    regionName = regionName.substring(0, regionName.lastIndexOf('.'));
                    AtlasRegion region = atlas.findRegion(regionName);
                    if (region == null)
                        throw new GdxRuntimeException("Tileset region not found: " + regionName);
                    tile = new StaticTiledMapTile(region);
                    tile.setId(tileid);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(tileid, tile);
                }
            }
            if (tile != null) {
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");
        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();
        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {
                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset.getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }
                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) MapProperties(com.badlogic.gdx.maps.MapProperties) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) LongArray(com.badlogic.gdx.utils.LongArray) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) IntArray(com.badlogic.gdx.utils.IntArray) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 58 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class BaseTmxMapLoader method getRelativeFileHandle.

protected static FileHandle getRelativeFileHandle(FileHandle file, String path) {
    StringTokenizer tokenizer = new StringTokenizer(path, "\\/");
    FileHandle result = file.parent();
    while (tokenizer.hasMoreElements()) {
        String token = tokenizer.nextToken();
        if (token.equals(".."))
            result = result.parent();
        else {
            result = result.child(token);
        }
    }
    return result;
}
Also used : StringTokenizer(java.util.StringTokenizer) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 59 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class TideMapLoader method loadTileSheet.

private void loadTileSheet(TiledMap map, Element element, FileHandle tideFile, ImageResolver imageResolver) {
    if (element.getName().equals("TileSheet")) {
        String id = element.getAttribute("Id");
        String description = element.getChildByName("Description").getText();
        String imageSource = element.getChildByName("ImageSource").getText();
        Element alignment = element.getChildByName("Alignment");
        String sheetSize = alignment.getAttribute("SheetSize");
        String tileSize = alignment.getAttribute("TileSize");
        String margin = alignment.getAttribute("Margin");
        String spacing = alignment.getAttribute("Spacing");
        String[] sheetSizeParts = sheetSize.split(" x ");
        int sheetSizeX = Integer.parseInt(sheetSizeParts[0]);
        int sheetSizeY = Integer.parseInt(sheetSizeParts[1]);
        String[] tileSizeParts = tileSize.split(" x ");
        int tileSizeX = Integer.parseInt(tileSizeParts[0]);
        int tileSizeY = Integer.parseInt(tileSizeParts[1]);
        String[] marginParts = margin.split(" x ");
        int marginX = Integer.parseInt(marginParts[0]);
        int marginY = Integer.parseInt(marginParts[1]);
        String[] spacingParts = margin.split(" x ");
        int spacingX = Integer.parseInt(spacingParts[0]);
        int spacingY = Integer.parseInt(spacingParts[1]);
        FileHandle image = getRelativeFileHandle(tideFile, imageSource);
        TextureRegion texture = imageResolver.getImage(image.path());
        TiledMapTileSets tilesets = map.getTileSets();
        int firstgid = 1;
        for (TiledMapTileSet tileset : tilesets) {
            firstgid += tileset.size();
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(id);
        tileset.getProperties().put("firstgid", firstgid);
        int gid = firstgid;
        int stopWidth = texture.getRegionWidth() - tileSizeX;
        int stopHeight = texture.getRegionHeight() - tileSizeY;
        for (int y = marginY; y <= stopHeight; y += tileSizeY + spacingY) {
            for (int x = marginX; x <= stopWidth; x += tileSizeX + spacingX) {
                TiledMapTile tile = new StaticTiledMapTile(new TextureRegion(texture, x, y, tileSizeX, tileSizeY));
                tile.setId(gid);
                tileset.putTile(gid++, tile);
            }
        }
        Element properties = element.getChildByName("Properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        tilesets.addTileSet(tileset);
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Example 60 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class TideMapLoader method getRelativeFileHandle.

private static FileHandle getRelativeFileHandle(FileHandle file, String path) {
    StringTokenizer tokenizer = new StringTokenizer(path, "\\/");
    FileHandle result = file.parent();
    while (tokenizer.hasMoreElements()) {
        String token = tokenizer.nextToken();
        if (token.equals(".."))
            result = result.parent();
        else {
            result = result.child(token);
        }
    }
    return result;
}
Also used : StringTokenizer(java.util.StringTokenizer) FileHandle(com.badlogic.gdx.files.FileHandle)

Aggregations

FileHandle (com.badlogic.gdx.files.FileHandle)79 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)23 IOException (java.io.IOException)22 Array (com.badlogic.gdx.utils.Array)18 File (java.io.File)18 Element (com.badlogic.gdx.utils.XmlReader.Element)9 Texture (com.badlogic.gdx.graphics.Texture)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)5 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)4 Json (com.badlogic.gdx.utils.Json)4 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3 Color (com.badlogic.gdx.graphics.Color)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Writer (java.io.Writer)3