com.badlogic.gdx.utils.JsonWriter Java Examples

The following examples show how to use com.badlogic.gdx.utils.JsonWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MusicEventManager.java    From gdx-soundboard with MIT License 6 votes vote down vote up
/**
 * Load a save file.
 * 
 * @param fileName
 *            The path to the file.
 */
public void load(FileHandle file) {
    this.clear();
    final Json json = new Json(JsonWriter.OutputType.json);

    container = json.fromJson(Container.class,
            file.readString());

    String path = file.path();
    int lastSlash = path.lastIndexOf("/");
    container.basePath =  path.substring(0, lastSlash);
    
    for (int i = 0; i < container.states.size; i++) {
        final MusicState state = container.states.get(i);
        add(state);
    }
}
 
Example #2
Source File: CourseDataAccessor.java    From beatoraja with GNU General Public License v3.0 6 votes vote down vote up
/**
 * コースデータを保存する
 *
 * @param cd コースデータ
 */
public void write(String name, CourseData[] cd) {
    try {
    	for(CourseData c : cd) {
    		c.shrink();
    	}
        Json json = new Json();
        json.setOutputType(JsonWriter.OutputType.json);
        OutputStreamWriter fw = new OutputStreamWriter(new BufferedOutputStream(
                new FileOutputStream(coursedir + "/" + name + ".json")), "UTF-8");
        fw.write(json.prettyPrint(cd));
        fw.flush();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: Save.java    From Unlucky with MIT License 5 votes vote down vote up
public Save(Player player, String path) {
    this.player = player;
    psave = new PlayerAccessor();
    json = new Json();
    json.setOutputType(JsonWriter.OutputType.json);
    json.setUsePrototypes(false);
    file = Gdx.files.local(path);
}
 
Example #4
Source File: DescriptorConverter.java    From Mundus with Apache License 2.0 5 votes vote down vote up
public static ProjectSettings convert(ProjectSettingsDescriptor descriptor) {
    ProjectSettings settings = new ProjectSettings();
    if(descriptor == null) return settings;

    // export settings
    settings.getExport().allAssets = descriptor.isExportAllAssets();
    settings.getExport().compressScenes = descriptor.isExportCompressScenes();
    if(descriptor.getExportOutputFolder() != null && descriptor.getExportOutputFolder().length() > 0) {
        settings.getExport().outputFolder = new FileHandle(descriptor.getExportOutputFolder());
    }
    settings.getExport().jsonType = JsonWriter.OutputType.valueOf(descriptor.getJsonType());

    return settings;
}
 
Example #5
Source File: VersionData.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
@Override
public VersionData read(Json json, JsonValue jsonData, Class type) {
    VersionData data = new VersionData();
    data.plainJson = jsonData.prettyPrint(JsonWriter.OutputType.json, 0);

    for (JsonValue entry = jsonData.child; entry != null; entry = entry.next) {
        switch (entry.name) {
            case "id":
                data.id = entry.asInt();
                break;
            case "html_url":
                data.url = entry.asString();
                break;
            case "tag_name":
                String tagName = entry.asString();
                data.tag = tagName;
                data.version = new Version(tagName);
                break;
            case "name":
                data.name = entry.asString();
                break;
            case "body":
                data.description = entry.asString();
                break;
        }
    }

    return data;
}
 
Example #6
Source File: PlayerConfig.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
public static void write(String playerpath, PlayerConfig player) {
	try (FileWriter writer = new FileWriter(Paths.get(playerpath + "/" + player.getId() + "/config.json").toFile())) {
		Json json = new Json();
		json.setOutputType(JsonWriter.OutputType.json);
		json.setUsePrototypes(false);
		writer.write(json.prettyPrint(player));
		writer.flush();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #7
Source File: JsonDataModifier.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
/**
 * Returns modified json data.
 *
 * @param oldJsonData Old data as json string.
 * @return New data as json string
 */
@SuppressWarnings("unchecked")
public String modify(String oldJsonData) {
    T oldData = JsonProcessor.process(wantedType, oldJsonData);
    T newData = (T) transactionFunction.apply(oldData);
    Json json = new Json();
    json.setTypeName(null);
    json.setQuoteLongValues(true);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(JsonWriter.OutputType.json);
    return json.toJson(newData, wantedType);
}
 
Example #8
Source File: MapTransformer.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
static String mapToJSON(Map<String, Object> map) {
    Json json = new Json();
    json.setTypeName(null);
    json.setQuoteLongValues(true);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(JsonWriter.OutputType.json);
    return json.toJson(map, HashMap.class);
}
 
Example #9
Source File: StringGenerator.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
public static String dataToString(Object object) {
    if (isPrimitiveType(object))
        return object.toString();
    Json json = new Json();
    json.setTypeName(null);
    json.setQuoteLongValues(false);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(JsonWriter.OutputType.json);
    return json.toJson(object);
}
 
Example #10
Source File: BvbProject.java    From talos with Apache License 2.0 5 votes vote down vote up
@Override
public String getProjectString() {
    Json json = new Json();
    json.setOutputType(JsonWriter.OutputType.json);
    String data = json.prettyPrint(bvBAddon.workspace);
    return data;
}
 
Example #11
Source File: JsonListMapDeserializer.java    From gdx-fireapp with Apache License 2.0 4 votes vote down vote up
private Object processObject(Json json, JsonValue jsonData) {
    return json.fromJson(this.objectType, jsonData.toJson(JsonWriter.OutputType.json));
}
 
Example #12
Source File: ControllerMappingsTest.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
@Test
public void testJsonSaveLoad() {
    ControllerMappings mappings = new ControllerMappings();

    mappings.addConfiguredInput(new ConfiguredInput(ConfiguredInput.Type.button, 1));
    mappings.addConfiguredInput(new ConfiguredInput(ConfiguredInput.Type.axis, 2));
    mappings.addConfiguredInput(new ConfiguredInput(ConfiguredInput.Type.axis, 3));
    mappings.addConfiguredInput(new ConfiguredInput(ConfiguredInput.Type.axis, 4));

    mappings.commitConfig();

    // now we connect a Controller... and map
    MockedController controller = new MockedController();
    controller.axisValues = new float[3];
    controller.povDirection = PovDirection.center;
    controller.axisValues[0] = 1f;
    controller.pressedButton = -1;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 2));
    controller.pressedButton = 2;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 1));
    controller.pressedButton = 1;
    assertEquals(ControllerMappings.RecordResult.need_second_button, mappings.recordMapping(controller, 3));
    controller.pressedButton = 0;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 3));
    controller.pressedButton = -1;
    controller.povDirection = PovDirection.east;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 4));

    JsonValue json = mappings.toJson();
    System.out.println(json.toJson(JsonWriter.OutputType.json));
    MappedController mappedController = new MappedController(controller, mappings);
    mappings.resetMappings(controller);

    mappedController.refreshMappingCache();
    assertEquals(0, mappedController.getConfiguredAxisValue(4), 0.1f);

    mappings.fillFromJson(json);
    mappedController.refreshMappingCache();

    assertEquals(1, mappedController.getConfiguredAxisValue(4), 0.1f);
    controller.pressedButton = 2;
    assertTrue(mappedController.isButtonPressed(1));
    controller.pressedButton = 1;
    assertEquals(1, mappedController.getConfiguredAxisValue(3), 0.1f);
}
 
