use of com.badlogic.gdx.scenes.scene2d.ui.Skin.TintedDrawable in project skin-composer by raeleus.
the class JsonData method readFile.
public Array<String> readFile(FileHandle fileHandle) throws Exception {
Array<String> warnings = new Array<>();
main.getProjectData().setChangesSaved(false);
// read drawables from texture atlas file
FileHandle atlasHandle = fileHandle.sibling(fileHandle.nameWithoutExtension() + ".atlas");
if (atlasHandle.exists()) {
main.getProjectData().getAtlasData().readAtlas(atlasHandle);
} else {
warnings.add("[RED]ERROR:[] Atlas file [BLACK]" + atlasHandle.name() + "[] does not exist.");
return warnings;
}
// folder for critical files to be copied to
FileHandle saveFile = main.getProjectData().getSaveFile();
FileHandle targetDirectory;
if (saveFile != null) {
targetDirectory = saveFile.sibling(saveFile.nameWithoutExtension() + "_data");
} else {
targetDirectory = Gdx.files.local("temp/" + main.getProjectData().getId() + "_data");
}
// read json file and create styles
JsonReader reader = new JsonReader();
JsonValue val = reader.parse(fileHandle.reader("UTF-8"));
for (JsonValue child : val.iterator()) {
// fonts
if (child.name().equals(BitmapFont.class.getName())) {
for (JsonValue font : child.iterator()) {
if (font.get("file") != null) {
FileHandle fontFile = fileHandle.sibling(font.getString("file"));
if (!fontFile.exists()) {
warnings.add("[RED]ERROR:[] Font file [BLACK]" + fontFile.name() + "[] does not exist.");
return warnings;
}
FileHandle fontCopy = targetDirectory.child(font.getString("file"));
if (!fontCopy.parent().equals(fontFile.parent())) {
fontFile.copyTo(fontCopy);
}
FontData fontData = new FontData(font.name(), fontCopy);
// delete fonts with the same name
for (FontData originalData : new Array<>(fonts)) {
if (originalData.getName().equals(fontData.getName())) {
fonts.removeValue(originalData, true);
}
}
fonts.add(fontData);
BitmapFont.BitmapFontData bitmapFontData = new BitmapFont.BitmapFontData(fontCopy, false);
for (String path : bitmapFontData.imagePaths) {
FileHandle file = new FileHandle(path);
main.getProjectData().getAtlasData().getDrawable(file.nameWithoutExtension()).visible = false;
}
}
}
} else // FreeType fonts
if (child.name().equals(FreeTypeFontGenerator.class.getName())) {
for (JsonValue font : child.iterator()) {
if (font.get("font") != null) {
FreeTypeFontData data = new FreeTypeFontData();
data.name = font.name;
data.previewTTF = font.getString("previewTTF", null);
data.useCustomSerializer = font.getBoolean("useCustomSerializer", false);
data.size = font.getInt("size", 16);
data.mono = font.getBoolean("mono", false);
data.hinting = font.getString("hinting", "AutoMedium");
data.color = font.getString("color", null);
data.gamma = font.getFloat("gamma", 1.8f);
data.renderCount = font.getInt("renderCount", 2);
data.borderWidth = font.getFloat("borderWidth", 0);
data.borderColor = font.getString("borderColor", null);
data.borderStraight = font.getBoolean("borderStraight", false);
data.borderGamma = font.getFloat("borderGamma", 1.8f);
data.shadowOffsetX = font.getInt("shadowOffsetX", 0);
data.shadowOffsetY = font.getInt("shadowOffsetY", 0);
data.shadowColor = font.getString("shadowColor", null);
data.spaceX = font.getInt("spaceX", 0);
data.spaceY = font.getInt("spaceY", 0);
data.characters = font.getString("characters", "");
data.kerning = font.getBoolean("kerning", true);
data.flip = font.getBoolean("flip", false);
data.genMipMaps = font.getBoolean("genMipMaps", false);
data.minFilter = font.getString("minFilter", "Nearest");
data.magFilter = font.getString("magFilter", "Nearest");
data.incremental = font.getBoolean("bitmapFont", false);
FileHandle fontFile = fileHandle.sibling(font.getString("font"));
if (!fontFile.exists()) {
warnings.add("[RED]ERROR:[] Font file [BLACK]" + fontFile.name() + "[] does not exist.");
return warnings;
}
FileHandle fontCopy = targetDirectory.child(font.getString("font"));
if (!fontCopy.parent().equals(fontFile.parent())) {
fontFile.copyTo(fontCopy);
}
data.file = fontCopy;
data.createBitmapFont(main);
if (data.bitmapFont != null) {
// delete fonts with the same name
for (FontData duplicate : new Array<>(fonts)) {
if (duplicate.getName().equals(data.name)) {
fonts.removeValue(duplicate, false);
}
}
for (FreeTypeFontData duplicate : new Array<>(freeTypeFonts)) {
if (duplicate.name.equals(data.name)) {
freeTypeFonts.removeValue(duplicate, false);
}
}
freeTypeFonts.add(data);
}
}
}
} else // colors
if (child.name().equals(Color.class.getName())) {
for (JsonValue color : child.iterator()) {
ColorData colorData = new ColorData(color.name, new Color(color.getFloat("r", 0.0f), color.getFloat("g", 0.0f), color.getFloat("b", 0.0f), color.getFloat("a", 0.0f)));
// delete colors with the same name
for (ColorData originalData : new Array<>(colors)) {
if (originalData.getName().equals(colorData.getName())) {
colors.removeValue(originalData, true);
}
}
colors.add(colorData);
}
} else if (child.name().equals(TiledDrawable.class.getName())) {
for (JsonValue tiledDrawable : child.iterator()) {
DrawableData drawableData = new DrawableData(main.getProjectData().getAtlasData().getDrawable(tiledDrawable.getString("region")).file);
drawableData.name = tiledDrawable.name;
drawableData.tiled = true;
drawableData.visible = true;
drawableData.tintName = tiledDrawable.getString("color");
drawableData.minWidth = tiledDrawable.getFloat("minWidth", 0.0f);
drawableData.minHeight = tiledDrawable.getFloat("minHeight", 0.0f);
// delete drawables with the same name
for (DrawableData originalData : new Array<>(main.getProjectData().getAtlasData().getDrawables())) {
if (originalData.name.equals(drawableData.name)) {
main.getProjectData().getAtlasData().getDrawables().removeValue(originalData, true);
}
}
main.getProjectData().getAtlasData().getDrawables().add(drawableData);
}
} else // tinted drawables
if (child.name().equals(TintedDrawable.class.getName())) {
for (JsonValue tintedDrawable : child.iterator()) {
DrawableData drawableData = new DrawableData(main.getProjectData().getAtlasData().getDrawable(tintedDrawable.getString("name")).file);
drawableData.name = tintedDrawable.name;
if (!tintedDrawable.get("color").isString()) {
drawableData.tint = new Color(tintedDrawable.get("color").getFloat("r", 0.0f), tintedDrawable.get("color").getFloat("g", 0.0f), tintedDrawable.get("color").getFloat("b", 0.0f), tintedDrawable.get("color").getFloat("a", 0.0f));
} else {
drawableData.tintName = tintedDrawable.getString("color");
}
// delete drawables with the same name
for (DrawableData originalData : new Array<>(main.getProjectData().getAtlasData().getDrawables())) {
if (originalData.name.equals(drawableData.name)) {
main.getProjectData().getAtlasData().getDrawables().removeValue(originalData, true);
}
}
main.getProjectData().getAtlasData().getDrawables().add(drawableData);
}
} else // styles
{
int classIndex = 0;
if (testClassString(child.name)) {
Class matchClass = ClassReflection.forName(child.name);
for (Class clazz : Main.STYLE_CLASSES) {
if (clazz.equals(matchClass)) {
break;
} else {
classIndex++;
}
}
Class clazz = Main.BASIC_CLASSES[classIndex];
for (JsonValue style : child.iterator()) {
StyleData data = newStyle(clazz, style.name);
for (JsonValue property : style.iterator()) {
StyleProperty styleProperty = data.properties.get(property.name);
if (styleProperty.type.equals(Float.TYPE)) {
styleProperty.value = (double) property.asFloat();
} else if (styleProperty.type.equals(Color.class)) {
if (property.isString()) {
styleProperty.value = property.asString();
} else {
Gdx.app.error(getClass().getName(), "Can't import JSON files that do not use predefined colors.");
warnings.add("Property [BLACK]" + styleProperty.name + "[] value cleared for [BLACK]" + clazz.getSimpleName() + ": " + data.name + "[] (Unsupported color definition)");
}
} else {
if (property.isString()) {
styleProperty.value = property.asString();
} else {
Gdx.app.error(getClass().getName(), "Can't import JSON files that do not use String names for field values.");
warnings.add("Property [BLACK]" + styleProperty.name + "[] value cleared for [BLACK]" + clazz.getSimpleName() + ": " + data.name + "[] (Unsupported propety value)");
}
}
}
}
} else {
// custom classes
CustomClass customClass = new CustomClass(child.name, child.name.replaceFirst(".*(\\.|\\$)", ""));
customClass.setMain(main);
CustomClass existingClass = getCustomClass(customClass.getDisplayName());
if (existingClass != null) {
customClasses.removeValue(existingClass, true);
}
customClasses.add(customClass);
for (JsonValue style : child.iterator()) {
CustomStyle customStyle = new CustomStyle(style.name);
customStyle.setParentClass(customClass);
customStyle.setMain(main);
CustomStyle existingStyle = customClass.getStyle(style.name);
if (existingStyle != null) {
customClass.getStyles().removeValue(existingStyle, true);
}
if (customStyle.getName().equals("default")) {
customStyle.setDeletable(false);
}
customClass.getStyles().add(customStyle);
for (JsonValue property : style.iterator()) {
CustomProperty customProperty = new CustomProperty();
customProperty.setName(property.name);
customProperty.setParentStyle(customStyle);
customProperty.setMain(main);
CustomProperty existingProperty = customStyle.getProperty(property.name);
if (existingProperty != null) {
customStyle.getProperties().removeValue(existingProperty, true);
}
if (property.isNumber()) {
customProperty.setType(PropertyType.NUMBER);
customProperty.setValue(property.asDouble());
} else if (property.isString()) {
customProperty.setType(PropertyType.TEXT);
customProperty.setValue(property.asString());
} else if (property.isBoolean()) {
customProperty.setType(PropertyType.BOOL);
customProperty.setValue(property.asBoolean());
} else if (property.isObject()) {
customProperty.setType(PropertyType.RAW_TEXT);
customProperty.setValue(property.toJson(OutputType.minimal));
} else if (property.isArray()) {
customProperty.setType(PropertyType.RAW_TEXT);
customProperty.setValue(property.toJson(OutputType.minimal));
} else {
customProperty = null;
}
if (customProperty != null) {
customStyle.getProperties().add(customProperty);
// add to template style as necessary
if (customClass.getTemplateStyle().getProperty(customProperty.getName()) == null) {
CustomProperty dupeProperty = customProperty.copy();
dupeProperty.setValue(null);
customClass.getTemplateStyle().getProperties().add(dupeProperty);
}
}
}
}
// ensure default style has all the template styles.
for (CustomStyle style : customClass.getStyles()) {
if (style.getName().equals("default")) {
for (CustomProperty templateProperty : customClass.getTemplateStyle().getProperties()) {
boolean hasProperty = false;
for (CustomProperty customProperty : style.getProperties()) {
if (customProperty.getName().equals(templateProperty.getName())) {
hasProperty = true;
break;
}
}
if (!hasProperty) {
style.getProperties().add(templateProperty.copy());
}
}
break;
}
}
}
}
}
return warnings;
}
Aggregations