00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "gluonviewerpart.h"
00022 #include <graphics/renderwidget.h>
00023 #include <engine/gameproject.h>
00024 #include <engine/game.h>
00025
00026 #include <QtGui/QWidget>
00027 #include <kdemacros.h>
00028 #include <kparts/genericfactory.h>
00029 #include <KDE/KUrl>
00030 #include <QtCore/QThread>
00031 #include <QTimer>
00032 #include <KActionCollection>
00033 #include <kaction.h>
00034
00035 using namespace GluonCreator;
00036
00037 class GluonViewerPart::GluonViewerPartPrivate
00038 {
00039 public:
00040 GluonGraphics::RenderWidget* widget;
00041 GluonEngine::GameProject* project;
00042
00043 bool autoplay;
00044 };
00045
00046 GluonCreator::GluonViewerPart::GluonViewerPart( QWidget* parentWidget, QObject* parent, const QVariantList& args )
00047 : ReadOnlyPart( parent ),
00048 d( new GluonViewerPartPrivate )
00049 {
00050 Q_UNUSED( parentWidget )
00051
00052 KComponentData data( "gluonviewerpart", "gluoncreator" );
00053 setComponentData( data );
00054
00055 d->autoplay = true;
00056 d->widget = new GluonGraphics::RenderWidget();
00057 setWidget( d->widget );
00058
00059 connect( GluonEngine::Game::instance(), SIGNAL( painted( int ) ), d->widget, SLOT( updateGL() ) );
00060
00061 foreach( const QVariant & arg, args )
00062 {
00063 QString keyValue = arg.toString();
00064 if( keyValue == "autoplay=false" )
00065 d->autoplay = false;
00066 }
00067
00068 QActionGroup* group = new QActionGroup( actionCollection() );
00069 group->setExclusive( true );
00070
00071 KAction* solid = new KAction( KIcon( "draw-polyline" ), i18n( "Solid" ), actionCollection() );
00072 solid->setCheckable( true );
00073 solid->setChecked( true );
00074 connect( solid, SIGNAL( triggered( bool ) ), this, SLOT( setSolid() ) );
00075 group->addAction( solid );
00076 actionCollection()->addAction( "toggleSolidAction", solid );
00077
00078 KAction* wire = new KAction( KIcon( "draw-line" ), i18n( "Wireframe" ), actionCollection() );
00079 wire->setCheckable( true );
00080 connect( wire, SIGNAL( triggered( bool ) ), this, SLOT( setWireframe() ) );
00081 group->addAction( wire );
00082 actionCollection()->addAction( "toggleWireframeAction", wire );
00083
00084 KAction* points = new KAction( KIcon( "edit-node" ), i18n( "Points" ), actionCollection() );
00085 points->setCheckable( true );
00086 connect( points, SIGNAL( triggered( bool ) ), this, SLOT( setPoints() ) );
00087 group->addAction( points );
00088 actionCollection()->addAction( "togglePointsAction", points );
00089
00090 setXMLFile( "gluonviewerpartui.rc" );
00091 }
00092
00093 GluonCreator::GluonViewerPart::~GluonViewerPart()
00094 {
00095 GluonEngine::Game::instance()->stopGame();
00096 delete d;
00097 }
00098
00099 bool GluonCreator::GluonViewerPart::openFile()
00100 {
00101 GluonCore::GluonObjectFactory::instance()->loadPlugins();
00102
00103 d->project = new GluonEngine::GameProject();
00104 d->project->loadFromFile( url() );
00105
00106 GluonEngine::Game::instance()->setGameProject( d->project );
00107 GluonEngine::Game::instance()->setCurrentScene( d->project->entryPoint() );
00108
00109 if( d->autoplay )
00110 {
00111 QTimer::singleShot( 100, this, SLOT( startGame() ) );
00112 }
00113
00114 return true;
00115 }
00116
00117 void GluonViewerPart::startGame()
00118 {
00119 GluonEngine::Game::instance()->runGame();
00120 }
00121
00122 void GluonViewerPart::setSolid()
00123 {
00124 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
00125 }
00126
00127 void GluonViewerPart::setWireframe()
00128 {
00129 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
00130 }
00131
00132 void GluonViewerPart::setPoints()
00133 {
00134 glPolygonMode( GL_FRONT_AND_BACK, GL_POINT );
00135 }
00136
00137 K_PLUGIN_FACTORY( GluonViewerPartFactory, registerPlugin<GluonViewerPart>(); )
00138 K_EXPORT_PLUGIN( GluonViewerPartFactory( "GluonViewerPart", "GluonViewerPart" ) )
00139
00140