Package com.badlogic.gdx.utils.XmlReader

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


      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

    }
  }

  /** May return null. */
  protected FileHandle loadAtlas (Element root, FileHandle tmxFile) throws IOException {
    Element e = root.getChildByName("properties");

    if (e != null) {
      for (Element property : e.getChildrenByName("property")) {
        String name = property.getAttribute("name", null);
        String value = property.getAttribute("value", null);
        if (name.equals("atlas")) {
          if (value == null) {
            value = property.getText();
View Full Code Here

    mapTileHeight = tileHeight;
    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

          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");
          imageSource = imageElement.getAttribute("source");
          imageWidth = imageElement.getIntAttribute("width", 0);
          imageHeight = imageElement.getIntAttribute("height", 0);
        } 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);
        }
      }

      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 = atlasHandle.nameWithoutExtension();

      if (parameter != null && parameter.forceTextureFilters) {
        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);

      for (AtlasRegion region : atlas.findRegions(regionsName)) {
        // handle unused tile ids
        if (region != null) {
          StaticTiledMapTile tile = new StaticTiledMapTile(region);
          int tileid = firstgid + region.index;
          tile.setId(tileid);
          tile.setOffsetX(offsetX);
          tile.setOffsetY(-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(-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);
          }
        }
      }

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

            layer.setCell(x, height - 1 - y, cell);
          }
        }
      }

      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

      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;
          }
          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;
          }
          Polyline polyline = new Polyline(vertices);
          polyline.setPosition(x, y);
          object = new PolylineMapObject(polyline);
        } else if ((child = element.getChildByName("ellipse")) != null) {
          object = new EllipseMapObject(x, y - height, width, height);
        }
      }
      if (object == null) {
        object = new RectangleMapObject(x, y - height, 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 gid = element.getIntAttribute("gid", -1);
      if (gid != -1) {
        object.getProperties().put("gid", gid);
      }
      object.getProperties().put("x", x * scaleX);
      object.getProperties().put("y", (y - height) * scaleY);
      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

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

   * @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;
  }
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.