Search in sources :

Example 1 with LevelEditorEntityLibrary

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

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

the class LevelEditorView method display.

/**
	 * Renders the scene 
	 */
public void display(GLAutoDrawable drawable) {
    // camera
    Camera cam = engine.getCamera();
    // reload entity library
    if (reloadEntityLibrary == true) {
        LevelEditorEntityLibrary entityLibrary = TDMELevelEditor.getInstance().getEntityLibrary();
        for (int i = 0; i < entityLibrary.getEntityCount(); i++) {
            selectedEntity = entityLibrary.getEntityAt(i);
            Tools.oseThumbnail(drawable, selectedEntity);
        }
        reloadEntityLibrary = false;
        TDMELevelEditor.getInstance().getLevelEditorEntityLibraryScreenController().setEntityLibrary();
    }
    // do camera rotation by middle mouse button
    if (mouseRotationX != MOUSE_ROTATION_NONE) {
        camLookRotationY.setAngle(camLookRotationY.getAngle() + mouseRotationX);
        camLookRotationY.update();
        mouseRotationX = 0;
    }
    if (mouseRotationY != MOUSE_ROTATION_NONE) {
        camLookRotationX.setAngle(camLookRotationX.getAngle() + mouseRotationY);
        camLookRotationX.update();
        mouseRotationY = 0;
    }
    // 	transfer keyboard inputs to rotations
    if (keyA)
        camLookRotationY.setAngle(camLookRotationY.getAngle() + 1.0f);
    if (keyD)
        camLookRotationY.setAngle(camLookRotationY.getAngle() - 1.0f);
    if (keyW)
        camLookRotationX.setAngle(camLookRotationX.getAngle() + 1.0f);
    if (keyS)
        camLookRotationX.setAngle(camLookRotationX.getAngle() - 1.0f);
    if (keyMinus)
        camScale += 0.05f;
    if (keyPlus)
        camScale -= 0.05f;
    if (camScale < camScaleMin)
        camScale = camScaleMin;
    if (camScale > camScaleMax)
        camScale = camScaleMax;
    if (keyR) {
        camLookRotationX.setAngle(-45.0f);
        camLookRotationX.update();
        camLookRotationY.setAngle(0.0f);
        camLookRotationY.update();
        cam.getLookAt().set(level.computeCenter());
        camScale = 1.0f;
    }
    // update cam look from rotations if changed
    if (keyA || keyD)
        camLookRotationY.update();
    if (keyW || keyS) {
        if (camLookRotationX.getAngle() > 89.99f)
            camLookRotationX.setAngle(89.99f);
        if (camLookRotationX.getAngle() < -89.99f)
            camLookRotationX.setAngle(-89.99f);
        camLookRotationX.update();
    }
    // look at -> look to vector
    camLookRotationX.getQuaternion().multiply(FORWARD_VECTOR, tmpVector3);
    camLookRotationY.getQuaternion().multiply(tmpVector3, tmpVector3);
    // apply look from rotations
    camLookAtToFromVector.set(tmpVector3).scale(camScale * 10.0f);
    // timing
    Timing timing = engine.getTiming();
    // do camera movement with arrow keys
    camLookRotationY.getQuaternion().multiply(FORWARD_VECTOR, camForwardVector).scale(timing.getDeltaTime() / 1000f * 60f);
    camLookRotationY.getQuaternion().multiply(SIDE_VECTOR, camSideVector).scale(timing.getDeltaTime() / 1000f * 60f);
    if (keyUp)
        cam.getLookAt().sub(tmpVector3.set(camForwardVector).scale(0.1f));
    if (keyDown)
        cam.getLookAt().add(tmpVector3.set(camForwardVector).scale(0.1f));
    if (keyLeft)
        cam.getLookAt().sub(tmpVector3.set(camSideVector).scale(0.1f));
    if (keyRight)
        cam.getLookAt().add(tmpVector3.set(camSideVector).scale(0.1f));
    // mouse panning
    if (mousePanningForward != MOUSE_PANNING_NONE) {
        cam.getLookAt().sub(tmpVector3.set(camForwardVector).scale(mousePanningForward / 30f * camScale));
        mousePanningForward = MOUSE_PANNING_NONE;
    }
    if (mousePanningSide != MOUSE_PANNING_NONE) {
        cam.getLookAt().sub(tmpVector3.set(camSideVector).scale(mousePanningSide / 30f * camScale));
        mousePanningSide = MOUSE_PANNING_NONE;
    }
    // look from with rotations
    cam.getLookFrom().set(cam.getLookAt()).add(camLookAtToFromVector);
    // up vector
    cam.computeUpVector(cam.getLookFrom(), cam.getLookAt(), cam.getUpVector());
    // update grid
    gridCenter.set(cam.getLookAt());
    updateGrid();
    // do GUI
    engine.getGUI().render();
    engine.getGUI().handleEvents();
}
Also used : LevelEditorEntityLibrary(net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary) Camera(net.drewke.tdme.engine.Camera) Timing(net.drewke.tdme.engine.Timing)

