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 "component.h" 00022 #include "componentprivate.h" 00023 #include "gameobject.h" 00024 00025 #include "core/debughelper.h" 00026 #include "asset.h" 00027 00028 #include <QtCore/QString> 00029 #include <QtCore/QMetaProperty> 00030 00031 using namespace GluonEngine; 00032 00033 Component::Component( QObject* parent ) 00034 : GluonObject( parent ) 00035 , d( new ComponentPrivate ) 00036 { 00037 } 00038 00039 Component::Component( const Component& other, QObject* parent ) 00040 : GluonObject( parent ) 00041 , d( other.d ) 00042 { 00043 } 00044 00045 Component::~Component() 00046 { 00047 } 00048 00049 QString 00050 Component::category() const 00051 { 00052 return QString( "Uncategorised" ); 00053 } 00054 00055 QVariant 00056 Component::toVariant( GluonCore::GluonObject* wrapThis ) 00057 { 00058 if(QString::fromAscii(staticMetaObject.className()).compare("GluonCore::Component" ) ) 00059 { 00060 DEBUG_BLOCK 00061 DEBUG_TEXT( QString( "Found attempt to use class without toVariant as property. Offending class: %1" ).arg( staticMetaObject.className() ) ); 00062 } 00063 return QVariant::fromValue<GluonEngine::Component*>( qobject_cast<GluonEngine::Component*>( wrapThis ) ); 00064 } 00065 00066 void 00067 Component::update( int elapsedMilliseconds ) 00068 { 00069 Q_UNUSED( elapsedMilliseconds ); 00070 } 00071 00072 void 00073 Component::draw( int timeLapse ) 00074 { 00075 Q_UNUSED( timeLapse ); 00076 } 00077 00078 void 00079 Component::sanitize() 00080 { 00081 if( parent() ) 00082 { 00083 if( parent()->metaObject() ) 00084 { 00085 if( QString::compare( parent()->metaObject()->className(), "GameObject" ) ) 00086 { 00087 GameObject* theParent = qobject_cast<GameObject*>( parent() ); 00088 if( theParent ) 00089 { 00090 theParent->addComponent( this ); 00091 d->gameObject = theParent; 00092 } 00093 } 00094 } 00095 } 00096 GluonObject::sanitize(); 00097 } 00098 00099 // Property getter-setters 00100 00101 void 00102 Component::setDescription( const QString& newDescription ) 00103 { 00104 d->description = newDescription; 00105 } 00106 00107 QString 00108 Component::description() const 00109 { 00110 return d->description; 00111 } 00112 00113 void 00114 Component::setEnabled( bool newEnabled ) 00115 { 00116 d->enabled = newEnabled; 00117 } 00118 00119 bool 00120 Component::enabled() const 00121 { 00122 return d->enabled; 00123 } 00124 00125 GameObject * 00126 Component::gameObject() 00127 { 00128 return d->gameObject; 00129 } 00130 00131 void 00132 Component::setGameObject( GameObject* newGameObject ) 00133 { 00134 d->gameObject = newGameObject; 00135 } 00136 00137 QString 00138 Component::stringFromProperty( const QString& propertyName, const QString& indentChars ) const 00139 { 00140 DEBUG_FUNC_NAME 00141 QMetaProperty prop = metaObject()->property( metaObject()->indexOfProperty( propertyName.toUtf8() ) ); 00142 if( QString( prop.typeName() ) == QString( "GluonEngine::Asset*" ) ) 00143 { 00144 GluonEngine::Asset* asset = prop.read( this ).value<GluonEngine::Asset*>(); 00145 if( asset ) 00146 { 00147 return QString( "\n%1%2 GluonEngine::Asset(%3)" ).arg( indentChars, propertyName, asset->fullyQualifiedName() ); 00148 } 00149 } 00150 return GluonCore::GluonObject::stringFromProperty( propertyName, indentChars ); 00151 } 00152 00153 #include "component.moc"