List of usage examples for com.badlogic.gdx.utils JsonValue iterator
public JsonIterator iterator()
From source file:com.jupiter.europa.entity.component.EffectsComponent.java
License:Open Source License
@Override public void read(Json json, JsonValue jsonData) { if (jsonData.has(EFFECTS_KEY)) { JsonValue selectedData = jsonData.get(EFFECTS_KEY); if (selectedData.isArray()) { selectedData.iterator().forEach((JsonValue value) -> { if (value.has(EFFECT_CLASS_KEY) && value.has(EFFECT_DATA_KEY)) { String typeName = value.getString(EFFECT_CLASS_KEY); try { Class<?> type = Class.forName(typeName); if (Effect.class.isAssignableFrom(type)) { this.effects .add((Effect) json.fromJson(type, value.get(EFFECT_DATA_KEY).toString())); }// www . j av a 2 s . co m } catch (ClassNotFoundException ex) { } } }); } } }
From source file:com.jupiter.europa.entity.EuropaEntity.java
License:Open Source License
@Override public void read(Json json, JsonValue jsonData) { if (jsonData.has(OLD_ID_KEY)) { long oldID = jsonData.getLong(OLD_ID_KEY); EuropaGame.game.lastIdMapping.put(oldID, this); }//from w w w . j a va 2 s . com if (jsonData.has(COMPONENTS_KEY)) { JsonValue componentsValue = jsonData.get(COMPONENTS_KEY); componentsValue.iterator().forEach((JsonValue value) -> { try { if (value.has(COMPONENT_CLASS_KEY)) { Class<?> type = Class.forName(value.getString(COMPONENT_CLASS_KEY)); if (Component.class.isAssignableFrom(type)) { this.add((Component) json.fromJson(type, value.get(COMPONENT_DATA_KEY).prettyPrint(EuropaGame.PRINT_SETTINGS))); } } } catch (ClassNotFoundException ex) { } }); this.initializeComponents(); } }
From source file:com.jupiter.europa.entity.stats.AttributeSet.java
License:Open Source License
@Override public void read(Json json, JsonValue jsonData) { jsonData.iterator().forEach((JsonValue value) -> { Attributes attr = Attributes.valueOf(value.name()); if (attr != null) { this.attributes.put(attr, value.asInt()); }/*from w w w.j av a 2 s .c om*/ }); }
From source file:com.jupiter.europa.entity.stats.SkillSet.java
License:Open Source License
@Override public void read(Json json, JsonValue jsonData) { jsonData.iterator().forEach((JsonValue value) -> { SkillSet.Skills skill = SkillSet.Skills.valueOf(value.name()); if (skill != null) { this.skills.put(skill, value.asInt()); }/* w w w . ja va2s .c om*/ }); }
From source file:com.jupiter.europa.entity.trait.TraitPool.java
License:Open Source License
@Override public void read(Json json, JsonValue jsonData) { if (jsonData.has(CAPACITY_KEY)) { this.capacity = jsonData.getInt(CAPACITY_KEY); }//from w w w .j a v a 2 s.c o m if (jsonData.has(SELECTED_KEY)) { JsonValue selectedData = jsonData.get(SELECTED_KEY); if (selectedData.isArray()) { selectedData.iterator().forEach((JsonValue value) -> { if (value.has(ITEM_CLASS_KEY) && value.has(ITEM_DATA_KEY)) { String typeName = value.getString(ITEM_CLASS_KEY); try { Class<?> type = Class.forName(typeName); if (Trait.class.isAssignableFrom(type)) { this.selected.add((T) json.fromJson(type, value.get(ITEM_DATA_KEY).toString())); } } catch (ClassNotFoundException ex) { } } }); } } }
From source file:com.mbrlabs.mundus.runtime.SceneLoader.java
License:Apache License
private GameObject convertGameObject(SceneGraph sceneGraph, JsonValue jsonGo) { final GameObject go = new GameObject(sceneGraph, jsonGo.getString(JsonScene.GO_NAME, ""), jsonGo.getInt(JsonScene.GO_ID)); go.active = jsonGo.getBoolean(JsonScene.GO_ACTIVE, true); // TODO tags// w w w. j a va 2 s .co m // model component JsonValue modelComp = jsonGo.get(JsonScene.GO_MODEL_COMPONENT); if (modelComp != null) { ModelComponent mc = new ModelComponent(go, mundus.getShaders().getModelShader()); mc.setModel((ModelAsset) assetManager .findAssetByID(modelComp.getString(JsonScene.MODEL_COMPONENT_MODEL_ID)), false); JsonValue mats = modelComp.get(JsonScene.MODEL_COMPONENT_MATERIALS); for (JsonValue mat : mats.iterator()) { mc.getMaterials().put(mat.name, (MaterialAsset) assetManager.findAssetByID(mats.getString(mat.name))); } mc.applyMaterials(); try { go.addComponent(mc); } catch (InvalidComponentException e) { e.printStackTrace(); } } // TODO terrain component JsonValue terrainComp = jsonGo.get(JsonScene.GO_TERRAIN_COMPONENT); if (terrainComp != null) { TerrainComponent tc = new TerrainComponent(go, mundus.getShaders().getTerrainShader()); tc.setTerrain((TerrainAsset) assetManager .findAssetByID(terrainComp.getString(JsonScene.TERRAIN_COMPONENT_TERRAIN_ID))); try { go.addComponent(tc); } catch (InvalidComponentException e) { e.printStackTrace(); } } // transformation final float[] transform = jsonGo.get(JsonScene.GO_TRANSFORM).asFloatArray(); go.setLocalPosition(transform[0], transform[1], transform[2]); go.setLocalRotation(transform[3], transform[4], transform[5], transform[6]); go.setLocalScale(transform[7], transform[8], transform[9]); // children JsonValue children = jsonGo.get(JsonScene.GO_CHILDREN); if (children != null) { for (JsonValue c : children) { go.addChild(convertGameObject(sceneGraph, c)); } } return go; }
From source file:com.o2d.pkayjava.editor.data.migrations.migrators.VersionMigTo009.java
License:Apache License
private void fixAnimations(JsonValue value) { if (value.get("sComposites") == null) return;/*from w w w . j a v a 2s . c om*/ if (value.get("sComposites") != null) { JsonValue.JsonIterator compositeArray = value.get("sComposites").iterator(); while (compositeArray.hasNext()) { JsonValue composite = compositeArray.next(); if (composite != null) { fixAnimations(composite.get("composite")); } } } if (value.get("sSpriteAnimations") != null) { JsonValue.JsonIterator spriteArray = value.get("sSpriteAnimations").iterator(); while (spriteArray.hasNext()) { JsonValue valAnim = spriteArray.next(); if (valAnim.get("animations") != null) { String val = valAnim.get("animations").asString(); JsonValue animationsInnerJson = jsonReader.parse(val); JsonValue.JsonIterator innerArray = animationsInnerJson.iterator(); String cnt = "["; while (innerArray.hasNext()) { JsonValue innerVal = innerArray.next(); int startFrame = innerVal.get("startFrame").asInt(); int endFrame = innerVal.get("endFrame").asInt(); String currName = innerVal.get("name").asString(); cnt += "{\"name\":\"" + currName + "\", \"startFrame\":" + startFrame + ", \"endFrame\":" + endFrame + "}, "; } if (cnt.length() == 1) { cnt = "[]"; } else { cnt = cnt.substring(0, cnt.length() - 2) + "]"; } JsonValue newVal = jsonReader.parse(cnt); setNewKeyToJson(valAnim, "frameRangeMap", "animations", newVal); } } } }
From source file:com.ray3k.skincomposer.data.JsonData.java
License:Open Source License
public void readFile(FileHandle fileHandle) throws Exception { main.getProjectData().setChangesSaved(false); //read drawables from texture atlas file FileHandle atlasHandle = fileHandle.sibling(fileHandle.nameWithoutExtension() + ".atlas"); if (atlasHandle.exists()) { main.getProjectData().getAtlasData().readAtlas(atlasHandle); }//from w w w.j av a 2 s . c om //folder for critical files to be copied to FileHandle saveFile = main.getProjectData().getSaveFile(); FileHandle targetDirectory; if (saveFile != null) { targetDirectory = saveFile.sibling(saveFile.nameWithoutExtension() + "_data"); } else { targetDirectory = Gdx.files.local("temp/" + main.getProjectData().getId() + "_data"); } //read json file and create styles JsonReader reader = new JsonReader(); JsonValue val = reader.parse(fileHandle); for (JsonValue child : val.iterator()) { //fonts if (child.name().equals(BitmapFont.class.getName())) { for (JsonValue font : child.iterator()) { FileHandle fontFile = fileHandle.sibling(font.getString("file")); FileHandle fontCopy = targetDirectory.child(font.getString("file")); if (!fontCopy.parent().equals(fontFile.parent())) { fontFile.copyTo(fontCopy); } FontData fontData = new FontData(font.name(), fontCopy); //delete fonts with the same name for (FontData originalData : new Array<>(fonts)) { if (originalData.getName().equals(fontData.getName())) { fonts.removeValue(originalData, true); } } fonts.add(fontData); BitmapFont.BitmapFontData bitmapFontData = new BitmapFont.BitmapFontData(fontCopy, false); for (String path : bitmapFontData.imagePaths) { FileHandle file = new FileHandle(path); main.getProjectData().getAtlasData() .getDrawable(file.nameWithoutExtension()).visible = false; } } } //colors else if (child.name().equals(Color.class.getName())) { for (JsonValue color : child.iterator()) { ColorData colorData = new ColorData(color.name, new Color(color.getFloat("r", 0.0f), color.getFloat("g", 0.0f), color.getFloat("b", 0.0f), color.getFloat("a", 0.0f))); //delete colors with the same name for (ColorData originalData : new Array<>(colors)) { if (originalData.getName().equals(colorData.getName())) { colors.removeValue(originalData, true); } } colors.add(colorData); } } //tinted drawables else if (child.name().equals(TintedDrawable.class.getName())) { for (JsonValue tintedDrawable : child.iterator()) { DrawableData drawableData = new DrawableData(main.getProjectData().getAtlasData() .getDrawable(tintedDrawable.getString("name")).file); drawableData.name = tintedDrawable.name; if (!tintedDrawable.get("color").isString()) { drawableData.tint = new Color(tintedDrawable.get("color").getFloat("r", 0.0f), tintedDrawable.get("color").getFloat("g", 0.0f), tintedDrawable.get("color").getFloat("b", 0.0f), tintedDrawable.get("color").getFloat("a", 0.0f)); } else { drawableData.tintName = tintedDrawable.getString("color"); } //todo:test overwriting a base drawable that is depended on by another tint //delete drawables with the same name for (DrawableData originalData : new Array<>( main.getProjectData().getAtlasData().getDrawables())) { if (originalData.name.equals(drawableData.name)) { main.getProjectData().getAtlasData().getDrawables().removeValue(originalData, true); } } main.getProjectData().getAtlasData().getDrawables().add(drawableData); } } //styles else { int classIndex = 0; Class matchClass = ClassReflection.forName(child.name); for (Class clazz : Main.STYLE_CLASSES) { if (clazz.equals(matchClass)) { break; } else { classIndex++; } } Class clazz = Main.BASIC_CLASSES[classIndex]; for (JsonValue style : child.iterator()) { StyleData data = newStyle(clazz, style.name); for (JsonValue property : style.iterator()) { StyleProperty styleProperty = data.properties.get(property.name); if (styleProperty.type.equals(Float.TYPE)) { styleProperty.value = (double) property.asFloat(); } else if (styleProperty.type.equals(Color.class)) { if (property.isString()) { styleProperty.value = property.asString(); } else { Gdx.app.error(getClass().getName(), "Can't import JSON files that do not use predefined colors."); } } else { if (property.isString()) { styleProperty.value = property.asString(); } else { Gdx.app.error(getClass().getName(), "Can't import JSON files that do not use String names for field values."); } } } } } } }
From source file:com.siondream.core.animation.SpriteAnimationLoader.java
License:Apache License
/** * Aynchronously loads the animation data animations *///w w w. jav a 2s . c o m @Override public void loadAsync(AssetManager manager, String fileName, FileHandle file, AnimationParameter parameter) { logger.info("loading " + fileName); animationData = new SpriteAnimationData(); // Retrieve texture animationData.texture = manager.get(stripExtension(fileName) + ".png", Texture.class); try { JsonReader reader = new JsonReader(); JsonValue root = reader.parse(file); animationData.rows = root.getInt("rows"); animationData.columns = root.getInt("columns"); animationData.frameDuration = root.getFloat("frameDuration"); JsonValue animations = root.get("animations"); JsonIterator animationsIt = animations.iterator(); boolean first = true; while (animationsIt.hasNext()) { JsonValue animationValue = animationsIt.next(); String name = animationValue.getString("name"); String frames = animationValue.getString("frames"); Animation animation = new Animation(animationData.frameDuration, getAnimationFrames(animationData.texture, frames), getPlayMode(animationValue.getString("mode", "normal"))); animationData.animations.put(name, animation); logger.info("" + fileName + " loaded animation " + name); if (first) { animationData.defaultAnimation = animation; first = false; } } } catch (Exception e) { logger.error("error loading file " + fileName + " " + e.getMessage()); } }
From source file:com.siondream.core.physics.MapBodyManager.java
License:Open Source License
private void loadMaterialsFile(FileHandle materialsFile) { logger.info("adding default material"); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = 1.0f;//from ww w .ja v a2 s. co m fixtureDef.friction = 1.0f; fixtureDef.restitution = 0.0f; materials.put("default", fixtureDef); logger.info("loading materials file"); try { JsonReader reader = new JsonReader(); JsonValue root = reader.parse(materialsFile); JsonIterator materialIt = root.iterator(); while (materialIt.hasNext()) { JsonValue materialValue = materialIt.next(); if (!materialValue.has("name")) { logger.error("material without name"); continue; } String name = materialValue.getString("name"); fixtureDef = new FixtureDef(); fixtureDef.density = materialValue.getFloat("density", 1.0f); fixtureDef.friction = materialValue.getFloat("friction", 1.0f); fixtureDef.restitution = materialValue.getFloat("restitution", 0.0f); logger.info("adding material " + name); materials.put(name, fixtureDef); } } catch (Exception e) { logger.error("error loading " + materialsFile.name() + " " + e.getMessage()); } }