Example usage for com.badlogic.gdx.utils JsonValue asFloat

List of usage examples for com.badlogic.gdx.utils JsonValue asFloat

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils JsonValue asFloat.

Prototype

public float asFloat() 

Source Link

Document

Returns this value as a float.

Usage

From source file:com.ray3k.skincomposer.data.JsonData.java

License:Open Source License

public void readFile(FileHandle fileHandle) throws Exception {
    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);
    }//w ww.  j a va 2s.  co  m

    //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);

    for (JsonValue child : val.iterator()) {
        //fonts
        if (child.name().equals(BitmapFont.class.getName())) {
            for (JsonValue font : child.iterator()) {
                FileHandle fontFile = fileHandle.sibling(font.getString("file"));
                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;
                }
            }
        } //colors
        else 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);
            }
        } //tinted drawables
        else 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");
                }

                //todo:test overwriting a base drawable that is depended on by another tint
                //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);
            }
        } //styles
        else {
            int classIndex = 0;
            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.");
                        }
                    } 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.");
                        }
                    }
                }
            }
        }
    }
}

From source file:com.strategames.engine.gameobject.types.Balloon.java

License:Open Source License

@Override
protected void readValue(JsonValue jsonData) {
    String name = jsonData.name();
    if (name.contentEquals("liftfactor")) {
        setLiftFactor(Float.valueOf(jsonData.asFloat()));
    }/*from  w ww  . j a v a 2  s. c  o m*/
}

From source file:com.strategames.engine.gameobject.types.Star.java

License:Open Source License

@Override
protected void readValue(JsonValue jsonData) {
    String name = jsonData.name();
    if (name.contentEquals("rotationSpeed")) {
        this.rotationSpeed = jsonData.asFloat();
    }//w  w  w .ja v  a2s. c o m
}

From source file:com.strategames.engine.gameobject.types.Wall.java

License:Open Source License

@Override
protected void readValue(JsonValue jsonData) {
    String name = jsonData.name();
    if (name.contentEquals("length")) {
        setLength(jsonData.asFloat());
    } else if (name.contentEquals("border")) {
        setBorder(jsonData.asBoolean());
    }/* www .j  a v  a2s  . c  o m*/
}

From source file:mt.Json.java

License:Apache License

/** @param type May be null if the type is unknown.
 * @param elementType May be null if the type is unknown.
 * @return May be null. *///from  w  ww . j a  v  a2s .  c  om
public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
    if (jsonData == null)
        return null;

    if (jsonData.isObject()) {
        String className = typeName == null ? null : jsonData.getString(typeName, null);
        if (className != null) {
            jsonData.remove(typeName);
            try {
                type = (Class<T>) ClassReflection.forName(className);
            } catch (ReflectionException ex) {
                type = tagToClass.get(className);
                if (type == null)
                    throw new SerializationException(ex);
            }
        }

        Object object;
        if (type != null) {
            if (type == String.class || type == Integer.class || type == Boolean.class || type == Float.class
                    || type == Long.class || type == Double.class || type == Short.class || type == Byte.class
                    || type == Character.class || type.isEnum()) {
                return readValue("value", type, jsonData);
            }

            Serializer serializer = classToSerializer.get(type);
            if (serializer != null)
                return (T) serializer.read(this, jsonData, type);

            object = newInstance(type);

            if (object instanceof Serializable) {
                ((Serializable) object).read(this, jsonData);
                return (T) object;
            }

            if (object instanceof HashMap) {
                HashMap result = (HashMap) object;
                for (JsonValue child = jsonData.child(); child != null; child = child.next())
                    result.put(child.name(), readValue(elementType, null, child));
                return (T) result;
            }
        } else if (defaultSerializer != null) {
            return (T) defaultSerializer.read(this, jsonData, type);
        } else
            return (T) jsonData;

        if (object instanceof ObjectMap) {
            ObjectMap result = (ObjectMap) object;
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                result.put(child.name(), readValue(elementType, null, child));
            return (T) result;
        }
        readFields(object, jsonData);
        return (T) object;
    }

    if (type != null) {
        Serializer serializer = classToSerializer.get(type);
        if (serializer != null)
            return (T) serializer.read(this, jsonData, type);
    }

    if (jsonData.isArray()) {
        if ((type == null || type == Object.class) || ClassReflection.isAssignableFrom(Array.class, type)) {
            Array newArray = (type == null || type == Object.class) ? new Array() : (Array) newInstance(type);
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                newArray.add(readValue(elementType, null, child));
            return (T) newArray;
        }
        if (ClassReflection.isAssignableFrom(List.class, type)) {
            List newArray = (type == null || type.isInterface()) ? new ArrayList() : (List) newInstance(type);
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                newArray.add(readValue(elementType, null, child));
            return (T) newArray;
        }
        if (type.isArray()) {
            Class componentType = type.getComponentType();
            if (elementType == null)
                elementType = componentType;
            Object newArray = ArrayReflection.newInstance(componentType, jsonData.size);
            int i = 0;
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                ArrayReflection.set(newArray, i++, readValue(elementType, null, child));
            return (T) newArray;
        }
        throw new SerializationException(
                "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")");
    }

    if (jsonData.isNumber()) {
        try {
            if (type == null || type == float.class || type == Float.class)
                return (T) (Float) jsonData.asFloat();
            if (type == int.class || type == Integer.class)
                return (T) (Integer) jsonData.asInt();
            if (type == long.class || type == Long.class)
                return (T) (Long) jsonData.asLong();
            if (type == double.class || type == Double.class)
                return (T) (Double) (double) jsonData.asFloat();
            if (type == String.class)
                return (T) Float.toString(jsonData.asFloat());
            if (type == short.class || type == Short.class)
                return (T) (Short) (short) jsonData.asInt();
            if (type == byte.class || type == Byte.class)
                return (T) (Byte) (byte) jsonData.asInt();
        } catch (NumberFormatException ignored) {
        }
        jsonData = new JsonValue(jsonData.asString());
    }

    if (jsonData.isBoolean()) {
        try {
            if (type == null || type == boolean.class || type == Boolean.class)
                return (T) (Boolean) jsonData.asBoolean();
        } catch (NumberFormatException ignored) {
        }
        jsonData = new JsonValue(jsonData.asString());
    }

    if (jsonData.isString()) {
        String string = jsonData.asString();
        if (type == null || type == String.class)
            return (T) string;
        try {
            if (type == int.class || type == Integer.class)
                return (T) Integer.valueOf(string);
            if (type == float.class || type == Float.class)
                return (T) Float.valueOf(string);
            if (type == long.class || type == Long.class)
                return (T) Long.valueOf(string);
            if (type == double.class || type == Double.class)
                return (T) Double.valueOf(string);
            if (type == short.class || type == Short.class)
                return (T) Short.valueOf(string);
            if (type == byte.class || type == Byte.class)
                return (T) Byte.valueOf(string);
        } catch (NumberFormatException ignored) {
        }
        if (type == boolean.class || type == Boolean.class)
            return (T) Boolean.valueOf(string);
        if (type == char.class || type == Character.class)
            return (T) (Character) string.charAt(0);
        if (ClassReflection.isAssignableFrom(Enum.class, type)) {
            Object[] constants = type.getEnumConstants();
            for (int i = 0, n = constants.length; i < n; i++)
                if (string.equals(constants[i].toString()))
                    return (T) constants[i];
        }
        if (type == CharSequence.class)
            return (T) string;
        throw new SerializationException(
                "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")");
    }

    return null;
}