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

The following examples show how to use com.badlogic.gdx.utils.XmlReader.Element#getChildByName() . 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
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 2
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 3
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 4
Source File: NavTmxMapLoader.java    From pathfinding with Apache License 2.0 5 votes vote down vote up
private void loadNavigationLayer(TiledMap map, Element element, String layerName){
	int width = element.getIntAttribute("width", 0);
	int height = element.getIntAttribute("height", 0);
	
	int[] ids = getTileIds(element, width, height);
	TiledMapTileSets tilesets = map.getTileSets();
	GridCell[][] nodes = new GridCell[width][height];
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			int id = ids[y * width + x];
			TiledMapTile tile = tilesets.getTile(id & ~MASK_CLEAR);
			
			GridCell cell = new GridCell(x, height - 1 - y, false);
			if (tile != null) {
				MapProperties tileProp = tile.getProperties();
				
				String walkableProp = tileProp.get(navigationProperty, navigationClosedValue, String.class);
				cell.setWalkable( !walkableProp.equals(navigationClosedValue) );
			}
			nodes[cell.getX()][cell.getY()] = cell;
		}
	}
	
	NavigationTiledMapLayer layer = new NavigationTiledMapLayer(nodes);
	layer.setName(layerName);
	layer.setVisible(false);

	Element properties = element.getChildByName("properties");
	if (properties != null) {
		loadProperties(layer.getProperties(), properties);
	}
	map.getLayers().add(layer);
}
 
Example 5
Source File: MyNavTmxMapLoader.java    From Norii with Apache License 2.0 4 votes vote down vote up
private void loadProperties(Element element, MyNavigationTiledMapLayer layer) {
	Element properties = element.getChildByName("properties");
	if (properties != null) {
		loadProperties(layer.getProperties(), properties);
	}
}
 
Example 6
Source File: ProjectDataConverter.java    From libGDX-Path-Editor with Apache License 2.0 4 votes vote down vote up
private static ScreenData getScreenFromXML(String projPath, String path) throws Exception {
	File scrFile = new File(projPath + path);
	if (!scrFile.exists()) { throw new Exception(); }
	
	File xmlFile = new File(projPath + path);
	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(xmlFile), "utf8"));
	
	XmlReader xmlReader = new XmlReader();
	Element xmlRoot = xmlReader.parse(br);
	
	String name = xmlRoot.get("name", "");
	int w 		= xmlRoot.getInt("width",   -1);
	int h 		= xmlRoot.getInt("height",  -1);
	String xml  = xmlRoot.get("xmlPath",  "");
	String json = xmlRoot.get("jsonPath", "");
	if ((name.length() <= 0) || (xml.length() <= 0) || (json.length() <= 0) ||
		(w <= 0) || (h <= 0)) { throw new Exception(); }
	
	ScreenData scrData = new ScreenData();
	scrData.setName(name);
	scrData.setWidth(w);
	scrData.setHeight(h);
	scrData.setXmlPath(xml);
	scrData.setJsonPath(json);
	
	Element bgRoot = xmlRoot.getChildByName("bg");
	if (bgRoot != null) {
		String bgName    = bgRoot.get("name", "");
		String bgTexPath = bgRoot.get("texturePath", "");
		float bgScaleX 	 = bgRoot.getFloat("scaleX", -1f);
		float bgScaleY 	 = bgRoot.getFloat("scaleY", -1f);
		float bgX 	     = bgRoot.getFloat("x", -1f);
		float bgY 	     = bgRoot.getFloat("y", -1f);
		float bgAngle 	 = bgRoot.getFloat("angle", -1f);
		
		scrData.setBgImage(WidgetManager.createBGImage(bgName, projPath + bgTexPath, bgScaleX, bgScaleY, bgX, bgY, bgAngle));
	}
	
	Element pathRoot = xmlRoot.getChildByName("path");
	if (pathRoot != null) {
		xml = pathRoot.get("xmlPath", "");
		if (xml.length() > 0) {
			scrData.setPath(getPathFromXML(projPath, xml));
		}
	}
	
	return scrData;
}
 
Example 7
Source File: ProjectDataConverter.java    From libGDX-Path-Editor with Apache License 2.0 4 votes vote down vote up
private static GdxPath getPathFromXML(String projPath, String path) throws Exception {
	File scrFile = new File(projPath + path);
	if (!scrFile.exists()) { throw new Exception(); }
	
	File xmlFile = new File(projPath + path);
	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(xmlFile), "utf8"));
	
	XmlReader xmlReader = new XmlReader();
	Element xmlRoot = xmlReader.parse(br);
	
	String name		= xmlRoot.get("name", "");
	int pointsCnt 	= xmlRoot.getInt("pointsCnt", -1);
	String controlColor = xmlRoot.get("controlColor", "");
	String segmentColor = xmlRoot.get("segmentColor", "");
	String selectColor  = xmlRoot.get("selectColor", "");
	String xml  = xmlRoot.get("xmlPath",  "");
	String json = xmlRoot.get("jsonPath", "");
	if ((name.length() <= 0) || (pointsCnt < 0) ||
		(controlColor.length() <= 0) || (segmentColor.length() <= 0) || (selectColor.length() <= 0) ||
		(xml.length() <= 0) || (json.length() <= 0)) { throw new Exception(); }
	
	GdxPath gdxPath = new GdxPath();
	gdxPath.setName(name);
	gdxPath.setPointsCnt(pointsCnt);
	gdxPath.setControlColor(controlColor);
	gdxPath.setSegmentColor(segmentColor);
	gdxPath.setSelectColor(selectColor);
	gdxPath.setXmlPath(xml);
	gdxPath.setJsonPath(json);
	
	Element cvRoot = xmlRoot.getChildByName("controlVertices");
	if (cvRoot == null) { throw new Exception(); }
	
	ArrayList<Vector3> controlVertices = new ArrayList<Vector3>();
	Element cvertexRoot;
	for (int i=0; i<cvRoot.getChildCount(); i++) {
		cvertexRoot = cvRoot.getChild(i);
		controlVertices.add(new Vector3(cvertexRoot.getFloat("x", 0f), cvertexRoot.getFloat("y", 0f), 0f));
	}
	gdxPath.setControlPath(controlVertices);
	
	Element vRoot = xmlRoot.getChildByName("vertices");
	if (vRoot == null) { throw new Exception(); }
	
	Path vertices = new Path();
	Element vertexRoot;
	for (int i=0; i<vRoot.getChildCount(); i++) {
		vertexRoot = vRoot.getChild(i);
		vertices.addPathVertex(vertexRoot.getFloat("x", 0f),
							   vertexRoot.getFloat("y", 0f),
							   vertexRoot.getFloat("tanX", 0f),
							   vertexRoot.getFloat("tanY", 0f));
	}
	gdxPath.setPath(vertices);
	
	return gdxPath;
}