Search in sources :

Example 1 with LevelEditorLight

use of net.drewke.tdme.tools.shared.model.LevelEditorLight 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 LevelEditorLight

use of net.drewke.tdme.tools.shared.model.LevelEditorLight 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 LevelEditorLight

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

the class LevelEditorScreenController method onLightPresetApply.

/**
	 * On light preset apply for light
	 * @param i
	 */
public void onLightPresetApply(int lightIdx) {
    // get preset
    LevelEditorLight lightPreset = LevelPropertyPresets.getInstance().getLightPresets().get(lightsPresets[lightIdx].getController().getValue().toString());
    if (lightPreset == null)
        return;
    // apply preset
    view.applyLight(lightIdx, lightPreset.getAmbient(), lightPreset.getDiffuse(), lightPreset.getSpecular(), lightPreset.getPosition(), lightPreset.getConstantAttenuation(), lightPreset.getLinearAttenuation(), lightPreset.getQuadraticAttenuation(), lightPreset.getSpotTo(), lightPreset.getSpotDirection(), lightPreset.getSpotExponent(), lightPreset.getSpotCutOff(), lightPreset.isEnabled());
}
Also used : LevelEditorLight(net.drewke.tdme.tools.shared.model.LevelEditorLight)

Aggregations

LevelEditorLight (net.drewke.tdme.tools.shared.model.LevelEditorLight)3 File (java.io.File)2 IOException (java.io.IOException)2 Rotation (net.drewke.tdme.engine.Rotation)2 Transformations (net.drewke.tdme.engine.Transformations)2 Vector3 (net.drewke.tdme.math.Vector3)2 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)2 LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 LevelEditorEntityLibrary (net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary)1 PropertyModelClass (net.drewke.tdme.tools.shared.model.PropertyModelClass)1 JSONException (org.json.JSONException)1 JSONTokener (org.json.JSONTokener)1