00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "objectmanager.h"
00021
00022 #include "selectionmanager.h"
00023 #include "newobjectcommand.h"
00024 #include "deleteobjectcommand.h"
00025 #include "propertychangedcommand.h"
00026 #include "historymanager.h"
00027
00028 #include <core/debughelper.h>
00029 #include <engine/gameproject.h>
00030 #include <engine/gameobject.h>
00031 #include <engine/scene.h>
00032 #include <engine/game.h>
00033 #include <engine/component.h>
00034
00035 #include <KDE/KLocalizedString>
00036 #include <KDE/KMimeType>
00037 #include <QtCore/QFileInfo>
00038 #include <QtCore/QDir>
00039 #include <QtCore/QStringBuilder>
00040
00041 using namespace GluonCreator;
00042
00043 template<> GLUON_CREATOR_VISIBILITY ObjectManager* GluonCore::Singleton<ObjectManager>::m_instance = 0;
00044
00045 QString
00046 ObjectManager::humanifyClassName( const QString& fixThis, bool justRemoveNamespace ) const
00047 {
00048 QString fixedString;
00049 const QString classname = fixThis.right( fixThis.length() - fixThis.lastIndexOf( ':' ) - 1 );
00050 if( justRemoveNamespace )
00051 return classname;
00052 const int length = classname.size();
00053 for( int i = 0; i < length; ++i )
00054 {
00055 const QChar current = classname.at( i );
00056 if( i == 0 )
00057 {
00058
00059 fixedString = current.toUpper();
00060 }
00061 else
00062 {
00063 if( current.isUpper() )
00064 {
00065 fixedString = fixedString % ' ' % current;
00066 }
00067 else
00068 {
00069 fixedString = fixedString % current;
00070 }
00071 }
00072 }
00073 return fixedString;
00074 }
00075
00076 GluonEngine::Asset* ObjectManager::createNewAsset( const QString& fileName )
00077 {
00078 DEBUG_BLOCK
00079 KMimeType::Ptr type = KMimeType::findByFileContent( fileName );
00080 DEBUG_TEXT( QString( "Creating asset for file %1 of mimetype %2" ).arg( fileName ).arg( type->name() ) );
00081
00082 GluonCore::GluonObject* newasset = GluonCore::GluonObjectFactory::instance()->instantiateObjectByMimetype( type->name() );
00083 if( newasset )
00084 {
00085 GluonEngine::Asset* newChild = static_cast<GluonEngine::Asset*>( newasset );
00086
00087 QFileInfo theFileInfo( fileName );
00088
00089 GluonEngine::Game::instance()->gameProject()->addChild( newChild );
00090 newChild->setName( theFileInfo.fileName() );
00091
00092 if( !QDir::current().exists( "Assets" ) )
00093 QDir::current().mkdir( "Assets" );
00094
00095 DEBUG_TEXT( QString( "Copying file to %1" ).arg( newChild->fullyQualifiedFileName() ) );
00096 QUrl newLocation( QString( "Assets/%1" ).arg( newChild->fullyQualifiedFileName() ) );
00097 QFile( fileName ).copy( newLocation.toLocalFile() );
00098 newChild->setFile( newLocation );
00099 newChild->load();
00100
00101 HistoryManager::instance()->addCommand( new NewObjectCommand( newasset ) );
00102 return newChild;
00103 }
00104
00105 return 0;
00106 }
00107
00108 void ObjectManager::deleteGameObject( GluonEngine::GameObject* object )
00109 {
00110 HistoryManager::instance()->addCommand( new DeleteObjectCommand( object, object->parentGameObject() ) );
00111 }
00112
00113 void ObjectManager::changeProperty( GluonCore::GluonObject* object, QString& property, QVariant& oldValue, QVariant& newValue )
00114 {
00115 HistoryManager::instance()->addCommand( new PropertyChangedCommand( object, property, oldValue, newValue ) );
00116 }
00117
00118 GluonEngine::Component* ObjectManager::createNewComponent( const QString& type, GluonEngine::GameObject* parent )
00119 {
00120 DEBUG_BLOCK
00121 GluonCore::GluonObject* newObj = GluonCore::GluonObjectFactory::instance()->instantiateObjectByName( type );
00122 if( newObj )
00123 {
00124 newObj->setName( humanifyClassName( newObj->metaObject()->className(), true ) );
00125
00126 GluonEngine::Component* comp = qobject_cast<GluonEngine::Component*>( newObj );
00127 parent->addComponent( comp );
00128
00129
00130 comp->initialize();
00131
00132 emit newComponent( comp );
00133
00134 HistoryManager::instance()->addCommand( new NewObjectCommand( comp ) );
00135
00136 return comp;
00137 }
00138 return 0;
00139 }
00140
00141 GluonEngine::GameObject* ObjectManager::createNewGameObject()
00142 {
00143 DEBUG_FUNC_NAME
00144 GluonEngine::GameObject* newObj = new GluonEngine::GameObject();
00145 newObj->setName( humanifyClassName( newObj->metaObject()->className(), false ) );
00146 DEBUG_TEXT( QString( "Creating object: %1" ).arg( newObj->name() ) );
00147
00148 SelectionManager::SelectionList selection = SelectionManager::instance()->selection();
00149 if( selection.size() > 0 )
00150 {
00151 GluonEngine::GameObject* obj = qobject_cast<GluonEngine::GameObject*>( selection.at( 0 ) );
00152 if( obj )
00153 {
00154 DEBUG_TEXT( QString( "Item %1 selected in Scene tree - assign new object as child" ).arg( obj->fullyQualifiedName() ) );
00155 obj->addChild( newObj );
00156 }
00157 }
00158
00159 if( newObj->parentGameObject() == 0 )
00160 {
00161 DEBUG_TEXT( QString( "No parent game object yet - assign as child to Scene root" ) );
00162 GluonEngine::Game::instance()->currentScene()->sceneContents()->addChild( newObj );
00163 }
00164
00165 emit newGameObject( newObj );
00166
00167 HistoryManager::instance()->addCommand( new NewObjectCommand( newObj ) );
00168
00169 return newObj;
00170 }
00171
00172 GluonEngine::Scene* ObjectManager::createNewScene()
00173 {
00174 GluonEngine::Scene* newScn = new GluonEngine::Scene();
00175 newScn->setName( i18n( "NewScene" ) );
00176 newScn->setGameProject( GluonEngine::Game::instance()->gameProject() );
00177 GluonEngine::Game::instance()->gameProject()->addChild( newScn );
00178
00179 emit newScene( newScn );
00180
00181 HistoryManager::instance()->addCommand( new NewObjectCommand( newScn ) );
00182
00183 return newScn;
00184 }
00185
00186 ObjectManager::ObjectManager()
00187 {
00188 m_objectId = 0;
00189 m_sceneId = 0;
00190 }
00191
00192 ObjectManager::~ObjectManager()
00193 {
00194
00195 }
00196
00197