00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (c) 2010 Arjen Hiemstra <ahiemstra@heimr.nl> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 00021 #include "newobjectcommand.h" 00022 #include <engine/gameobject.h> 00023 #include <engine/component.h> 00024 00025 using namespace GluonCreator; 00026 00027 class NewObjectCommand::NewObjectCommandPrivate 00028 { 00029 public: 00030 GluonCore::GluonObject* parent; 00031 bool applied; 00032 }; 00033 00034 NewObjectCommand::NewObjectCommand( GluonCore::GluonObject* newObject ) : d( new NewObjectCommandPrivate ) 00035 { 00036 setObject( newObject ); 00037 d->parent = qobject_cast<GluonCore::GluonObject*>( newObject->parent() ); 00038 d->applied = true; 00039 00040 setCommandName( "NewObjectCommand" ); 00041 } 00042 00043 NewObjectCommand::~NewObjectCommand() 00044 { 00045 if( !d->applied ) 00046 delete object(); 00047 00048 delete d; 00049 } 00050 00051 void 00052 NewObjectCommand::undo() 00053 { 00054 setCommandDirection( "undo" ); 00055 d->applied = false; 00056 if( d->parent->children().indexOf( object() ) != -1 ) 00057 d->parent->removeChild( object() ); 00058 00059 GluonEngine::GameObject* obj = qobject_cast<GluonEngine::GameObject*>( object() ); 00060 if( obj ) 00061 { 00062 if( obj->parentGameObject()->childIndex( obj ) != -1 ) 00063 obj->parentGameObject()->removeChild( obj ); 00064 } 00065 00066 GluonEngine::Component* comp = qobject_cast<GluonEngine::Component*>( object() ); 00067 if( comp ) 00068 comp->gameObject()->removeComponent( comp ); 00069 } 00070 00071 void 00072 NewObjectCommand::redo() 00073 { 00074 setCommandDirection( "redo" ); 00075 d->applied = true; 00076 object()->setParent( d->parent ); 00077 00078 GluonEngine::GameObject* gobjParent = qobject_cast< GluonEngine::GameObject* >( d->parent ); 00079 GluonEngine::GameObject* obj = qobject_cast<GluonEngine::GameObject*>( object() ); 00080 if( obj && gobjParent ) 00081 gobjParent->addChild( obj ); 00082 00083 GluonEngine::Component* comp = qobject_cast<GluonEngine::Component*>( object() ); 00084 if( comp && gobjParent ) 00085 gobjParent->addComponent( comp ); 00086 00087 } 00088