exchangegraphics.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           cargodialog.cpp  -  description
00003                              -------------------
00004     begin                : Tue Oct 24 2000
00005     copyright            : (C) 2000 by Martin Bickel
00006     email                : bickel@asc-hq.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <boost/regex.hpp>
00019 
00020 #include "../paradialog.h"
00021 #include "fileselector.h"
00022 #include "../textfile_evaluation.h"
00023 #include "../spfst.h"
00024 #include "../spfst-legacy.h"
00025 #include "../graphicset.h"
00026 #include "../widgets/dropdownselector.h"
00027 #include "exchangegraphics.h"
00028 #include "../widgets/textrenderer.h"
00029 #include "../fieldimageloader.h"
00030 #include "../itemrepository.h"
00031 
00032 template<typename T, typename U>
00033 void eraseElement( T& t, const U& u)
00034 {
00035    if ( t.find( u ) != t.end() )
00036       t.erase( t.find(u) );
00037 }
00038 
00039 
00040 class ExchangeGraphics: public ASC_PG_Dialog 
00041 {
00042       static const int dlg_width = 200;
00043 
00044       typedef map<int, Surface> OrgTerrainGFX;
00045       static OrgTerrainGFX orgTerrainGFX;
00046 
00047       typedef map<const LoadableItemType*, Surface> OrgTerrainSurf;
00048       static OrgTerrainSurf orgTerrainSurf;
00049 
00050       typedef map<int,ASCString> NewGFX;
00051       static NewGFX newGFX;
00052 
00053       typedef map<ASCString,ASCString> NewSurfaces;
00054       static NewSurfaces newSurfaces;
00055 
00056       PG_LineEdit* filename;
00057       PG_LineEdit* imageNum;
00058       PG_Label* terrain;
00059       PG_Label* object;
00060 
00061       DropDownSelector* selectedType;
00062 
00063       bool fileSelect()
00064       {
00065          ASCString fn = selectFile("*.png;*.pcx", true );
00066          if ( !fn.empty() )
00067             filename->SetText( fn );
00068          return true;
00069       }
00070 
00071       ASCString getIdentifier( const TerrainType::Weather* w)
00072       {
00073          return "T: " + w->terraintype->filename;
00074       }
00075 
00076       ASCString getIdentifier( const ObjectType* o)
00077       {
00078          return "O: " + o->filename;
00079       }
00080 
00081       void setnewgfx ( int id, const ASCString& filename)
00082       {
00083          if ( orgTerrainGFX.find( id ) == orgTerrainGFX.end() )
00084             orgTerrainGFX[id] = GraphicSetManager::Instance().getPic( id );
00085 
00086          GraphicSetManager::Instance().setPic( id, loadASCFieldImage ( filename ) );
00087          newGFX[id] = filename;
00088       }
00089 
00090       void setnewimage( TerrainType::Weather* trr, const ASCString& filename )
00091       {
00092          if ( orgTerrainSurf.find( trr ) == orgTerrainSurf.end() )
00093             orgTerrainSurf[trr] = trr->image;
00094 
00095          trr->image = loadASCFieldImage ( filename );
00096          newSurfaces[getIdentifier(trr)] = filename;
00097       }
00098 
00099       void setnewimage( ObjectType* obj, const ASCString& filename )
00100       {
00101          obj->weatherPicture[0].images = loadASCFieldImageArray(filename, obj->weatherPicture[0].images.size() );
00102          obj->displayMethod = 0;
00103          /*
00104          if ( orgTerrainSurf.find( obj ) == orgTerrainSurf.end() )
00105             orgTerrainSurf[obj] = trr->image;
00106 
00107          trr->image = loadASCFieldImage ( filename );
00108          newSurfaces[getIdentifier(trr)] = filename;
00109          */
00110       }
00111 
00112 
00113       bool apply()
00114       { 
00115          MapField* fld = actmap->getField( actmap->getCursor() );
00116          if ( fld ) {
00117             try {
00118                if ( selectedType->GetSelectedItemIndex() == 0 ) {
00119                   if ( fld->typ->bi_pict >= 0 ) {
00120                      setnewgfx( fld->typ->bi_pict, filename->GetText() );
00121                   } else {
00122                      setnewimage( fld->typ, filename->GetText() );
00123                   }
00124                }
00125                if ( selectedType->GetSelectedItemIndex() == 1 ) {
00126                   if ( fld->objects.size() )
00127                      setnewimage( objectTypeRepository.getObject_byID( fld->objects[0].typ->id ), filename->GetText() );
00128                }
00129             }
00130             catch ( ... ) {
00131                warningMessage( "operation failed");
00132             }
00133          }
00134          repaintMap();
00135 
00136          return true;
00137       }
00138 
00139       bool close()
00140       {
00141          Hide();
00142          return true;
00143       }
00144 
00145       bool restore()
00146       {
00147          MapField* fld = actmap->getField( actmap->getCursor() );
00148          if ( fld ) {
00149             if ( selectedType->GetSelectedItemIndex() == 0 ) {
00150                if ( fld->typ->bi_pict >= 0 ) {
00151                   if ( orgTerrainGFX.find(fld->typ->bi_pict) != orgTerrainGFX.end() ) {
00152                      GraphicSetManager::Instance().setPic( fld->typ->bi_pict, orgTerrainGFX[fld->typ->bi_pict] );
00153                      eraseElement( newGFX, fld->typ->bi_pict );
00154                   }
00155                } else {
00156                   if ( orgTerrainSurf.find(fld->typ) != orgTerrainSurf.end() ) {
00157                      fld->typ->image = orgTerrainSurf[fld->typ];
00158                      eraseElement( newSurfaces, getIdentifier(fld->typ) );
00159                   }
00160                }
00161             }
00162          }
00163          repaintMap();
00164          return true;
00165       }
00166 
00167       bool readFile()
00168       {
00169          ASCString filename = selectFile("*.txt", true );
00170          if ( filename.empty() )
00171             return false;
00172 
00173          try {
00174             tnfilestream stream ( filename, tnstream::reading );
00175             bool finished = false;
00176             while ( !finished ) {
00177 
00178                ASCString line;
00179                finished = !stream.readTextString( line );
00180 
00181                boost::smatch what;
00182 
00183                static boost::regex gfx( "^GFX+(\\d+) -> (\\S+)");
00184                if( boost::regex_match( line, what, gfx)) {
00185                   ASCString ids ( what[1] );
00186                   int id = atoi( ids.c_str() );
00187 
00188                   ASCString name ( what[2] );
00189                   setnewgfx( id, name );
00190                }
00191 
00192                static boost::regex trr( "^(T: \\S+) -> (\\S+)");
00193                if( boost::regex_match( line, what, trr)) {
00194                   ASCString file ( what[1] );
00195 
00196                   ASCString name ( what[2] );
00197 
00198                   for ( int i = 0; i < terrainTypeRepository.getNum(); ++i ) {
00199                      TerrainType* t = terrainTypeRepository.getObject_byPos(i);
00200                      for ( int w = 0; w < cwettertypennum; ++w)
00201                         if ( t->weather[w] )
00202                            if ( getIdentifier(t->weather[w]) == file )
00203                               setnewimage( t->weather[w], name );
00204                   }
00205                }
00206             }
00207          }
00208          catch(...) {
00209             errorMessage("an error occured");
00210          }
00211          repaintMap();
00212          return true;
00213       }
00214 
00215       bool snow()
00216       {
00217          MapField* fld = actmap->getField( actmap->getCursor() );
00218          if ( fld ) {
00219             if ( selectedType->GetSelectedItemIndex() == 0 ) {
00220                if ( fld->typ->bi_pict < 0 )
00221                   snowify( fld->typ->image );
00222             }
00223          }
00224          repaintMap();
00225 
00226          return true;
00227       }
00228 
00229 
00230       bool summary()
00231       {
00232          ASCString s;
00233          for ( NewGFX::iterator i = newGFX.begin(); i != newGFX.end(); ++i )
00234             s += "GFX" + ASCString::toString(i->first) + " -> " + i->second + "\n";
00235 
00236          for ( NewSurfaces::iterator i = newSurfaces.begin(); i != newSurfaces.end(); ++i )
00237             s += i->first + " -> " + i->second + "\n";
00238 
00239 
00240          ViewFormattedText vft("Graphics replacement summary", s, PG_Rect( -1, -1, 500, 400 ));
00241          vft.Show();
00242          vft.RunModal();
00243          return true;
00244       }
00245 
00246       void newCursorPos()
00247       {
00248          MapField* fld = actmap->getField( actmap->getCursor() );
00249          if ( !fld )
00250             return;
00251 
00252          if ( terrain ) {
00253             ASCString s = "T: ID=" + ASCString::toString( fld->typ->terraintype->id );
00254             if ( fld->typ->bi_pict >= 0 )
00255                s += " GFX=" + ASCString::toString( fld->typ->bi_pict );
00256 
00257             if ( newGFX.find( fld->typ->bi_pict ) != newGFX.end() || newSurfaces.find( getIdentifier(fld->typ) ) != newSurfaces.end() )
00258                s += " (R) ";
00259 
00260             terrain->SetText( s );
00261          }
00262 
00263          if ( object ) {
00264             if ( fld->objects.size() ) {
00265                ASCString s = "O: ID=" + ASCString::toString( fld->objects.front().typ->id );
00266                if ( fld->objects.front().typ->weatherPicture[0].bi3pic[0] >= 0 )
00267                   s += " GFX=" + ASCString::toString( fld->objects.front().typ->weatherPicture[0].bi3pic[0] );
00268                   
00269                object->SetText( s );
00270             } else
00271                object->SetText("-");
00272          }
00273 
00274       }
00275 
00276 
00277    public:
00278       ExchangeGraphics() : ASC_PG_Dialog( NULL, PG_Rect( PG_Application::GetScreenWidth() - dlg_width, -1, dlg_width, 450 ), "Exchange Graphics"), filename(NULL), imageNum(NULL),terrain(NULL), object(NULL), selectedType(NULL)
00279       {
00280 
00281          selectedType = new DropDownSelector( this, PG_Rect( 10, 30, 180, 25 ));
00282          selectedType->AddItem( "Terrain" );
00283          selectedType->AddItem( "Object" );
00284          selectedType->SelectItem( 0 );
00285 
00286          (new PG_Button( this, PG_Rect( 10, 100, 180, 30), "Select Filename"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::fileSelect ));
00287 
00288          terrain = new PG_Label( this, PG_Rect( 10, 140, 180, 25));
00289          object  = new PG_Label( this, PG_Rect( 10, 170, 180, 25));
00290 
00291 
00292          (new PG_Button( this, PG_Rect( 10, 200, 180, 30), "Apply"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::apply ));
00293          (new PG_Button( this, PG_Rect( 10, 240, 180, 30), "Restore Original"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::restore ));
00294          (new PG_Button( this, PG_Rect( 10, 280, 180, 30), "Replacement Summary"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::summary ));
00295          (new PG_Button( this, PG_Rect( 10, 320, 180, 30), "Load from file"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::readFile ));
00296          (new PG_Button( this, PG_Rect( 10, 360, 180, 30), "Close"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::close ));
00297          (new PG_Button( this, PG_Rect( 10, 400, 180, 30), "Snow"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::snow ));
00298 
00299          cursorMoved.connect( SigC::slot( *this, &ExchangeGraphics::newCursorPos ));
00300          updateFieldInfo.connect( SigC::slot( *this, &ExchangeGraphics::newCursorPos ));
00301    
00302          newCursorPos();
00303 
00304          filename = new PG_LineEdit( this, PG_Rect( 10, 65, 180, 30 ));
00305 
00306       }
00307 };
00308 
00309 ExchangeGraphics::OrgTerrainGFX  ExchangeGraphics::orgTerrainGFX;
00310 ExchangeGraphics::OrgTerrainSurf ExchangeGraphics::orgTerrainSurf;
00311 ExchangeGraphics::NewGFX ExchangeGraphics::newGFX;
00312 ExchangeGraphics::NewSurfaces ExchangeGraphics::newSurfaces;
00313 
00314 
00315 
00316 void exchangeGraphics()
00317 {
00318    static ExchangeGraphics* eg = NULL;
00319    if  ( !eg )
00320       eg = new ExchangeGraphics();
00321    eg->Show();
00322 }

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