Package com.badlogic.gdx.utils

Examples of com.badlogic.gdx.utils.SerializationException


      public Skin read (Json json, JsonValue typeToValueMap, Class ignored) {
        for (JsonValue valueMap = typeToValueMap.child(); valueMap != null; valueMap = valueMap.next()) {
          try {
            readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap);
          } catch (ReflectionException ex) {
            throw new SerializationException(ex);
          }
        }
        return skin;
      }

      private void readNamedObjects (Json json, Class type, JsonValue valueMap) {
        Class addType = type == TintedDrawable.class ? Drawable.class : type;
        for (JsonValue valueEntry = valueMap.child(); valueEntry != null; valueEntry = valueEntry.next()) {
          Object object = json.readValue(type, valueEntry);
          if (object == null) continue;
          try {
            add(valueEntry.name(), object, addType);
          } catch (Exception ex) {
            throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name(), ex);
          }
        }
      }
    });

    json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {
      public BitmapFont read (Json json, JsonValue jsonData, Class type) {
        String path = json.readValue("file", String.class, jsonData);

        FileHandle fontFile = skinFile.parent().child(path);
        if (!fontFile.exists()) fontFile = Gdx.files.internal(path);
        if (!fontFile.exists()) throw new SerializationException("Font file not found: " + fontFile);

        // Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file.
        String regionName = fontFile.nameWithoutExtension();
        try {
          TextureRegion region = skin.optional(regionName, TextureRegion.class);
          if (region != null)
            return new BitmapFont(fontFile, region, false);
          else {
            FileHandle imageFile = fontFile.parent().child(regionName + ".png");
            if (imageFile.exists())
              return new BitmapFont(fontFile, imageFile, false);
            else
              return new BitmapFont(fontFile, false);
          }
        } catch (RuntimeException ex) {
          throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
        }
      }
    });

    json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {
View Full Code Here


      // Animations.
      for (int i = 0, n = input.readInt(true); i < n; i++)
        readAnimation(input.readString(), input, skeletonData);

    } catch (IOException ex) {
      throw new SerializationException("Error reading skeleton file.", ex);
    } finally {
      try {
        input.close();
      } catch (IOException ignored) {
      }
View Full Code Here

        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[eventCount - 1]);
      }
    } catch (IOException ex) {
      throw new SerializationException("Error reading skeleton file.", ex);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
  }
View Full Code Here

    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);
        }
      }
View Full Code Here

    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);
            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);
View Full Code Here

  public void load (FileHandle skinFile) {
    try {
      getJsonLoader(skinFile).fromJson(Skin.class, skinFile);
    } catch (SerializationException ex) {
      throw new SerializationException("Error reading file: " + skinFile, ex);
    }
  }
View Full Code Here

          if (entry.value.equals(object)) {
            json.writeValue(entry.key);
            return;
          }
        }
        throw new SerializationException(object.getClass().getSimpleName() + " not found: " + object);
      }

      public Object read (Json json, Object jsonData, Class type) {
        throw new UnsupportedOperationException();
      }
    }

    json.setSerializer(Skin.class, new Serializer<Skin>() {
      public void write (Json json, Skin skin, Class valueType) {
        json.writeObjectStart();
        json.writeValue("resources", skin.resources);
        for (Entry<Class, ObjectMap<String, Object>> entry : resources.entries())
          json.setSerializer(entry.key, new AliasWriter(entry.key));
        json.writeField(skin, "styles");
        json.writeObjectEnd();
      }

      public Skin read (Json json, Object jsonData, Class ignored) {
        ObjectMap map = (ObjectMap)jsonData;
        readTypeMap(json, (ObjectMap)map.get("resources"), true);
        readTypeMap(json, (ObjectMap)map.get("styles"), false);
        return skin;
      }

      private void readTypeMap (Json json, ObjectMap<String, ObjectMap> typeToValueMap, boolean isResource) {
        if (typeToValueMap == null)
          throw new SerializationException("Skin file is missing a \"" + (isResource ? "resources" : "styles")
            + "\" section.");
        for (Entry<String, ObjectMap> typeEntry : typeToValueMap.entries()) {
          String className = typeEntry.key;
          ObjectMap<String, ObjectMap> valueMap = (ObjectMap)typeEntry.value;
          try {
            readNamedObjects(json, Class.forName(className), valueMap, isResource);
          } catch (ClassNotFoundException ex) {
            throw new SerializationException(ex);
          }
        }
      }

      private void readNamedObjects (Json json, Class type, ObjectMap<String, ObjectMap> valueMap, boolean isResource) {
        for (Entry<String, ObjectMap> valueEntry : valueMap.entries()) {
          String name = valueEntry.key;
          Object object = json.readValue(type, valueEntry.value);
          if (object == null) continue;
          try {
            if (isResource)
              addResource(name, object);
            else
              addStyle(name, object);
          } catch (Exception ex) {
            throw new SerializationException("Error reading " + type.getSimpleName() + ": " + valueEntry.key, ex);
          }
        }
      }
    });

    json.setSerializer(TextureRegion.class, new Serializer<TextureRegion>() {
      public void write (Json json, TextureRegion region, Class valueType) {
        json.writeObjectStart();
        json.writeValue("x", region.getRegionX());
        json.writeValue("y", region.getRegionY());
        json.writeValue("width", region.getRegionWidth());
        json.writeValue("height", region.getRegionHeight());
        json.writeObjectEnd();
      }

      public TextureRegion read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((String)jsonData, TextureRegion.class);
        int x = json.readValue("x", int.class, jsonData);
        int y = json.readValue("y", int.class, jsonData);
        int width = json.readValue("width", int.class, jsonData);
        int height = json.readValue("height", int.class, jsonData);
        return new TextureRegion(skin.texture, x, y, width, height);
      }
    });

    json.setSerializer(BitmapFont.class, new Serializer<BitmapFont>() {
      public void write (Json json, BitmapFont font, Class valueType) {
        json.writeObjectStart();
        json.writeValue("file", font.getData().getFontFile().toString().replace('\\', '/'));
        json.writeObjectEnd();
      }

      public BitmapFont read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((String)jsonData, BitmapFont.class);
        String path = json.readValue("file", String.class, jsonData);

        FileHandle fontFile = skinFile.parent().child(path);
        if (!fontFile.exists()) fontFile = Gdx.files.internal(path);
        if (!fontFile.exists()) throw new SerializationException("Font file not found: " + fontFile);

        // Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file.
        String regionName = fontFile.nameWithoutExtension();
        try {
          if (skin.hasResource(regionName, TextureRegion.class))
            return new BitmapFont(fontFile, skin.getResource(regionName, TextureRegion.class), false);
          else {
            FileHandle imageFile = fontFile.parent().child(regionName + ".png");
            if (imageFile.exists())
              return new BitmapFont(fontFile, imageFile, false);
            else
              return new BitmapFont(fontFile, false);
          }
        } catch (RuntimeException ex) {
          throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
        }
      }
    });

    json.setSerializer(NinePatch.class, new Serializer<NinePatch>() {
View Full Code Here

          if (entry.value.equals(object)) {
            json.writeValue(entry.key);
            return;
          }
        }
        throw new SerializationException(object.getClass().getSimpleName() + " not found: " + object);
      }
View Full Code Here

TOP

Related Classes of com.badlogic.gdx.utils.SerializationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.