Search in sources :

Example 1 with LevelEditorObject

use of net.drewke.tdme.tools.shared.model.LevelEditorObject 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) {
            }
    }
}
Also used : PrintStream(java.io.PrintStream) LevelEditorLight(net.drewke.tdme.tools.shared.model.LevelEditorLight) PropertyModelClass(net.drewke.tdme.tools.shared.model.PropertyModelClass) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Vector3(net.drewke.tdme.math.Vector3) IOException(java.io.IOException) Rotation(net.drewke.tdme.engine.Rotation) LevelEditorEntityLibrary(net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary) JSONObject(org.json.JSONObject) FileOutputStream(java.io.FileOutputStream) Transformations(net.drewke.tdme.engine.Transformations) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) File(java.io.File) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity)

Example 2 with LevelEditorObject

use of net.drewke.tdme.tools.shared.model.LevelEditorObject 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();
}
Also used : LevelEditorLight(net.drewke.tdme.tools.shared.model.LevelEditorLight) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) Vector3(net.drewke.tdme.math.Vector3) IOException(java.io.IOException) Rotation(net.drewke.tdme.engine.Rotation) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) Transformations(net.drewke.tdme.engine.Transformations) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) File(java.io.File)

Example 3 with LevelEditorObject

use of net.drewke.tdme.tools.shared.model.LevelEditorObject in project tdme by andreasdr.

the class LevelEditorView method objectPropertiesPreset.

/**
	 * Apply object property preset
	 * @param preset id
	 */
public void objectPropertiesPreset(String presetId) {
    if (selectedObjects.size() != 1)
        return;
    // handle single object
    Entity selectedObject = selectedObjects.get(0);
    LevelEditorObject levelEditorObject = level.getObjectById(selectedObject.getId());
    if (levelEditorObject == null)
        return;
    // clear object properties
    levelEditorObject.clearProperties();
    // add object properties by preset if missing
    ArrayList<PropertyModelClass> objectPropertyPresetVector = LevelPropertyPresets.getInstance().getObjectPropertiesPresets().get(presetId);
    if (objectPropertyPresetVector != null) {
        for (PropertyModelClass objectPropertyPreset : objectPropertyPresetVector) {
            levelEditorObject.addProperty(objectPropertyPreset.getName(), objectPropertyPreset.getValue());
        }
    }
    // update object properties to gui
    levelEditorScreenController.setObjectProperties(presetId, levelEditorObject.getProperties(), null);
}
Also used : Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) PropertyModelClass(net.drewke.tdme.tools.shared.model.PropertyModelClass) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject)

Example 4 with LevelEditorObject

use of net.drewke.tdme.tools.shared.model.LevelEditorObject in project tdme by andreasdr.

the class LevelEditorView method colorObject.

/**
	 * Centers selected objects
	 */
public void colorObject() {
    // skip if no objects selected
    if (selectedObjects.size() == 0)
        return;
    // color selected objects in blue
    for (Entity selectedObject : selectedObjects) {
        LevelEditorObject levelEditorObject = level.getObjectById(selectedObject.getId());
        if (levelEditorObject == null)
            continue;
        String color = OBJECTCOLOR_NAMES[0];
        PropertyModelClass colorProperty = levelEditorObject.getProperty("object.color");
        if (colorProperty == null) {
            levelEditorObject.addProperty("object.color", color);
        } else {
            // switch color
            color = colorProperty.getValue();
            for (int i = 0; i < OBJECTCOLOR_NAMES.length; i++) {
                if (color.equalsIgnoreCase(OBJECTCOLOR_NAMES[i])) {
                    color = OBJECTCOLOR_NAMES[(i + 1) % OBJECTCOLOR_NAMES.length];
                    break;
                }
            }
            // set up color in object properties
            if (color.equals("none")) {
                levelEditorObject.removeProperty("object.color");
            } else {
                levelEditorObject.updateProperty(colorProperty.getName(), "object.color", color);
            }
        }
        setStandardObjectColorEffect(selectedObject);
        setHighlightObjectColorEffect(selectedObject);
    }
    // set object properties if changed
    if (selectedObjects.size() == 1) {
        Entity selectedObject = selectedObjects.get(0);
        if (selectedObject != null && selectedObject.getId().startsWith("leveleditor.") == false) {
            LevelEditorObject levelEditorObject = level.getObjectById(selectedObject.getId());
            PropertyModelClass preset = levelEditorObject.getProperty("preset");
            levelEditorScreenController.setObjectProperties(preset != null ? preset.getValue() : "", levelEditorObject.getProperties(), null);
        } else {
            levelEditorScreenController.unsetObjectProperties();
        }
    } else if (selectedObjects.size() > 1) {
        levelEditorScreenController.unsetObjectProperties();
    }
}
Also used : Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) PropertyModelClass(net.drewke.tdme.tools.shared.model.PropertyModelClass) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject)

Example 5 with LevelEditorObject

use of net.drewke.tdme.tools.shared.model.LevelEditorObject in project tdme by andreasdr.

the class LevelEditorView method objectPropertyRemove.

/**
	 * Remove a object property from object properties
	 * @param name
	 * @return success
	 */
public boolean objectPropertyRemove(String name) {
    if (selectedObjects.size() != 1)
        return false;
    // handle single object
    Entity selectedObject = selectedObjects.get(0);
    LevelEditorObject levelEditorObject = level.getObjectById(selectedObject.getId());
    if (levelEditorObject == null)
        return false;
    // try to remove property
    int idx = levelEditorObject.getPropertyIndex(name);
    if (idx != -1 && levelEditorObject.removeProperty(name) == true) {
        // get property first at index that was removed
        PropertyModelClass property = levelEditorObject.getPropertyByIndex(idx);
        if (property == null) {
            // if current index does not work, take current one -1
            property = levelEditorObject.getPropertyByIndex(idx - 1);
        }
        // reload model properties
        levelEditorScreenController.setObjectProperties(null, levelEditorObject.getProperties(), property == null ? null : property.getName());
        //
        return true;
    }
    //
    return false;
}
Also used : Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) PropertyModelClass(net.drewke.tdme.tools.shared.model.PropertyModelClass) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject)

Aggregations

LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)24 Entity (net.drewke.tdme.engine.Entity)17 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)17 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)14 PropertyModelClass (net.drewke.tdme.tools.shared.model.PropertyModelClass)9 Vector3 (net.drewke.tdme.math.Vector3)8 Transformations (net.drewke.tdme.engine.Transformations)6 Object3D (net.drewke.tdme.engine.Object3D)4 Rotation (net.drewke.tdme.engine.Rotation)4 File (java.io.File)3 ObjectParticleSystemEntity (net.drewke.tdme.engine.ObjectParticleSystemEntity)3 PointsParticleSystemEntity (net.drewke.tdme.engine.PointsParticleSystemEntity)3 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)3 IOException (java.io.IOException)2 RigidBody (net.drewke.tdme.engine.physics.RigidBody)2 LevelEditorEntityLibrary (net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary)2 LevelEditorLight (net.drewke.tdme.tools.shared.model.LevelEditorLight)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 FileOutputStream (java.io.FileOutputStream)1