Package com.badlogic.gdx.utils.XmlReader

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


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


  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));
            layer.setCell(x++, y, cell); // TODO: Reuse existing animated tiles
View Full Code Here

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

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

      TextureRegion texture = imageResolver.getImage(image.path());

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

      int stopWidth = texture.getRegionWidth() - tilewidth;
      int stopHeight = texture.getRegionHeight() - tileheight;

      int id = firstgid;

      for (int y = margin; y <= stopHeight; y += tileheight + spacing) {
        for (int x = margin; x <= stopWidth; x += tilewidth + spacing) {
          TextureRegion tileRegion = new TextureRegion(texture, x, y, tilewidth, tileheight);
          TiledMapTile tile = new StaticTiledMapTile(tileRegion);
          tile.setId(id);
          tile.setOffsetX(offsetX);
          tile.setOffsetY(-offsetY);
          tileset.putTile(id++, tile);
        }
      }

      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>();
            LongArray intervals = new LongArray();
            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);
    }
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

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.