mainscreenwidget.cpp

Go to the documentation of this file.
00001 
00002 /***************************************************************************
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  ***************************************************************************/
00010 
00011 
00012 #include "global.h"
00013 
00014 #include <stdio.h>
00015 #include <cstring>
00016 #include <stdlib.h>
00017 #include <new>
00018 #include <cstdlib>
00019 #include <ctype.h>
00020 #include <algorithm>
00021 #include <memory>
00022 #include <SDL_image.h>
00023 #include <SDL_mixer.h>
00024 
00025 #include "paradialog.h"
00026 
00027 #include "mainscreenwidget.h"
00028 #include "misc.h"
00029 #include "iconrepository.h"
00030 #include "graphics/blitter.h"
00031 #include "graphics/drawing.h"
00032 #include "overviewmappanel.h"
00033 #include "spfst.h"
00034 #include "itemrepository.h"
00035 #include "mapdisplay.h"
00036 #include "events.h"
00037 
00038 
00039 
00040 MainScreenWidget::StandardActionLocker::StandardActionLocker( MainScreenWidget* mainScreenWidget, int options ) : widget( mainScreenWidget ), locked(false)
00041 {
00042    this->options = options;
00043    lock();
00044 };
00045 
00046 MainScreenWidget::StandardActionLocker::StandardActionLocker( const StandardActionLocker& locker ) : widget(NULL), locked(false)
00047 {
00048    widget  = locker.widget;
00049    options = locker.options;
00050    lock();
00051 }
00052 
00053 
00054 
00055 void MainScreenWidget::StandardActionLocker::lock()
00056 {
00057    if ( !widget )
00058       return;
00059 
00060    widget->lockStandardActions(1, options );
00061    locked = true;
00062 };
00063 
00064 void MainScreenWidget::StandardActionLocker::unlock()
00065 {
00066    if ( !widget )
00067       return;
00068 
00069    if ( locked ) {
00070       widget->lockStandardActions(-1);
00071       locked = false;
00072    }
00073 };
00074 
00075 MainScreenWidget::StandardActionLocker::~StandardActionLocker()
00076 {
00077    unlock();
00078 };
00079 
00080 
00081 MainScreenWidget::MainScreenWidget( PG_Application& application )
00082               : PG_Widget(NULL, PG_Rect ( 0, 0, app.GetScreen()->w, app.GetScreen()->h ), false),
00083               app ( application ) , lastMessageTime(0), lastMouseScrollTime(0), overviewMapPanel(NULL), lockOptions(0), messageLine(NULL)
00084 {
00085 }
00086 
00087 
00088 void MainScreenWidget::setup( bool messageLine, const PG_Rect& mapView )
00089 {
00090    displayLogMessage ( 5, "MainScreenWidget: initializing panels:\n");
00091 
00092    dataLoaderTicker();
00093 
00094    displayLogMessage ( 7, "  Mapdisplay ");
00095    mapDisplay = new MapDisplayPG( this, mapView );
00096    mapDisplay->SetID( ASC_PG_App::mapDisplayID );
00097    dataLoaderTicker();
00098 
00099    SetID( ASC_PG_App::mainScreenID );
00100 
00101    displayLogMessage ( 5, "done\nMainScreenWidget completed\n");
00102    dataLoaderTicker();
00103 
00104    repaintDisplay.connect ( SigC::bind( SigC::slot( *this, &MainScreenWidget::Update ), true ));
00105    
00106    buildBackgroundImage( messageLine );
00107    dataLoaderTicker();
00108 
00109    PG_Application::GetApp()->sigAppIdle.connect( SigC::slot( *this, &MainScreenWidget::idleHandler ));
00110 
00111    mapChanged.connect( SigC::slot( OverviewMapHolder::clearmap ));
00112    dataLoaderTicker();
00113    
00114    MessagingHub::Instance().statusInformation.connect( SigC::slot( *this, &MainScreenWidget::displayMessage ));
00115    MessagingHub::Instance().messageWindowFactory.connect( SigC::slot( *this, &MainScreenWidget::createStatusWindow ));
00116 }
00117 
00118 
00119 
00120 void MainScreenWidget :: activateMapLayer( const ASCString& name, bool active )
00121 {
00122    if ( mapDisplay )
00123       mapDisplay->activateMapLayer( name , active );
00124 }
00125 
00126 void MainScreenWidget :: toggleMapLayer( const ASCString& name )
00127 {
00128    if ( mapDisplay )
00129       mapDisplay->toggleMapLayer( name );
00130 }
00131 
00132 
00133 bool MainScreenWidget :: idleHandler( PG_MessageObject* msgObj )
00134 {
00135    if ( ticker > lastMessageTime + 500 ) {
00136       displayMessage( "" );
00137       lastMessageTime = 0xfffffff;
00138    }
00139 
00140    mouseScrollChecker();
00141    return true;
00142 }
00143 
00144 void MainScreenWidget :: mouseScrollChecker()
00145 {
00146    if ( getPGApplication().isFullscreen() && IsMouseInside() ) {
00147 
00148       if ( ticker > lastMouseScrollTime + 30 ) {
00149          int x,y;
00150          SDL_GetMouseState( &x, &y);
00151    
00152          if ( y <= 0 ) {
00153             if ( x <= 0 ) {
00154                mapDisplay->scrollMap( 7 );
00155                return;
00156             }
00157 
00158             if ( x >= PG_Application::GetScreenWidth() - 1 ) {
00159                mapDisplay->scrollMap( 1 );
00160                return;
00161             } 
00162 
00163             mapDisplay->scrollMap(0);
00164             return;
00165          }
00166 
00167          if ( y >= PG_Application::GetScreenHeight() - 1 ) {
00168             if ( x <= 0 ) {
00169                mapDisplay->scrollMap( 5 );
00170                return;
00171             }
00172 
00173             if ( x >= PG_Application::GetScreenWidth() - 1 ) {
00174                mapDisplay->scrollMap( 3 );
00175                return;
00176             }
00177 
00178             mapDisplay->scrollMap(4);
00179             return;
00180          }
00181 
00182          if ( x >= PG_Application::GetScreenWidth() - 1 ) {
00183             mapDisplay->scrollMap( 2 );
00184             return;
00185          }
00186 
00187          if ( x <= 0 ) {
00188             mapDisplay->scrollMap( 6 );
00189             return;
00190          }
00191       }
00192    }
00193 }
00194 
00195 
00196 
00197 void MainScreenWidget::buildBackgroundImage( bool messageLine )
00198 {
00199    if ( !backgroundImage.valid() ) {
00200       backgroundImage = Surface::createSurface( Width(), Height(), 32 );
00201       
00202       Surface& source = IconRepository::getIcon( getBackgroundImageFilename() );
00203       
00204       MegaBlitter< gamemapPixelSize, gamemapPixelSize, ColorTransform_None,ColorMerger_PlainOverwrite,SourcePixelSelector_DirectZoom> blitter;
00205       blitter.setSize( source.w(), source.h(), Width(), Height(), false );
00206       
00207       dataLoaderTicker();
00208       blitter.blit( source, backgroundImage, SPoint(0,0) );
00209 
00210       dataLoaderTicker();
00211             
00212       assert( mapDisplay );
00213       PG_Rect r = *mapDisplay;
00214       
00215       const int borderWidth = 5;
00216       for ( int i = 0; i <= borderWidth; ++i ) 
00217          rectangle<4> ( backgroundImage, SPoint(r.x-i, r.y-i), r.w+2*i, r.h+2*i, ColorMerger_Brightness<4>( 0.6 ), ColorMerger_Brightness<4>( 1.5 ));
00218 
00219       int x1 = r.x + 1;
00220       int y1 = r.y + 1;
00221       int x2 = r.x + r.Width() - 1;
00222       int y2 = r.y + r.Height() -1;
00223 
00224       blitRects[0] = PG_Rect(  0, 0, Width(), y1 ); 
00225       blitRects[1] = PG_Rect(  0, y1, x1,     y2 ); 
00226       blitRects[2] = PG_Rect( x2, y1, Width() - x2, y2 ); 
00227       blitRects[3] = PG_Rect( 0, y2, Width(), Height() - y2 );
00228       
00229       if ( messageLine ) {
00230       
00231          Surface& msgstart = IconRepository::getIcon("msgline1.png");
00232          Surface& msgmid   = IconRepository::getIcon("msgline2.png");
00233          Surface& msgend   = IconRepository::getIcon("msgline3.png");
00234    
00235          dataLoaderTicker();
00236          
00237          int mx1 = x1 - borderWidth;
00238          int mx2 = x2 + borderWidth;
00239          int my1 = y2 + 10;
00240          
00241          int msglength = mx2 - mx1;
00242          int midlength = msglength - msgend.w();
00243    
00244          backgroundImage.Blit( msgstart, SPoint( mx1, my1 ));
00245    
00246          int x = msgstart.w();
00247          while ( x + msgmid.w() < midlength ) {
00248             backgroundImage.Blit( msgmid, SPoint( mx1 + x, my1 ));
00249             x += msgmid.w();
00250          }
00251          dataLoaderTicker();
00252          backgroundImage.Blit( msgmid, SPoint( mx1 + msglength - msgend.w() - msgmid.w(), my1 ));
00253          
00254          dataLoaderTicker();
00255          backgroundImage.Blit( msgend, SPoint( mx1 + msglength - msgend.w(), my1) );
00256    
00257          this->messageLine = new PG_Label ( this, PG_Rect( mx1 + 20, my1 + 9, msglength - 30, msgend.h() - 18), "", "ThemeWidget" );
00258          this->messageLine->SetFontSize(11);
00259          dataLoaderTicker();
00260       }
00261          
00262    }
00263 }
00264 
00265 void MainScreenWidget::displayMessage( const ASCString& message )
00266 {
00267    if ( messageLine ) {
00268       messageLine->SetText( message );
00269       lastMessageTime = ticker;
00270    }   
00271 }
00272 
00273 
00274 
00275 void MainScreenWidget::eventBlit (SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst) 
00276 {
00277    SDL_Rect dstrect;
00278    Surface s = Surface::Wrap( PG_Application::GetScreen() );
00279    dstrect.x = blitRects[0].x;
00280    dstrect.y = blitRects[0].y;
00281    s.Blit( backgroundImage, blitRects[0], dstrect );
00282    
00283    dstrect.x = blitRects[1].x;
00284    dstrect.y = blitRects[1].y;
00285    s.Blit( backgroundImage, blitRects[1], dstrect );
00286    
00287    dstrect.x = blitRects[2].x;
00288    dstrect.y = blitRects[2].y;
00289    s.Blit( backgroundImage, blitRects[2], dstrect );
00290    
00291    dstrect.x = blitRects[3].x;
00292    dstrect.y = blitRects[3].y;
00293    s.Blit( backgroundImage, blitRects[3], dstrect );
00294 }
00295 
00296 StatusMessageWindowHolder MainScreenWidget::createStatusWindow( const ASCString& msg )
00297 {
00298    return StatusMessageWindowHolder( new PG_StatusWindowData( msg ));
00299 }
00300 
00301 void MainScreenWidget::spawnOverviewMapPanel ( const ASCString& panelName )
00302 {
00303    assert( mapDisplay);
00304    overviewMapPanel = new OverviewMapPanel( this, PG_Rect(Width()-170, 0, 170, 160), mapDisplay, panelName );
00305    overviewMapPanel->Show();
00306 }
00307 
00308 

Generated on Mon May 21 01:26:34 2012 for Advanced Strategic Command by  doxygen 1.5.1