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

The following examples show how to use com.badlogic.gdx.utils.XmlReader.Element#get() . 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: TmxObjectsLoader.java    From RuinsOfRevenge with MIT License 5 votes vote down vote up
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 2
Source File: SpriteAnimation.java    From RuinsOfRevenge with MIT License 5 votes vote down vote up
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 3
Source File: ProjectDataConverter.java    From libGDX-Path-Editor with Apache License 2.0 5 votes vote down vote up
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 4
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 5
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;
}