Advanced Strategic Command
importbi3map.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #include <sstream>
23 #include <pgimage.h>
24 
25 #include "../gameoptions.h"
26 #include "../gamemap.h"
27 #include "../paradialog.h"
28 #include "importbi3map.h"
29 #include "fileselector.h"
30 #include "../itemrepository.h"
31 #include "../loadbi3.h"
32 #include "../widgets/textrenderer.h"
33 #include "pglistboxitem.h"
34 
35 
37 
38  GameMap* actmap;
39  bool insert;
40 
41  PG_LineEdit* directory;
42  PG_ListBox* importTable;
43 
44  vector<ASCString> importTables;
45 
46 
47  bool checkBI3Path( ASCString filename )
48  {
49  filename += "mis";
50  filename += pathdelimitterstring;
51  filename += "*.dat";
52 
53  if ( exist( filename ))
54  return true;
55  else {
56  errorMessage("Could not find files in BI3 directory\nlooking for " + filename );
57  return false;
58  }
59  }
60 
61  bool ok() {
62 
63  ASCString bi3Path = directory->GetText();
64  appendbackslash( bi3Path );
65 
66  if ( !checkBI3Path( bi3Path ))
67  return false;
68 
69 
70  if ( importTable->GetSelectedIndex() < 0 ) {
71  if ( importTables.size() > 0 ) {
72  errorMessage("No import table selected" );
73  return false;
74  } else {
75  errorMessage("No import tables available. Aborting" );
76  QuitModal();
77  }
78  }
79 
80  if ( bi3Path != CGameOptions::Instance()->BI3directory ) {
83  }
84 
85  ASCString wildcard = bi3Path + "mis" + pathdelimitterstring + "*.dat" ;
86 
87  ASCString filename = selectFile( wildcard, true );
88  if ( !filename.empty() ) {
89  ASCString errorOutput;
90  try {
91  if ( insert==false ) {
92  importbattleislemap ( bi3Path, filename, terrainTypeRepository.getObject_byID(9999)->weather[0], getImportTable(), &errorOutput );
93  if ( !errorOutput.empty() ) {
94  ViewFormattedText vft( "Results of Import", errorOutput, PG_Rect(-1,-1,400,400));
95  vft.Show();
96  vft.RunModal();
97  }
98  } else {
99  int x = actmap->getCursor().x;
100  int y = actmap->getCursor().y;
101  if ( x < 0 )
102  x = 0;
103 
104  if ( y < 0 )
105  y = 0;
106  insertbattleislemap ( x, y, bi3Path, filename, getImportTable() );
107  }
108  } catch ( ASCexception ) {
109  errorMessage("importing the map failed");
110  }
111  }
112 
113  QuitModal();
114  return true;
115  }
116 
117  bool cancel() {
118  QuitModal();
119  return true;
120  }
121 
122  public:
123  ImportBI3MapDialog( GameMap* gamemap, bool insert );
124 
126  if ( importTable->GetSelectedIndex() >= 0 && importTable->GetSelectedIndex() < importTables.size() )
127  return importTables[importTable->GetSelectedIndex()];
128  else
129  return "";
130  }
131 };
132 
133 
134 ImportBI3MapDialog::ImportBI3MapDialog( GameMap* gamemap, bool insert ) : ASC_PG_Dialog( NULL, PG_Rect(-1,-1,450,370), "Import BI Map") , actmap(gamemap)
135 {
136  this->insert = insert;
137  AddStandardButton("OK")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &ImportBI3MapDialog::ok )));
138  AddStandardButton("Cancel")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &ImportBI3MapDialog::cancel )));
139 
140  int ypos = 30;
141  new PG_Label( this, PG_Rect( 20, ypos, Width()-40,20), "Directory of BI3 installation:");
142  ypos += 25;
143 
144  directory = new PG_LineEdit( this, PG_Rect( 20, ypos, Width() - 40, 20 ));
145  directory->SetText( CGameOptions::Instance()->BI3directory );
146  ypos += 40;
147 
148 
149 
150  new PG_Label( this, PG_Rect( 20, ypos, Width()-40,20), "BI3 item translation table (note that data is only read at startup of ASC):");
151  ypos += 25;
152 
153  importTable = new PG_ListBox( this, PG_Rect(20, ypos, Width() - 40, 150 ));
154 
155  importTables = getBI3ImportTables();
156  for ( vector<ASCString>::iterator i = importTables.begin(); i != importTables.end(); ++i )
157  new PG_ListBoxItem( importTable, 20, *i );
158 
159 };
160 
161 void importBI3Map( GameMap* gamemap, bool insert )
162 {
163  ImportBI3MapDialog ibi3md ( gamemap, insert );
164  ibi3md.Show();
165  ibi3md.RunModal();
166 }
167 
168 
MapCoordinate & getCursor()
Definition: gamemap.cpp:933
vector< ASCString > getBI3ImportTables()
returns the names of the import tables that are available
Definition: loadbi3.cpp:366
ImportBI3MapDialog(GameMap *gamemap, bool insert)
void insertbattleislemap(int x, int y, const ASCString &path, const ASCString &mapfilename, const ASCString &importTable)
Imports a Battle Isle map and inserts it into the current ASC map.
Definition: loadbi3.cpp:1688
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString BI3directory
Definition: gameoptions.h:223
void setChanged(bool flag=true)
Definition: gameoptions.h:272
ASCString getImportTable()
ItemRepositoryLoader< TerrainType > terrainTypeRepository("terraintype")
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
void errorMessage(const ASCString &string)
static CGameOptions * Instance()
returns the only Instance
Definition: gameoptions.cpp:38
const char * pathdelimitterstring
Definition: unix/fileio.cpp:24
void appendbackslash(char *string)
Definition: basestrm.cpp:2596
ASCString selectFile(const ASCString &ext, bool load, bool overwriteMessage)
void importBI3Map(GameMap *gamemap, bool insert)
GameMap * actmap
Definition: spfst.cpp:64
bool exist(const ASCString &s)
does a file s exist (wildcards allowed)
Definition: basestrm.cpp:2444
PG_Button * AddStandardButton(const ASCString &name)
Definition: paradialog.cpp:516
void importbattleislemap(const ASCString &path, const ASCString &mapfilename, TerrainType::Weather *trrn, const ASCString &importTable, ASCString *errorOutput, bool fakemap)
Imports a Battle Isle map.
Definition: loadbi3.cpp:1666
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182