Example usage for com.badlogic.gdx.utils JsonWriter object

List of usage examples for com.badlogic.gdx.utils JsonWriter object

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils JsonWriter object.

Prototype

public JsonWriter object(String name) throws IOException 

Source Link

Usage

From source file:com.steelkiwi.patheditor.proj.ProjectDataConverter.java

License:Apache License

private static void saveScreenToJSON(String projPath, ScreenData scrData) throws Exception {
    String path = projPath + PATH_SEPARATOR + scrData.getJsonPath();
    File scrFile = new File(path);

    if (scrFile.exists()) {
        scrFile.delete();/*  w  w w  .j  a v  a2  s. co m*/
    }
    scrFile.createNewFile();

    StringWriter strWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(strWriter);
    jsonWriter.object().object("screen").set("name", scrData.getName()).set("width", scrData.getWidth())
            .set("height", scrData.getHeight()).set("xmlPath", scrData.getXmlPath())
            .set("jsonPath", scrData.getJsonPath());

    if ((scrData.getBgImage() != null)) {
        jsonWriter.object("bg").set("name", scrData.getBgImage().name).set("type",
                (getWidgetType(scrData.getBgImage()) != null) ? getWidgetType(scrData.getBgImage()).ordinal()
                        : -1)
                .set("texturePath",
                        FileUtils.getElementRelativePath(projPath,
                                ((GdxImage) scrData.getBgImage()).getTexPath()))
                .set("scaleX", scrData.getBgImage().scaleX).set("scaleY", scrData.getBgImage().scaleY)
                .set("x", scrData.getBgImage().x).set("y", scrData.getBgImage().y)
                .set("angle", scrData.getBgImage().rotation).pop();
    }

    if ((scrData.getPath() != null) && (scrData.getPath().getPath() != null)) {
        jsonWriter.object("path").set("xmlPath", scrData.getPath().getXmlPath())
                .set("jsonPath", scrData.getPath().getJsonPath()).pop();
    }

    jsonWriter.pop();
    jsonWriter.pop();
    jsonWriter.close();

    FileWriter writer = new FileWriter(new File(path));
    writer.write(strWriter.toString());
    writer.close();
}

From source file:com.steelkiwi.patheditor.proj.ProjectDataConverter.java

License:Apache License

private static void savePathToJSON(String projPath, GdxPath gdxPath) throws Exception {
    String path = projPath + gdxPath.getJsonPath();
    File pathFile = new File(path);

    if (pathFile.exists()) {
        pathFile.delete();//w w  w. j a v  a  2 s  .  co m
    }
    pathFile.createNewFile();

    StringWriter strWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(strWriter);
    jsonWriter.object().object("path").set("name", gdxPath.getName()).set("pointsCnt", gdxPath.getPointsCnt())
            .set("controlColor", gdxPath.getControlColor()).set("segmentColor", gdxPath.getSegmentColor())
            .set("selectColor", gdxPath.getSelectColor()).set("xmlPath", gdxPath.getXmlPath())
            .set("jsonPath", gdxPath.getJsonPath());

    if ((gdxPath.getPath() != null) && (gdxPath.getPath().getPathVerticesCount() > 0)) {
        jsonWriter.array("controlVertices");
        Vector3 controlVertex;
        for (int i = 0; i < gdxPath.getControlPath().size(); i++) {
            controlVertex = gdxPath.getControlPath().get(i);
            jsonWriter.object().set("id", i).set("x", controlVertex.x).set("y", controlVertex.y).pop();
        }
        jsonWriter.pop();

        jsonWriter.array("vertices");
        PathVertex vertex;
        for (int i = 0; i < gdxPath.getPath().getPathVerticesCount(); i++) {
            vertex = gdxPath.getPath().getPathVertexByIndex(i);
            jsonWriter.object().set("id", i).set("x", vertex.getPosition().x).set("y", vertex.getPosition().y)
                    .set("tanX", vertex.getTangentNornal().x).set("tanY", vertex.getTangentNornal().y)
                    .set("angle", vertex.getAngle()).pop();
        }
        jsonWriter.pop();
    }

    jsonWriter.pop();
    jsonWriter.pop();
    jsonWriter.close();

    FileWriter writer = new FileWriter(new File(path));
    writer.write(strWriter.toString());
    writer.close();
}