00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "plasmaapplet.h"
00026 #include "gamesoverlay.h"
00027 #include "views/gamesview.h"
00028 #include "gamedetailsoverlay.h"
00029
00030 #include <engine/game.h>
00031 #include <engine/gameproject.h>
00032 #include <engine/scene.h>
00033 #include <graphics/item.h>
00034 #include <graphics/camera.h>
00035 #include <graphics/engine.h>
00036 #include <models/gamesmodel.h>
00037
00038 #include <QFileDialog>
00039 #include <QTimer>
00040 #include <QGraphicsSceneResizeEvent>
00041 #include <QGraphicsLinearLayout>
00042
00043 using namespace GluonPlayer;
00044 using namespace GluonGraphics;
00045
00046 PlasmaApplet::PlasmaApplet( QObject* parent, const QVariantList& args )
00047 : GLFBOApplet( parent, args )
00048 , m_viewportWidth( 0 )
00049 , m_viewportHeight( 0 )
00050 , m_project( 0 )
00051 , m_camera( 0 )
00052 , m_gamesOverlay( 0 )
00053 , m_gameDetailsOverlay( 0 )
00054 {
00055 setHasConfigurationInterface( true );
00056 setAspectRatioMode( Plasma::IgnoreAspectRatio );
00057 setBackgroundHints( Plasma::Applet::NoBackground );
00058 setMinimumSize( 200, 200 );
00059 resize( 500, 500 );
00060 }
00061
00062 PlasmaApplet::~PlasmaApplet()
00063 {
00064 kDebug() << "QUIT";
00065
00066 if( m_gameFileName.isEmpty() )
00067 {
00068 return;
00069 }
00070
00071 GluonEngine::Game::instance()->stopGame();
00072 GluonEngine::Game::instance()->cleanupAll();
00073 }
00074
00075 void PlasmaApplet::init()
00076 {
00077 m_gamesModel = new GamesModel( this );
00078 m_layout = new QGraphicsLinearLayout( Qt::Vertical );
00079 setLayout( m_layout );
00080 showGames();
00081 }
00082
00083 void PlasmaApplet::setProject( const QModelIndex& index )
00084 {
00085 m_gameFileName = index.data().toString();
00086 m_gamesOverlay->hide();
00087 openProject();
00088 }
00089
00090 void PlasmaApplet::openProject()
00091 {
00092 if( m_gameFileName.isEmpty() )
00093 {
00094 return;
00095 }
00096
00097 setBusy( true );
00098
00099 initGL();
00100 GluonCore::GluonObjectFactory::instance()->loadPlugins();
00101
00102 m_project = new GluonEngine::GameProject();
00103 m_project->loadFromFile( QUrl( m_gameFileName ) );
00104 GluonEngine::Game::instance()->setGameProject( m_project );
00105 GluonEngine::Game::instance()->setCurrentScene( m_project->entryPoint() );
00106
00107 connect( GluonEngine::Game::instance(), SIGNAL( painted( int ) ), SLOT( doPaint() ) );
00108 QTimer::singleShot( 1000, this, SLOT( startGame() ) );
00109 }
00110
00111 void PlasmaApplet::doPaint()
00112 {
00113 makeCurrent();
00114 update();
00115 }
00116
00117 void PlasmaApplet::startGame()
00118 {
00119 setBusy( false );
00120 GluonEngine::Game::instance()->runGame();
00121 }
00122
00123 void PlasmaApplet::initGL()
00124 {
00125 Engine::instance()->initialize();
00126 m_camera = Engine::instance()->activeCamera();
00127 connect( Engine::instance(), SIGNAL( activeCameraChanged( GluonGraphics::Camera* ) ),
00128 SLOT( setCamera( GluonGraphics::Camera* ) ) );
00129
00130 glClearColor( 0.0, 0.0, 0.0, 1.0 );
00131 glShadeModel( GL_SMOOTH );
00132 glEnable( GL_DEPTH_TEST );
00133 glEnable( GL_BLEND );
00134 glEnable( GL_SCISSOR_TEST );
00135 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00136 }
00137
00138 void PlasmaApplet::resizeEvent( QGraphicsSceneResizeEvent* event )
00139 {
00140 m_viewportWidth = event->newSize().width();
00141 m_viewportHeight = event->newSize().height();
00142
00143 glViewport( 0, 0, m_viewportWidth, m_viewportHeight );
00144 }
00145
00146 void PlasmaApplet::showGames()
00147 {
00148 if( !m_gamesOverlay )
00149 {
00150 m_gamesOverlay = new GamesOverlay( this );
00151 m_gamesOverlay->gamesView()->setModel( m_gamesModel );
00152 m_gamesOverlay->setGeometry( geometry() );
00153 connect( m_gamesOverlay, SIGNAL( gameToPlaySelected( QModelIndex ) ), SLOT( setProject( QModelIndex ) ) );
00154 connect( m_gamesOverlay, SIGNAL( gameSelected( QModelIndex ) ), SLOT( showGameDetails( QModelIndex ) ) );
00155 }
00156
00157 if( m_gameDetailsOverlay )
00158 {
00159 m_gameDetailsOverlay->hide();
00160 m_layout->removeItem( m_gameDetailsOverlay );
00161 m_gameDetailsOverlay->deleteLater();
00162 m_gameDetailsOverlay = 0;
00163 }
00164
00165 m_layout->addItem( m_gamesOverlay );
00166 m_gamesOverlay->show();
00167 }
00168
00169 void PlasmaApplet::showGameDetails( const QModelIndex& index )
00170 {
00171 QString id = index.sibling(index.row(), GamesModel::IdColumn).data().toString();
00172 if (id.isEmpty()) {
00173 return;
00174 }
00175
00176 m_gameDetailsOverlay = new GameDetailsOverlay(id, this );
00177 m_gamesOverlay->hide();
00178 m_layout->removeItem( m_gamesOverlay );
00179 m_gameDetailsOverlay->show();
00180 m_layout->addItem( m_gameDetailsOverlay );
00181 connect( m_gameDetailsOverlay, SIGNAL( back() ), SLOT( showGames() ) );
00182 }
00183
00184 void PlasmaApplet::setCamera( Camera* camera )
00185 {
00186 m_camera = camera;
00187 }
00188
00189 void PlasmaApplet::render()
00190 {
00191 Engine::instance()->render();
00192 }
00193
00194 void PlasmaApplet::paintGLInterface( QPainter* painter, const QStyleOptionGraphicsItem* option )
00195 {
00196 Q_UNUSED( painter );
00197 Q_UNUSED( option );
00198
00199 glScissor( 0, 0, 400, 400 );
00200 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00201 glLoadIdentity();
00202 glClearColor( 0.0, 0.0, 0.0, 1.0 );
00203 glClear( GL_COLOR_BUFFER_BIT );
00204
00205 Engine::instance()->render();
00206
00207 glPushAttrib( GL_ALL_ATTRIB_BITS );
00208 render();
00209 glPopAttrib();
00210 }
00211
00212 #include "plasmaapplet.moc"