Advanced Strategic Command
common.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
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 "../sg.h"
23 #include "../ascstring.h"
24 #include "../vehicle.h"
25 #include "../gamemap.h"
26 #include "../spfst.h"
27 #include "../spfst-legacy.h"
28 
29 #include "../itemrepository.h"
30 #include "../dlg_box.h"
31 #include "../paradialog.h"
32 #include "../gameeventsystem.h"
33 #include "../loaders.h"
34 #include "../accessconstraints.h"
35 
36 #include <pgpropertyfield_integer.h>
37 #include <pgpropertyfield_intdropdown.h>
38 #include <pgpropertyfield_checkbox.h>
39 #include <pgpropertyfield_string.h>
40 #include <pgpropertyfield_button.h>
41 #include <pgpropertyeditor.h>
42 
43 #include "common.h"
44 
46 {
47  return actmap;
48 }
49 
50 const ObjectType* getObjectType( int id )
51 {
52  return objectTypeRepository.getObject_byID( id );
53 }
54 
55 const BuildingType* getBuildingType( int id )
56 {
57  return buildingTypeRepository.getObject_byID( id );
58 }
59 
60 const VehicleType* getUnitType( int id )
61 {
62  return vehicleTypeRepository.getObject_byID( id );
63 }
64 
65 const TerrainType* getTerrainType( int id )
66 {
67  return terrainTypeRepository.getObject_byID( id );
68 }
69 
70 
71  bool PropertyDialog :: ok() {
72  result = true;
73  propertyEditor->Apply();
74  QuitModal();
75  return true;
76  }
77 
78  bool PropertyDialog :: cancel() {
79  result = false;
80  QuitModal();
81  return true;
82  }
83 
84 
85  void PropertyDialog :: setup()
86  {
87  propertyEditor = new ASC_PropertyEditor( this, PG_Rect( 10, GetTitlebarHeight()+10, Width() - 20, Height() - GetTitlebarHeight() - 60 ), "PropertyEditor", 70 );
88 
90  AddStandardButton("OK")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PropertyDialog::ok )));
91  AddStandardButton("Cancel")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PropertyDialog::cancel )));
92  }
93 
94 
95  PropertyDialog :: PropertyDialog( const ASCString& title ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 400, 500 ), title ), propertyEditor(NULL), result(false)
96  {
97  setup();
98  }
99 
100  PropertyDialog :: PropertyDialog( const ASCString& title, const PG_Rect& pos ) : ASC_PG_Dialog( NULL, pos, title ), propertyEditor(NULL), result(false)
101  {
102  setup();
103  }
104 
105 
106  void PropertyDialog :: addBool( const ASCString& name, bool defaultValue )
107  {
108  boolValues[name] = defaultValue ;
109  new PG_PropertyField_Checkbox<bool>( propertyEditor, name, &boolValues[name] );
110  }
111 
112  void PropertyDialog :: addInteger( const ASCString& name, int defaultValue )
113  {
114  intValues[name] = defaultValue;
115  new PG_PropertyField_Integer<int>( propertyEditor, name, &intValues[name] );
116  }
117 
118  void PropertyDialog :: addIntDropdown( const ASCString& name, const StringArray& names, int defaultValue )
119  {
120  intValues[name] = defaultValue;
121  new PG_PropertyField_IntDropDown<int,vector<ASCString>::const_iterator>( propertyEditor, name, &intValues[name], names.values.begin(), names.values.end() );
122  }
123 
124  void PropertyDialog :: addString( const ASCString& name, const ASCString& defaultValue )
125  {
126  stringValues[name] = defaultValue ;
127  new PG_PropertyField_String<ASCString>( propertyEditor, name, &stringValues[name] );
128  }
129 
130  std::string PropertyDialog :: getString( const ASCString& name )
131  {
132  return stringValues[name];
133  }
134 
136  {
137  return intValues[name];
138  }
139 
141  {
142  return boolValues[name];
143  }
144 
146  Show();
147  RunModal();
148  Hide();
149  return result;
150  }
151 
152 
153 int selectString ( const ASCString& title, const StringArray& entries, int defaultEntry )
154 {
155  vector<ASCString> buttons;
156  buttons.push_back("OK" );
157  buttons.push_back("Cancel" );
158 
159  pair<int,int> r = new_chooseString ( title, entries.values, buttons, defaultEntry );
160  if ( r.first == 1 )
161  return -1;
162  else
163  return r.second;
164 }
165 
167 {
168  return eventLocalizationMap;
169 }
170 
171 
172 void setLocalizedEventMessage( GameMap* eventLocalizationMap, int eventID, const ASCString& message )
173 {
174  if ( !eventLocalizationMap )
175  return;
176 
177  for ( GameMap::Events::const_iterator i = eventLocalizationMap->events.begin(); i != eventLocalizationMap->events.end(); ++i )
178  if ( (*i)->id == eventID )
179  (*i)->action->setLocalizationString( message );
180 }
181 
182 void setLocalizedContainerName( GameMap* map, const MapCoordinate& pos, const std::string& name )
183 {
184  if ( !map )
185  return;
186 
187  MapField* fld = map->getField( pos );
188  if ( !fld )
189  return;
190 
191  ContainerBase* c = fld->getContainer();
192  if ( !c )
193  return;
194 
195  if ( !checkModificationConstraints( c ))
196  return;
197 
198  c->setName( name );
199 }
200 
201 
202 
204 {
205  if ( gamemap )
206  return gamemap->getCursor();
207  else
208  return MapCoordinate(-1,-1);
209 }
210 
211 void setCursorPosition( const GameMap* gamemap, const MapCoordinate& pos )
212 {
213  if ( gamemap && pos.valid() )
214  if ( pos.x < gamemap->xsize && pos.y < gamemap->ysize )
215  gamemap->getCursor() = pos;
216 }
217 
218 void assertSuccess( const ActionResult& result )
219 {
220  if ( result.successful() )
221  throw ASCmsgException(result.getMessage() );
222 }
GameMap * getActiveMap()
Definition: common.cpp:45
The type of a field.
Definition: terraintype.h:75
ItemRepositoryLoader< ObjectType > objectTypeRepository("objecttype")
MapCoordinate & getCursor()
Definition: gamemap.cpp:933
int xsize
the size of the map
Definition: gamemap.h:201
void setLocalizedContainerName(GameMap *map, const MapCoordinate &pos, const std::string &name)
Definition: common.cpp:182
MapCoordinate getCursorPosition(const GameMap *gamemap)
Definition: common.cpp:203
bool valid() const
Definition: typen.h:221
bool checkModificationConstraints(const ContainerBase *container)
checks if a modification is allowed under the current circumstances A player may for example not alte...
void StandardButtonDirection(StandardButtonDirectonType dir)
Definition: paradialog.cpp:511
Events events
Definition: gamemap.h:291
ItemRepositoryLoader< BuildingType > buildingTypeRepository("buildingtype")
GameMap * getLoadingMap()
Definition: common.cpp:166
ContainerBase * getContainer()
returns a pointer to the ContainerBase of the field or NULL if there is none
Definition: mapfield.cpp:331
a single field of the map
Definition: mapfield.h:26
const VehicleType * getUnitType(int id)
Definition: common.cpp:60
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString getMessage() const
void setLocalizedEventMessage(GameMap *eventLocalizationMap, int eventID, const ASCString &message)
Definition: common.cpp:172
const ObjectType * getObjectType(int id)
Definition: common.cpp:50
std::string getString(const ASCString &name)
Definition: common.cpp:130
vector< ASCString > values
Definition: common.h:44
const TerrainType * getTerrainType(int id)
Definition: common.cpp:65
int selectString(const ASCString &title, const StringArray &entries, int defaultEntry)
Definition: common.cpp:153
void addInteger(const ASCString &name, int defaultValue)
Definition: common.cpp:112
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
void assertSuccess(const ActionResult &result)
Definition: common.cpp:218
The class describing properties that are common to all buildings of a certain kind.
Definition: buildingtype.h:35
int getInteger(const ASCString &name)
Definition: common.cpp:135
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
void addBool(const ASCString &name, bool defaultValue)
Definition: common.cpp:106
ItemRepositoryLoader< TerrainType > terrainTypeRepository("terraintype")
An object that can be placed on fields. Roads, pipelines and ditches are examples of objects...
Definition: objecttype.h:30
Coordinate on the twodimensional map.
Definition: typen.h:202
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
void addString(const ASCString &name, const ASCString &defaultValue)
Definition: common.cpp:124
void setCursorPosition(const GameMap *gamemap, const MapCoordinate &pos)
Definition: common.cpp:211
const BuildingType * getBuildingType(int id)
Definition: common.cpp:55
bool successful() const
pair< int, int > new_chooseString(const ASCString &title, const vector< ASCString > &entries, const vector< ASCString > &buttons, int defaultEntry)
Definition: paradialog.cpp:712
GameMap * eventLocalizationMap
used by setLocalizedEventMessage to get the map being loaded It's not nice having a global variable f...
Definition: loaders.cpp:1013
int ysize
Definition: gamemap.h:201
void addIntDropdown(const ASCString &name, const StringArray &names, int defaultValue)
Definition: common.cpp:118
GameMap * actmap
Definition: spfst.cpp:64
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
bool run()
Definition: common.cpp:145
PG_Button * AddStandardButton(const ASCString &name)
Definition: paradialog.cpp:516
bool getBool(const ASCString &name)
Definition: common.cpp:140
virtual void setName(const ASCString &name)
PropertyDialog(const ASCString &title)
Definition: common.cpp:95
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465