com.badlogic.gdx.utils.XmlReader.Element Java Examples
The following examples show how to use
com.badlogic.gdx.utils.XmlReader.Element.
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 |
@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 |
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: MyNavTmxMapLoader.java From Norii with Apache License 2.0 | 6 votes |
private GridCell[][] createGridCells(TiledMap map, Element element, int width, int height) { 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; } } return nodes; }
Example #4
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 6 votes |
private void readImageTag(Element image) throws RuntimeException { if (!image.getAttributes().containsKey("file")) throw new RuntimeException("need file=\"...\" attribute"); Texture tex = new Texture(fileLocation.getFile(image.get("file"))); for (int i = 0; i < image.getChildCount(); i++) { Element child = image.getChild(i); switch (child.getName()) { case "region": readRegionTag(tex, child); break; case "animation": readAnimationTag(tex, child); break; default: throw new RuntimeException("Expected <region> or <animation> tag"); } } }
Example #5
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 6 votes |
private void readResourcesTag(Element resources) throws RuntimeException { for (int i = 0; i < resources.getChildCount(); i++) { Element child = resources.getChild(i); switch (child.getName()) { case "images": readImagesTag(child); break; case "skins": readSkinsTag(child); break; default: throw new RuntimeException("Expected <images> or <skins> tag"); } } }
Example #6
Source File: UAtlasTmxMapLoader.java From uracer-kotd with Apache License 2.0 | 5 votes |
/** 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 #7
Source File: ProjectDataConverter.java From libGDX-Path-Editor with Apache License 2.0 | 5 votes |
public static ProjectData openProject(String path) throws Exception { File projectFile = new File(path); if (!projectFile.exists()) { throw new Exception(); } File xmlFile = new File(path); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(xmlFile), "utf8")); XmlReader xmlReader = new XmlReader(); Element xmlRoot = xmlReader.parse(br); if (xmlRoot == null) { throw new Exception(); } String projName = xmlRoot.get("name", ""); String projPath = new File(path).getParent(); //xmlRoot.get("path", ""); //TODO if ((projName.length() <= 0) || (projPath.length() <= 0)) { throw new Exception(); } ProjectData projData = new ProjectData(); projData.setName(projName); projData.setPath(projPath); Array<Element> screensRoot = xmlRoot.getChildrenByName("screen"); if ((screensRoot == null) || (screensRoot.size <= 0)) { return projData; } String xmlPath; String jsonPath; for (int i=0; i<screensRoot.size; i++) { xmlPath = screensRoot.get(i).get("xml", ""); jsonPath = screensRoot.get(i).get("json", ""); if ((xmlPath.length() <= 0) || (jsonPath.length() <= 0)) { throw new Exception(); } ScreenData scrData = getScreenFromJSON(projPath, jsonPath); //ScreenData scrData = getScreenFromXML(projPath, xmlPath); if (scrData == null) { throw new Exception(); } projData.getScreens().add(scrData); } return projData; }
Example #8
Source File: SpriteAnimation.java From RuinsOfRevenge with MIT License | 5 votes |
public SpriteAnimation(Texture texture, Element elem) throws RuntimeException { this.tex = texture; this.name = elem.get("name", "noname"); int frames = elem.getChildCount(); if (frames == 0) throw new RuntimeException("animation is empty"); delays = new float[frames]; keyframes = new TextureRegion[frames]; if (elem.getAttributes().containsKey("delay")) { setDelay(elem.getFloat("delay")); } for (int i = 0; i < elem.getChildCount(); i++) { Element child = elem.getChild(i); keyframes[i] = XmlUtils.getTexReg(tex, child.getAttribute("bounds")); if (child.getAttributes().containsKey("delay")) { delays[i] = child.getFloat("delay"); } } float totalDelay = 0; for (float delay : delays) { totalDelay += delay; } totalDelays = totalDelay; }
Example #9
Source File: TmxObjectsLoader.java From RuinsOfRevenge with MIT License | 5 votes |
public static void main(String... args) throws IOException { GdxNativesLoader.load(); Physics physics = new Physics(Vector2.Zero, true); File mapfile = new File("data/maps/newmap/map005.tmx"); Element mapXML = new XmlReader().parse(new FileInputStream(mapfile)); System.out.println(mapXML); new TmxObjectsLoader(mapXML) .loadToPhysics(physics, 32, 32, 50, 50); }
Example #10
Source File: TmxObjectsLoader.java From RuinsOfRevenge with MIT License | 5 votes |
public TmxObjectGroup parseObjectgroup(Element element) { TmxObjectGroup group = new TmxObjectGroup( element.get("name", ""), element.getFloat("width", 0), element.getFloat("height", 0)); Array<Element> objs = element.getChildrenByName("object"); for (Element obj : objs) { Type type = null; String points = ""; if (obj.getChildCount() == 0) type = Type.RECTANGLE; else { switch (obj.getChild(0).getName().toLowerCase()) { case "polygon": type = Type.POLYGON; break; case "polyline": type = Type.POLYLINE; break; case "ellipse": type = Type.ELLIPSE; break; default: type = Type.RECTANGLE; break; } if (type == Type.POLYGON || type == Type.POLYLINE) { points = obj.getChild(0).getAttribute("points", "0,0"); } } group.objects.add(new TmxObject( obj.get("name", ""), obj.get("type", ""), points, type, obj.getFloat("x", 0), obj.getFloat("y", 0), obj.getFloat("width", 0), obj.getFloat("height", 0))); } return group; }
Example #11
Source File: TmxObjectsLoader.java From RuinsOfRevenge with MIT License | 5 votes |
public TmxObjectsLoader(Element tmxRootElement) { this.groups = new ArrayList<>(); Array<Element> objectgroups = tmxRootElement.getChildrenByName("objectgroup"); for (Element objectgroupElement : objectgroups) { groups.add(parseObjectgroup(objectgroupElement)); } this.tileWidth = tmxRootElement.getFloat("tilewidth", 32); this.tileHeight = tmxRootElement.getFloat("tileheight", 32); this.mapwidth = tmxRootElement.getInt("width", 50); this.mapheight = tmxRootElement.getInt("height", 50); }
Example #12
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 5 votes |
private void readSkinsTag(Element resources) { for (int i = 0; i < resources.getChildCount(); i++) { Element child = resources.getChild(i); switch (child.getName()) { case "skin": readSkinTag(child); break; default: throw new RuntimeException("Expected <skin /> tag"); } } }
Example #13
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 5 votes |
private void readImagesTag(Element images) throws RuntimeException { for (int i = 0; i < images.getChildCount(); i++) { Element child = images.getChild(i); switch (child.getName()) { case "image": readImageTag(child); break; default: throw new RuntimeException("Expected <image> tag"); } } }
Example #14
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 5 votes |
public ResourceLoader(FileLocation fileLocation, FileHandle resourceXml) throws IOException { this.fileLocation = fileLocation; regions = new ObjectMap<>(); anims = new ObjectMap<>(); skins = new ObjectMap<>(); Element resources = new XmlReader().parse(resourceXml); readResourcesTag(resources); }
Example #15
Source File: NavTmxMapLoader.java From pathfinding with Apache License 2.0 | 5 votes |
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 #16
Source File: NavTmxMapLoader.java From pathfinding with Apache License 2.0 | 5 votes |
@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 #17
Source File: MyNavTmxMapLoader.java From Norii with Apache License 2.0 | 5 votes |
private void loadNavigationLayer(TiledMap map, Element element, String layerName){ int width = element.getIntAttribute("width", 0); int height = element.getIntAttribute("height", 0); GridCell[][] nodes = createGridCells(map, element, width, height); MyNavigationTiledMapLayer layer = createNavigationTiledMapLayer(layerName, nodes); loadProperties(element, layer); map.getLayers().add(layer); }
Example #18
Source File: MyNavTmxMapLoader.java From Norii with Apache License 2.0 | 5 votes |
@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 #19
Source File: UAtlasTmxMapLoader.java From uracer-kotd with Apache License 2.0 | 5 votes |
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 #20
Source File: UAtlasTmxMapLoader.java From uracer-kotd with Apache License 2.0 | 5 votes |
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 #21
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 4 votes |
private void readRegionTag(Texture tex, Element region) throws RuntimeException { if (!region.getAttributes().containsKey("name") || !region.getAttributes().containsKey("bounds")) throw new RuntimeException("need name=\"...\" and bounds=\"...\" properties"); regions.put(region.get("name"), XmlUtils.getTexReg(tex, region.get("bounds"))); }
Example #22
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 4 votes |
private void readAnimationTag(Texture tex, Element anim) throws RuntimeException { if (!anim.getAttributes().containsKey("name")) throw new RuntimeException("need name=\"...\" property"); anims.put(anim.get("name"), new SpriteAnimation(tex, anim)); }
Example #23
Source File: MyNavTmxMapLoader.java From Norii with Apache License 2.0 | 4 votes |
private void loadProperties(Element element, MyNavigationTiledMapLayer layer) { Element properties = element.getChildByName("properties"); if (properties != null) { loadProperties(layer.getProperties(), properties); } }
Example #24
Source File: ResourceLoader.java From RuinsOfRevenge with MIT License | 4 votes |
private void readSkinTag(Element skin) { if (!skin.getAttributes().containsKey("name") || !skin.getAttributes().containsKey("file")) throw new RuntimeException("need name=\"...\" and file=\"...\" properties"); skins.put(skin.get("name"), new Skin(fileLocation.getFile(skin.get("file")))); }
Example #25
Source File: UAtlasTmxMapLoader.java From uracer-kotd with Apache License 2.0 | 4 votes |
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; }
Example #26
Source File: ProjectDataConverter.java From libGDX-Path-Editor with Apache License 2.0 | 4 votes |
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 #27
Source File: ProjectDataConverter.java From libGDX-Path-Editor with Apache License 2.0 | 4 votes |
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; }