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

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

Generated on Tue Jun 24 01:27:41 2008 for Advanced Strategic Command by  doxygen 1.4.2