Package com.badlogic.gdx.utils.XmlReader

Examples of com.badlogic.gdx.utils.XmlReader.Element


    }
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;

    for (int i = 0, j = root.getChildCount(); i < j; i++) {
      Element element = root.getChild(i);
      String elementName = element.getName();
      if (elementName.equals("properties")) {
        loadProperties(map.getProperties(), element);
      } else if (elementName.equals("tileset")) {
        loadTileset(map, element, tmxFile, resolver, parameter);
      } else if (elementName.equals("layer")) {
View Full Code Here


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

      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(tileset.getProperties(), properties);
      }
      map.getTileSets().addTileSet(tileset);
    }
View Full Code Here

      layer.setOpacity(opacity);
      layer.setName(name);

      TiledMapTileSets tilesets = map.getTileSets();

      Element data = element.getChildByName("data");
      String encoding = data.getAttribute("encoding", null);
      String compression = data.getAttribute("compression", null);
      if (encoding == null) { // no 'encoding' attribute means that the encoding is XML
        throw new GdxRuntimeException("Unsupported encoding (XML) for TMX Layer Data");
      }
      if (encoding.equals("csv")) {
        String[] array = data.getText().split(",");
        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            int id = (int)Long.parseLong(array[y * width + x].trim());

            final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
            final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
            final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

            id = id & ~MASK_CLEAR;

            tilesets.getTile(id);
            TiledMapTile tile = tilesets.getTile(id);
            if (tile != null) {
              Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
              cell.setTile(tile);
              layer.setCell(x, yUp ? height - 1 - y : y, cell);
            }
          }
        }
      } else {
        if (encoding.equals("base64")) {
          byte[] bytes = Base64Coder.decode(data.getText());
          if (compression == null) {
            int read = 0;
            for (int y = 0; y < height; y++) {
              for (int x = 0; x < width; x++) {

                int id = unsignedByteToInt(bytes[read++]) | unsignedByteToInt(bytes[read++]) << 8
                  | unsignedByteToInt(bytes[read++]) << 16 | unsignedByteToInt(bytes[read++]) << 24;

                final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                id = id & ~MASK_CLEAR;

                tilesets.getTile(id);
                TiledMapTile tile = tilesets.getTile(id);
                if (tile != null) {
                  Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                  cell.setTile(tile);
                  layer.setCell(x, yUp ? height - 1 - y : y, cell);
                }
              }
            }
          } else if (compression.equals("gzip")) {
            GZIPInputStream GZIS = null;
            try {
              GZIS = new GZIPInputStream(new ByteArrayInputStream(bytes), bytes.length);
            } catch (IOException e) {
              throw new GdxRuntimeException("Error Reading TMX Layer Data - IOException: " + e.getMessage());
            }

            byte[] temp = new byte[4];
            for (int y = 0; y < height; y++) {
              for (int x = 0; x < width; x++) {
                try {
                  GZIS.read(temp, 0, 4);
                  int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8
                    | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24;

                  final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                  final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                  final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                  id = id & ~MASK_CLEAR;

                  tilesets.getTile(id);
                  TiledMapTile tile = tilesets.getTile(id);
                  if (tile != null) {
                    Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                    cell.setTile(tile);
                    layer.setCell(x, yUp ? height - 1 - y : y, cell);
                  }
                } catch (IOException e) {
                  throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                }
              }
            }
          } else if (compression.equals("zlib")) {
            Inflater zlib = new Inflater();

            byte[] temp = new byte[4];

            zlib.setInput(bytes, 0, bytes.length);

            for (int y = 0; y < height; y++) {
              for (int x = 0; x < width; x++) {
                try {
                  zlib.inflate(temp, 0, 4);
                  int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8
                    | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24;

                  final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                  final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                  final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                  id = id & ~MASK_CLEAR;

                  tilesets.getTile(id);
                  TiledMapTile tile = tilesets.getTile(id);
                  if (tile != null) {
                    Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                    cell.setTile(tile);
                    layer.setCell(x, yUp ? height - 1 - y : y, cell);
                  }

                } catch (DataFormatException e) {
                  throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                }
              }
            }
          }
        } else {
          // any other value of 'encoding' is one we're not aware of, probably a feature of a future version of Tiled
          // or another editor
          throw new GdxRuntimeException("Unrecognised encoding (" + encoding + ") for TMX Layer Data");
        }
      }
      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(layer.getProperties(), properties);
      }
      map.getLayers().add(layer);
    }
View Full Code Here

  protected void loadObjectGroup (TiledMap map, Element element) {
    if (element.getName().equals("objectgroup")) {
      String name = element.getAttribute("name", null);
      MapLayer layer = new MapLayer();
      layer.setName(name);
      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(layer.getProperties(), properties);
      }

      for (Element objectElement : element.getChildrenByName("object")) {
View Full Code Here

      int width = element.getIntAttribute("width", 0);
      int height = element.getIntAttribute("height", 0);

      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] = Integer.parseInt(point[0]);
            vertices[i * 2 + 1] = Integer.parseInt(point[1]);
            if (yUp) {
              vertices[i * 2 + 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] = Integer.parseInt(point[0]);
            vertices[i * 2 + 1] = Integer.parseInt(point[1]);
            if (yUp) {
              vertices[i * 2 + 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, yUp ? y - height : y, width, height);
        }
      }
      if (object == null) {
        object = new RectangleMapObject(x, yUp ? y - height : y, width, height);
      }
      object.setName(element.getAttribute("name", null));
      String type = element.getAttribute("type", null);
      if (type != null) {
        object.getProperties().put("type", type);
      }
      int gid = element.getIntAttribute("gid", -1);
      if (gid != -1) {
        object.getProperties().put("gid", gid);
      }
      object.getProperties().put("x", x);
      object.getProperties().put("y", yUp ? y - height : y);
      object.setVisible(element.getIntAttribute("visible", 1) == 1);
      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(object.getProperties(), properties);
      }
      layer.getObjects().add(object);
    }
