List of usage examples for com.badlogic.gdx.utils XmlReader parse
public Element parse(FileHandle file) throws IOException
From source file:com.sixteencolorgames.sandbox.helpers.Loaders.java
public static void loadMaterials(GameLauncher game, FileHandle xml) throws IOException { XmlReader reader = new XmlReader(); XmlReader.Element root = reader.parse(xml); System.out.println("Loading mats"); Array<Element> xmlMaterials = root.getChildrenByName("material"); for (Element mat : xmlMaterials) { int id = mat.getInt("id", 0);//TODO map claimed values String name = mat.get("name", Language.getLocal("mat.noname")); String texture = mat.get("texture", "brick.png"); int variations = mat.getInt("variations", 1); game.materials.put(id, new Material(id, name, texture, variations)); game.matTex.put(name, new Texture(Gdx.files.internal("materials/" + texture))); System.out.println("Adding material: " + name + " id of " + id); }/*from www. j a v a 2s.c o m*/ }
From source file:com.agateau.utils.FileUtils.java
License:Apache License
public static XmlReader.Element parseXml(FileHandle handle) { XmlReader reader = new XmlReader(); XmlReader.Element root = reader.parse(handle); if (root == null) { NLog.e("Failed to parse xml file from %s. No root element.", handle.path()); return null; }// w w w.j ava 2s.c o m return root; }
From source file:com.explatcreations.sft.loading.MapLoader.java
License:Open Source License
public static ITiledMap fromFile(FileHandle file) { final XmlReader xml = new XmlReader(); try {/* www. j a v a 2s.co m*/ final XmlReader.Element root = xml.parse(file); final Map<String, String> propertiesMap = new HashMap<String, String>(); final XmlReader.Element properties = root.getChildByName("properties"); for (int i = 0; i < properties.getChildCount(); i += 1) { XmlReader.Element prop = properties.getChild(i); final String name = prop.getAttribute("name"); final String value = prop.getAttribute("value"); propertiesMap.put(name, value); } final XmlReader.Element layers = root.getChildByName("layer"); final String csv = layers.getChild(0).getText(); final int[][] tiles = parseCsv(csv); return new ITiledMap() { @Override public String getProperty(String key) { return propertiesMap.get(key); } @Override public int[][] getTiles() { return tiles; //To change body of implemented methods use File | Settings | File Templates. } @Override public int getCols() { return tiles.length; } @Override public int getRows() { return tiles[0].length; } }; } catch (IOException ioe) { throw new RuntimeException(ioe); } }
From source file:com.nebula2d.assets.AssetManager.java
License:Open Source License
public static void installAssets(FileHandle assetsFile) throws IOException { XmlReader reader = new XmlReader(); XmlReader.Element root = reader.parse(assetsFile); Array<XmlReader.Element> assets = root.getChildrenByName("asset"); for (XmlReader.Element assetElement : assets) { String path = assetElement.getAttribute("path"); String type = assetElement.getAttribute("assetType"); String sceneName = assetElement.getAttribute("sceneName"); List<AssetDescriptor> assetList = assetMap.getOrDefault(sceneName, new ArrayList<AssetDescriptor>()); if (type.equalsIgnoreCase("SPRITE")) { assetList.add(new AssetDescriptor<Sprite>(path, Sprite.class)); } else if (type.equalsIgnoreCase("MUSIC")) { assetList.add(new AssetDescriptor<MusicTrack>(path, MusicTrack.class)); } else if (type.equalsIgnoreCase("SFX")) { assetList.add(new AssetDescriptor<SoundEffect>(path, SoundEffect.class)); } else if (type.equalsIgnoreCase("SCRIPT")) { assetList.add(new AssetDescriptor<Script>(path, Script.class)); } else if (type.equalsIgnoreCase("TILED_TILE_SHEET")) { assetList.add(new AssetDescriptor<TiledTileSheet>(path, TiledTileSheet.class)); }//from www.ja v a 2s .com assetMap.put(sceneName, assetList); } }
From source file:com.turbogerm.helljump.dataaccess.RiseSectionMetadataReader.java
License:Open Source License
public static RiseSectionsMetadata read(FileHandle fileHandle) { XmlReader reader = new XmlReader(); Element riseSectionsMetaNode = null; try {/*from w w w. j a v a 2 s. c o m*/ riseSectionsMetaNode = reader.parse(fileHandle); } catch (IOException e) { Logger.error(e.getMessage()); return null; } Element riseSectionsNode = riseSectionsMetaNode.getChildByName("risesections"); int numRiseSections = riseSectionsNode.getChildCount(); Array<RiseSectionMetadata> riseSectionMetadataList = new Array<RiseSectionMetadata>(true, numRiseSections); for (int i = 0; i < numRiseSections; i++) { Element riseSectionNode = riseSectionsNode.getChild(i); RiseSectionMetadata riseSection = getRiseSectionMetadata(riseSectionNode); riseSectionMetadataList.add(riseSection); } return new RiseSectionsMetadata(riseSectionMetadataList); }
From source file:com.spritertest.GdxSpriter.java
License:Apache License
private static SpriterData load(FileHandle file) { XmlReader reader = new XmlReader(); SpriterData data = new SpriterData(); try {/* w w w . ja va 2 s . c o m*/ Element root = reader.parse(file); loadFolders(root.getChildrenByName("folder"), data); loadEntities(root.getChildrenByName("entity"), data); } catch (IOException e) { e.printStackTrace(); } return data; }
From source file:com.turbogerm.helljump.dataaccess.RiseSectionDataReader.java
License:Open Source License
public static RiseSectionData read(String type, String name, FileHandle fileHandle) { XmlReader reader = new XmlReader(); Element riseSectionNode = null; try {/*from ww w. j a v a 2 s . c o m*/ riseSectionNode = reader.parse(fileHandle); } catch (IOException e) { Logger.error(e.getMessage()); return null; } int stepRange = ReaderUtilities.getIntAttribute(riseSectionNode, "steprange"); int difficulty = ReaderUtilities.getIntAttribute(riseSectionNode, "difficulty"); NextGeneratedId nextGeneratedId = new NextGeneratedId(); Element platformsNode = riseSectionNode.getChildByName("platforms"); Array<PlatformData> platformDataList = getPlatformsData(nextGeneratedId, platformsNode); Element enemiesNode = riseSectionNode.getChildByName("enemies"); Array<EnemyData> enemyDataList = getEnemiesData(enemiesNode); Element itemsNode = riseSectionNode.getChildByName("items"); Array<ItemData> itemDataList = getItemsData(itemsNode); return new RiseSectionData(type, name, stepRange, difficulty, platformDataList, enemyDataList, itemDataList); }
From source file:com.nebula2d.scene.SceneManager.java
License:Open Source License
public static void loadSceneData(FileHandle scenesFile) throws IOException { XmlReader reader = new XmlReader(); XmlReader.Element root = reader.parse(scenesFile); Array<XmlReader.Element> sceneElements = root.getChildrenByName("scene"); String startScene = root.getAttribute("startScene"); for (XmlReader.Element sceneElement : sceneElements) { String sceneName = sceneElement.getAttribute("name"); XmlReader.Element gravityElement = sceneElement.getChildByName("gravity"); float gX = gravityElement != null ? gravityElement.getFloatAttribute("x", 0.0f) : 0.0f; float gY = gravityElement != null ? gravityElement.getFloatAttribute("y", 0.0f) : 0.0f; boolean sleepPhysics = sceneElement.getBooleanAttribute("sleepPhysics", false); Scene scene = new Scene(sceneName, new Vector2(gX, gY), sleepPhysics); Array<XmlReader.Element> layerElements = sceneElement.getChildrenByName("layer"); for (int i = 0; i < layerElements.size; ++i) { XmlReader.Element layerElement = layerElements.get(i); String layerName = layerElement.getAttribute("name"); Layer layer = new Layer(layerName, i); Array<XmlReader.Element> gameObjectElements = layerElement.getChildrenByName("gameObject"); for (XmlReader.Element gameObjectElement : gameObjectElements) { String gameObjectName = gameObjectElement.getAttribute("name"); float rotation = gameObjectElement.getFloatAttribute("rot"); XmlReader.Element position = gameObjectElement.getChildByName("position"); float posX = position.getFloatAttribute("x"); float posY = position.getFloatAttribute("y"); XmlReader.Element scale = gameObjectElement.getChildByName("scale"); float scaleX = scale.getFloatAttribute("x"); float scaleY = scale.getFloatAttribute("y"); GameObject gameObject = new GameObject(gameObjectName); gameObject.setPosition(posX, posY); gameObject.setScale(scaleX, scaleY); gameObject.setRotation(rotation); Array<XmlReader.Element> componentElements = gameObjectElement.getChildrenByName("component"); for (XmlReader.Element componentElement : componentElements) { String componentName = componentElement.getAttribute("name"); String componentType = componentElement.getAttribute("componentType"); boolean enabled = componentElement.getBooleanAttribute("enabled"); Component component = null; if (componentType.equalsIgnoreCase("RENDER")) { String rendererType = componentElement.getAttribute("rendererType"); if (rendererType.equalsIgnoreCase("SPRITE_RENDERER")) { String spritePath = componentElement.getAttribute("sprite"); component = new SpriteRenderer(componentName, spritePath); } else if (rendererType.equalsIgnoreCase("TILE_MAP_RENDERER")) { String tileSheetPath = componentElement.getAttribute("tileSheet"); component = new TiledMapRenderer(componentName, tileSheetPath); }/* w w w .j a va2 s . c o m*/ } else if (componentType.equalsIgnoreCase("MUSIC")) { String trackPath = componentElement.getAttribute("track"); component = new MusicSource(componentName, trackPath); } else if (componentType.equalsIgnoreCase("SFX")) { String sfxPath = componentElement.getAttribute("sfx"); component = new SoundEffectSource(componentName, sfxPath); } else if (componentType.equalsIgnoreCase("BEHAVE")) { String scriptPath = componentElement.getAttribute("script"); component = new Behavior(componentName, scriptPath); } else if (componentType.equalsIgnoreCase("RIGID_BODY")) { boolean isKinematic = componentElement.getBooleanAttribute("isKinematic"); boolean fixedRotation = componentElement.getBooleanAttribute("fixedRotation"); boolean isBullet = componentElement.getBooleanAttribute("isBullet"); float mass = componentElement.getFloatAttribute("mass"); XmlReader.Element shapeElement = componentElement.getChildByName("collisionShape"); String shapeType = shapeElement.getAttribute("shapeType"); XmlReader.Element materialElement = shapeElement.getChild(0); float density = materialElement.getFloatAttribute("density"); float friction = materialElement.getFloatAttribute("friction"); float restitution = materialElement.getFloatAttribute("restitution"); PhysicsMaterial material = new PhysicsMaterial(density, friction, restitution); CollisionShape shape = null; if (shapeType.equalsIgnoreCase("BOX")) { float w = shapeElement.getFloatAttribute("w"); float h = shapeElement.getFloatAttribute("h"); shape = new BoundingBox(material, w, h); } else if (shapeType.equalsIgnoreCase("CIRCLE")) { float r = shapeElement.getFloatAttribute("r"); shape = new Circle(material, r); } if (shape != null) { component = new RigidBody(componentName, shape, isKinematic, mass, fixedRotation, isBullet); } } else if (componentType.equalsIgnoreCase("COLLIDER")) { boolean isSensor = componentElement.getBooleanAttribute("isSensor"); XmlReader.Element shapeElement = componentElement.getChildByName("collisionShape"); String shapeType = shapeElement.getAttribute("shapeType"); XmlReader.Element materialElement = shapeElement.getChild(0); float density = materialElement.getFloatAttribute("density"); float friction = materialElement.getFloatAttribute("friction"); float restitution = materialElement.getFloatAttribute("restitution"); PhysicsMaterial material = new PhysicsMaterial(density, friction, restitution); CollisionShape shape = null; if (shapeType.equalsIgnoreCase("BOX")) { float w = shapeElement.getFloatAttribute("w"); float h = shapeElement.getFloatAttribute("h"); shape = new BoundingBox(material, w, h); } else if (shapeType.equalsIgnoreCase("CIRCLE")) { float r = shapeElement.getFloatAttribute("r"); shape = new Circle(material, r); } if (shape != null) { component = new Collider(componentName, shape, isSensor); } } if (component != null) { component.setEnabled(enabled); gameObject.addComponent(component); } } layer.addGameObject(gameObject); } scene.addLayer(layer); } addScene(scene); } setCurrentScene(startScene); }
From source file:com.steelkiwi.patheditor.proj.ProjectDataConverter.java
License:Apache License
public static ProjectData openProject(String path) throws Exception { File projectFile = new File(path); if (!projectFile.exists()) { throw new Exception(); }/*from w w w . j a v a 2s .co m*/ 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; }
From source file:com.steelkiwi.patheditor.proj.ProjectDataConverter.java
License:Apache License
private static ScreenData getScreenFromXML(String projPath, String path) throws Exception { File scrFile = new File(projPath + path); if (!scrFile.exists()) { throw new Exception(); }// www. j ava 2 s. c o m 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; }