List of usage examples for com.badlogic.gdx.utils JsonValue getFloat
public float getFloat(String name, float defaultValue)
From source file:com.ahsgaming.valleyofbones.map.TileLayer.java
License:Apache License
public static TileLayer createFromJson(JsonValue value) { TileLayer tileLayer = new TileLayer(); tileLayer.traversible = value.getBoolean("traversible", true); tileLayer.collidable = value.getBoolean("collidable", false); tileLayer.visible = value.getBoolean("visible", true); tileLayer.opacity = value.getFloat("opacity", 1); tileLayer.data = new Array<Integer>(); for (JsonValue v : value.get("data")) { tileLayer.data.add(v.asInt());//ww w . j a va2 s . co m } return tileLayer; }
From source file:com.company.minery.utils.spine.SkeletonJson.java
License:Open Source License
public SkeletonData readSkeletonData(FileHandle file) { if (file == null) throw new IllegalArgumentException("file cannot be null."); float scale = this.scale; SkeletonData skeletonData = new SkeletonData(); skeletonData.name = file.nameWithoutExtension(); JsonValue root = new JsonReader().parse(file); // Skeleton./* w w w .ja v a2s . c om*/ JsonValue skeletonMap = root.get("skeleton"); if (skeletonMap != null) { skeletonData.hash = skeletonMap.getString("hash", null); skeletonData.version = skeletonMap.getString("spine", null); skeletonData.width = skeletonMap.getFloat("width", 0); skeletonData.height = skeletonMap.getFloat("height", 0); skeletonData.imagesPath = skeletonMap.getString("images", null); } // Bones. for (JsonValue boneMap = root.getChild("bones"); boneMap != null; boneMap = boneMap.next) { BoneData parent = null; String parentName = boneMap.getString("parent", null); if (parentName != null) { parent = skeletonData.findBone(parentName); if (parent == null) throw new SerializationException("Parent bone not found: " + parentName); } BoneData boneData = new BoneData(boneMap.getString("name"), parent); boneData.length = boneMap.getFloat("length", 0) * scale; boneData.x = boneMap.getFloat("x", 0) * scale; boneData.y = boneMap.getFloat("y", 0) * scale; boneData.rotation = boneMap.getFloat("rotation", 0); boneData.scaleX = boneMap.getFloat("scaleX", 1); boneData.scaleY = boneMap.getFloat("scaleY", 1); boneData.flipX = boneMap.getBoolean("flipX", false); boneData.flipY = boneMap.getBoolean("flipY", false); boneData.inheritScale = boneMap.getBoolean("inheritScale", true); boneData.inheritRotation = boneMap.getBoolean("inheritRotation", true); String color = boneMap.getString("color", null); if (color != null) boneData.getColor().set(Color.valueOf(color)); skeletonData.bones.add(boneData); } // IK constraints. for (JsonValue ikMap = root.getChild("ik"); ikMap != null; ikMap = ikMap.next) { IkConstraintData ikConstraintData = new IkConstraintData(ikMap.getString("name")); for (JsonValue boneMap = ikMap.getChild("bones"); boneMap != null; boneMap = boneMap.next) { String boneName = boneMap.asString(); BoneData bone = skeletonData.findBone(boneName); if (bone == null) throw new SerializationException("IK bone not found: " + boneName); ikConstraintData.bones.add(bone); } String targetName = ikMap.getString("target"); ikConstraintData.target = skeletonData.findBone(targetName); if (ikConstraintData.target == null) throw new SerializationException("Target bone not found: " + targetName); ikConstraintData.bendDirection = ikMap.getBoolean("bendPositive", true) ? 1 : -1; ikConstraintData.mix = ikMap.getFloat("mix", 1); skeletonData.ikConstraints.add(ikConstraintData); } // Slots. for (JsonValue slotMap = root.getChild("slots"); slotMap != null; slotMap = slotMap.next) { String slotName = slotMap.getString("name"); String boneName = slotMap.getString("bone"); BoneData boneData = skeletonData.findBone(boneName); if (boneData == null) throw new SerializationException("Slot bone not found: " + boneName); SlotData slotData = new SlotData(slotName, boneData); String color = slotMap.getString("color", null); if (color != null) slotData.getColor().set(Color.valueOf(color)); slotData.attachmentName = slotMap.getString("attachment", null); slotData.additiveBlending = slotMap.getBoolean("additive", false); skeletonData.slots.add(slotData); } // Skins. for (JsonValue skinMap = root.getChild("skins"); skinMap != null; skinMap = skinMap.next) { Skin skin = new Skin(skinMap.name); for (JsonValue slotEntry = skinMap.child; slotEntry != null; slotEntry = slotEntry.next) { int slotIndex = skeletonData.findSlotIndex(slotEntry.name); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotEntry.name); for (JsonValue entry = slotEntry.child; entry != null; entry = entry.next) { Attachment attachment = readAttachment(skin, entry.name, entry); if (attachment != null) skin.addAttachment(slotIndex, entry.name, attachment); } } skeletonData.skins.add(skin); if (skin.name.equals("default")) skeletonData.defaultSkin = skin; } // Events. for (JsonValue eventMap = root.getChild("events"); eventMap != null; eventMap = eventMap.next) { EventData eventData = new EventData(eventMap.name); eventData.intValue = eventMap.getInt("int", 0); eventData.floatValue = eventMap.getFloat("float", 0f); eventData.stringValue = eventMap.getString("string", null); skeletonData.events.add(eventData); } // Animations. for (JsonValue animationMap = root .getChild("animations"); animationMap != null; animationMap = animationMap.next) readAnimation(animationMap.name, animationMap, skeletonData); skeletonData.bones.shrink(); skeletonData.slots.shrink(); skeletonData.skins.shrink(); skeletonData.animations.shrink(); return skeletonData; }
From source file:com.company.minery.utils.spine.SkeletonJson.java
License:Open Source License
private Attachment readAttachment(Skin skin, String name, JsonValue map) { float scale = this.scale; name = map.getString("name", name); String path = map.getString("path", name); switch (AttachmentType.valueOf(map.getString("type", AttachmentType.region.name()))) { case region: { RegionAttachment region = attachmentLoader.newRegionAttachment(skin, name, path); if (region == null) return null; region.setPath(path);/*from ww w. ja v a 2 s . c o m*/ region.setX(map.getFloat("x", 0) * scale); region.setY(map.getFloat("y", 0) * scale); region.setScaleX(map.getFloat("scaleX", 1)); region.setScaleY(map.getFloat("scaleY", 1)); region.setRotation(map.getFloat("rotation", 0)); region.setWidth(map.getFloat("width") * scale); region.setHeight(map.getFloat("height") * scale); String color = map.getString("color", null); if (color != null) region.getColor().set(Color.valueOf(color)); region.updateOffset(); return region; } case boundingbox: { BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name); if (box == null) return null; float[] vertices = map.require("vertices").asFloatArray(); if (scale != 1) { for (int i = 0, n = vertices.length; i < n; i++) vertices[i] *= scale; } box.setVertices(vertices); return box; } case mesh: { MeshAttachment mesh = attachmentLoader.newMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); float[] vertices = map.require("vertices").asFloatArray(); if (scale != 1) { for (int i = 0, n = vertices.length; i < n; i++) vertices[i] *= scale; } mesh.setVertices(vertices); mesh.setTriangles(map.require("triangles").asShortArray()); mesh.setRegionUVs(map.require("uvs").asFloatArray()); mesh.updateUVs(); String color = map.getString("color", null); if (color != null) mesh.getColor().set(Color.valueOf(color)); if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); mesh.setWidth(map.getFloat("width", 0) * scale); mesh.setHeight(map.getFloat("height", 0) * scale); return mesh; } case skinnedmesh: { SkinnedMeshAttachment mesh = attachmentLoader.newSkinnedMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); float[] uvs = map.require("uvs").asFloatArray(); float[] vertices = map.require("vertices").asFloatArray(); FloatArray weights = new FloatArray(uvs.length * 3 * 3); IntArray bones = new IntArray(uvs.length * 3); for (int i = 0, n = vertices.length; i < n;) { int boneCount = (int) vertices[i++]; bones.add(boneCount); for (int nn = i + boneCount * 4; i < nn;) { bones.add((int) vertices[i]); weights.add(vertices[i + 1] * scale); weights.add(vertices[i + 2] * scale); weights.add(vertices[i + 3]); i += 4; } } mesh.setBones(bones.toArray()); mesh.setWeights(weights.toArray()); mesh.setTriangles(map.require("triangles").asShortArray()); mesh.setRegionUVs(uvs); mesh.updateUVs(); String color = map.getString("color", null); if (color != null) mesh.getColor().set(Color.valueOf(color)); if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); mesh.setWidth(map.getFloat("width", 0) * scale); mesh.setHeight(map.getFloat("height", 0) * scale); return mesh; } } // RegionSequenceAttachment regionSequenceAttachment = (RegionSequenceAttachment)attachment; // // float fps = map.getFloat("fps"); // regionSequenceAttachment.setFrameTime(fps); // // String modeString = map.getString("mode"); // regionSequenceAttachment.setMode(modeString == null ? Mode.forward : Mode.valueOf(modeString)); return null; }
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 ww. j a v a 2 s. co m //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.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 w w w .j a v a 2 s . c o 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()); } }
From source file:com.siondream.core.physics.PhysicsLoader.java
License:Apache License
private void loadBodyDef(JsonValue root) { logger.info("loading BodyDef"); physicsData.bodyDef.bullet = root.getBoolean("bullet", false); physicsData.bodyDef.active = root.getBoolean("active", true); physicsData.bodyDef.fixedRotation = root.getBoolean("fixedRotation", false); physicsData.bodyDef.gravityScale = root.getFloat("gravityScale", 1.0f); physicsData.bodyDef.linearDamping = root.getFloat("linearDamping", 0.0f); physicsData.bodyDef.angularDamping = root.getFloat("angularDamping", 0.0f); String type = root.getString("type", "dynamic"); if (type.equals("dynamic")) { physicsData.bodyDef.type = BodyDef.BodyType.DynamicBody; } else if (type.equals("kynematic")) { physicsData.bodyDef.type = BodyDef.BodyType.KinematicBody; } else if (type.equals("static")) { physicsData.bodyDef.type = BodyDef.BodyType.KinematicBody; } else {//from w w w.j av a 2 s . c o m logger.error("unknown body type " + type); } }
From source file:com.siondream.core.physics.PhysicsLoader.java
License:Apache License
private void loadMassData(JsonValue root) { JsonValue massData = root.get("massData"); if (massData != null) { logger.info("loading mass data"); physicsData.massData.center.x = massData.getFloat("centerX", 0.0f); physicsData.massData.center.y = massData.getFloat("centerY", 0.0f); physicsData.massData.I = massData.getFloat("i", 0.0f); physicsData.massData.mass = massData.getFloat("mass", 1.0f); }/*from w w w.j a v a2 s . c om*/ }
From source file:com.siondream.core.physics.PhysicsLoader.java
License:Apache License
private void loadFixtureDefs(JsonValue root) { JsonValue fixtures = root.get("fixtures"); JsonIterator fixturesIt = fixtures.iterator(); int index = 0; while (fixturesIt.hasNext()) { JsonValue fixture = fixturesIt.next(); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = fixture.getFloat("density", 1.0f); fixtureDef.restitution = fixture.getFloat("restitution", 0.0f); fixtureDef.friction = fixture.getFloat("friction", 1.0f); fixtureDef.isSensor = fixture.getBoolean("isSensor", false); fixtureDef.shape = loadShape(fixture); loadFilter(fixture, fixtureDef.filter); String id = fixture.getString("id", ""); logger.info("loading fixture with id " + id); physicsData.fixtureNames.add(id); physicsData.fixtureIdx.put(id, index); physicsData.fixtureDefs.add(fixtureDef); ++index;//w w w .j a va 2 s . c o m } }
From source file:com.siondream.core.physics.PhysicsLoader.java
License:Apache License
private Shape loadShape(JsonValue root) { Shape shape = null;/* w w w . j av a 2s . c o m*/ JsonValue shapeValue = root.get("shape"); if (shapeValue == null) { return shape; } String type = shapeValue.getString("type"); float x = shapeValue.getFloat("centerX", 0.0f); float y = shapeValue.getFloat("centerY", 0.0f); if (type.equals("circle")) { logger.info("loading cicle shape"); CircleShape circle = new CircleShape(); circle.setPosition(new Vector2(x, y)); circle.setRadius(shapeValue.getFloat("radius", 1.0f)); shape = circle; } else if (type.equals("polygon")) { logger.info("loading polygon shape"); PolygonShape polygon = new PolygonShape(); polygon.setAsBox(shapeValue.getFloat("width", 1.0f), shapeValue.getFloat("height", 1.0f), new Vector2(x, y), 0.0f); shape = polygon; } else { logger.error("shape unknown " + type); } return shape; }
From source file:com.tnf.ptm.common.ShipConfig.java
License:Apache License
public static ShipConfig load(HullConfigManager hullConfigs, JsonValue rootNode, ItemManager itemManager) { if (rootNode == null) { return null; }//from w ww . j a v a2 s. co m String hullName = rootNode.getString("hull"); HullConfig hull = hullConfigs.getConfig(new ResourceUrn(hullName)); String items = rootNode.getString("items"); int money = rootNode.getInt("money", 0); float density = rootNode.getFloat("density", -1); ShipConfig guard; if (rootNode.hasChild("guard")) { guard = load(hullConfigs, rootNode.get("guard"), itemManager); } else { guard = null; } return new ShipConfig(hull, items, money, density, guard, itemManager); }