Example 3 with LevelEditorEntityLibrary

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

the class LevelEditorEntityLibraryScreenController method onValueChanged.

/*
	 * (non-Javadoc)
	 * @see net.drewke.tdme.gui.events.GUIChangeListener#onValueChanged(net.drewke.tdme.gui.nodes.GUIElementNode)
	 */
public void onValueChanged(GUIElementNode node) {
    if (node.getId().equals("entity_library_listbox") == true) {
        onEntitySelectionChanged();
    } else if (node.getId().equals("dropdown_entity_action") == true) {
        if (node.getController().getValue().equals("edit") == true) {
            onEditEntity();
        } else if (node.getController().getValue().equals("delete") == true) {
            onDeleteEntity();
        } else // model
        if (node.getController().getValue().equals("create_model") == true) {
            // model library
            final LevelEditorEntityLibrary entityLibrary = TDMELevelEditor.getInstance().getEntityLibrary();
            //
            popUps.getFileDialogScreenController().show(modelPath, "Load from: ", new String[] { "tmm", "dae", "tm" }, "", new Action() {

                public void performAction() {
                    try {
                        // add model
                        LevelEditorEntity entity = entityLibrary.addModel(LevelEditorEntityLibrary.ID_ALLOCATE, popUps.getFileDialogScreenController().getFileName(), "", popUps.getFileDialogScreenController().getPathName(), popUps.getFileDialogScreenController().getFileName(), new Vector3(0f, 0f, 0f));
                        entity.setDefaultBoundingVolumes();
                        setEntityLibrary();
                        entityLibraryListBox.getController().setValue(entityLibraryListBoxSelection.set(entity.getId()));
                        onEditEntity();
                    } catch (Exception exception) {
                        popUps.getInfoDialogScreenController().show("Error", "An error occurred: " + exception.getMessage());
                    }
                    modelPath = popUps.getFileDialogScreenController().getPathName();
                    popUps.getFileDialogScreenController().close();
                }
            });
        } else // trigger
        if (node.getController().getValue().equals("create_trigger") == true) {
            try {
                LevelEditorEntity model = TDMELevelEditor.getInstance().getEntityLibrary().addTrigger(LevelEditorEntityLibrary.ID_ALLOCATE, "New trigger", "", 1f, 1f, 1f);
                setEntityLibrary();
                entityLibraryListBox.getController().setValue(entityLibraryListBoxSelection.set(model.getId()));
                onEditEntity();
            } catch (Exception exception) {
                popUps.getInfoDialogScreenController().show("Error", "An error occurred: " + exception.getMessage());
            }
        } else // empty
        if (node.getController().getValue().equals("create_empty") == true) {
            try {
                LevelEditorEntity model = TDMELevelEditor.getInstance().getEntityLibrary().addEmpty(LevelEditorEntityLibrary.ID_ALLOCATE, "New empty", "");
                setEntityLibrary();
                entityLibraryListBox.getController().setValue(entityLibraryListBoxSelection.set(model.getId()));
                onEditEntity();
            } catch (Exception exception) {
                popUps.getInfoDialogScreenController().show("Error", "An error occurred: " + exception.getMessage());
            }
        } else // light
        if (node.getController().getValue().equals("create_light") == true) {
        } else // particle
        if (node.getController().getValue().equals("create_particlesystem") == true) {
            try {
                LevelEditorEntity model = TDMELevelEditor.getInstance().getEntityLibrary().addParticleSystem(LevelEditorEntityLibrary.ID_ALLOCATE, "New particle system", "");
                setEntityLibrary();
                entityLibraryListBox.getController().setValue(entityLibraryListBoxSelection.set(model.getId()));
                onEditEntity();
            } catch (Exception exception) {
                popUps.getInfoDialogScreenController().show("Error", "An error occurred: " + exception.getMessage());
            }
        } else {
            System.out.println("LevelEditorEntityLibraryScreenController::onValueChanged: dropdown_model_create: " + node.getController().getValue());
        }
        // reset
        node.getController().setValue(dropdownEntityActionReset);
    } else {
        System.out.println("LevelEditorEntityLibraryScreenController::onValueChanged: " + node.getId());
    }
}
Also used : LevelEditorEntityLibrary(net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary) Action(net.drewke.tdme.gui.events.Action) Vector3(net.drewke.tdme.math.Vector3) MutableString(net.drewke.tdme.utils.MutableString) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity)

