Search in sources :

Example 1 with Element

use of com.badlogic.gdx.utils.XmlReader.Element in project libgdx by libgdx.

the class BaseTmxMapLoader method loadImageLayer.

protected void loadImageLayer(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    if (element.getName().equals("imagelayer")) {
        int x = Integer.parseInt(element.getAttribute("x", "0"));
        int y = Integer.parseInt(element.getAttribute("y", "0"));
        if (flipY)
            y = mapHeightInPixels - y;
        TextureRegion texture = null;
        Element image = element.getChildByName("image");
        if (image != null) {
            String source = image.getAttribute("source");
            FileHandle handle = getRelativeFileHandle(tmxFile, source);
            texture = imageResolver.getImage(handle.path());
            y -= texture.getRegionHeight();
        }
        TiledMapImageLayer layer = new TiledMapImageLayer(texture, x, y);
        loadBasicLayerInfo(layer, element);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(layer.getProperties(), properties);
        }
        map.getLayers().add(layer);
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

Example 2 with Element

use of com.badlogic.gdx.utils.XmlReader.Element in project libgdx by libgdx.

the class TideMapLoader method loadLayer.

private void loadLayer(TiledMap map, Element element) {
    if (element.getName().equals("Layer")) {
        String id = element.getAttribute("Id");
        String visible = element.getAttribute("Visible");
        Element dimensions = element.getChildByName("Dimensions");
        String layerSize = dimensions.getAttribute("LayerSize");
        String tileSize = dimensions.getAttribute("TileSize");
        String[] layerSizeParts = layerSize.split(" x ");
        int layerSizeX = Integer.parseInt(layerSizeParts[0]);
        int layerSizeY = Integer.parseInt(layerSizeParts[1]);
        String[] tileSizeParts = tileSize.split(" x ");
        int tileSizeX = Integer.parseInt(tileSizeParts[0]);
        int tileSizeY = Integer.parseInt(tileSizeParts[1]);
        TiledMapTileLayer layer = new TiledMapTileLayer(layerSizeX, layerSizeY, tileSizeX, tileSizeY);
        layer.setName(id);
        layer.setVisible(visible.equalsIgnoreCase("True"));
        Element tileArray = element.getChildByName("TileArray");
        Array<Element> rows = tileArray.getChildrenByName("Row");
        TiledMapTileSets tilesets = map.getTileSets();
        TiledMapTileSet currentTileSet = null;
        int firstgid = 0;
        int x, y;
        for (int row = 0, rowCount = rows.size; row < rowCount; row++) {
            Element currentRow = rows.get(row);
            y = rowCount - 1 - row;
            x = 0;
            for (int child = 0, childCount = currentRow.getChildCount(); child < childCount; child++) {
                Element currentChild = currentRow.getChild(child);
                String name = currentChild.getName();
                if (name.equals("TileSheet")) {
                    currentTileSet = tilesets.getTileSet(currentChild.getAttribute("Ref"));
                    firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
                } else if (name.equals("Null")) {
                    x += currentChild.getIntAttribute("Count");
                } else if (name.equals("Static")) {
                    Cell cell = new Cell();
                    cell.setTile(currentTileSet.getTile(firstgid + currentChild.getIntAttribute("Index")));
                    layer.setCell(x++, y, cell);
                } else if (name.equals("Animated")) {
                    // Create an AnimatedTile
                    int interval = currentChild.getInt("Interval");
                    Element frames = currentChild.getChildByName("Frames");
                    Array<StaticTiledMapTile> frameTiles = new Array<StaticTiledMapTile>();
                    for (int frameChild = 0, frameChildCount = frames.getChildCount(); frameChild < frameChildCount; frameChild++) {
                        Element frame = frames.getChild(frameChild);
                        String frameName = frame.getName();
                        if (frameName.equals("TileSheet")) {
                            currentTileSet = tilesets.getTileSet(frame.getAttribute("Ref"));
                            firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
                        } else if (frameName.equals("Static")) {
                            frameTiles.add((StaticTiledMapTile) currentTileSet.getTile(firstgid + frame.getIntAttribute("Index")));
                        }
                    }
                    Cell cell = new Cell();
                    cell.setTile(new AnimatedTiledMapTile(interval / 1000f, frameTiles));
                    // TODO: Reuse existing animated tiles
                    layer.setCell(x++, y, cell);
                }
            }
        }
        Element properties = element.getChildByName("Properties");
        if (properties != null) {
            loadProperties(layer.getProperties(), properties);
        }
        map.getLayers().add(layer);
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) Element(com.badlogic.gdx.utils.XmlReader.Element) Array(com.badlogic.gdx.utils.Array) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Example 3 with Element

use of com.badlogic.gdx.utils.XmlReader.Element in project libgdx by libgdx.

the class TideMapLoader method loadTileSheets.

/** Loads the tilesets
	 * @param root the root XML element
	 * @return a list of filenames for images containing tiles
	 * @throws IOException */
private Array<FileHandle> loadTileSheets(Element root, FileHandle tideFile) throws IOException {
    Array<FileHandle> images = new Array<FileHandle>();
    Element tilesheets = root.getChildByName("TileSheets");
    for (Element tileset : tilesheets.getChildrenByName("TileSheet")) {
        Element imageSource = tileset.getChildByName("ImageSource");
        FileHandle image = getRelativeFileHandle(tideFile, imageSource.getText());
        images.add(image);
    }
    return images;
}
Also used : Array(com.badlogic.gdx.utils.Array) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

Example 4 with Element

use of com.badlogic.gdx.utils.XmlReader.Element in project libgdx by libgdx.

the class TideMapLoader method loadProperties.

private void loadProperties(MapProperties properties, Element element) {
    if (element.getName().equals("Properties")) {
        for (Element property : element.getChildrenByName("Property")) {
            String key = property.getAttribute("Key", null);
            String type = property.getAttribute("Type", null);
            String value = property.getText();
            if (type.equals("Int32")) {
                properties.put(key, Integer.parseInt(value));
            } else if (type.equals("String")) {
                properties.put(key, value);
            } else if (type.equals("Boolean")) {
                properties.put(key, value.equalsIgnoreCase("true"));
            } else {
                properties.put(key, value);
            }
        }
    }
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element)

Example 5 with Element

use of com.badlogic.gdx.utils.XmlReader.Element in project libgdx by libgdx.

the class TmxMapLoader method loadTilemap.

/** Loads the map data, given the XML root element and an {@link ImageResolver} used to return the tileset Textures
	 * @param root the XML root element
	 * @param tmxFile the Filehandle of the tmx file
	 * @param imageResolver the {@link ImageResolver}
	 * @return the {@link TiledMap} */
protected TiledMap loadTilemap(Element root, FileHandle tmxFile, ImageResolver imageResolver) {
    TiledMap map = new TiledMap();
    String mapOrientation = root.getAttribute("orientation", null);
    int mapWidth = root.getIntAttribute("width", 0);
    int mapHeight = root.getIntAttribute("height", 0);
    int tileWidth = root.getIntAttribute("tilewidth", 0);
    int tileHeight = root.getIntAttribute("tileheight", 0);
    int hexSideLength = root.getIntAttribute("hexsidelength", 0);
    String staggerAxis = root.getAttribute("staggeraxis", null);
    String staggerIndex = root.getAttribute("staggerindex", null);
    String mapBackgroundColor = root.getAttribute("backgroundcolor", null);
    MapProperties mapProperties = map.getProperties();
    if (mapOrientation != null) {
        mapProperties.put("orientation", mapOrientation);
    }
    mapProperties.put("width", mapWidth);
    mapProperties.put("height", mapHeight);
    mapProperties.put("tilewidth", tileWidth);
    mapProperties.put("tileheight", tileHeight);
    mapProperties.put("hexsidelength", hexSideLength);
    if (staggerAxis != null) {
        mapProperties.put("staggeraxis", staggerAxis);
    }
    if (staggerIndex != null) {
        mapProperties.put("staggerindex", staggerIndex);
    }
    if (mapBackgroundColor != null) {
        mapProperties.put("backgroundcolor", mapBackgroundColor);
    }
    mapTileWidth = tileWidth;
    mapTileHeight = tileHeight;
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;
    if (mapOrientation != null) {
        if ("staggered".equals(mapOrientation)) {
            if (mapHeight > 1) {
                mapWidthInPixels += tileWidth / 2;
                mapHeightInPixels = mapHeightInPixels / 2 + tileHeight / 2;
            }
        }
    }
    Element properties = root.getChildByName("properties");
    if (properties != null) {
        loadProperties(map.getProperties(), properties);
    }
    Array<Element> tilesets = root.getChildrenByName("tileset");
    for (Element element : tilesets) {
        loadTileSet(map, element, tmxFile, imageResolver);
        root.removeChild(element);
    }
    for (int i = 0, j = root.getChildCount(); i < j; i++) {
        Element element = root.getChild(i);
        String name = element.getName();
        if (name.equals("layer")) {
            loadTileLayer(map, element);
        } else if (name.equals("objectgroup")) {
            loadObjectGroup(map, element);
        } else if (name.equals("imagelayer")) {
            loadImageLayer(map, element, tmxFile, imageResolver);
        }
    }
    return map;
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element) MapProperties(com.badlogic.gdx.maps.MapProperties)

Aggregations

Element (com.badlogic.gdx.utils.XmlReader.Element)19 FileHandle (com.badlogic.gdx.files.FileHandle)9 Array (com.badlogic.gdx.utils.Array)7 IntArray (com.badlogic.gdx.utils.IntArray)5 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)4 MapProperties (com.badlogic.gdx.maps.MapProperties)4 AnimatedTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile)4 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)4 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)4 IOException (java.io.IOException)4 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2 MapObject (com.badlogic.gdx.maps.MapObject)2 EllipseMapObject (com.badlogic.gdx.maps.objects.EllipseMapObject)2 PolygonMapObject (com.badlogic.gdx.maps.objects.PolygonMapObject)2 PolylineMapObject (com.badlogic.gdx.maps.objects.PolylineMapObject)2 RectangleMapObject (com.badlogic.gdx.maps.objects.RectangleMapObject)2 Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)2 TiledMapTileMapObject (com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject)2 LongArray (com.badlogic.gdx.utils.LongArray)2 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)1