File: exchangegraphics.cpp

package info (click to toggle)
asc 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,080 kB
  • ctags: 24,943
  • sloc: cpp: 155,023; sh: 8,829; ansic: 6,890; makefile: 650; perl: 138
file content (322 lines) | stat: -rw-r--r-- 11,285 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/***************************************************************************
                          cargodialog.cpp  -  description
                             -------------------
    begin                : Tue Oct 24 2000
    copyright            : (C) 2000 by Martin Bickel
    email                : bickel@asc-hq.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <boost/regex.hpp>

#include "../paradialog.h"
#include "fileselector.h"
#include "../textfile_evaluation.h"
#include "../spfst.h"
#include "../spfst-legacy.h"
#include "../graphicset.h"
#include "../widgets/dropdownselector.h"
#include "exchangegraphics.h"
#include "../widgets/textrenderer.h"
#include "../fieldimageloader.h"
#include "../itemrepository.h"

template<typename T, typename U>
void eraseElement( T& t, const U& u)
{
   if ( t.find( u ) != t.end() )
      t.erase( t.find(u) );
}


class ExchangeGraphics: public ASC_PG_Dialog 
{
      static const int dlg_width = 200;

      typedef map<int, Surface> OrgTerrainGFX;
      static OrgTerrainGFX orgTerrainGFX;

      typedef map<const LoadableItemType*, Surface> OrgTerrainSurf;
      static OrgTerrainSurf orgTerrainSurf;

      typedef map<int,ASCString> NewGFX;
      static NewGFX newGFX;

      typedef map<ASCString,ASCString> NewSurfaces;
      static NewSurfaces newSurfaces;

      PG_LineEdit* filename;
      PG_LineEdit* imageNum;
      PG_Label* terrain;
      PG_Label* object;

      DropDownSelector* selectedType;

      bool fileSelect()
      {
         ASCString fn = selectFile("*.png;*.pcx", true );
         if ( !fn.empty() )
            filename->SetText( fn );
         return true;
      }

      ASCString getIdentifier( const TerrainType::Weather* w)
      {
         return "T: " + w->terraintype->filename;
      }

      ASCString getIdentifier( const ObjectType* o)
      {
         return "O: " + o->filename;
      }

      void setnewgfx ( int id, const ASCString& filename)
      {
         if ( orgTerrainGFX.find( id ) == orgTerrainGFX.end() )
            orgTerrainGFX[id] = GraphicSetManager::Instance().getPic( id );

         GraphicSetManager::Instance().setPic( id, loadASCFieldImage ( filename ) );
         newGFX[id] = filename;
      }

      void setnewimage( TerrainType::Weather* trr, const ASCString& filename )
      {
         if ( orgTerrainSurf.find( trr ) == orgTerrainSurf.end() )
            orgTerrainSurf[trr] = trr->image;

         trr->image = loadASCFieldImage ( filename );
         newSurfaces[getIdentifier(trr)] = filename;
      }

      void setnewimage( ObjectType* obj, const ASCString& filename )
      {
         obj->weatherPicture[0].images = loadASCFieldImageArray(filename, obj->weatherPicture[0].images.size() );
         obj->displayMethod = 0;
         /*
         if ( orgTerrainSurf.find( obj ) == orgTerrainSurf.end() )
            orgTerrainSurf[obj] = trr->image;

         trr->image = loadASCFieldImage ( filename );
         newSurfaces[getIdentifier(trr)] = filename;
         */
      }


      bool apply()
      { 
         tfield* fld = actmap->getField( actmap->getCursor() );
         if ( fld ) {
            try {
               if ( selectedType->GetSelectedItemIndex() == 0 ) {
                  if ( fld->typ->bi_pict >= 0 ) {
                     setnewgfx( fld->typ->bi_pict, filename->GetText() );
                  } else {
                     setnewimage( fld->typ, filename->GetText() );
                  }
               }
               if ( selectedType->GetSelectedItemIndex() == 1 ) {
                  if ( fld->objects.size() )
                     setnewimage( objectTypeRepository.getObject_byID( fld->objects[0].typ->id ), filename->GetText() );
               }
            }
            catch ( ... ) {
               warningMessage( "operation failed");
            }
         }
         repaintMap();

         return true;
      }

      bool close()
      {
         Hide();
         return true;
      }

      bool restore()
      {
         tfield* fld = actmap->getField( actmap->getCursor() );
         if ( fld ) {
            if ( selectedType->GetSelectedItemIndex() == 0 ) {
               if ( fld->typ->bi_pict >= 0 ) {
                  if ( orgTerrainGFX.find(fld->typ->bi_pict) != orgTerrainGFX.end() ) {
                     GraphicSetManager::Instance().setPic( fld->typ->bi_pict, orgTerrainGFX[fld->typ->bi_pict] );
                     eraseElement( newGFX, fld->typ->bi_pict );
                  }
               } else {
                  if ( orgTerrainSurf.find(fld->typ) != orgTerrainSurf.end() ) {
                     fld->typ->image = orgTerrainSurf[fld->typ];
                     eraseElement( newSurfaces, getIdentifier(fld->typ) );
                  }
               }
            }
         }
         repaintMap();
         return true;
      }

      bool readFile()
      {
         ASCString filename = selectFile("*.txt", true );
         if ( filename.empty() )
            return false;

         try {
            tnfilestream stream ( filename, tnstream::reading );
            bool finished = false;
            while ( !finished ) {

               ASCString line;
               finished = !stream.readTextString( line );

               boost::smatch what;

               static boost::regex gfx( "^GFX+(\\d+) -> (\\S+)");
               if( boost::regex_match( line, what, gfx)) {
                  ASCString ids ( what[1] );
                  int id = atoi( ids.c_str() );

                  ASCString name ( what[2] );
                  setnewgfx( id, name );
               }

               static boost::regex trr( "^(T: \\S+) -> (\\S+)");
               if( boost::regex_match( line, what, trr)) {
                  ASCString file ( what[1] );

                  ASCString name ( what[2] );

                  for ( int i = 0; i < terrainTypeRepository.getNum(); ++i ) {
                     TerrainType* t = terrainTypeRepository.getObject_byPos(i);
                     for ( int w = 0; w < cwettertypennum; ++w)
                        if ( t->weather[w] )
                           if ( getIdentifier(t->weather[w]) == file )
                              setnewimage( t->weather[w], name );
                  }
               }
            }
         }
         catch(...) {
            errorMessage("an error occured");
         }
         repaintMap();
         return true;
      }

      bool snow()
      {
         tfield* fld = actmap->getField( actmap->getCursor() );
         if ( fld ) {
            if ( selectedType->GetSelectedItemIndex() == 0 ) {
               if ( fld->typ->bi_pict < 0 )
                  snowify( fld->typ->image );
            }
         }
         repaintMap();

         return true;
      }


      bool summary()
      {
         ASCString s;
         for ( NewGFX::iterator i = newGFX.begin(); i != newGFX.end(); ++i )
            s += "GFX" + ASCString::toString(i->first) + " -> " + i->second + "\n";

         for ( NewSurfaces::iterator i = newSurfaces.begin(); i != newSurfaces.end(); ++i )
            s += i->first + " -> " + i->second + "\n";


         ViewFormattedText vft("Graphics replacement summary", s, PG_Rect( -1, -1, 500, 400 ));
         vft.Show();
         vft.RunModal();
         return true;
      }

      void newCursorPos()
      {
         tfield* fld = actmap->getField( actmap->getCursor() );
         if ( !fld )
            return;

         if ( terrain ) {
            ASCString s = "T: ID=" + ASCString::toString( fld->typ->terraintype->id );
            if ( fld->typ->bi_pict >= 0 )
               s += " GFX=" + ASCString::toString( fld->typ->bi_pict );

            if ( newGFX.find( fld->typ->bi_pict ) != newGFX.end() || newSurfaces.find( getIdentifier(fld->typ) ) != newSurfaces.end() )
               s += " (R) ";

            terrain->SetText( s );
         }

         if ( object ) {
            if ( fld->objects.size() ) {
               ASCString s = "O: ID=" + ASCString::toString( fld->objects.front().typ->id );
               if ( fld->objects.front().typ->weatherPicture[0].bi3pic[0] >= 0 )
                  s += " GFX=" + ASCString::toString( fld->objects.front().typ->weatherPicture[0].bi3pic[0] );
                  
               object->SetText( s );
            } else
               object->SetText("-");
         }

      }


   public:
      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)
      {

         selectedType = new DropDownSelector( this, PG_Rect( 10, 30, 180, 25 ));
         selectedType->AddItem( "Terrain" );
         selectedType->AddItem( "Object" );
         selectedType->SelectItem( 0 );

         (new PG_Button( this, PG_Rect( 10, 100, 180, 30), "Select Filename"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::fileSelect ));

         terrain = new PG_Label( this, PG_Rect( 10, 140, 180, 25));
         object  = new PG_Label( this, PG_Rect( 10, 170, 180, 25));


         (new PG_Button( this, PG_Rect( 10, 200, 180, 30), "Apply"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::apply ));
         (new PG_Button( this, PG_Rect( 10, 240, 180, 30), "Restore Original"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::restore ));
         (new PG_Button( this, PG_Rect( 10, 280, 180, 30), "Replacement Summary"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::summary ));
         (new PG_Button( this, PG_Rect( 10, 320, 180, 30), "Load from file"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::readFile ));
         (new PG_Button( this, PG_Rect( 10, 360, 180, 30), "Close"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::close ));
         (new PG_Button( this, PG_Rect( 10, 400, 180, 30), "Snow"))->sigClick.connect( SigC::slot( *this, &ExchangeGraphics::snow ));

         cursorMoved.connect( SigC::slot( *this, &ExchangeGraphics::newCursorPos ));
         updateFieldInfo.connect( SigC::slot( *this, &ExchangeGraphics::newCursorPos ));
   
         newCursorPos();

         filename = new PG_LineEdit( this, PG_Rect( 10, 65, 180, 30 ));

      }
};

ExchangeGraphics::OrgTerrainGFX  ExchangeGraphics::orgTerrainGFX;
ExchangeGraphics::OrgTerrainSurf ExchangeGraphics::orgTerrainSurf;
ExchangeGraphics::NewGFX ExchangeGraphics::newGFX;
ExchangeGraphics::NewSurfaces ExchangeGraphics::newSurfaces;



void exchangeGraphics()
{
   static ExchangeGraphics* eg = NULL;
   if  ( !eg )
      eg = new ExchangeGraphics();
   eg->Show();
}