List of usage examples for com.badlogic.gdx.utils JsonValue getChild
public JsonValue getChild(String name)
From source file:be.ac.ucl.lfsab1509.bouboule.game.physicEditor.BodyEditorLoader.java
License:Open Source License
private Model readJson(String str) { Model m = new Model(); JsonValue map = new JsonReader().parse(str); JsonValue bodyElem = map.getChild("rigidBodies"); for (; bodyElem != null; bodyElem = bodyElem.next()) { RigidBodyModel rbModel = readRigidBody(bodyElem); m.rigidBodies.put(rbModel.name, rbModel); }/*from ww w. ja va 2 s . c om*/ return m; }
From source file:be.ac.ucl.lfsab1509.bouboule.game.physicEditor.BodyEditorLoader.java
License:Open Source License
private RigidBodyModel readRigidBody(JsonValue bodyElem) { RigidBodyModel rbModel = new RigidBodyModel(); rbModel.name = bodyElem.getString("name"); rbModel.imagePath = bodyElem.getString("imagePath"); JsonValue originElem = bodyElem.get("origin"); rbModel.origin.x = originElem.getFloat("x"); rbModel.origin.y = originElem.getFloat("y"); // polygons/* ww w . j a v a 2 s.c o m*/ JsonValue polygonsElem = bodyElem.getChild("polygons"); for (; polygonsElem != null; polygonsElem = polygonsElem.next()) { PolygonModel polygon = new PolygonModel(); rbModel.polygons.add(polygon); JsonValue vertexElem = polygonsElem.child(); for (; vertexElem != null; vertexElem = vertexElem.next()) { float x = vertexElem.getFloat("x"); float y = vertexElem.getFloat("y"); polygon.vertices.add(new Vector2(x, y)); } polygon.buffer = new Vector2[polygon.vertices.size()]; } // circles JsonValue circleElem = bodyElem.getChild("circles"); for (; circleElem != null; circleElem = circleElem.next()) { CircleModel circle = new CircleModel(); rbModel.circles.add(circle); circle.center.x = circleElem.getFloat("cx"); circle.center.y = circleElem.getFloat("cy"); circle.radius = circleElem.getFloat("r"); } return rbModel; }
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 ww.j a va 2 s .c o m 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 void readAnimation(String name, JsonValue map, SkeletonData skeletonData) { float scale = this.scale; Array<Timeline> timelines = new Array(); float duration = 0; // Slot timelines. for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) { int slotIndex = skeletonData.findSlotIndex(slotMap.name); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.name); for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) { String timelineName = timelineMap.name; if (timelineName.equals("color")) { ColorTimeline timeline = new ColorTimeline(timelineMap.size); timeline.slotIndex = slotIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { Color color = Color.valueOf(valueMap.getString("color")); timeline.setFrame(frameIndex, valueMap.getFloat("time"), color.r, color.g, color.b, color.a);/* w w w. ja va2 s. c om*/ readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]); } else if (timelineName.equals("attachment")) { AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size); timeline.slotIndex = slotIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) timeline.setFrame(frameIndex++, valueMap.getFloat("time"), valueMap.getString("name")); timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } else throw new RuntimeException( "Invalid timeline type for a slot: " + timelineName + " (" + slotMap.name + ")"); } } // Bone timelines. for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) { int boneIndex = skeletonData.findBoneIndex(boneMap.name); if (boneIndex == -1) throw new SerializationException("Bone not found: " + boneMap.name); for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) { String timelineName = timelineMap.name; if (timelineName.equals("rotate")) { RotateTimeline timeline = new RotateTimeline(timelineMap.size); timeline.boneIndex = boneIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("angle")); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]); } else if (timelineName.equals("translate") || timelineName.equals("scale")) { TranslateTimeline timeline; float timelineScale = 1; if (timelineName.equals("scale")) timeline = new ScaleTimeline(timelineMap.size); else { timeline = new TranslateTimeline(timelineMap.size); timelineScale = scale; } timeline.boneIndex = boneIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0); timeline.setFrame(frameIndex, valueMap.getFloat("time"), x * timelineScale, y * timelineScale); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]); } else if (timelineName.equals("flipX") || timelineName.equals("flipY")) { boolean x = timelineName.equals("flipX"); FlipXTimeline timeline = x ? new FlipXTimeline(timelineMap.size) : new FlipYTimeline(timelineMap.size); timeline.boneIndex = boneIndex; String field = x ? "x" : "y"; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getBoolean(field, false)); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]); } else throw new RuntimeException( "Invalid timeline type for a bone: " + timelineName + " (" + boneMap.name + ")"); } } // IK timelines. for (JsonValue ikMap = map.getChild("ik"); ikMap != null; ikMap = ikMap.next) { IkConstraintData ikConstraint = skeletonData.findIkConstraint(ikMap.name); IkConstraintTimeline timeline = new IkConstraintTimeline(ikMap.size); timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true); int frameIndex = 0; for (JsonValue valueMap = ikMap.child; valueMap != null; valueMap = valueMap.next) { timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("mix"), valueMap.getBoolean("bendPositive") ? 1 : -1); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]); } // FFD timelines. for (JsonValue ffdMap = map.getChild("ffd"); ffdMap != null; ffdMap = ffdMap.next) { Skin skin = skeletonData.findSkin(ffdMap.name); if (skin == null) throw new SerializationException("Skin not found: " + ffdMap.name); for (JsonValue slotMap = ffdMap.child; slotMap != null; slotMap = slotMap.next) { int slotIndex = skeletonData.findSlotIndex(slotMap.name); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.name); for (JsonValue meshMap = slotMap.child; meshMap != null; meshMap = meshMap.next) { FfdTimeline timeline = new FfdTimeline(meshMap.size); Attachment attachment = skin.getAttachment(slotIndex, meshMap.name); if (attachment == null) throw new SerializationException("FFD attachment not found: " + meshMap.name); timeline.slotIndex = slotIndex; timeline.attachment = attachment; int vertexCount; if (attachment instanceof MeshAttachment) vertexCount = ((MeshAttachment) attachment).getVertices().length; else vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2; int frameIndex = 0; for (JsonValue valueMap = meshMap.child; valueMap != null; valueMap = valueMap.next) { float[] vertices; JsonValue verticesValue = valueMap.get("vertices"); if (verticesValue == null) { if (attachment instanceof MeshAttachment) vertices = ((MeshAttachment) attachment).getVertices(); else vertices = new float[vertexCount]; } else { vertices = new float[vertexCount]; int start = valueMap.getInt("offset", 0); System.arraycopy(verticesValue.asFloatArray(), 0, vertices, start, verticesValue.size); if (scale != 1) { for (int i = start, n = i + verticesValue.size; i < n; i++) vertices[i] *= scale; } if (attachment instanceof MeshAttachment) { float[] meshVertices = ((MeshAttachment) attachment).getVertices(); for (int i = 0; i < vertexCount; i++) vertices[i] += meshVertices[i]; } } timeline.setFrame(frameIndex, valueMap.getFloat("time"), vertices); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } } } // Draw order timeline. JsonValue drawOrdersMap = map.get("drawOrder"); if (drawOrdersMap == null) drawOrdersMap = map.get("draworder"); if (drawOrdersMap != null) { DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrdersMap.size); int slotCount = skeletonData.slots.size; int frameIndex = 0; for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next) { int[] drawOrder = null; JsonValue offsets = drawOrderMap.get("offsets"); if (offsets != null) { drawOrder = new int[slotCount]; for (int i = slotCount - 1; i >= 0; i--) drawOrder[i] = -1; int[] unchanged = new int[slotCount - offsets.size]; int originalIndex = 0, unchangedIndex = 0; for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) { int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot")); if (slotIndex == -1) throw new SerializationException("Slot not found: " + offsetMap.getString("slot")); // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (int i = slotCount - 1; i >= 0; i--) if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex]; } timeline.setFrame(frameIndex++, drawOrderMap.getFloat("time"), drawOrder); } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } // Event timeline. JsonValue eventsMap = map.get("events"); if (eventsMap != null) { EventTimeline timeline = new EventTimeline(eventsMap.size); int frameIndex = 0; for (JsonValue eventMap = eventsMap.child; eventMap != null; eventMap = eventMap.next) { EventData eventData = skeletonData.findEvent(eventMap.getString("name")); if (eventData == null) throw new SerializationException("Event not found: " + eventMap.getString("name")); Event event = new Event(eventData); event.intValue = eventMap.getInt("int", eventData.getInt()); event.floatValue = eventMap.getFloat("float", eventData.getFloat()); event.stringValue = eventMap.getString("string", eventData.getString()); timeline.setFrame(frameIndex++, eventMap.getFloat("time"), event); } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } timelines.shrink(); skeletonData.animations.add(new Animation(name, timelines, duration)); }
From source file:com.esotericsoftware.spine.JsonRollback.java
License:Open Source License
static public void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("Usage: <inputFile> [outputFile]"); System.exit(0);/*from ww w .j a va 2 s . c o m*/ } JsonValue root = new Json().fromJson(null, new FileHandle(args[0])); // In 3.2 skinnedmesh was renamed to weightedmesh. setValue(root, "skinnedmesh", "skins", "*", "*", "*", "type", "weightedmesh"); // In 3.2 shear was added. delete(root, "animations", "*", "bones", "*", "shear"); // In 3.3 ffd was renamed to deform. rename(root, "ffd", "animations", "*", "deform"); // In 3.3 mesh is now a single type, previously they were skinnedmesh if they had weights. for (JsonValue value : find(root, new Array(), 0, "skins", "*", "*", "*", "type", "mesh")) if (value.parent.get("uvs").size != value.parent.get("vertices").size) value.set("skinnedmesh"); // In 3.3 linkedmesh is now a single type, previously they were linkedweightedmesh if they had weights. for (JsonValue value : find(root, new Array(), 0, "skins", "*", "*", "*", "type", "linkedmesh")) { String slot = value.parent.parent.name.replaceAll("", ""); String skinName = value.parent.getString("skin", "default"); String parentName = value.parent.getString("parent"); if (find(root, new Array(), 0, ("skins~~" + skinName + "~~" + slot + "~~" + parentName + "~~type~~skinnedmesh") .split("~~")).size > 0) value.set("weightedlinkedmesh"); } // In 3.3 bounding boxes can be weighted. for (JsonValue value : find(root, new Array(), 0, "skins", "*", "*", "*", "type", "boundingbox")) if (value.parent.getInt("vertexCount") * 2 != value.parent.get("vertices").size) value.parent.parent.remove(value.parent.name); // In 3.3 paths were added. for (JsonValue value : find(root, new Array(), 0, "skins", "*", "*", "*", "type", "path")) { String attachment = value.parent.name; value.parent.parent.remove(attachment); String slot = value.parent.parent.name; // Also remove path deform timelines. delete(root, "animations", "*", "ffd", "*", slot, attachment); } // In 3.3 IK constraint timelines no longer require bendPositive. for (JsonValue value : find(root, new Array(), 0, "animations", "*", "ik", "*")) for (JsonValue child = value.child; child != null; child = child.next) if (!child.has("bendPositive")) child.addChild("bendPositive", new JsonValue(true)); // In 3.3 transform constraints can have more than 1 bone. for (JsonValue child = root.getChild("transform"); child != null; child = child.next) { JsonValue bones = child.remove("bones"); if (bones != null) child.addChild("bone", new JsonValue(bones.child.asString())); } if (args.length > 1) new FileHandle(args[1]).writeString(root.prettyPrint(OutputType.json, 130), false, "UTF-8"); else System.out.println(root.prettyPrint(OutputType.json, 130)); }
From source file:com.tnf.ptm.common.CollisionMeshLoader.java
License:Apache License
private void readModel(JsonValue rootNode) { for (JsonValue rbNode = rootNode.getChild("rigidBodies"); rbNode != null; rbNode = rbNode.next()) { readRigidBody(rbNode);//ww w . j a v a 2s . c o m } }
From source file:mobi.shad.s3lib.gfx.g3d.shaders.ShaderToyShader.java
License:Apache License
public ShaderToyShader(String shaderName, int processIdx, boolean hasColor, boolean hasNormal, int numTexCoords) { this.shaderName = shaderName; this.hasColor = hasColor; this.hasNormal = hasNormal; this.numTexCoords = numTexCoords; if (!shaderName.equalsIgnoreCase("") && !shaderName.equalsIgnoreCase("default") && !shaderName.equalsIgnoreCase("-= default =-")) { String shaderFile = "shader_toy/" + shaderName + ".json"; S3Log.log("SimpleShader::ReadShader", "Read file shader: " + shaderFile); JsonReader jsonReader = new JsonReader(); JsonValue jsonValue = jsonReader.parse(S3File.getFileHandle(shaderFile)); JsonValue jsonValueChild0; jsonValueChild0 = jsonValue.get(processIdx); if (jsonValueChild0 == null) { jsonValueChild0 = jsonValue.get(0); }/*from w w w .ja v a 2s . c o m*/ final String ver = jsonValueChild0.getString("ver"); final JsonValue child1 = jsonValueChild0.get(1); final JsonValue child2 = jsonValueChild0.get(2); final JsonValue renderpass = jsonValueChild0.getChild("renderpass"); final String id = child1.getString("id"); final String date = child1.getString("date"); final String viewed = child1.getString("viewed"); final String name = child1.getString("name"); final String username = child1.getString("username"); final String description = child1.getString("description"); final String code = renderpass.getString("code"); final String type = renderpass.getString("type"); S3Log.log("EffectShader", "Load shader: id: " + id + " name: " + name); vertexInit = ""; vertexMain = ""; fragmentInit = ""; fragmentMain = code; fragmentFragColor = ""; } else { vertexInit = ""; vertexMain = ""; fragmentInit = ""; fragmentMain = ""; fragmentFragColor = ""; } compileShader(); }
From source file: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 .j a va 2 s . co m*/ 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.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.blendMode = BlendMode.valueOf(slotMap.getString("blend", BlendMode.normal.name())); 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.events.shrink(); skeletonData.animations.shrink(); skeletonData.ikConstraints.shrink(); return skeletonData; }
From source file:spine.SkeletonJson.java
License:Open Source License
private void readAnimation(String name, JsonValue map, SkeletonData skeletonData) { float scale = this.scale; Array<Timeline> timelines = new Array(); float duration = 0; // Slot timelines. for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) { int slotIndex = skeletonData.findSlotIndex(slotMap.name); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.name); for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) { String timelineName = timelineMap.name; if (timelineName.equals("color")) { ColorTimeline timeline = new ColorTimeline(timelineMap.size); timeline.slotIndex = slotIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { Color color = Color.valueOf(valueMap.getString("color")); timeline.setFrame(frameIndex, valueMap.getFloat("time"), color.r, color.g, color.b, color.a);/*from w w w . j a va 2s.c om*/ readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]); } else if (timelineName.equals("attachment")) { AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size); timeline.slotIndex = slotIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) timeline.setFrame(frameIndex++, valueMap.getFloat("time"), valueMap.getString("name")); timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } else throw new RuntimeException( "Invalid timeline type for a slot: " + timelineName + " (" + slotMap.name + ")"); } } // Bone timelines. for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) { int boneIndex = skeletonData.findBoneIndex(boneMap.name); if (boneIndex == -1) throw new SerializationException("Bone not found: " + boneMap.name); for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) { String timelineName = timelineMap.name; if (timelineName.equals("rotate")) { RotateTimeline timeline = new RotateTimeline(timelineMap.size); timeline.boneIndex = boneIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("angle")); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]); } else if (timelineName.equals("translate") || timelineName.equals("scale")) { TranslateTimeline timeline; float timelineScale = 1; if (timelineName.equals("scale")) timeline = new ScaleTimeline(timelineMap.size); else { timeline = new TranslateTimeline(timelineMap.size); timelineScale = scale; } timeline.boneIndex = boneIndex; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0); timeline.setFrame(frameIndex, valueMap.getFloat("time"), x * timelineScale, y * timelineScale); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]); } else throw new RuntimeException( "Invalid timeline type for a bone: " + timelineName + " (" + boneMap.name + ")"); } } // IK timelines. for (JsonValue ikMap = map.getChild("ik"); ikMap != null; ikMap = ikMap.next) { IkConstraintData ikConstraint = skeletonData.findIkConstraint(ikMap.name); IkConstraintTimeline timeline = new IkConstraintTimeline(ikMap.size); timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true); int frameIndex = 0; for (JsonValue valueMap = ikMap.child; valueMap != null; valueMap = valueMap.next) { timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("mix"), valueMap.getBoolean("bendPositive") ? 1 : -1); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]); } // FFD timelines. for (JsonValue ffdMap = map.getChild("ffd"); ffdMap != null; ffdMap = ffdMap.next) { Skin skin = skeletonData.findSkin(ffdMap.name); if (skin == null) throw new SerializationException("Skin not found: " + ffdMap.name); for (JsonValue slotMap = ffdMap.child; slotMap != null; slotMap = slotMap.next) { int slotIndex = skeletonData.findSlotIndex(slotMap.name); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.name); for (JsonValue meshMap = slotMap.child; meshMap != null; meshMap = meshMap.next) { FfdTimeline timeline = new FfdTimeline(meshMap.size); Attachment attachment = skin.getAttachment(slotIndex, meshMap.name); if (attachment == null) throw new SerializationException("FFD attachment not found: " + meshMap.name); timeline.slotIndex = slotIndex; timeline.attachment = attachment; int vertexCount; if (attachment instanceof MeshAttachment) vertexCount = ((MeshAttachment) attachment).getVertices().length; else vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2; int frameIndex = 0; for (JsonValue valueMap = meshMap.child; valueMap != null; valueMap = valueMap.next) { float[] vertices; JsonValue verticesValue = valueMap.get("vertices"); if (verticesValue == null) { if (attachment instanceof MeshAttachment) vertices = ((MeshAttachment) attachment).getVertices(); else vertices = new float[vertexCount]; } else { vertices = new float[vertexCount]; int start = valueMap.getInt("offset", 0); System.arraycopy(verticesValue.asFloatArray(), 0, vertices, start, verticesValue.size); if (scale != 1) { for (int i = start, n = i + verticesValue.size; i < n; i++) vertices[i] *= scale; } if (attachment instanceof MeshAttachment) { float[] meshVertices = ((MeshAttachment) attachment).getVertices(); for (int i = 0; i < vertexCount; i++) vertices[i] += meshVertices[i]; } } timeline.setFrame(frameIndex, valueMap.getFloat("time"), vertices); readCurve(timeline, frameIndex, valueMap); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } } } // Draw order timeline. JsonValue drawOrdersMap = map.get("drawOrder"); if (drawOrdersMap == null) drawOrdersMap = map.get("draworder"); if (drawOrdersMap != null) { DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrdersMap.size); int slotCount = skeletonData.slots.size; int frameIndex = 0; for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next) { int[] drawOrder = null; JsonValue offsets = drawOrderMap.get("offsets"); if (offsets != null) { drawOrder = new int[slotCount]; for (int i = slotCount - 1; i >= 0; i--) drawOrder[i] = -1; int[] unchanged = new int[slotCount - offsets.size]; int originalIndex = 0, unchangedIndex = 0; for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) { int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot")); if (slotIndex == -1) throw new SerializationException("Slot not found: " + offsetMap.getString("slot")); // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (int i = slotCount - 1; i >= 0; i--) if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex]; } timeline.setFrame(frameIndex++, drawOrderMap.getFloat("time"), drawOrder); } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } // Event timeline. JsonValue eventsMap = map.get("events"); if (eventsMap != null) { EventTimeline timeline = new EventTimeline(eventsMap.size); int frameIndex = 0; for (JsonValue eventMap = eventsMap.child; eventMap != null; eventMap = eventMap.next) { EventData eventData = skeletonData.findEvent(eventMap.getString("name")); if (eventData == null) throw new SerializationException("Event not found: " + eventMap.getString("name")); Event event = new Event(eventMap.getFloat("time"), eventData); event.intValue = eventMap.getInt("int", eventData.getInt()); event.floatValue = eventMap.getFloat("float", eventData.getFloat()); event.stringValue = eventMap.getString("string", eventData.getString()); timeline.setFrame(frameIndex++, event); } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } timelines.shrink(); skeletonData.animations.add(new Animation(name, timelines, duration)); }