use of net.drewke.tdme.tools.shared.model.LevelEditorEntity in project tdme by andreasdr.
the class LevelFileExport method export.
/**
* Exports a level to a TDME level file
* @param file name
*/
public static void export(String fileName, LevelEditorLevel level) throws Exception {
FileOutputStream fos = null;
PrintStream fops = null;
level.setFileName(new File(fileName).getName());
try {
// generate json
LevelEditorEntityLibrary entityLibrary = level.getEntityLibrary();
JSONObject jRoot = new JSONObject();
jRoot.put("version", "0.99");
jRoot.put("ro", level.getRotationOrder().toString());
JSONArray jLights = new JSONArray();
for (int i = 0; i < level.getLightCount(); i++) {
LevelEditorLight light = level.getLightAt(i);
JSONObject jLight = new JSONObject();
jLight.put("id", i);
jLight.put("ar", light.getAmbient().getRed());
jLight.put("ag", light.getAmbient().getGreen());
jLight.put("ab", light.getAmbient().getBlue());
jLight.put("aa", light.getAmbient().getAlpha());
jLight.put("dr", light.getDiffuse().getRed());
jLight.put("dg", light.getDiffuse().getGreen());
jLight.put("db", light.getDiffuse().getBlue());
jLight.put("da", light.getDiffuse().getAlpha());
jLight.put("sr", light.getSpecular().getRed());
jLight.put("sg", light.getSpecular().getGreen());
jLight.put("sb", light.getSpecular().getBlue());
jLight.put("sa", light.getSpecular().getAlpha());
jLight.put("px", light.getPosition().getX());
jLight.put("py", light.getPosition().getY());
jLight.put("pz", light.getPosition().getZ());
jLight.put("pw", light.getPosition().getW());
jLight.put("stx", light.getSpotTo().getX());
jLight.put("sty", light.getSpotTo().getY());
jLight.put("stz", light.getSpotTo().getZ());
jLight.put("sdx", light.getSpotDirection().getX());
jLight.put("sdy", light.getSpotDirection().getY());
jLight.put("sdz", light.getSpotDirection().getZ());
jLight.put("se", light.getSpotExponent());
jLight.put("sco", light.getSpotCutOff());
jLight.put("ca", light.getConstantAttenuation());
jLight.put("la", light.getLinearAttenuation());
jLight.put("qa", light.getQuadraticAttenuation());
jLight.put("e", light.isEnabled());
jLights.put(jLight);
}
jRoot.put("lights", jLights);
JSONArray jEntityLibrary = new JSONArray();
for (int i = 0; i < entityLibrary.getEntityCount(); i++) {
LevelEditorEntity entity = entityLibrary.getEntityAt(i);
JSONObject jModel = new JSONObject();
jModel.put("id", entity.getId());
jModel.put("type", entity.getType());
jModel.put("name", entity.getName());
jModel.put("descr", entity.getDescription());
jModel.put("entity", ModelMetaDataFileExport.exportToJSON(entity));
jEntityLibrary.put(jModel);
}
JSONArray jMapProperties = new JSONArray();
for (PropertyModelClass mapProperty : level.getProperties()) {
JSONObject jMapProperty = new JSONObject();
jMapProperty.put("name", mapProperty.getName());
jMapProperty.put("value", mapProperty.getValue());
jMapProperties.put(jMapProperty);
}
jRoot.put("properties", jMapProperties);
jRoot.put("models", jEntityLibrary);
JSONArray jObjects = new JSONArray();
for (int i = 0; i < level.getObjectCount(); i++) {
LevelEditorObject levelEditorObject = level.getObjectAt(i);
JSONObject jObject = new JSONObject();
Transformations transformations = levelEditorObject.getTransformations();
Vector3 translation = transformations.getTranslation();
Vector3 scale = transformations.getScale();
Rotation rotationAroundXAxis = transformations.getRotations().get(level.getRotationOrder().getAxisXIndex());
Rotation rotationAroundYAxis = transformations.getRotations().get(level.getRotationOrder().getAxisYIndex());
Rotation rotationAroundZAxis = transformations.getRotations().get(level.getRotationOrder().getAxisZIndex());
jObject.put("id", levelEditorObject.getId());
jObject.put("descr", levelEditorObject.getDescription());
jObject.put("mid", levelEditorObject.getEntity().getId());
jObject.put("tx", translation.getX());
jObject.put("ty", translation.getY());
jObject.put("tz", translation.getZ());
jObject.put("sx", scale.getX());
jObject.put("sy", scale.getY());
jObject.put("sz", scale.getZ());
jObject.put("rx", rotationAroundXAxis.getAngle());
jObject.put("ry", rotationAroundYAxis.getAngle());
jObject.put("rz", rotationAroundZAxis.getAngle());
JSONArray jObjectProperties = new JSONArray();
for (PropertyModelClass objectProperty : levelEditorObject.getProperties()) {
JSONObject jObjectProperty = new JSONObject();
jObjectProperty.put("name", objectProperty.getName());
jObjectProperty.put("value", objectProperty.getValue());
jObjectProperties.put(jObjectProperty);
}
jObject.put("properties", jObjectProperties);
jObjects.put(jObject);
}
jRoot.put("objects", jObjects);
jRoot.put("objects_eidx", level.getObjectIdx());
// save to file
fos = new FileOutputStream(new File(fileName));
fops = new PrintStream(fos);
fops.print(jRoot.toString(2));
} catch (JSONException je) {
je.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (fops != null)
fops.close();
if (fos != null)
try {
fos.close();
} catch (IOException ioe) {
}
}
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntity in project tdme by andreasdr.
the class LevelFileImport method doImport.
/**
* Imports a level from a TDME level file to Level Editor
* @param path name
* @param file name
* @param level
* @param object id prefix
*/
public static void doImport(String pathName, String fileName, LevelEditorLevel level, String objectIdPrefix) throws Exception {
pathName = pathName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
fileName = fileName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
JSONObject jRoot = null;
InputStream is = null;
try {
jRoot = new JSONObject(new JSONTokener(FileSystem.getInstance().getContent(pathName, fileName)));
} catch (IOException ioe) {
throw ioe;
} finally {
if (is != null)
try {
is.close();
} catch (IOException ioei) {
}
}
// game root path
level.setGameRoot(Tools.getGameRootPath(pathName));
// check for version
float version = Float.parseFloat(jRoot.getString("version"));
// new: rotation order
level.setRotationOrder(jRoot.has("ro") == true ? RotationOrder.valueOf(jRoot.getString("ro")) : RotationOrder.XYZ);
// map properties
level.clearProperties();
// parse properties
JSONArray jMapProperties = jRoot.getJSONArray("properties");
for (int i = 0; i < jMapProperties.length(); i++) {
JSONObject jMapProperty = jMapProperties.getJSONObject(i);
level.addProperty(jMapProperty.getString("name"), jMapProperty.getString("value"));
}
// lights
if (jRoot.has("lights") == true) {
JSONArray jLights = jRoot.getJSONArray("lights");
for (int i = 0; i < jLights.length(); i++) {
JSONObject jLight = jLights.getJSONObject(i);
LevelEditorLight light = level.getLightAt(jLight.has("id") ? jLight.getInt("id") : i);
// set up light in level
light.getAmbient().set((float) jLight.getDouble("ar"), (float) jLight.getDouble("ag"), (float) jLight.getDouble("ab"), (float) jLight.getDouble("aa"));
light.getDiffuse().set((float) jLight.getDouble("dr"), (float) jLight.getDouble("dg"), (float) jLight.getDouble("db"), (float) jLight.getDouble("da"));
light.getSpecular().set((float) jLight.getDouble("sr"), (float) jLight.getDouble("sg"), (float) jLight.getDouble("sb"), (float) jLight.getDouble("sa"));
light.getPosition().set((float) jLight.getDouble("px"), (float) jLight.getDouble("py"), (float) jLight.getDouble("pz"), (float) jLight.getDouble("pw"));
light.setConstantAttenuation((float) jLight.getDouble("ca"));
light.setLinearAttenuation((float) jLight.getDouble("la"));
light.setQuadraticAttenuation((float) jLight.getDouble("qa"));
light.getSpotTo().set((float) jLight.getDouble("stx"), (float) jLight.getDouble("sty"), (float) jLight.getDouble("stz"));
light.getSpotDirection().set((float) jLight.getDouble("sdx"), (float) jLight.getDouble("sdy"), (float) jLight.getDouble("sdz"));
light.setSpotExponent((float) jLight.getDouble("se"));
light.setSpotCutOff((float) jLight.getDouble("sco"));
light.setEnabled(jLight.getBoolean("e"));
}
}
// entities
level.getEntityLibrary().clear();
JSONArray jModels = jRoot.getJSONArray("models");
for (int i = 0; i < jModels.length(); i++) {
JSONObject jModel = jModels.getJSONObject(i);
// add model to library
LevelEditorEntity levelEditorEntity = ModelMetaDataFileImport.doImportFromJSON(jModel.getInt("id"), new File(pathName).getCanonicalPath(), jModel.getJSONObject("entity"));
// do we have a valid entity?
if (levelEditorEntity == null) {
throw new Exception("Invalid entity");
}
// add entity
level.getEntityLibrary().addEntity(levelEditorEntity);
// parse optional model properties
if (jModel.has("properties")) {
JSONArray jModelProperties = jModel.getJSONArray("properties");
for (int j = 0; j < jModelProperties.length(); j++) {
JSONObject jModelProperty = jModelProperties.getJSONObject(j);
levelEditorEntity.addProperty(jModelProperty.getString("name"), jModelProperty.getString("value"));
}
}
}
// objects
level.clearObjects();
JSONArray jObjects = jRoot.getJSONArray("objects");
for (int i = 0; i < jObjects.length(); i++) {
JSONObject jObject = jObjects.getJSONObject(i);
LevelEditorEntity model = level.getEntityLibrary().getEntity(jObject.getInt("mid"));
Transformations transformations = new Transformations();
transformations.getPivot().set(model.getPivot());
transformations.getTranslation().set((float) jObject.getDouble("tx"), (float) jObject.getDouble("ty"), (float) jObject.getDouble("tz"));
transformations.getScale().set((float) jObject.getDouble("sx"), (float) jObject.getDouble("sy"), (float) jObject.getDouble("sz"));
Vector3 rotation = new Vector3((float) jObject.getDouble("rx"), (float) jObject.getDouble("ry"), (float) jObject.getDouble("rz"));
transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis0VectorIndex()], level.getRotationOrder().getAxis0()));
transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis1VectorIndex()], level.getRotationOrder().getAxis1()));
transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis2VectorIndex()], level.getRotationOrder().getAxis2()));
transformations.update();
LevelEditorObject levelEditorObject = new LevelEditorObject(objectIdPrefix != null ? objectIdPrefix + jObject.getString("id") : jObject.getString("id"), jObject.has("descr") ? jObject.getString("descr") : "", transformations, model);
levelEditorObject.clearProperties();
// parse optional object properties, new in LE 0.3a
if (jObject.has("properties")) {
JSONArray jObjectProperties = jObject.getJSONArray("properties");
for (int j = 0; j < jObjectProperties.length(); j++) {
JSONObject jObjectProperty = jObjectProperties.getJSONObject(j);
levelEditorObject.addProperty(jObjectProperty.getString("name"), jObjectProperty.getString("value"));
}
}
// check if entity already exists
// small workaround for a bug that would allow to place to objects at same place with same model
/*
boolean skipObject = false;
for (int j = 0; j < level.getObjectCount(); j++) {
LevelEditorObject _levelEditorObject = level.getObjectAt(j);
if (_levelEditorObject.getModel() == levelEditorObject.getModel() &&
_levelEditorObject.getTransformations().getTranslation().equals(levelEditorObject.getTransformations().getTranslation())) {
System.out.println("Skipping '" + levelEditorObject + "' as we already have a object with this model and translation.");
// we already have a object with selected model on this translation
skipObject = true;
break;
}
}
// skip on object if requested
if (skipObject == true) continue;
*/
// otherwise add
level.addObject(levelEditorObject);
}
level.setObjectIdx(jRoot.getInt("objects_eidx"));
level.setPathName(pathName);
level.setFileName(fileName);
level.computeDimension();
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntity in project tdme by andreasdr.
the class ModelMetaDataFileImport method doImport.
/**
* Imports a model meta data file from file
* @param id or LevelEditorEntity.ID_NONE
* @param path name
* @param file name
*/
public static LevelEditorEntity doImport(int id, String pathName, String fileName) throws Exception {
fileName = fileName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
JSONObject jEntityRoot = null;
InputStream is = null;
try {
jEntityRoot = new JSONObject(new JSONTokener(FileSystem.getInstance().getContent(pathName, fileName)));
} catch (IOException ioe) {
throw ioe;
} finally {
if (is != null)
try {
is.close();
} catch (IOException ioei) {
}
}
// do the work
LevelEditorEntity levelEditorEntity = doImportFromJSON(id, pathName, jEntityRoot);
levelEditorEntity.setEntityFileName(pathName + "/" + fileName);
return levelEditorEntity;
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntity in project tdme by andreasdr.
the class ParticleSystemView method loadParticleSystem.
/*
* (non-Javadoc)
* @see net.drewke.tdme.tools.shared.views.ParticleSystemView#loadParticleSystem(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
protected LevelEditorEntity loadParticleSystem(String name, String description, String pathName, String fileName) throws Exception {
if (fileName.toLowerCase().endsWith(".tps")) {
LevelEditorEntity levelEditorEntity = ModelMetaDataFileImport.doImport(LevelEditorEntityLibrary.ID_ALLOCATE, pathName, fileName);
levelEditorEntity.setDefaultBoundingVolumes();
TDMELevelEditor.getInstance().getEntityLibrary().addEntity(levelEditorEntity);
return levelEditorEntity;
}
return null;
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntity in project tdme by andreasdr.
the class TriggerView method triggerApply.
/**
* Trigger apply
* @param width
* @param height
* @param depth
*/
public void triggerApply(float width, float height, float depth) {
if (entity == null)
return;
// create new trigger, replacing old with new
try {
// save reference to old entity, create new entity
LevelEditorEntity oldModel = entity;
entity = TDMELevelEditor.getInstance().getEntityLibrary().addTrigger(LevelEditorEntityLibrary.ID_ALLOCATE, oldModel.getName(), oldModel.getDescription(), width, height, depth);
// clone properties
for (int i = 0; i < oldModel.getPropertyCount(); i++) {
PropertyModelClass property = oldModel.getPropertyByIndex(i);
entity.addProperty(property.getName(), property.getValue());
}
// replace old with new
TDMELevelEditor.getInstance().getLevel().replaceEntity(oldModel.getId(), entity.getId());
TDMELevelEditor.getInstance().getEntityLibrary().removeEntity(oldModel.getId());
TDMELevelEditor.getInstance().getLevelEditorEntityLibraryScreenController().setEntityLibrary();
// init entity
initModelRequested = true;
//
updateGUIElements();
} catch (Exception exception) {
popUps.getInfoDialogScreenController().show("Error", "An error occurred: " + exception.getMessage());
}
}
Aggregations