//----------------------------------------------------------------------------------------
int CDotSceneSerializer::Export(bool SaveAs, Ogre::String exportfile)
{
    OgitorsRoot *ogRoot = OgitorsRoot::getSingletonPtr();
    OgitorsSystem *mSystem = OgitorsSystem::getSingletonPtr();

    PROJECTOPTIONS *pOpt = ogRoot->GetProjectOptions();
    Ogre::String fileName = pOpt->ProjectName;

    UTFStringVector extlist;
    extlist.push_back(OTR("DotScene File"));
    extlist.push_back("*.scene");
    extlist.push_back(OTR("DotScene File"));
    extlist.push_back("*.xml");
    fileName = mSystem->DisplaySaveDialog(OTR("Export DotScene File"),extlist);
    if(fileName == "")
        return SCF_CANCEL;

    Ogre::String newDir = OgitorsUtils::ExtractFilePath(fileName);

    extractOFS(newDir);
    mSystem->DeleteFile(newDir + "/" + pOpt->ProjectName + ".ogscene");

    TiXmlDocument *pXMLDoc = new TiXmlDocument();
    pXMLDoc->InsertEndChild(TiXmlDeclaration("1.0", "UTF-8", ""));
    pXMLDoc->InsertEndChild(TiXmlElement("scene"));

    // export basic info
    TiXmlElement *pRoot = pXMLDoc->RootElement();
    pRoot->SetAttribute("formatVersion", "1.0.0");
    pRoot->SetAttribute("generator", Ogre::String(Ogre::String("Ogitor SceneBuilder ") + Ogre::String(OGITOR_VERSION)).c_str());

    // export resource locations
    TiXmlElement *pResourceLocations = pRoot->InsertEndChild(TiXmlElement("resourceLocations"))->ToElement();

    for(unsigned int r = 0; r < pOpt->ResourceDirectories.size(); r++)
    {
        TiXmlElement *pResourceLocation = pResourceLocations->InsertEndChild(TiXmlElement("resourceLocation"))->ToElement();
        Ogre::String loc = pOpt->ResourceDirectories[r];

        pResourceLocation->SetAttribute("type", "FileSystem");

        std::replace(loc.begin(),loc.end(),'\\','/');

        if(loc[0] != '.')
            loc = "./" + loc;

        pResourceLocation->SetAttribute("name", loc.c_str());
    }

    //TODO: do we need all those object id's ?

    TiXmlElement *pEnvironment = pRoot->InsertEndChild(TiXmlElement("environment"))->ToElement();

    // export octree scenemanagers
    NameObjectPairList smList = ogRoot->GetObjectsByTypeName("OctreeSceneManager");
    NameObjectPairList::const_iterator smIt = smList.begin();
    while(smIt != smList.end())
    {
        TiXmlElement *result = smIt->second->exportDotScene(pEnvironment);
        saveUserData(smIt->second->getCustomProperties(), result);
        smIt++;
    }

    // export viewports
    NameObjectPairList vpList = ogRoot->GetObjectsByTypeName("Viewport Object");
    NameObjectPairList::const_iterator vpIt = vpList.begin();
    while(vpIt != vpList.end())
    {
        TiXmlElement *result = vpIt->second->exportDotScene(pEnvironment);
        saveUserData(vpIt->second->getCustomProperties(), result);
        vpIt++;
    }

    // export terrains
    NameObjectPairList terrainList = ogRoot->GetObjectsByType(ETYPE_TERRAIN_MANAGER);
    NameObjectPairList::const_iterator tlIt = terrainList.begin();
    while(tlIt != terrainList.end())
    {
        TiXmlElement *result = tlIt->second->exportDotScene(pRoot);
        saveUserData(tlIt->second->getCustomProperties(), result);
        tlIt++;
    }

    NameObjectPairList items = ogRoot->GetSceneManagerEditor()->getChildren();

    // export lights
    NameObjectPairList::const_iterator nodeIt = items.begin();

    while(nodeIt != items.end())
    {
        if(nodeIt->second->getEditorType() == ETYPE_LIGHT)
        {
            TiXmlElement *result = nodeIt->second->exportDotScene(pRoot);
            saveUserData(nodeIt->second->getCustomProperties(), result);
        }
        nodeIt++;
    }

    // export cameras
    nodeIt = items.begin();

    while(nodeIt != items.end())
    {
        if(nodeIt->second->getEditorType() == ETYPE_CAMERA)
        {
            TiXmlElement *result = nodeIt->second->exportDotScene(pRoot);
            saveUserData(nodeIt->second->getCustomProperties(), result);
        }
        nodeIt++;
    }

    // export nodes
    TiXmlElement *pNodes = pRoot->InsertEndChild(TiXmlElement("nodes"))->ToElement();
    nodeIt = items.begin();

    while(nodeIt != items.end())
    {
        if( nodeIt->second->getEditorType() != ETYPE_TERRAIN_MANAGER &&
                nodeIt->second->getEditorType() != ETYPE_LIGHT &&
                nodeIt->second->getEditorType() != ETYPE_CAMERA )
        {
            TiXmlElement *result = nodeIt->second->exportDotScene(pNodes);
            saveUserData(nodeIt->second->getCustomProperties(), result);
        }
        nodeIt++;
    }

    if (pXMLDoc->SaveFile(fileName.c_str()))
    {
        OgitorsSystem::getSingletonPtr()->DisplayMessageDialog(OTR("Scene has been exported succesfully"), DLGTYPE_OK);
        delete pXMLDoc;
    }
    else
        OgitorsSystem::getSingletonPtr()->DisplayMessageDialog(OTR("An error occured during export.. :("), DLGTYPE_OK);

    return SCF_OK;
}
Ejemplo n.º 2
0
//-----------------------------------------------------------------------------
int COFSSceneSerializer::Export(bool SaveAs, Ogre::String exportfile)
{
    OgitorsRoot *ogRoot = OgitorsRoot::getSingletonPtr();
    OgitorsSystem *mSystem = OgitorsSystem::getSingletonPtr();
    OFS::OfsPtr& mFile = ogRoot->GetProjectFile();

    PROJECTOPTIONS *pOpt = ogRoot->GetProjectOptions();

    bool forceSave = false;
    Ogre::String fileLocation = ogRoot->GetProjectFile()->getFileSystemName();
    Ogre::String fileName = "";

    if (!exportfile.empty())
    {
        // Save location was passed, so use this filename
        fileLocation = exportfile;
    }

    if (SaveAs)
    {
        // Saving at a different location
        UTFStringVector extlist;

        if( mFile->getFileSystemType() == OFS::OFS_PACKED )
        {
            extlist.push_back(OTR("Ogitor File System File"));
            extlist.push_back("*.ofs");
        }
        else
        {
            extlist.push_back(OTR("Ogitor Scene File"));
            extlist.push_back("*" + Globals::OGSCENE_FORMAT_EXTENSION);
        }

        Ogre::String newfileLocation = mSystem->DisplaySaveDialog(OTR("Save As"), extlist, fileLocation);
        if(newfileLocation == "") 
            return SCF_CANCEL;

        mSystem->SetSetting("system", "oldOpenPath", newfileLocation);

        if(Ogre::StringUtil::match(newfileLocation, fileLocation, false))
        {
            SaveAs = false;
        } 
        else 
        {
            forceSave = true;
            fileLocation = newfileLocation;
        }
    }

    Ogre::String filePath = OgitorsUtils::ExtractFilePath(fileLocation);
    fileName = OgitorsUtils::ExtractFileName(fileLocation);

    // Change the project directory to the new path
    pOpt->ProjectDir = filePath;

    if(fileName.substr(fileName.size() - 4, 4) != ".ofs")
        fileLocation = filePath;

    int dotpos = fileName.find_last_of(".");
    if (dotpos > 0)
    {
        fileName.erase(dotpos, fileName.length() - dotpos);
    }

    if (SaveAs && mFile->moveFileSystemTo(fileLocation.c_str()) != OFS::OFS_OK)
    {
        return SCF_ERRFILE;
    }

    if (SaveAs)
    {
        mFile->deleteFile((pOpt->ProjectName + Globals::OGSCENE_FORMAT_EXTENSION).c_str());
        pOpt->ProjectName = fileName;
    }

    if (_writeFile(fileName + Globals::OGSCENE_FORMAT_EXTENSION, forceSave) != SCF_OK)
    {
        return SCF_ERRFILE;
    }

    return SCF_OK;
}