Advanced Strategic Command
newmap.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 #include <sstream>
22 #include <pgimage.h>
23 #include <pgpropertyeditor.h>
24 #include <pgpropertyfield_integer.h>
25 #include <pgpropertyfield_string.h>
26 #include <pgpropertyfield_checkbox.h>
27 #include <pgpropertyfield_button.h>
28 #include <pgpropertyfield_intdropdown.h>
29 
30 #include "newmap.h"
31 #include "editmapparam.h"
32 #include "../gamemap.h"
33 #include "../paradialog.h"
34 #include "../itemrepository.h"
35 #include "../edselfnt.h"
36 #include "../edgen.h"
37 
38 
39 class NewMap: public ASC_PG_Dialog {
40  private:
41  PG_PropertyEditor* properties;
42  GameMap* gamemap;
43  bool create;
44  int xsize,ysize;
45  bool random;
46  PG_Button* terrainButton;
47  int terrainid;
48  bool success;
49 
50 
51  bool selectTerrain() {
52  selectItemID( terrainid, terrainTypeRepository );
53  updateButton();
54  return true;
55  }
56 
57  void updateButton()
58  {
59  TerrainType::Weather* w = terrainTypeRepository.getObject_byID( terrainid)->weather[0];
60  if ( w )
61  terrainButton->SetIcon( w->image.getBaseSurface() );
62  else
63  terrainButton->SetIcon( NULL );
64  }
65 
66 
67  bool ok()
68  {
69  if ( !properties->Apply() )
70  return false;
71 
72 
73  if ( ysize & 1 ) {
74  warningMessage( "Height must be an even number!" );
75  return false;
76  }
77 
78 
79  if ( create ) {
80  TerrainType::Weather* w = terrainTypeRepository.getObject_byID( terrainid)->weather[0];
81  if ( !w ) {
82  warningMessage( "please choose a terrain!" );
83  return false;
84  }
85 
86  gamemap->allocateFields( xsize, ysize, w );
87 
88  if ( random)
89  mapgenerator();
90 
91  }
92  success = true;
93 
94  QuitModal();
95 
96  return true;
97  }
98 
99  bool cancel()
100  {
101  if ( create ) {
102  delete gamemap;
103  gamemap = NULL;
104  }
105 
106  QuitModal();
107  return true;
108  }
109 
110  public:
111  NewMap( GameMap* map ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 500, 400 ), "Map Properties" ), terrainid( 2998 ), success(false)
112  {
113  if ( map ) {
114  create = false;
115  gamemap = map;
116  } else {
117  create = true;
118  gamemap = new GameMap;
119  }
120 
121  int ypos = 40;
122 
123  xsize = 10;
124  ysize = 20;
125  random = false;
126 
127  properties = new ASC_PropertyEditor( this, PG_Rect( 20 , ypos, Width()-20, Height() - ypos - 60 ));
128  new PG_PropertyField_String<ASCString>( properties, "Title", &gamemap->maptitle );
129  if ( create ) {
130  new PG_PropertyField_Integer<int>( properties, "Width", &xsize );
131  new PG_PropertyField_Integer<int>( properties, "Height", &ysize );
132  new PG_PropertyField_Checkbox<bool>( properties, "Random", &ysize );
133  }
134 
135  new PG_PropertyField_Checkbox<bool>( properties, "Campaign", &gamemap->campaign.avail );
136  new PG_PropertyField_Integer<int>( properties, "Map ID", &gamemap->campaign.id );
137  new PG_PropertyField_Checkbox<bool>( properties, "Direct Access", &gamemap->campaign.directaccess );
138  (new PG_PropertyField_String<ASCString>( properties, "Password", &gamemap->codeWord )); //->SetPassHidden('*');
139  terrainButton = (new PG_PropertyField_Button( properties, "Terrain", "", 50 ))->GetButton();
140  terrainButton->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &NewMap::selectTerrain )));
141 
142  (new PG_PropertyField_Integer<int>( properties, "Wind Speed", &gamemap->weather.windSpeed ))->SetRange(0,255);
143 
144  static const char* directionNames[7] = { "South", "SouthWest", "NorthWest", "North", "NorthEast", "SouthEast", NULL };
145 
146  new PG_PropertyField_IntDropDown<int>( properties, "Wind Direction", &gamemap->weather.windDirection, directionNames );
147  new PG_PropertyField_String<ASCString>( properties, "Native Event Language", &gamemap->nativeMessageLanguage );
148 
149  updateButton();
150 
151  ypos += 200;
152 
154 
155  AddStandardButton ( "Cancel" )->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &NewMap::cancel )));
156  AddStandardButton ( "OK" )->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &NewMap::ok )));
157 
158  };
159 
161  {
162  if ( !success )
163  return NULL;
164 
165  GameMap* res = gamemap;
166  gamemap = NULL;
167  return res;
168  }
169 
171  {
172  if ( create ) {
173  delete gamemap;
174  gamemap = NULL;
175  }
176  }
177 };
178 
179 
180 
181 
183 {
184  NewMap nm(NULL);
185  nm.Show();
186  nm.RunModal();
187  return nm.GetResult();
188 }
189 
190 void editMap( GameMap* map )
191 {
192  NewMap nm(map);
193  nm.Show();
194  nm.RunModal();
195 }
~NewMap()
Definition: newmap.cpp:170
int id
an identification for identifying a map in the chain of maps that make up a campaign ...
Definition: gamemap.h:224
ASCString codeWord
the codeword for accessing a map in a campaign
Definition: gamemap.h:210
NewMap(GameMap *map)
Definition: newmap.cpp:111
void StandardButtonDirection(StandardButtonDirectonType dir)
Definition: paradialog.cpp:511
void warningMessage(const ASCString &str)
int mapgenerator(void)
Definition: edgen.cpp:858
SDL_Surface * getBaseSurface()
Definition: surface.h:116
struct GameMap::Weather weather
ItemRepositoryLoader< TerrainType > terrainTypeRepository("terraintype")
Surface image
the image of the field
Definition: terraintype.h:103
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
void allocateFields(int x, int y, TerrainType::Weather *terrain=NULL)
Definition: gamemap.cpp:982
bool avail
is this a campaign map?
Definition: gamemap.h:221
bool directaccess
can the map be loaded just by knowing its filenmae? If 0, the codeword is required ...
Definition: gamemap.h:227
bool selectItemID(int &id, const ItemRepository< ItemType > &itemRepository)
opens a window in which the user can select an item of the given type ( template parameter ItemType )...
Definition: edselfnt.h:210
GameMap * GetResult()
Definition: newmap.cpp:160
ASCString maptitle
the title of the map
Definition: gamemap.h:213
GameMap * createNewMap()
Definition: newmap.cpp:182
void editMap(GameMap *map)
Definition: newmap.cpp:190
ASCString nativeMessageLanguage
the language string that describes the messages which are saved in the game events For example: en_US...
Definition: gamemap.h:432
PG_Button * AddStandardButton(const ASCString &name)
Definition: paradialog.cpp:516
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
struct GameMap::Campaign campaign