Пример #1
0
  @SuppressWarnings("unchecked")
  public Map<?, ?> read(Json json, JsonValue jsonData) {

    Map<K, V> values;

    try {
      values = (Map<K, V>) map.map().newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
      throw new GdxRuntimeException(e);
    }

    JsonValue entry = jsonData.getChild(name);

    while (entry != null) {

      JsonValue keyValue = entry.get("key");
      K key = json.readValue((Class<K>) map.key(), keyValue);

      JsonValue valueValue = entry.get("value");
      V value = json.readValue((Class<V>) map.value(), valueValue);

      values.put(key, value);

      entry = entry.next;
    }

    return values;
  }
  @Override
  public IntMap read(Json json, JsonValue jsonData, Class type) {
    IntMap intMap = new IntMap(json.readValue(VALUE_SIZE, int.class, jsonData));

    for (JsonValue entry = jsonData.getChild(VALUE_ENTRIES); entry != null; entry = entry.next) {
      intMap.put(Integer.parseInt(entry.name), json.readValue(entry.name, null, jsonData));
    }

    return intMap;
  }
Пример #3
0
  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
    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;
  }
Пример #4
0
  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);
    }

    return m;
  }
Пример #5
0
  @SuppressWarnings("unchecked")
  public <M> M read(
      Json json, JsonValue jsonData, Supplier<M> newInstance, KeyValueConsumer<M, K, V> put) {

    M values = newInstance.get();

    JsonValue entry = jsonData.getChild(name);

    while (entry != null) {

      JsonValue keyValue = entry.get("key");
      K key = json.readValue((Class<K>) map.key(), keyValue);

      JsonValue valueValue = entry.get("value");
      V value = json.readValue((Class<V>) map.value(), valueValue);

      put.accept(values, key, value);

      entry = entry.next;
    }

    return values;
  }
Пример #6
0
  public SkeletonData readSkeletonData(FileHandle file) {
    if (file == null) throw new IllegalArgumentException("file cannot be null.");

    SkeletonData skeletonData = new SkeletonData();
    skeletonData.setName(file.nameWithoutExtension());

    JsonValue root = new JsonReader().parse(file);

    // 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);
      skeletonData.addBone(boneData);
    }

    // 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.setAttachmentName(slotMap.getString("attachment", null));

      slotData.additiveBlending = slotMap.getBoolean("additive", false);

      skeletonData.addSlot(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());
        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.addSkin(skin);
      if (skin.name.equals("default")) skeletonData.setDefaultSkin(skin);
    }

    // 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;
  }
Пример #7
0
  private void readAnimation(String name, JsonValue map, SkeletonData skeletonData) {
    Array<Timeline> timelines = new Array();
    float duration = 0;

    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(TIMELINE_ROTATE)) {
          RotateTimeline timeline = new RotateTimeline(timelineMap.size());
          timeline.setBoneIndex(boneIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            timeline.setFrame(frameIndex, 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(TIMELINE_TRANSLATE) || timelineName.equals(TIMELINE_SCALE)) {
          TranslateTimeline timeline;
          float timelineScale = 1;
          if (timelineName.equals(TIMELINE_SCALE)) timeline = new ScaleTimeline(timelineMap.size());
          else {
            timeline = new TranslateTimeline(timelineMap.size());
            timelineScale = scale;
          }
          timeline.setBoneIndex(boneIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0);
            timeline.setFrame(frameIndex, 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() + ")");
      }
    }

    for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next()) {
      int slotIndex = skeletonData.findSlotIndex(slotMap.name());

      for (JsonValue timelineMap = slotMap.child();
          timelineMap != null;
          timelineMap = timelineMap.next()) {
        String timelineName = timelineMap.name();
        if (timelineName.equals(TIMELINE_COLOR)) {
          ColorTimeline timeline = new ColorTimeline(timelineMap.size());
          timeline.setSlotIndex(slotIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            Color color = Color.valueOf(valueMap.getString("color"));
            timeline.setFrame(frameIndex, 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(TIMELINE_ATTACHMENT)) {
          AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size());
          timeline.setSlotIndex(slotIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            timeline.setFrame(frameIndex++, 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() + ")");
      }
    }

    timelines.shrink();
    skeletonData.addAnimation(new Animation(name, timelines, duration));
  }