controler.cpp

00001 
00002 #include "main.h"
00003 #include "controler.h"
00004 #include "photo.h"
00005 #include "table.h"
00006 #include "mouse.h"
00007 #include "photoCollection.h"
00008 #include "irrlichtEventReceiver.h"
00009 #include "irrlichtDebugHelper.h"
00010 #include "physxDebugHelper.h"
00011 
00012 
00013 
00014 
00015 // *** private prototypes, used in this file ***
00016 
00017 // this function is called by our IrrlichtEventReceiver if a key is pressed
00018 ERR_TYPE onKeyEvent( EKEY_CODE keyCode );
00019 // pick up a photo at mouse x/y ("elastic strap" pick up)
00020 ERR_TYPE onMouseButtonLeftEvent( s32 x, s32 y, f32 wheel );
00021 // drop picked photo on table
00022 ERR_TYPE onMouseButtonLeftReleaseEvent();
00023 // zoom a photo in front of camera
00024 ERR_TYPE onMouseButtonLeftDoubleClickEvent( s32 x, s32 y, f32 wheel );
00025 // pick up a photo at mouse x/y ("fixed" pick up)
00026 ERR_TYPE onMouseButtonRightEvent( s32 x, s32 y, f32 wheel );
00027 // drop picked photo on table
00028 ERR_TYPE onMouseButtonRightReleaseEvent();
00029 // move picked photo to mouse x/y position
00030 ERR_TYPE onMouseMoveEvent( s32 x, s32 y );
00031 // this function is called to check if a photo should be deleted
00032 ERR_TYPE onCheckForPhotoDelete( Photo* photo );
00033 
00034 
00035 
00036 // only visible within this file
00037 static PhotoCollection*                 myPhotoCollection               = NULL;
00038 static Table*                                   myTable                                 = NULL;
00039 static Mouse*                                   myMouse                                 = NULL;
00040 static ICameraSceneNode*                myCamera                                = NULL;
00041 static ILightSceneNode*                 myLight                                 = NULL;
00042 static IrrlichtEventReceiver*   myEvents                                = NULL;
00043 static bool                                             isRunning                               = true;
00044 static Photo*                                   myZoomedPhoto                   = NULL;
00045 
00046 
00047 // *** public interface: ***
00048 
00049 
00050 // setup a custom test scene
00051 ERR_TYPE controlerInit()
00052 {
00053         // attach our local event handlers
00054         myEvents = new IrrlichtEventReceiver();
00055         myEvents->registerKeyEventCallback( onKeyEvent );
00056         myEvents->registerMouseButtonLeftEventCallback( onMouseButtonLeftEvent );
00057         myEvents->registerMouseButtonLeftReleaseEventCallback( onMouseButtonLeftReleaseEvent );
00058         myEvents->registerMouseButtonLeftDoubleClickEventCallback( onMouseButtonLeftDoubleClickEvent );
00059         myEvents->registerMouseButtonRightEventCallback( onMouseButtonRightEvent );
00060         myEvents->registerMouseButtonRightReleaseEventCallback( onMouseButtonRightReleaseEvent );
00061         myEvents->registerMouseMoveEventCallback( onMouseMoveEvent );
00062         
00063         // create an empty list of photos. 
00064         // list can add(), addDirectory(), reset() and auto-delete photos
00065         myPhotoCollection = new PhotoCollection();
00066         // attach our local "check if photo should be deleted" function
00067         myPhotoCollection->registerCheckForDeleteCallback( onCheckForPhotoDelete );
00068 
00069         // create the one and only table
00070         myTable = new Table();
00071         myTable->load(  "../../media/table_v4_irr.3DS",         // mesh
00072                                         "../../media/tabtex_metall.jpg",        // diffuse map
00073                                         vector3df(1.0f, 1.0f, 1.0f) );          // scale
00074 
00075         // setup irrlicht standard camera
00076         vector3df camPosition(0.0f, 72.0f, -0.2f); // straight up, slightly to the user
00077         vector3df camLookAt(0.0f, 0.0f, 0.0f); // table center
00078         myCamera = smgr->addCameraSceneNode( NULL, camPosition, camLookAt );
00079                                 
00080         // create the virtual mouse-pointer, used to pick photos
00081         // mouse dimension is 1 units in size
00082         // mouse operates 12 units above (0,0,0)
00083         // picked objects will be lifted by 0.0 units
00084         myMouse = new Mouse( 1.0f, 12.0f, 0.0f );
00085 
00086         // add some debug arrows to the scene
00087         // 510 right from tableCenter, 200 above table, 200 into space 
00088         irrlichtDebugHelperAddArrows( vector3df(510.0f, 200.0f, 200.0f) );
00089 
00090         // setup a light source, looks better than "no dynamic lighting"
00091         vector3df lightPos( -40.0f, 100.0f, 40.0f );
00092         SColorf   lightColor( 1.0f, 1.0f, 1.0f, 1.0f ); // rgb a
00093         f32       lightRadius = 200.0f;
00094         myLight = smgr->addLightSceneNode( NULL, lightPos, lightColor, lightRadius );
00095         smgr->setShadowColor( SColor(100, 0, 0, 0) ); // a rgb  // 255 = no transparancy
00096 
00097         // a moving light is cool
00098         //ISceneNodeAnimator* anim = smgr->createFlyCircleAnimator( vector3df(0, 100, 0), 50, 0.001f );
00099         //myLight->addAnimator( anim );
00100         //anim->drop();
00101 
00102         // visualize position of light source
00103         IBillboardSceneNode* bill = smgr->addBillboardSceneNode( myLight, dimension2df(30.0f, 30.0f) );
00104         bill->setMaterialFlag( EMF_LIGHTING, false );
00105         bill->setMaterialType( EMT_TRANSPARENT_ADD_COLOR );
00106         bill->setMaterialTexture( 0, driver->getTexture("../../media/particlewhite.bmp") );
00107 
00108         // output some info
00109         logger->log( "" );
00110         logger->log( "Press C, P, R, L, D, ESC" );
00111         logger->log( "Use Mouse To Move Photos, Doubleclick To Zoom." );
00112 
00113         return ERR_OK;
00114 }
00115 
00116 
00117 // to be called every frame
00118 ERR_TYPE controlerUpdate()
00119 {
00120         u32 now = timer->getRealTime();
00121 
00122         // quit application?
00123         if ( ! isRunning )
00124         {
00125                 return ERR_UNKWON;
00126         }
00127 
00128         // update all photos
00129         myPhotoCollection->update( now );
00130 
00131         if ( NULL != myTable )
00132         {
00133                 // does nothing
00134                 myTable->update( now );
00135         }
00136 
00137         return ERR_OK;
00138 }
00139 
00140 
00141 // destroy test scene
00142 ERR_TYPE controlerDeinit()
00143 {
00144         if ( NULL != myMouse )
00145         {
00146                 delete myMouse;
00147                 myMouse = NULL;
00148         }
00149         if ( NULL != myPhotoCollection )
00150         {
00151                 delete myPhotoCollection;
00152                 myPhotoCollection = NULL;
00153         }
00154         if ( NULL != myTable )
00155         {
00156                 delete myTable;
00157                 myTable = NULL;
00158         }
00159         if ( NULL != myEvents )
00160         {
00161                 delete myEvents;
00162                 myEvents = NULL;
00163         }
00164 
00165         return ERR_OK;
00166 }
00167 
00168 
00169 
00170 
00171 // *** private stuff: ***
00172 
00173 
00174 // this function is called by our IrrlichtEventReceiver if a key is pressed
00175 ERR_TYPE onKeyEvent( EKEY_CODE keyCode )
00176 {
00177         switch ( keyCode )
00178         {
00179                 case KEY_KEY_P:
00180                 {
00181                         // create new object with default texture
00182                         myPhotoCollection->addPhoto( stringc("../../media/stones.jpg"), NxVec3(0.0f, 60.0f, 0.0f) );
00183                         return ERR_EVENT_CONSUMED;
00184                 }
00185 
00186                 case KEY_KEY_R:
00187                 {
00188                         // reset all objects' position
00189                         myPhotoCollection->resetPhotos();
00190                         return ERR_EVENT_CONSUMED;
00191                 }
00192 
00193                 case KEY_KEY_L:
00194                 {
00195                         // load all photos found in directory ../../media/photo
00196                         myPhotoCollection->addPhotosFromDirectory( stringc("../../media/photo") );
00197                         return ERR_EVENT_CONSUMED;
00198                 }
00199 
00200                 case KEY_KEY_D:
00201                 {
00202                         // switch physx "draw lines around actors"
00203                         switchPhysxDebugRenderingEnabled();
00204                         return ERR_EVENT_CONSUMED;
00205                 }
00206 
00207                 case KEY_KEY_C:
00208                 {
00209                         static bool useFPSCamera = true;
00210                         static vector3df oldCamPos(0, 0, 0);
00211                         static vector3df oldCamTarget(0, 0, 0);
00212                         
00213                         // store other camara
00214                         oldCamPos = myCamera->getPosition();
00215                         oldCamTarget = myCamera->getTarget();
00216 
00217                         if ( useFPSCamera )
00218                         {
00219                                 myCamera->remove();
00220                                 // setup irrlicht FPS camera
00221                                 myCamera = smgr->addCameraSceneNodeFPS();
00222                                 myCamera->setPosition( oldCamPos );
00223                                 myCamera->setTarget( oldCamTarget );
00224                                 logger->log( "Using FPS Camera" );
00225                         }
00226                         else
00227                         {
00228                                 myCamera->remove();
00229                                 // setup irrlicht standard camera
00230                                 myCamera = smgr->addCameraSceneNode();
00231                                 myCamera->setPosition( oldCamPos );
00232                                 myCamera->setTarget( oldCamTarget );
00233                                 logger->log( "Using Fixed Camera" );
00234                         }
00235                         // switch FPS <-> Fixed camera
00236                         useFPSCamera = !useFPSCamera;
00237 
00238                         return ERR_EVENT_CONSUMED;
00239                 }
00240 
00241                 case KEY_KEY_S:
00242                 {
00243                         // special fx test
00244                         IParticleSystemSceneNode* p = smgr->addParticleSystemSceneNode();
00245                         p->setParticleSize( dimension2d<f32>(5.0f, 5.0f) );
00246                         IParticleEmitter* em = p->createBoxEmitter(     aabbox3d<f32>(-5.0f, 9.0f, -5.0f, 5.0f, 10.0f, 5.0f),
00247                                                                                                                 vector3df(0.0f, 0.02f, 0.0f),
00248                                                                                                                 10,
00249                                                                                                                 20,
00250                                                                                                                 SColor(255,255,255,255),
00251                                                                                                                 SColor(255,255,255,255),
00252                                                                                                                 1100,
00253                                                                                                                 2000 );
00254                         p->setEmitter( em );
00255                         p->setMaterialTexture( 0, driver->getTexture("../../media/particlewhite.bmp") );
00256                         p->setMaterialFlag( EMF_LIGHTING, false );
00257                         p->setMaterialType( EMT_TRANSPARENT_ADD_COLOR );
00258                         em->drop();
00259                         IParticleAffector* paf = p->createFadeOutParticleAffector();
00260                         p->addAffector( paf );
00261                         paf->drop();
00262 
00263                         return ERR_EVENT_CONSUMED;
00264                 }
00265 
00266                 case KEY_ESCAPE:
00267                 {
00268                         // quit application
00269                         isRunning = false;
00270                         return ERR_EVENT_CONSUMED;
00271                 }
00272         }
00273 
00274         return ERR_OK;
00275 }
00276 
00277 
00278 // pick up a photo at mouse x/y ("elastic strap" pick up)
00279 ERR_TYPE onMouseButtonLeftEvent( s32 x, s32 y, f32 wheel )
00280 {
00281         NxActor* tempActor = NULL;
00282 
00283         // test if zoom is active
00284         if ( NULL != myZoomedPhoto )
00285         {
00286                 myZoomedPhoto->zoomEnd();
00287                 myZoomedPhoto = NULL;
00288                 return ERR_EVENT_CONSUMED;
00289         }
00290 
00291         // test if user hit something
00292         tempActor = myMouse->actorHit( x, y );
00293         if ( NULL != tempActor )
00294         {
00295                 stringc name = tempActor->getName();
00296                 // output picked item
00297                 //stringc text( "NxActor hit: " );
00298                 //text += name;
00299                 //logger->log( text.c_str() );
00300 
00301                 // check if we picked table or photo. do not touch table
00302                 if ( name.equals_ignore_case( "photo" ) )
00303                 {
00304                         myMouse->pickActorElasticStrap( tempActor );
00305                         return ERR_EVENT_CONSUMED;
00306                 }
00307         }
00308         
00309         return ERR_OK;
00310 }
00311 
00312 
00313 // drop picked photo on table
00314 ERR_TYPE onMouseButtonLeftReleaseEvent()
00315 {
00316         // this call also checks whether something was picked or not
00317         myMouse->dropActor();
00318         return ERR_EVENT_CONSUMED;
00319 }
00320 
00321 
00322 // zoom a photo in front of camera
00323 ERR_TYPE onMouseButtonLeftDoubleClickEvent( s32 x, s32 y, f32 wheel )
00324 {
00325         NxActor* tempActor = NULL;
00326 
00327         // ensure nothing is picked right now
00328         myMouse->dropActor();
00329 
00330         // test if zoom is active
00331         if ( NULL != myZoomedPhoto )
00332         {
00333                 myZoomedPhoto->zoomEnd();
00334                 myZoomedPhoto = NULL;
00335                 return ERR_EVENT_CONSUMED;
00336         }
00337 
00338         // test if user hit something
00339         tempActor = myMouse->actorHit( x, y );
00340         if ( NULL != tempActor )
00341         {
00342                 stringc name = tempActor->getName();
00343 
00344                 // check if we hit table or photo. do not touch table
00345                 if ( name.equals_ignore_case( "photo" ) )
00346                 {
00347                         // get Photo from NxActor
00348                         myZoomedPhoto = (Photo*) tempActor->userData;
00349                         // setup zoom, done in photo's update()
00350                         vector3df camPos = myCamera->getPosition();
00351                         NxVec3 destinationPos( camPos.X, camPos.Y-10.0f, camPos.Z ); 
00352                         myZoomedPhoto->zoomStart( destinationPos, 1000 );
00353                 }
00354         }
00355 
00356         return ERR_EVENT_CONSUMED;
00357 }
00358 
00359 
00360 // pick up a photo at mouse x/y ("fixed" pick up)
00361 ERR_TYPE onMouseButtonRightEvent( s32 x, s32 y, f32 wheel )
00362 {
00363         NxActor* tempActor = NULL;
00364 
00365         // test if zoom is active
00366         if ( NULL != myZoomedPhoto )
00367         {
00368                 myZoomedPhoto->zoomEnd();
00369                 myZoomedPhoto = NULL;
00370                 return ERR_EVENT_CONSUMED;
00371         }
00372 
00373         // test if user hit something
00374         tempActor = myMouse->actorHit( x, y );
00375         if ( NULL != tempActor )
00376         {
00377                 stringc name = tempActor->getName();
00378 
00379                 // check if we picked table or photo. do not touch table
00380                 if ( name.equals_ignore_case( "photo" ) )
00381                 {
00382                         myMouse->pickActorFixed( tempActor );
00383                         return ERR_EVENT_CONSUMED;
00384                 }
00385         }
00386         
00387         return ERR_OK;
00388 }
00389 
00390 
00391 // drop picked photo on table
00392 ERR_TYPE onMouseButtonRightReleaseEvent()
00393 {
00394         // this call also checks whether something was picked or not
00395         myMouse->dropActor();
00396         return ERR_EVENT_CONSUMED;
00397 }
00398 
00399 
00400 // moves the virtual mouse pointer in 3D space from screen x/y position
00401 ERR_TYPE onMouseMoveEvent( s32 x, s32 y )
00402 {
00403         myMouse->moveTo( x, y );
00404         return ERR_EVENT_CONSUMED;
00405 }
00406 
00407 
00408 // this function is called to check if a photo should be deleted
00409 ERR_TYPE onCheckForPhotoDelete( Photo* photo )
00410 {
00411         NxVec3 pos;
00412         photo->getPosition( &pos );
00413 
00414         if ( (pos.y < -30.0f) && (pos.y > -100.0f) )
00415         {
00416                 // start OR update some fancy particels around table hole
00417                 // this also updates emitting position
00418                 photo->particelStart( stringc("../../media/particlewhite.bmp") );
00419         }
00420 
00421         if ( pos.y < -100.0f )
00422         {
00423                 // enough particels emitted
00424                 photo->particelEnd();
00425         }
00426 
00427         if ( pos.y > 0.0f )
00428         {
00429                 // if photo was below table and photo is now on table, stop particels
00430                 // i.e. user droped photo and catched it again
00431                 photo->particelEnd();
00432         }
00433 
00434         // dispose photo if its altitude is below -900 units
00435         if ( pos.y < -900.0f )
00436         {
00437                 return ERR_YES;
00438         }
00439 
00440         return ERR_NO;
00441 }

Generated on Sun Dec 2 03:10:22 2007 for TableTop by  doxygen 1.5.4