Package com.badlogic.gdx.utils

Examples of com.badlogic.gdx.utils.ObjectMap$Entries


  public <T> void addResource (String name, T resource) {
    if (resource == null) throw new IllegalArgumentException("resource cannot be null.");
    ObjectMap<String, Object> typeResources = data.resources.get(resource.getClass());
    if (typeResources == null) {
      typeResources = new ObjectMap();
      data.resources.put(resource.getClass(), typeResources);
    }
    typeResources.put(name, resource);
  }
View Full Code Here


  public <T> void addStyle (String name, T style) {
    if (style == null) throw new IllegalArgumentException("style cannot be null.");
    ObjectMap<String, Object> typeStyles = styles.get(style.getClass());
    if (typeStyles == null) {
      typeStyles = new ObjectMap();
      styles.put(style.getClass(), typeStyles);
    }
    typeStyles.put(name, style);
  }
View Full Code Here

        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);
        for (Entry<Class, ObjectMap<String, Object>> entry : data.resources.entries())
          json.setSerializer(entry.key, new AliasSerializer(entry.value));
        readTypeMap(json, (ObjectMap)map.get("styles"), false);
        return skin;
      }

      private void readTypeMap (Json json, ObjectMap<String, ObjectMap> typeToValueMap, boolean isResource) {
        if (typeToValueMap == null)
View Full Code Here

  public void add (String name, Object resource, Class type) {
    if (name == null) throw new IllegalArgumentException("name cannot be null.");
    if (resource == null) throw new IllegalArgumentException("resource cannot be null.");
    ObjectMap<String, Object> typeResources = resources.get(type);
    if (typeResources == null) {
      typeResources = new ObjectMap();
      resources.put(type, typeResources);
    }
    typeResources.put(name, resource);
  }
View Full Code Here

  public void addResource (String name, Object resource) {
    if (name == null) throw new IllegalArgumentException("name cannot be null.");
    if (resource == null) throw new IllegalArgumentException("resource cannot be null.");
    ObjectMap<String, Object> typeResources = resources.get(resource.getClass());
    if (typeResources == null) {
      typeResources = new ObjectMap();
      resources.put(resource.getClass(), typeResources);
    }
    typeResources.put(name, resource);
  }
View Full Code Here

  public void addStyle (String name, Object style) {
    if (name == null) throw new IllegalArgumentException("name cannot be null.");
    if (style == null) throw new IllegalArgumentException("style cannot be null.");
    ObjectMap<String, Object> typeStyles = styles.get(style.getClass());
    if (typeStyles == null) {
      typeStyles = new ObjectMap();
      styles.put(style.getClass(), typeStyles);
    }
    typeStyles.put(name, style);
  }
View Full Code Here

        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>() {
      public void write (Json json, NinePatch ninePatch, Class valueType) {
        TextureRegion[] patches = ninePatch.getPatches();
        boolean singlePatch = patches[0] == null && patches[1] == null && patches[2] == null && patches[3] == null
          && patches[4] != null && patches[5] == null && patches[6] == null && patches[7] == null && patches[8] == null;
        if (ninePatch.getColor() != null) {
          json.writeObjectStart();
          json.writeValue("color", ninePatch.getColor());
          if (singlePatch)
            json.writeValue("region", patches[4]);
          else
            json.writeValue("regions", patches);
          json.writeObjectEnd();
        } else {
          if (singlePatch)
            json.writeValue(patches[4]);
          else
            json.writeValue(patches);
        }
      }

      public NinePatch read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((String)jsonData, NinePatch.class);
        if (jsonData instanceof Array) {
          TextureRegion[] regions = json.readValue(TextureRegion[].class, jsonData);
          if (regions.length == 1) return new NinePatch(regions[0]);
          return new NinePatch(regions);
        } else {
          ObjectMap map = (ObjectMap)jsonData;
          NinePatch ninePatch;
          if (map.containsKey("regions"))
            ninePatch = new NinePatch(json.readValue("regions", TextureRegion[].class, jsonData));
          else if (map.containsKey("region"))
            ninePatch = new NinePatch(json.readValue("region", TextureRegion.class, jsonData));
          else
            ninePatch = new NinePatch(json.readValue(TextureRegion.class, jsonData));
          // throw new SerializationException("Missing ninepatch regions: " + map);
          if (map.containsKey("color")) ninePatch.setColor(json.readValue("color", Color.class, jsonData));
          return ninePatch;
        }
      }
    });

    json.setSerializer(Color.class, new Serializer<Color>() {
      public void write (Json json, Color color, Class valueType) {
        json.writeObjectStart();
        json.writeFields(color);
        json.writeObjectEnd();
      }

      public Color read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((String)jsonData, Color.class);
        ObjectMap map = (ObjectMap)jsonData;
        float r = json.readValue("r", float.class, 0f, jsonData);
        float g = json.readValue("g", float.class, 0f, jsonData);
        float b = json.readValue("b", float.class, 0f, jsonData);
        float a = json.readValue("a", float.class, 1f, jsonData);
        return new Color(r, g, b, a);
View Full Code Here

TOP

Related Classes of com.badlogic.gdx.utils.ObjectMap$Entries

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.