Example 4 with LevelEditorEntityLibrary

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

the class DAEReader method readLevel.

/**
	 * Reads Collada DAE file level
	 * @param path name
	 * @param file name
	 * @throws Exception
	 * @return Model instance
	 */
public static LevelEditorLevel readLevel(String pathName, String fileName) throws Exception {
    // (re)create tm files folder
    File tmFilesFolder = new File(pathName + "/" + fileName + "-models");
    if (tmFilesFolder.exists()) {
        tmFilesFolder.delete();
    }
    tmFilesFolder.mkdir();
    // create level
    LevelEditorLevel levelEditorLevel = new LevelEditorLevel();
    LevelPropertyPresets.getInstance().setDefaultLevelProperties(levelEditorLevel);
    // load dae xml document
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(FileSystem.getInstance().getInputStream(pathName, fileName));
    Element xmlRoot = document.getDocumentElement();
    // authoring tool
    AuthoringTool authoringTool = getAuthoringTool(xmlRoot);
    // up vector and rotation order
    UpVector upVector = getUpVector(xmlRoot);
    RotationOrder rotationOrder = null;
    switch(upVector) {
        case Y_UP:
            rotationOrder = RotationOrder.ZYX;
        case Z_UP:
            rotationOrder = RotationOrder.YZX;
    }
    levelEditorLevel.setRotationOrder(rotationOrder);
    // parse scene from xml
    String xmlSceneId = null;
    Element xmlScene = getChildrenByTagName(xmlRoot, "scene").get(0);
    for (Element xmlInstanceVisualscene : getChildrenByTagName(xmlScene, "instance_visual_scene")) {
        xmlSceneId = xmlInstanceVisualscene.getAttribute("url").substring(1);
    }
    // check for xml scene id
    if (xmlSceneId == null) {
        throw new ModelFileIOException("No scene id found");
    }
    // parse visual scenes
    Element xmlLibraryVisualScenes = getChildrenByTagName(xmlRoot, "library_visual_scenes").get(0);
    for (Element xmlLibraryVisualScene : getChildrenByTagName(xmlLibraryVisualScenes, "visual_scene")) {
        String xmlVisualSceneId = xmlLibraryVisualScene.getAttribute("id");
        if (xmlVisualSceneId.equals(xmlSceneId)) {
            // default FPS
            float fps = 30f;
            // parse frames per second
            List<Element> xmlExtraNodes = getChildrenByTagName(xmlLibraryVisualScene, "extra");
            if (xmlExtraNodes.isEmpty() == false) {
                Element xmlExtraNode = xmlExtraNodes.get(0);
                for (Element xmlTechnique : getChildrenByTagName(xmlExtraNode, "technique")) {
                    List<Element> xmlFrameRateNodes = getChildrenByTagName(xmlTechnique, "frame_rate");
                    if (xmlFrameRateNodes.isEmpty() == false) {
                        fps = Float.parseFloat(xmlFrameRateNodes.get(0).getTextContent());
                        break;
                    }
                }
            }
            // visual scene root nodes
            LevelEditorEntityLibrary entityLibrary = levelEditorLevel.getEntityLibrary();
            LevelEditorEntity emptyEntity = null;
            int nodeIdx = 0;
            for (Element xmlNode : getChildrenByTagName(xmlLibraryVisualScene, "node")) {
                // derive model name from node id
                String modelName = xmlNode.getAttribute("id");
                // replace blender _|-NUMBER, not sure if this is a good idea for all cases, we will see
                modelName = modelName.replaceAll("[\\-\\_]{1}+[0-9]+$", "");
                // replace number at the end still, not sure if this is a good idea for all cases, we will see
                modelName = modelName.replaceAll("[0-9]+$", "");
                // check if name is available, if not extend with numbers :DDD
                boolean haveName = entityLibrary.getEntityCount() == 0;
                if (haveName == false) {
                    for (int i = 0; i < 10000; i++) {
                        haveName = true;
                        String modelNameTry = modelName + (i == 0 ? "" : String.valueOf(i));
                        for (int entityIdx = 0; entityIdx < entityLibrary.getEntityCount(); entityIdx++) {
                            LevelEditorEntity entity = entityLibrary.getEntityAt(entityIdx);
                            if (entity.getName().equals(modelNameTry) == true) {
                                haveName = false;
                                break;
                            }
                        }
                        if (haveName == true) {
                            modelName = modelNameTry;
                            break;
                        }
                    }
                }
                // do we have a name now?
                if (haveName == false) {
                    // nope, cant imagine this will happen 
                    System.out.println("DAEReader::readLevel(): Skipping model '" + modelName + "' as no name could be created for it.");
                    continue;
                }
                // 	create model
                Model model = new Model(pathName + File.separator + fileName + '-' + modelName, fileName + '-' + modelName, upVector, rotationOrder, null);
                // import matrix
                setupModelImportRotationMatrix(xmlRoot, model);
                Matrix4x4 modelImportRotationMatrix = new Matrix4x4(model.getImportTransformationsMatrix());
                setupModelImportScaleMatrix(xmlRoot, model);
                // translation, scaling, rotation
                Vector3 translation = new Vector3();
                Vector3 scale = new Vector3();
                Vector3 rotation = new Vector3();
                Vector3 xAxis = new Vector3();
                Vector3 yAxis = new Vector3();
                Vector3 zAxis = new Vector3();
                // set up local transformations matrix
                Matrix4x4 nodeTransformationsMatrix = null;
                List<Element> xmlMatrixElements = getChildrenByTagName(xmlNode, "matrix");
                if (xmlMatrixElements.size() == 1) {
                    String xmlMatrix = xmlMatrixElements.get(0).getTextContent();
                    StringTokenizer t = new StringTokenizer(xmlMatrix, " \n\r");
                    // 
                    nodeTransformationsMatrix = new Matrix4x4(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken())).transpose();
                }
                // check if we have node transformations matrix
                if (nodeTransformationsMatrix == null) {
                    throw new ModelFileIOException("missing node transformations matrix for node " + xmlNode.getAttribute("id"));
                }
                // extract coordinate system axes
                nodeTransformationsMatrix.getAxes(xAxis, yAxis, zAxis);
                nodeTransformationsMatrix.getTranslation(translation);
                nodeTransformationsMatrix.getScale(scale);
                // normalize coordinate axes
                xAxis.normalize();
                yAxis.normalize();
                zAxis.normalize();
                // write back normalized x axis
                nodeTransformationsMatrix.setAxes(xAxis, yAxis, zAxis);
                // TODO: move me into Matrix4x4 decomposing code?
                if ((upVector == UpVector.Y_UP && Vector3.computeDotProduct(Vector3.computeCrossProduct(xAxis, yAxis), zAxis) < 0.0f) || (upVector == UpVector.Z_UP && Vector3.computeDotProduct(Vector3.computeCrossProduct(xAxis, zAxis), yAxis) < 0.0f)) {
                    // negate axes
                    xAxis.scale(-1f);
                    yAxis.scale(-1f);
                    zAxis.scale(-1f);
                    // and write back to node transformation matrices
                    nodeTransformationsMatrix.setAxes(xAxis, yAxis, zAxis);
                    // scale
                    scale.scale(-1f);
                }
                // determine rotation
                nodeTransformationsMatrix.computeEulerAngles(rotation);
                // apply model import matrix
                modelImportRotationMatrix.multiply(scale, scale);
                modelImportRotationMatrix.multiply(rotation, rotation);
                model.getImportTransformationsMatrix().multiply(translation, translation);
                // set up frames per seconds
                model.setFPS(fps);
                // read sub groups
                Group group = readVisualSceneNode(authoringTool, pathName, model, null, xmlRoot, xmlNode, fps);
                if (group != null) {
                    group.getTransformationsMatrix().identity();
                    model.getSubGroups().put(group.getId(), group);
                    model.getGroups().put(group.getId(), group);
                }
                // set up joints
                ModelHelper.setupJoints(model);
                // fix animation length
                ModelHelper.fixAnimationLength(model);
                // prepare for indexed rendering
                ModelHelper.prepareForIndexedRendering(model);
                // check if empty model
                EntityType entityType = EntityType.MODEL;
                ModelStatistics modelStatistics = ModelUtilities.computeModelStatistics(model);
                if (modelStatistics.getOpaqueFaceCount() == 0 && modelStatistics.getTransparentFaceCount() == 0) {
                    entityType = EntityType.EMPTY;
                }
                // level editor entity
                LevelEditorEntity levelEditorEntity = null;
                // model
                if (entityType == EntityType.MODEL) {
                    // check if we have that model already
                    for (int i = 0; i < levelEditorLevel.getEntityLibrary().getEntityCount(); i++) {
                        LevelEditorEntity levelEditorEntityCompare = levelEditorLevel.getEntityLibrary().getEntityAt(i);
                        if (levelEditorEntityCompare.getType() != EntityType.MODEL)
                            continue;
                        if (ModelUtilities.equals(model, levelEditorEntityCompare.getModel()) == true) {
                            levelEditorEntity = levelEditorEntityCompare;
                            break;
                        }
                    }
                    // create level editor model, if not yet exists
                    if (levelEditorEntity == null) {
                        // save model
                        TMWriter.write(model, pathName + "/" + fileName + "-models", modelName + ".tm");
                        // create level editor entity
                        levelEditorEntity = entityLibrary.addModel(nodeIdx++, modelName, modelName, pathName + "/" + fileName + "-models", modelName + ".tm", new Vector3());
                    }
                } else // empty
                if (entityType == EntityType.EMPTY) {
                    if (emptyEntity == null) {
                        emptyEntity = entityLibrary.addEmpty(nodeIdx++, "Default Empty", "");
                    }
                    levelEditorEntity = emptyEntity;
                } else {
                    System.out.println("DAEReader::readLevel(): unknown entity type. Skipping");
                    continue;
                }
                // level editor object transformations
                Transformations levelEditorObjectTransformations = new Transformations();
                levelEditorObjectTransformations.getTranslation().set(translation);
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis0VectorIndex()], rotationOrder.getAxis0()));
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis1VectorIndex()], rotationOrder.getAxis1()));
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis2VectorIndex()], rotationOrder.getAxis2()));
                levelEditorObjectTransformations.getScale().set(scale);
                levelEditorObjectTransformations.update();
                // level editor object
                LevelEditorObject object = new LevelEditorObject(xmlNode.getAttribute("id"), xmlNode.getAttribute("id"), levelEditorObjectTransformations, levelEditorEntity);
                // add object to level
                levelEditorLevel.addObject(object);
            }
        }
    }
    // save level
    LevelFileExport.export(pathName + "/" + fileName + ".tl", levelEditorLevel);
    //
    return levelEditorLevel;
}
Also used : Group(net.drewke.tdme.engine.model.Group) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ModelStatistics(net.drewke.tdme.engine.subsystems.object.ModelUtilitiesInternal.ModelStatistics) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) UpVector(net.drewke.tdme.engine.model.Model.UpVector) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation) Joint(net.drewke.tdme.engine.model.Joint) Matrix4x4(net.drewke.tdme.math.Matrix4x4) EntityType(net.drewke.tdme.tools.shared.model.LevelEditorEntity.EntityType) LevelEditorEntityLibrary(net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary) StringTokenizer(java.util.StringTokenizer) LevelEditorLevel(net.drewke.tdme.tools.shared.model.LevelEditorLevel) DocumentBuilder(javax.xml.parsers.DocumentBuilder) RotationOrder(net.drewke.tdme.engine.model.RotationOrder) Model(net.drewke.tdme.engine.model.Model) Transformations(net.drewke.tdme.engine.Transformations) File(java.io.File)

