00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "materialasset.h"
00021
00022 #include <core/debughelper.h>
00023 #include <graphics/material.h>
00024 #include <graphics/materialinstance.h>
00025 #include <graphics/engine.h>
00026
00027 #include <QtCore/QUrl>
00028 #include <QtCore/QMimeData>
00029 #include <QtGui/QAction>
00030 #include <QtGui/QDialog>
00031
00032 REGISTER_OBJECTTYPE( GluonEngine, MaterialAsset )
00033
00034 using namespace GluonEngine;
00035
00036 class MaterialAsset::MaterialAssetPrivate
00037 {
00038 public:
00039 MaterialAssetPrivate() {}
00040 ~MaterialAssetPrivate() {}
00041
00042 QPixmap icon;
00043
00044 GluonGraphics::Material* material;
00045
00046 QList<QAction*> actions;
00047 };
00048
00049 MaterialAsset::MaterialAsset( QObject* parent )
00050 : Asset( parent )
00051 , d( new MaterialAssetPrivate )
00052 {
00053 d->material = GluonGraphics::Engine::instance()->createMaterial( name() );
00054
00055 QAction* newInstance = new QAction( "New instance", this );
00056 connect( newInstance, SIGNAL( triggered( bool ) ), SLOT( createInstance() ) );
00057 d->actions.append( newInstance );
00058 }
00059
00060 MaterialAsset::~MaterialAsset()
00061 {
00062 delete d;
00063 }
00064
00065 QIcon MaterialAsset::icon() const
00066 {
00067 if( d->icon.isNull() )
00068 return GluonEngine::Asset::icon();
00069
00070 return QIcon( d->icon );
00071 }
00072
00073 const QStringList MaterialAsset::supportedMimeTypes() const
00074 {
00075 QStringList types;
00076
00077 types << "application/x-gluon-material";
00078
00079 return types;
00080 }
00081
00082 void MaterialAsset::load()
00083 {
00084 if( !file().isEmpty() )
00085 {
00086 if( d->material->load( file() ) )
00087 {
00088 mimeData()->setText( name() );
00089 setLoaded( true );
00090 return;
00091 }
00092 }
00093
00094 debug( "Error loading material: %1", name() );
00095 }
00096
00097 const QList<AssetTemplate*> MaterialAsset::templates()
00098 {
00099 QList<AssetTemplate*> templates;
00100 templates.append( new AssetTemplate( "Material", "material_template.gml", "material", this ) );
00101 return templates;
00102 }
00103
00104 QList<QAction*> MaterialAsset::actions()
00105 {
00106 return d->actions;
00107 }
00108
00109 void MaterialAsset::setName( const QString& newName )
00110 {
00111 GluonGraphics::Engine::instance()->removeMaterial( name() );
00112 GluonGraphics::Engine::instance()->addMaterial( newName, d->material );
00113 GluonEngine::Asset::setName( newName );
00114 }
00115
00116 QString MaterialAsset::childrenToGDL( int indentLevel ) const
00117 {
00118 return GluonObject::childrenToGDL( indentLevel );
00119 }
00120
00121 void MaterialAsset::sanitize()
00122 {
00123 GluonCore::GluonObject::sanitize();
00124
00125 QObjectList allChildren = children();
00126 foreach( QObject * child, allChildren )
00127 {
00128 GluonGraphics::MaterialInstance* instance = qobject_cast<GluonGraphics::MaterialInstance*>( child );
00129 if( instance )
00130 instance->setMaterial( d->material );
00131 }
00132 }
00133
00134 void MaterialAsset::createInstance()
00135 {
00136 GluonGraphics::MaterialInstance* instance = new GluonGraphics::MaterialInstance( this );
00137 instance->setName( "New Instance" );
00138 instance->setPropertiesFromMaterial();
00139 instance->setMaterial( d->material );
00140 }
00141
00142 Q_EXPORT_PLUGIN2( gluon_asset_material, GluonEngine::MaterialAsset )
00143
00144 #include "materialasset.moc"