View Full Code Here

  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));
View Full Code Here

      mapProperties.put("backgroundcolor", mapBackgroundColor);
    }
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;

    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);
      }
View Full Code Here

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

      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(tileset.getProperties(), properties);
      }
      map.getTileSets().addTileSet(tileset);
    }
View Full Code Here

      layer.setOpacity(opacity);
      layer.setName(name);

      TiledMapTileSets tilesets = map.getTileSets();

      Element data = element.getChildByName("data");
      String encoding = data.getAttribute("encoding", null);
      String compression = data.getAttribute("compression", null);
      if (encoding == null) { // no 'encoding' attribute means that the encoding is XML
        throw new GdxRuntimeException("Unsupported encoding (XML) for TMX Layer Data");
      }
      if (encoding.equals("csv")) {
        String[] array = data.getText().split(",");
        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            int id = (int)Long.parseLong(array[y * width + x].trim());

            final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
            final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
            final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

            id = id & ~MASK_CLEAR;

            tilesets.getTile(id);
            TiledMapTile tile = tilesets.getTile(id);
            if (tile != null) {
              Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
              cell.setTile(tile);
              layer.setCell(x, yUp ? height - 1 - y : y, cell);
            }
          }
        }
      } else {
        if (encoding.equals("base64")) {
          byte[] bytes = Base64Coder.decode(data.getText());
          if (compression == null) {
            int read = 0;
            for (int y = 0; y < height; y++) {
              for (int x = 0; x < width; x++) {

                int id = unsignedByteToInt(bytes[read++]) | unsignedByteToInt(bytes[read++]) << 8
                  | unsignedByteToInt(bytes[read++]) << 16 | unsignedByteToInt(bytes[read++]) << 24;

                final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                id = id & ~MASK_CLEAR;

                tilesets.getTile(id);
                TiledMapTile tile = tilesets.getTile(id);
                if (tile != null) {
                  Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                  cell.setTile(tile);
                  layer.setCell(x, yUp ? height - 1 - y : y, cell);
                }
              }
            }
          } else if (compression.equals("gzip")) {
            GZIPInputStream GZIS = null;
            try {
              GZIS = new GZIPInputStream(new ByteArrayInputStream(bytes), bytes.length);
            } catch (IOException e) {
              throw new GdxRuntimeException("Error Reading TMX Layer Data - IOException: " + e.getMessage());
            }

            byte[] temp = new byte[4];
            for (int y = 0; y < height; y++) {
              for (int x = 0; x < width; x++) {
                try {
                  GZIS.read(temp, 0, 4);
                  int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8
                    | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24;

                  final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                  final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                  final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                  id = id & ~MASK_CLEAR;

                  tilesets.getTile(id);
                  TiledMapTile tile = tilesets.getTile(id);
                  if (tile != null) {
                    Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                    cell.setTile(tile);
                    layer.setCell(x, yUp ? height - 1 - y : y, cell);
                  }
                } catch (IOException e) {
                  throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                }
              }
            }
          } else if (compression.equals("zlib")) {
            Inflater zlib = new Inflater();

            byte[] temp = new byte[4];

            zlib.setInput(bytes, 0, bytes.length);

            for (int y = 0; y < height; y++) {
              for (int x = 0; x < width; x++) {
                try {
                  zlib.inflate(temp, 0, 4);
                  int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8
                    | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24;

                  final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                  final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                  final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                  id = id & ~MASK_CLEAR;

                  tilesets.getTile(id);
                  TiledMapTile tile = tilesets.getTile(id);
                  if (tile != null) {
                    Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                    cell.setTile(tile);
                    layer.setCell(x, yUp ? height - 1 - y : y, cell);
                  }

                } catch (DataFormatException e) {
                  throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                }
              }
            }
          }
        } else {
          // any other value of 'encoding' is one we're not aware of, probably a feature of a future version of Tiled
          throw new GdxRuntimeException("Unrecognised encoding (" + encoding + ") for TMX Layer Data");
        }
      }
      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(layer.getProperties(), properties);
      }
      map.getLayers().add(layer);
    }
View Full Code Here

  protected void loadObjectGroup (TiledMap map, Element element) {
    if (element.getName().equals("objectgroup")) {
      String name = element.getAttribute("name", null);
      MapLayer layer = new MapLayer();
      layer.setName(name);
      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(layer.getProperties(), properties);
      }

      for (Element objectElement : element.getChildrenByName("object")) {
View Full Code Here

TOP

Related Classes of com.badlogic.gdx.utils.XmlReader.Element

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.