Example 5 with LevelEditorEntityLibrary

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

the class LevelEditorEntityLibraryScreenController method setEntityLibrary.

/**
	 * Set up complete entity library
	 */
public void setEntityLibrary() {
    // model library
    LevelEditorEntityLibrary entityLibrary = TDMELevelEditor.getInstance().getEntityLibrary();
    // store selection
    entityLibraryListBoxSelection.set(entityLibraryListBox.getController().getValue());
    // entity library list box inner
    GUIParentNode entityLibraryListBoxInnerNode = (GUIParentNode) (entityLibraryListBox.getScreenNode().getNodeById(entityLibraryListBox.getId() + "_inner"));
    // construct XML for sub nodes
    int idx = 1;
    String entityLibraryListBoxSubNodesXML = "";
    entityLibraryListBoxSubNodesXML += "<scrollarea-vertical id=\"" + entityLibraryListBox.getId() + "_inner_scrollarea\" width=\"100%\" height=\"100%\">\n";
    for (int i = 0; i < entityLibrary.getEntityCount(); i++) {
        int objectId = entityLibrary.getEntityAt(i).getId();
        String objectName = entityLibrary.getEntityAt(i).getName();
        entityLibraryListBoxSubNodesXML += "<selectbox-option text=\"" + GUIParser.escapeQuotes(objectName) + "\" value=\"" + objectId + "\" " + (i == 0 ? "selected=\"true\" " : "") + "/>\n";
    }
    entityLibraryListBoxSubNodesXML += "</scrollarea-vertical>\n";
    // inject sub nodes
    try {
        entityLibraryListBoxInnerNode.replaceSubNodes(entityLibraryListBoxSubNodesXML, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // reset selection
    if (entityLibraryListBoxSelection.length() > 0) {
        entityLibraryListBox.getController().setValue(entityLibraryListBoxSelection);
    }
    //
    onEntitySelectionChanged();
    //
    buttonEntityPlace.getController().setDisabled(entityLibrary.getEntityCount() == 0);
}
Also used : LevelEditorEntityLibrary(net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary) GUIParentNode(net.drewke.tdme.gui.nodes.GUIParentNode) MutableString(net.drewke.tdme.utils.MutableString)

Aggregations

LevelEditorEntityLibrary (net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary)5 Vector3 (net.drewke.tdme.math.Vector3)3 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)3 File (java.io.File)2 Rotation (net.drewke.tdme.engine.Rotation)2 Transformations (net.drewke.tdme.engine.Transformations)2 LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)2 MutableString (net.drewke.tdme.utils.MutableString)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 StringTokenizer (java.util.StringTokenizer)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 Camera (net.drewke.tdme.engine.Camera)1 Timing (net.drewke.tdme.engine.Timing)1 Group (net.drewke.tdme.engine.model.Group)1 Joint (net.drewke.tdme.engine.model.Joint)1 Model (net.drewke.tdme.engine.model.Model)1 UpVector (net.drewke.tdme.engine.model.Model.UpVector)1 RotationOrder (net.drewke.tdme.engine.model.RotationOrder)1