Example #13
Source File: ControllerMappingsTest.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
@Test
public void testPovToAxisMapping() {
    ControllerMappings mappings = new ControllerMappings();

    // We test 3 axis
    ConfiguredInput axis1 = new ConfiguredInput(ConfiguredInput.Type.axis, 5);
    ConfiguredInput axis2 = new ConfiguredInput(ConfiguredInput.Type.axisAnalog, 6);
    ConfiguredInput axis3 = new ConfiguredInput(ConfiguredInput.Type.axisDigital, 7);

    mappings.addConfiguredInput(axis1);
    mappings.addConfiguredInput(axis2);
    mappings.addConfiguredInput(axis3);

    // ok, configuration done...
    mappings.commitConfig();

    // now we connect a Controller... and map
    MockedController controller = new MockedController();
    controller.axisValues = new float[3];
    controller.povDirection = PovDirection.east;

    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 5));
    //next call too fast
    assertEquals(ControllerMappings.RecordResult.not_added, mappings.recordMapping(controller, 7));
    //not two directions
    controller.povDirection = PovDirection.northWest;
    assertEquals(ControllerMappings.RecordResult.not_added, mappings.recordMapping(controller, 7));

    //not same direction on different configurations
    controller.povDirection = PovDirection.west;
    assertEquals(ControllerMappings.RecordResult.not_added, mappings.recordMapping(controller, 7));

    //not for analog axis
    controller.povDirection = PovDirection.north;
    assertEquals(ControllerMappings.RecordResult.nothing_done, mappings.recordMapping(controller, 6));

    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 7));

    assertFalse(mappings.getControllerMapping(controller).checkCompleted());

    System.out.println(mappings.toJson().toJson(JsonWriter.OutputType.json));

    // now check
    TestControllerAdapter controllerAdapter = new TestControllerAdapter(mappings);

    assertFalse(controllerAdapter.povMoved(controller, 1, PovDirection.center));

    // the digital
    assertTrue(controllerAdapter.povMoved(controller, 0, PovDirection.center));
    assertTrue(controllerAdapter.lastEventId == 5 || controllerAdapter.lastEventId == 7);

    assertTrue(controllerAdapter.povMoved(controller, 0, PovDirection.east));
    assertTrue(controllerAdapter.lastEventId == 5 || controllerAdapter.lastEventId == 7);

    assertTrue(controllerAdapter.povMoved(controller, 0, PovDirection.southEast));
    assertTrue(controllerAdapter.lastEventId == 5 || controllerAdapter.lastEventId == 7);

    assertTrue(controllerAdapter.povMoved(controller, 0, PovDirection.west));
    assertTrue(controllerAdapter.lastEventId == 5 || controllerAdapter.lastEventId == 7);

    MappedController mappedController = new MappedController(controller, mappings);
    controller.povDirection = PovDirection.center;
    assertEquals(0, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(7), .01f);

    controller.povDirection = PovDirection.north;
    assertEquals(0, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(-1, mappedController.getConfiguredAxisValue(7), .01f);

    controller.povDirection = PovDirection.east;
    assertEquals(1, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(7), .01f);

    controller.povDirection = PovDirection.southWest;
    assertEquals(-1, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(1, mappedController.getConfiguredAxisValue(7), .01f);
}
 
Example #14
Source File: ControllerMappingsTest.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
@Test
public void testButtonToAxisMapping() {
    ControllerMappings mappings = new ControllerMappings();

    // We test 3 axis
    ConfiguredInput axis1 = new ConfiguredInput(ConfiguredInput.Type.axis, 5);
    ConfiguredInput axis2 = new ConfiguredInput(ConfiguredInput.Type.axisAnalog, 6);
    ConfiguredInput axis3 = new ConfiguredInput(ConfiguredInput.Type.axisDigital, 7);

    mappings.addConfiguredInput(axis1);
    mappings.addConfiguredInput(axis2);
    mappings.addConfiguredInput(axis3);

    // ok, configuration done...
    mappings.commitConfig();

    // now we connect a Controller... and map
    MockedController controller = new MockedController();
    controller.pressedButton = 0;
    controller.axisValues = new float[3];

    assertEquals(ControllerMappings.RecordResult.need_second_button, mappings.recordMapping(controller, 5));
    //next call too fast
    assertEquals(ControllerMappings.RecordResult.not_added_need_button, mappings.recordMapping(controller, 5));
    //now an axis is moved
    controller.axisValues[1] = .5f;
    controller.pressedButton = -1;
    assertEquals(ControllerMappings.RecordResult.not_added_need_button, mappings.recordMapping(controller, 5));
    controller.pressedButton = 1;
    controller.axisValues[1] = 0f;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 5));

    // analog do not accept buttons
    controller.pressedButton = 2;
    assertEquals(ControllerMappings.RecordResult.nothing_done, mappings.recordMapping(controller, 6));

    controller.pressedButton = 1;
    assertEquals(ControllerMappings.RecordResult.not_added, mappings.recordMapping(controller, 7));
    controller.pressedButton = 3;
    assertEquals(ControllerMappings.RecordResult.need_second_button, mappings.recordMapping(controller, 7));
    controller.pressedButton = 4;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 7));

    assertFalse(mappings.getControllerMapping(controller).checkCompleted());

    System.out.println(mappings.toJson().toJson(JsonWriter.OutputType.json));

    // now check
    TestControllerAdapter controllerAdapter = new TestControllerAdapter(mappings);

    // the digital
    assertTrue(controllerAdapter.buttonDown(controller, 0));
    assertEquals(5, controllerAdapter.lastEventId);

    assertTrue(controllerAdapter.buttonDown(controller, 1));
    assertEquals(5, controllerAdapter.lastEventId);

    assertTrue(controllerAdapter.buttonDown(controller, 3));
    assertEquals(7, controllerAdapter.lastEventId);
    assertTrue(controllerAdapter.buttonUp(controller, 1));
    assertEquals(5, controllerAdapter.lastEventId);
    assertFalse(controllerAdapter.buttonUp(controller, 2));
    assertEquals(5, controllerAdapter.lastEventId);

    MappedController mappedController = new MappedController(controller, mappings);
    controller.pressedButton = 5;
    assertEquals(0, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(7), .01f);

    controller.pressedButton = 3;
    assertEquals(0, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(1, mappedController.getConfiguredAxisValue(7), .01f);

    controller.pressedButton = 4;
    assertEquals(0, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(-1, mappedController.getConfiguredAxisValue(7), .01f);
}
 
Example #15
Source File: ControllerMappingsTest.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
@Test
public void testAxisToAxisMapping() {
    ControllerMappings mappings = new ControllerMappings();

    // We test 3 axis
    ConfiguredInput axis1 = new ConfiguredInput(ConfiguredInput.Type.axis, 5);
    ConfiguredInput axis2 = new ConfiguredInput(ConfiguredInput.Type.axisAnalog, 6);
    ConfiguredInput axis3 = new ConfiguredInput(ConfiguredInput.Type.axisDigital, 7);

    mappings.addConfiguredInput(axis1);
    mappings.addConfiguredInput(axis2);
    mappings.addConfiguredInput(axis3);

    // ok, configuration done...
    mappings.commitConfig();

    // now we connect a Controller... and map
    MockedController controller = new MockedController();
    controller.axisValues = new float[3];
    controller.povDirection = PovDirection.center;
    controller.axisValues[0] = .2f;
    controller.axisValues[1] = .6f;
    controller.axisValues[2] = -.2f;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 5));
    //next call too fast
    assertEquals(ControllerMappings.RecordResult.not_added, mappings.recordMapping(controller, 6));

    controller.axisValues[0] = .2f;
    controller.axisValues[1] = .1f;
    controller.axisValues[2] = 0;
    assertEquals(ControllerMappings.RecordResult.nothing_done, mappings.recordMapping(controller, 6));

    controller.axisValues[0] = .6f;
    controller.axisValues[1] = .5f;
    controller.axisValues[2] = 0;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 6));

    controller.axisValues[0] = .6f;
    controller.axisValues[1] = .5f;
    controller.axisValues[2] = -1;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 7));

    assertTrue(mappings.getControllerMapping(controller).checkCompleted());

    System.out.println(mappings.toJson().toJson(JsonWriter.OutputType.json));

    // now check
    TestControllerAdapter controllerAdapter = new TestControllerAdapter(mappings);

    // the digital
    assertTrue(controllerAdapter.axisMoved(controller, 2, .8f));
    assertEquals(7, controllerAdapter.lastEventId);
    assertTrue(controllerAdapter.axisMoved(controller, 2, -.8f));
    assertEquals(7, controllerAdapter.lastEventId);
    assertTrue(controllerAdapter.axisMoved(controller, 2, -.2f));
    assertEquals(7, controllerAdapter.lastEventId);

    assertTrue(controllerAdapter.axisMoved(controller, 1, .8f));
    assertEquals(5, controllerAdapter.lastEventId);

    assertTrue(controllerAdapter.axisMoved(controller, 0, -.9f));
    assertEquals(6, controllerAdapter.lastEventId);

    MappedController mappedController = new MappedController(controller, mappings);
    controller.axisValues[0] = .6f;
    controller.axisValues[1] = .5f;
    controller.axisValues[2] = -.7f;
    assertEquals(.6f, mappedController.getConfiguredAxisValue(6), .01f);
    assertEquals(.5f, mappedController.getConfiguredAxisValue(5), .01f);
    assertEquals(-1f, mappedController.getConfiguredAxisValue(7), .01f);
    assertEquals(0, mappedController.getConfiguredAxisValue(3), .01f);
}
 
