Search in sources :

Example 11 with Json

use of com.badlogic.gdx.utils.Json in project libgdx by libgdx.

the class ParticleEffectLoader method getDependencies.

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, ParticleEffectLoadParameter parameter) {
    Json json = new Json();
    ResourceData<ParticleEffect> data = json.fromJson(ResourceData.class, file);
    Array<AssetData> assets = null;
    synchronized (items) {
        ObjectMap.Entry<String, ResourceData<ParticleEffect>> entry = new ObjectMap.Entry<String, ResourceData<ParticleEffect>>();
        entry.key = fileName;
        entry.value = data;
        items.add(entry);
        assets = data.getAssets();
    }
    Array<AssetDescriptor> descriptors = new Array<AssetDescriptor>();
    for (AssetData<?> assetData : assets) {
        // If the asset doesn't exist try to load it from loading effect directory
        if (!resolve(assetData.filename).exists()) {
            assetData.filename = file.parent().child(Gdx.files.internal(assetData.filename).name()).path();
        }
        if (assetData.type == ParticleEffect.class) {
            descriptors.add(new AssetDescriptor(assetData.filename, assetData.type, parameter));
        } else
            descriptors.add(new AssetDescriptor(assetData.filename, assetData.type));
    }
    return descriptors;
}
Also used : AssetData(com.badlogic.gdx.graphics.g3d.particles.ResourceData.AssetData) Json(com.badlogic.gdx.utils.Json) Array(com.badlogic.gdx.utils.Array) ObjectMap(com.badlogic.gdx.utils.ObjectMap) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 12 with Json

use of com.badlogic.gdx.utils.Json in project gdx-skineditor by cobolfoo.

the class Skin method save.

/** Store all resources in the specified skin JSON file. */
public boolean save(FileHandle skinFile) {
    StringWriter jsonText = new StringWriter();
    JsonWriter writer = new JsonWriter(jsonText);
    Json json = new Json();
    json.setWriter(writer);
    json.writeObjectStart();
    //		Iterator it = resources.keys().iterator();
    //		while(it.hasNext()) {
    //			Object item = it.next();
    //			if (item instanceof com.badlogic.gdx.scenes.scene2d.utils.Drawable) {
    //				Drawable d = (Drawable) item;
    //				
    //			}
    //		}
    // Sort items
    Array<Class> items = new Array<Class>();
    items.add(com.badlogic.gdx.graphics.Color.class);
    items.add(com.badlogic.gdx.graphics.g2d.BitmapFont.class);
    items.add(Skin.TintedDrawable.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.ProgressBar.ProgressBarStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.SplitPane.SplitPaneStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.Touchpad.TouchpadStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.Tree.TreeStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.Slider.SliderStyle.class);
    items.add(com.badlogic.gdx.scenes.scene2d.ui.SelectBox.SelectBoxStyle.class);
    for (Class<?> item : items) {
        String name = item.getName();
        json.writeObjectStart(name);
        ObjectMap<String, Object> typeResources = resources.get(item);
        // Build a temporary array for string keys to prevent nested
        // iterators with getObjetName function.
        Array<String> styles = new Array<String>();
        Iterator itStyles = typeResources.keys().iterator();
        while (itStyles.hasNext()) {
            String style = (String) itStyles.next();
            styles.add(style);
        }
        for (String style : styles) {
            json.writeObjectStart(style);
            Field[] fields = ClassReflection.getFields(typeResources.get(style).getClass());
            // Handle functions
            if (typeResources.get(style) instanceof com.badlogic.gdx.graphics.g2d.BitmapFont) {
                BitmapFont font = (BitmapFont) typeResources.get(style);
                json.writeValue("file", font.getData().fontFile.name());
            }
            for (Field field : fields) {
                try {
                    Object object = field.get(typeResources.get(style));
                    if (object != null) {
                        if (object instanceof BitmapFont) {
                            String value = resolveObjectName(BitmapFont.class, object);
                            if (value != null) {
                                json.writeValue(field.getName(), value);
                            }
                        } else if (object instanceof Float) {
                            if ((Float) object != 0.0f) {
                                json.writeValue(field.getName(), object);
                            }
                        } else if (object instanceof Color) {
                            if (typeResources.get(style) instanceof Color) {
                            // Skip sub-color
                            } else {
                                json.writeValue(field.getName(), object);
                            }
                        } else if (object instanceof Drawable) {
                            if (typeResources.get(style) instanceof com.badlogic.gdx.scenes.scene2d.ui.Skin.TintedDrawable) {
                            // Skip drawable if it is from tinted drawable									
                            } else {
                                String value = null;
                                value = resolveObjectName(Drawable.class, object);
                                //									
                                if (value != null) {
                                    json.writeValue(field.getName(), value);
                                }
                            }
                        } else if (object instanceof ListStyle) {
                            String value = resolveObjectName(ListStyle.class, object);
                            if (value != null) {
                                json.writeValue(field.getName(), value);
                            }
                        } else if (object instanceof ScrollPaneStyle) {
                            String value = resolveObjectName(ScrollPaneStyle.class, object);
                            if (value != null) {
                                json.writeValue(field.getName(), value);
                            }
                        } else if (object instanceof String) {
                            // only used to get original drawable for tinted drawable
                            json.writeValue(field.getName(), object);
                        } else if (object instanceof char[]) {
                        // Don't store.
                        } else {
                            throw new IllegalArgumentException("resource object type is unknown: " + object.getClass().getCanonicalName());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            json.writeObjectEnd();
        }
        json.writeObjectEnd();
    }
    json.writeObjectEnd();
    PrettyPrintSettings settings = new PrettyPrintSettings();
    settings.outputType = OutputType.minimal;
    settings.singleLineColumns = 100;
    skinFile.writeString(json.prettyPrint(jsonText.toString(), settings), false);
    return true;
}
Also used : ListStyle(com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle) Field(com.badlogic.gdx.utils.reflect.Field) StringWriter(java.io.StringWriter) Iterator(java.util.Iterator) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) PrettyPrintSettings(com.badlogic.gdx.utils.JsonValue.PrettyPrintSettings) Color(com.badlogic.gdx.graphics.Color) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) Json(com.badlogic.gdx.utils.Json) JsonWriter(com.badlogic.gdx.utils.JsonWriter) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Array(com.badlogic.gdx.utils.Array) ScrollPaneStyle(com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle)

Aggregations

Json (com.badlogic.gdx.utils.Json)12 FileHandle (com.badlogic.gdx.files.FileHandle)4 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 Array (com.badlogic.gdx.utils.Array)4 Color (com.badlogic.gdx.graphics.Color)3 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)3 SerializationException (com.badlogic.gdx.utils.SerializationException)3 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)2 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)2 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)2 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)2 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)2 ReadOnlySerializer (com.badlogic.gdx.utils.Json.ReadOnlySerializer)2 JsonValue (com.badlogic.gdx.utils.JsonValue)2 ObjectMap (com.badlogic.gdx.utils.ObjectMap)2 Field (com.badlogic.gdx.utils.reflect.Field)2 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)1 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1