Java Code Examples for com.badlogic.gdx.utils.XmlReader.Element#getAttribute()

The following examples show how to use com.badlogic.gdx.utils.XmlReader.Element#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UAtlasTmxMapLoader.java    From uracer-kotd with Apache License 2.0 6 votes vote down vote up
@Override
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));
				}
			}
		}
	} catch (IOException e) {
		throw new GdxRuntimeException("Unable to parse .tmx file.");
	}
	return dependencies;
}
 
Example 2
Source File: UAtlasTmxMapLoader.java    From uracer-kotd with Apache License 2.0 6 votes vote down vote up
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")) {
			loadObject(layer, objectElement);
		}

		map.getLayers().add(layer);
	}
}
 
Example 3
Source File: UAtlasTmxMapLoader.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
/** 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();
				}

				if (value == null || value.length() == 0) {
					// keep trying until there are no more atlas properties
					continue;
				}

				return getRelativeFileHandle(tmxFile, value);
			}
		}
	} else {
		FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
		return atlasFile.exists() ? atlasFile : null;
	}

	return null;
}
 
Example 4
Source File: UAtlasTmxMapLoader.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
protected void loadTileLayer (TiledMap map, Element element) {
	if (element.getName().equals("layer")) {
		String name = element.getAttribute("name", null);
		int width = element.getIntAttribute("width", 0);
		int height = element.getIntAttribute("height", 0);
		int tileWidth = element.getParent().getIntAttribute("tilewidth", 0);
		int tileHeight = element.getParent().getIntAttribute("tileheight", 0);
		boolean visible = element.getIntAttribute("visible", 1) == 1;
		float opacity = element.getFloatAttribute("opacity", 1.0f);
		TiledMapTileLayer layer = new TiledMapTileLayer(width, height, tileWidth, tileHeight);
		layer.setVisible(visible);
		layer.setOpacity(opacity);
		layer.setName(name);

		int[] ids = TmxMapHelper.getTileIds(element, width, height);
		TiledMapTileSets tilesets = map.getTileSets();
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int id = ids[y * width + x];
				boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
				boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
				boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

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

		Element properties = element.getChildByName("properties");
		if (properties != null) {
			loadProperties(layer.getProperties(), properties);
		}
		map.getLayers().add(layer);
	}
}
 
Example 5
Source File: UAtlasTmxMapLoader.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
protected void loadProperties (MapProperties properties, Element element) {
	if (element.getName().equals("properties")) {
		for (Element property : element.getChildrenByName("property")) {
			String name = property.getAttribute("name", null);
			String value = property.getAttribute("value", null);
			if (value == null) {
				value = property.getText();
			}
			properties.put(name, value);
		}
	}
}
 
Example 6
Source File: MyNavTmxMapLoader.java    From Norii with Apache License 2.0 5 votes vote down vote up
@Override
protected void loadTileLayer(TiledMap map,MapLayers maplayers, Element element) {
	String layerName = element.getAttribute("name", null);
	if ( navigationLayerName.equals(layerName)){
		loadNavigationLayer(map, element, layerName);
	}
	else{
		super.loadTileLayer(map,maplayers, element);
	}
}
 
Example 7
Source File: NavTmxMapLoader.java    From pathfinding with Apache License 2.0 5 votes vote down vote up
@Override
protected void loadTileLayer(TiledMap map, Element element) {
	String layerName = element.getAttribute("name", null);
	if ( navigationLayerName.equals(layerName)){
		loadNavigationLayer(map, element, layerName);
	}
	else{
		super.loadTileLayer(map, element);
	}
}
 
Example 8
Source File: UAtlasTmxMapLoader.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
protected TiledMap loadMap (Element root, FileHandle tmxFile, AtlasResolver resolver, AtlasTiledMapLoaderParameters parameter) {
	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);
	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);
	if (mapBackgroundColor != null) {
		mapProperties.put("backgroundcolor", mapBackgroundColor);
	}

	mapTileWidth = tileWidth;
	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")) {
			loadTileLayer(map, element);
		} else if (elementName.equals("objectgroup")) {
			loadObjectGroup(map, element);
		}
	}
	return map;
}