Object object = map.get(name);
if (object == null) {
ObjectMap<String, Object> regions = data.resources.get(TextureRegion.class);
if (regions != null) {
object = regions.get(name);
if (object != null) object = new NinePatch((TextureRegion)object);
}
if (object == null)
throw new SerializationException("Skin has a " + type.getSimpleName()
+ " that could not be found in the resources: " + jsonData);
}
return object;
}
}
json.setSerializer(Skin.class, new Serializer<Skin>() {
public void write (Json json, Skin skin, Class valueType) {
json.writeObjectStart();
json.writeValue("resources", skin.data.resources);
for (Entry<Class, ObjectMap<String, Object>> entry : data.resources.entries())
json.setSerializer(entry.key, new AliasSerializer(entry.value));
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)
throw new SerializationException("Skin file is missing a \"" + (isResource ? "resources" : "styles")
+ "\" section.");
for (Entry<String, ObjectMap> typeEntry : typeToValueMap.entries()) {
Class type;
try {
type = Class.forName(typeEntry.key);
} catch (ClassNotFoundException ex) {
throw new SerializationException(ex);
}
ObjectMap<String, ObjectMap> valueMap = (ObjectMap)typeEntry.value;
for (Entry<String, ObjectMap> valueEntry : valueMap.entries()) {
try {
if (isResource)
addResource(valueEntry.key, json.readValue(type, valueEntry.value));
else
addStyle(valueEntry.key, json.readValue(type, valueEntry.value));
} 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) {
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.data.texture, x, y, width, height);
}
});
json.setSerializer(BitmapFont.class, new Serializer<BitmapFont>() {
public void write (Json json, BitmapFont font, Class valueType) {
json.writeValue(font.getData().getFontFile().toString().replace('\\', '/'));
}
public BitmapFont read (Json json, Object jsonData, Class type) {
String path = json.readValue(String.class, jsonData);
FileHandle file = skinFile.parent().child(path);
if (!file.exists()) file = Gdx.files.internal(path);
return new BitmapFont(file, false);
}
});
json.setSerializer(NinePatch.class, new Serializer<NinePatch>() {
public void write (Json json, NinePatch ninePatch, Class valueType) {
json.writeValue(ninePatch.getPatches());
}
public NinePatch read (Json json, Object jsonData, Class type) {
return new NinePatch(json.readValue(TextureRegion[].class, jsonData));
}
});
return json;
}