Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

FrontCamera.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TSGL - Teddy Space Game Library
00004     Copyright (C) 2002 Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: FrontCamera.cpp,v 1.1 2002/01/08 20:47:05 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/SpaceGame/FrontCamera.h"
00026 #include "Teddy/SpaceGame/PlayerShip.h"
00027 #include "Teddy/SpaceGame/RoamSphere.h"
00028 #include "Teddy/SpaceGame/Root.h"
00029 #include "Teddy/SpaceGame/ShipCamera.h"
00030 #include "Teddy/PhysicalComponents/WindowManager.h"
00031 #include "Teddy/PhysicalComponents/LayoutConstraint.h"
00032 #include "Teddy/PhysicalComponents/Style.h"
00033 #include "Teddy/PhysicalComponents/WindowFrame.h"
00034 #include "Teddy/PhysicalComponents/Label.h"
00035 #include "Teddy/Graphics/View.h"
00036 #include "SDL_mouse.h"
00037 using namespace Teddy::Graphics;
00038 using namespace Teddy::PhysicalComponents;
00039 
00040 
00041 namespace Teddy     {
00042 namespace SpaceGame {
00043 
00044 
00045 bool FrontCamera::keys[SDLK_LAST+1];
00046 
00047 
00049 FrontCamera::FrontCamera( const char *name, Camera *camera, Root *root, LayoutConstraint *lc, bool frame )
00050 :
00051 Projection   (name,camera),
00052 EventListener(
00053     EVENT_KEY_DOWN_M        |
00054     EVENT_KEY_UP_M          |
00055     EVENT_MOUSE_KEY_M       |
00056     EVENT_MOUSE_HOLD_DRAG_M
00057 )
00058 {
00059     int i;
00060 
00061     this->constraint  = lc;
00062     this->root        = root;
00063     this->player_ship = root->getObjectManager()->getPlayerShip();
00064 
00065     for( i=0; i<SDLK_LAST+1; i++ ){
00066         this->keys[i] = false;
00067     }
00068 
00069     if( frame ){
00070         window_frame = new WindowFrame( name );
00071         this->insert( window_frame );
00072     }else{
00073         window_frame = NULL;
00074     }
00075 
00076     title = new Label( "" );
00077     LayoutConstraint *cons = title->getLayoutConstraint();
00078     cons->local_x_offset_relative  = -0.5;
00079     cons->local_y_offset_relative  = -0.5;
00080     cons->parent_x_offset_relative =  0.5;
00081     cons->parent_y_offset_relative =  0;
00082     cons->local_y_offset_pixels    = 10;
00083     this->insert( title );
00084 
00085     for( i=0; i<4; i++ ){
00086         this->mouse_click_x[i] = 0;
00087         this->mouse_click_y[i] = 0;
00088         this->mouse_drag_x [i] = 0;
00089         this->mouse_drag_y [i] = 0;
00090     }
00091 }
00092 
00093 
00094 /*virtual*/ void FrontCamera::drawSelf(){
00095     title->setText( camera->getTitle() );
00096     title->place();
00097     Projection::drawSelf();
00098 }
00099 
00100 
00101 
00103 /*virtual*/ Area *FrontCamera::getTarget( const Event e ) const {
00104     return window_frame;
00105 }
00106 
00107 
00109 FrontCamera::~FrontCamera(){
00110     //  FIX
00111 }
00112 
00113 
00115 /*virtual*/ void FrontCamera::focusActive( const bool active ){
00116     if( active ){
00117         root->getObjectManager()->setActiveCamera( this->camera );
00118         if( window_frame!=NULL ){
00119             window_frame->setColor( Color::CYAN );
00120         }
00121     }else{
00122         if( window_frame!=NULL ){
00123             window_frame->setColor( style->frame_color );
00124         }
00125     }
00126 }
00127 
00128 
00130 /*virtual*/ void FrontCamera::mouseKey( const int button, const int state, const int x, const int y ){
00131     if( state == SDL_PRESSED ){
00132         mouse_click_x[button] = x;
00133         mouse_click_y[button] = y;
00134         mouse_drag_x [button] = x;
00135         mouse_drag_y [button] = y;
00136     }else{
00137         if( (button == SDL_BUTTON_LEFT       ) &&
00138             (state  == SDL_RELEASED          ) &&
00139             (x      == mouse_click_x[button] ) &&
00140             (y      == mouse_click_y[button] )    )
00141         {
00142             root->getObjectManager()->selectInstance( x, y );
00143         }
00144     }
00145     mouse_b[button] = state;
00146 }
00147 
00148 
00149 /*virtual*/ void FrontCamera::mouseMotion( const int x, const int y, const int dx, const int dy ){
00150     for( int b = 1; b < 4; b++ ){
00151         if( mouse_b[b] == SDL_PRESSED ){
00152             root->getEventManager()->mouseMotion( b, dx, dy );
00153         }
00154     }
00155 }
00156 
00157 
00159 /*virtual*/ void FrontCamera::keyDown( const SDL_keysym key ){
00160     ActionManager *am = root->getActionManager();
00161     EventManager  *em = root->getEventManager ();
00162     UserInterface *ui = root->getUserInterface();
00163     ObjectManager *om = root->getObjectManager();
00164     ShipCamera    *sc = root->getObjectManager()->getShipCamera();
00165 
00166     keys[ key.sym ] = true;
00167 
00168     switch( key.sym ){
00169     case '5': am->scannerCycle         (); break;
00170     case 'd': am->blendOn              (); break;
00171     case 'c': am->blendOff             (); break;
00172 //  case 'h': am->displayHelp          (); break;
00173 //  case 'e': am->displayExtensions    (); break;
00174     case 'u': am->renderModePoint      (); break;
00175     case 'i': am->renderModeLine       (); break;
00176     case 'o': am->renderModeFill       (); break;
00177     case 'p': am->renderModeFillOutline(); break;
00178     case 'v': am->cullFaceDisable      (); break;
00179     case 'b': am->cullFaceEnable       (); break;
00180     case 'f': am->depthTestDisable     (); break;
00181     case 'g': am->depthTestEnable      (); break;
00182     case 'k': am->lightingOff          (); break;
00183     case 'l': am->lightingOn           (); break;
00184     case 'n': am->fovNormal            (); break;
00185     case 'm': am->fovWide              (); break;
00186     case 'r': am->antialiseOff         (); break;
00187     case 't': am->antialiseOn          (); break;
00188     case '1': ui->toggleCamera         (); break;
00189     case '3': em->chooseMouseMode      (); break;
00190 //  case 'a': om->instanceFace         (); break;
00191 
00192 #if 0
00193     case '6': roam_update  = false; break;
00194     case '7': roam_update  = true;  break;
00195     case '8': roam_const  *= 0.9f;  break;
00196     case '9': roam_const  *= 1.1f;  break;
00197 #endif
00198 
00199     case 'q': am->quit();           break;
00200 
00201     case 'a': player_ship->controlFireWeapon( true ); break;
00202     case 'x': player_ship->controlPitchUp   ( true ); break;
00203     case 's': player_ship->controlPitchDown ( true ); break;
00204     case ',': player_ship->controlRollLeft  ( true ); break;
00205     case '.': player_ship->controlRollRight ( true ); break;
00206 
00207     case SDLK_F1:     sc->front (); break;
00208     case SDLK_F2:     sc->left  (); break;
00209     case SDLK_F3:     sc->right (); break;
00210     case SDLK_F4:     sc->rear  (); break;
00211     case SDLK_F5:     sc->top   (); break;
00212     case SDLK_F6:     sc->bottom(); break;
00213     case SDLK_SPACE:  player_ship->controlMoreSpeed( true ); break;
00214     case SDLK_TAB:    player_ship->controlLessSpeed( true ); break;
00215     case SDLK_ESCAPE: player_ship->controlStop     ( true ); break;
00216 
00217     case SDLK_KP0:
00218     case SDLK_KP1: {
00219         float roll = sc->getRoll();
00220         roll += (float)rads( 10 );
00221         sc->setRoll( roll );
00222     } break;
00223 
00224     case SDLK_KP2: {
00225         float pitch = sc->getPitch();
00226         pitch -= (float)rads( 10 );
00227         sc->setPitch( pitch );
00228     } break;
00229 
00230     case SDLK_KP3: {
00231         float roll = sc->getRoll();
00232         roll -= (float)rads( 10 );
00233         sc->setRoll( roll );
00234     } break;
00235 
00236     case SDLK_KP4: {
00237         float head = sc->getHeading();
00238         head += (float)rads( 10 );
00239         sc->setHeading( head );
00240     } break;
00241     case SDLK_KP5: {
00242         sc->setDistance( 0 );
00243     } break;
00244 
00245     case SDLK_KP6: {
00246         float head = sc->getHeading();
00247         head -= (float)rads( 10 );
00248         sc->setHeading( head );
00249     } break;
00250     case SDLK_KP7: {
00251         float roll = sc->getRoll();
00252         roll += (float)rads( 10 );
00253         sc->setRoll( roll );
00254     } break;
00255 
00256     case SDLK_KP8: {
00257         float pitch = sc->getPitch();
00258         pitch += (float)rads( 10 );
00259         sc->setPitch( pitch );
00260     } break;
00261     case SDLK_KP9: {
00262         float roll = sc->getRoll();
00263         roll -= (float)rads( 10 );
00264         sc->setRoll( roll );
00265     } break;
00266 
00267     case SDLK_KP_PERIOD  :
00268     case SDLK_KP_DIVIDE  :
00269     case SDLK_KP_MULTIPLY:  break;
00270     case SDLK_KP_MINUS   : {
00271         float distance = sc->getDistance();
00272         if( distance < sc->getShip()->getClipRadius() ){
00273             distance = sc->getShip()->getClipRadius();
00274         }
00275         distance += 4;
00276         distance *= (float)1.2;
00277         sc->setDistance( distance );
00278     } break;
00279 
00280     case SDLK_KP_PLUS   : {
00281         float distance = sc->getDistance();
00282         distance /= (float)1.2;
00283         distance -= 4;
00284         if( distance < sc->getShip()->getClipRadius() ){
00285             distance = 0;
00286         }
00287         sc->setDistance( distance );
00288     } break;
00289 
00290     case SDLK_KP_ENTER  :
00291     case SDLK_KP_EQUALS : break;
00292 
00293     default: break;
00294     }
00295 };
00296 
00297 
00298 /*virtual*/ void FrontCamera::keyUp( const SDL_keysym key ){
00299     keys[ key.sym ] = false;
00300 
00301     switch( key.sym ){
00302     case 'a':         player_ship->controlFireWeapon( false ); break;
00303     case 'x':         player_ship->controlPitchUp   ( false ); break;
00304     case 's':         player_ship->controlPitchDown ( false ); break;
00305     case ',':         player_ship->controlRollLeft  ( false ); break;
00306     case '.':         player_ship->controlRollRight ( false ); break;
00307     case SDLK_SPACE:  player_ship->controlMoreSpeed ( false ); break;
00308     case SDLK_TAB:    player_ship->controlLessSpeed ( false ); break;
00309     case SDLK_ESCAPE: player_ship->controlStop      ( false ); break;
00310     default: break;
00311     }
00312 
00313 };
00314 
00315 
00316 };  //  namespace SpaceGame
00317 };  //  namespace Teddy
00318