Example #16
Source File: ControllerMappingsTest.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
@Test
public void testButtonMapping() {
    ControllerMappings mappings = new ControllerMappings();

    // We test 4 buttons
    ConfiguredInput button1 = new ConfiguredInput(ConfiguredInput.Type.button, 1);
    ConfiguredInput button2 = new ConfiguredInput(ConfiguredInput.Type.button, 2);
    ConfiguredInput button3 = new ConfiguredInput(ConfiguredInput.Type.button, 3);
    ConfiguredInput button4 = new ConfiguredInput(ConfiguredInput.Type.button, 4);

    mappings.addConfiguredInput(button1);
    mappings.addConfiguredInput(button2);
    mappings.addConfiguredInput(button3);
    mappings.addConfiguredInput(button4);

    // ok, configuration done...
    mappings.commitConfig();

    // now we connect a Controller... and map
    MockedController controller = new MockedController();
    controller.pressedButton = 107;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 1));
    //next call too fast
    assertEquals(ControllerMappings.RecordResult.not_added, mappings.recordMapping(controller, 2));
    controller.pressedButton = 108;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 2));
    controller.pressedButton = 1;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 3));
    //TODO add assertion for check if record is complete
    assertFalse(mappings.getControllerMapping(controller).checkCompleted());
    controller.pressedButton = 4;
    assertEquals(ControllerMappings.RecordResult.recorded, mappings.recordMapping(controller, 4));
    controller.pressedButton = -1;
    assertTrue(mappings.getControllerMapping(controller).checkCompleted());

    System.out.println(mappings.toJson().toJson(JsonWriter.OutputType.json));

    // now check
    TestControllerAdapter controllerAdapter = new TestControllerAdapter(mappings);

    assertTrue(controllerAdapter.buttonDown(controller, 108));
    assertEquals(2, controllerAdapter.lastEventId);
    assertTrue(controllerAdapter.buttonDown(controller, 4));
    assertEquals(4, controllerAdapter.lastEventId);
    assertFalse(controllerAdapter.buttonDown(controller, 2));
    assertEquals(4, controllerAdapter.lastEventId);

    MappedController mappedController = new MappedController(controller, mappings);
    controller.pressedButton = 4;
    assertTrue(mappedController.isButtonPressed(4));
    controller.pressedButton = 108;
    assertTrue(mappedController.isButtonPressed(2));
    controller.pressedButton = 99;
    assertFalse(mappedController.isButtonPressed(2));
    assertFalse(mappedController.isButtonPressed(5));

}
 
Example #17
Source File: ProjectDataConverter.java    From libGDX-Path-Editor with Apache License 2.0 4 votes vote down vote up
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(); }
	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();
}
 
Example #18
Source File: ProjectDataConverter.java    From libGDX-Path-Editor with Apache License 2.0 4 votes vote down vote up
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(); }
	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();
}
 
Example #19
Source File: MusicEventManager.java    From gdx-soundboard with MIT License 3 votes vote down vote up
/**
 * Save to a file.
 * 
 * @param fileName
 *            The path to the file.
 */
public void save(FileHandle file) {

    final Json json = new Json(JsonWriter.OutputType.json);

    final Container container = new Container(states.values().toArray());

    file.writeString(json.prettyPrint(container), false);
}