00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (c) 2010 Arjen Hiemstra <ahiemstra@heimr.nl> 00004 * Copyright (C) 2010 Dan Leinir Turthra Jensen <admin@leinir.dk> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include "sceneprivate.h" 00022 #include "scene.h" 00023 #include "gameobject.h" 00024 00025 #include <core/debughelper.h> 00026 #include <core/gdlhandler.h> 00027 00028 #include <QtCore/QUrl> 00029 #include <QtCore/QFile> 00030 #include <QtCore/qtextstream.h> 00031 00032 using namespace GluonEngine; 00033 00034 ScenePrivate::ScenePrivate( Scene* q ) 00035 { 00036 this->q = q; 00037 00038 sceneContentsLoaded = false; 00039 sceneContentsStarted = false; 00040 sceneContents = new GameObject( q ); 00041 sceneContents->setName( q->name() ); 00042 } 00043 00044 ScenePrivate::ScenePrivate::~ScenePrivate() 00045 { 00046 } 00047 00048 void 00049 ScenePrivate::unloadContents() 00050 { 00051 if( sceneContentsLoaded ) 00052 qDeleteAll( sceneContents->children() ); 00053 sceneContentsLoaded = false; 00054 } 00055 00056 void 00057 ScenePrivate::loadContents( const QUrl& file ) 00058 { 00059 DEBUG_BLOCK 00060 if( file.isEmpty() ) 00061 { 00062 sceneContentsLoaded = true; 00063 return; 00064 } 00065 00066 QFile* sceneFile = new QFile( file.toLocalFile() ); 00067 if( !sceneFile->exists() ) 00068 { 00069 DEBUG_TEXT( QString( "File %1 does not exist, aborting scene load" ).arg( file.toLocalFile() ) ); 00070 return; 00071 } 00072 00073 if( !sceneFile->open( QIODevice::ReadOnly ) ) 00074 { 00075 DEBUG_TEXT( QString( "Failed to load scene contents as %1 could not be opened for reading" ).arg( file.toLocalFile() ) ) 00076 return; 00077 } 00078 00079 QTextStream sceneReader( sceneFile ); 00080 QList<GluonCore::GluonObject*> theContents = GluonCore::GDLHandler::instance()->parseGDL( sceneReader.readAll(), q ); 00081 sceneFile->close(); 00082 delete sceneFile; 00083 00084 if( sceneContents ) 00085 delete sceneContents; 00086 /*sceneContents = new GameObject(q); 00087 foreach(GluonObject * child, theContents) 00088 sceneContents->addChild(child);*/ 00089 if( theContents.count() > 0 ) 00090 sceneContents = qobject_cast<GluonEngine::GameObject*>( theContents.at( 0 ) ); 00091 if( !sceneContents ) 00092 sceneContents = new GluonEngine::GameObject( q ); 00093 00094 sceneContents->sanitize(); 00095 sceneContentsLoaded = true; 00096 q->savableDirty = false; 00097 00098 sceneContents->setName( q->name() ); 00099 } 00100 00101 void 00102 ScenePrivate::saveContents( const QUrl& file ) 00103 { 00104 QList<const GluonCore::GluonObject*> scene; 00105 foreach( const QObject * item, sceneContents->children() ) 00106 { 00107 scene.append( qobject_cast<const GluonCore::GluonObject*>( item ) ); 00108 } 00109 00110 QFile* sceneFile = new QFile( file.toLocalFile() ); 00111 if( !sceneFile->open( QIODevice::WriteOnly ) ) 00112 return; 00113 00114 QTextStream sceneWriter( sceneFile ); 00115 sceneWriter << GluonCore::GDLHandler::instance()->serializeGDL( scene ); 00116 sceneWriter.flush(); 00117 sceneFile->close(); 00118 delete sceneFile; 00119 q->savableDirty = false; 00120 }