Search in sources :

Example 16 with Element

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

the class BaseTmxMapLoader method loadObject.

protected void loadObject(TiledMap map, MapLayer layer, Element element) {
    if (element.getName().equals("object")) {
        MapObject object = null;
        float scaleX = convertObjectToTileSpace ? 1.0f / mapTileWidth : 1.0f;
        float scaleY = convertObjectToTileSpace ? 1.0f / mapTileHeight : 1.0f;
        float x = element.getFloatAttribute("x", 0) * scaleX;
        float y = (flipY ? (mapHeightInPixels - element.getFloatAttribute("y", 0)) : element.getFloatAttribute("y", 0)) * scaleY;
        float width = element.getFloatAttribute("width", 0) * scaleX;
        float height = element.getFloatAttribute("height", 0) * scaleY;
        if (element.getChildCount() > 0) {
            Element child = null;
            if ((child = element.getChildByName("polygon")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polygon polygon = new Polygon(vertices);
                polygon.setPosition(x, y);
                object = new PolygonMapObject(polygon);
            } else if ((child = element.getChildByName("polyline")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polyline polyline = new Polyline(vertices);
                polyline.setPosition(x, y);
                object = new PolylineMapObject(polyline);
            } else if ((child = element.getChildByName("ellipse")) != null) {
                object = new EllipseMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        if (object == null) {
            String gid = null;
            if ((gid = element.getAttribute("gid", null)) != null) {
                int id = (int) Long.parseLong(gid);
                boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                TiledMapTile tile = map.getTileSets().getTile(id & ~MASK_CLEAR);
                TiledMapTileMapObject tiledMapTileMapObject = new TiledMapTileMapObject(tile, flipHorizontally, flipVertically);
                TextureRegion textureRegion = tiledMapTileMapObject.getTextureRegion();
                tiledMapTileMapObject.getProperties().put("gid", id);
                tiledMapTileMapObject.setX(x);
                tiledMapTileMapObject.setY(flipY ? y : y - height);
                float objectWidth = element.getFloatAttribute("width", textureRegion.getRegionWidth());
                float objectHeight = element.getFloatAttribute("height", textureRegion.getRegionHeight());
                tiledMapTileMapObject.setScaleX(scaleX * (objectWidth / textureRegion.getRegionWidth()));
                tiledMapTileMapObject.setScaleY(scaleY * (objectHeight / textureRegion.getRegionHeight()));
                tiledMapTileMapObject.setRotation(element.getFloatAttribute("rotation", 0));
                object = tiledMapTileMapObject;
            } else {
                object = new RectangleMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        object.setName(element.getAttribute("name", null));
        String rotation = element.getAttribute("rotation", null);
        if (rotation != null) {
            object.getProperties().put("rotation", Float.parseFloat(rotation));
        }
        String type = element.getAttribute("type", null);
        if (type != null) {
            object.getProperties().put("type", type);
        }
        int id = element.getIntAttribute("id", 0);
        if (id != 0) {
            object.getProperties().put("id", id);
        }
        object.getProperties().put("x", x);
        if (object instanceof TiledMapTileMapObject) {
            object.getProperties().put("y", y);
        } else {
            object.getProperties().put("y", (flipY ? y - height : y));
        }
        object.getProperties().put("width", width);
        object.getProperties().put("height", height);
        object.setVisible(element.getIntAttribute("visible", 1) == 1);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(object.getProperties(), properties);
        }
        layer.getObjects().add(object);
    }
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) Polyline(com.badlogic.gdx.math.Polyline) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject)

Example 17 with Element

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

the class BaseTmxMapLoader method getTileIds.

public static int[] getTileIds(Element element, int width, int height) {
    Element data = element.getChildByName("data");
    String encoding = data.getAttribute("encoding", null);
    if (encoding == null) {
        // no 'encoding' attribute means that the encoding is XML
        throw new GdxRuntimeException("Unsupported encoding (XML) for TMX Layer Data");
    }
    int[] ids = new int[width * height];
    if (encoding.equals("csv")) {
        String[] array = data.getText().split(",");
        for (int i = 0; i < array.length; i++) ids[i] = (int) Long.parseLong(array[i].trim());
    } else {
        if (true)
            if (encoding.equals("base64")) {
                InputStream is = null;
                try {
                    String compression = data.getAttribute("compression", null);
                    byte[] bytes = Base64Coder.decode(data.getText());
                    if (compression == null)
                        is = new ByteArrayInputStream(bytes);
                    else if (compression.equals("gzip"))
                        is = new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(bytes), bytes.length));
                    else if (compression.equals("zlib"))
                        is = new BufferedInputStream(new InflaterInputStream(new ByteArrayInputStream(bytes)));
                    else
                        throw new GdxRuntimeException("Unrecognised compression (" + compression + ") for TMX Layer Data");
                    byte[] temp = new byte[4];
                    for (int y = 0; y < height; y++) {
                        for (int x = 0; x < width; x++) {
                            int read = is.read(temp);
                            while (read < temp.length) {
                                int curr = is.read(temp, read, temp.length - read);
                                if (curr == -1)
                                    break;
                                read += curr;
                            }
                            if (read != temp.length)
                                throw new GdxRuntimeException("Error Reading TMX Layer Data: Premature end of tile data");
                            ids[y * width + x] = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8 | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24;
                        }
                    }
                } catch (IOException e) {
                    throw new GdxRuntimeException("Error Reading TMX Layer Data - IOException: " + e.getMessage());
                } finally {
                    StreamUtils.closeQuietly(is);
                }
            } else {
                // or another editor
                throw new GdxRuntimeException("Unrecognised encoding (" + encoding + ") for TMX Layer Data");
            }
    }
    return ids;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Element(com.badlogic.gdx.utils.XmlReader.Element) IOException(java.io.IOException)

Example 18 with Element

use of com.badlogic.gdx.utils.XmlReader.Element 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 19 with Element

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

the class TideMapLoader method loadMap.

/** 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} */
private TiledMap loadMap(Element root, FileHandle tmxFile, ImageResolver imageResolver) {
    TiledMap map = new TiledMap();
    Element properties = root.getChildByName("Properties");
    if (properties != null) {
        loadProperties(map.getProperties(), properties);
    }
    Element tilesheets = root.getChildByName("TileSheets");
    for (Element tilesheet : tilesheets.getChildrenByName("TileSheet")) {
        loadTileSheet(map, tilesheet, tmxFile, imageResolver);
    }
    Element layers = root.getChildByName("Layers");
    for (Element layer : layers.getChildrenByName("Layer")) {
        loadLayer(map, layer);
    }
    return map;
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element)

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