Advanced Strategic Command
editmapparam.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 
24 #include "selectionwindow.h"
25 #include "sigc++/adaptors/retype.h"
26 #include "editmapparam.h"
27 #include "fileselector.h"
28 
29 #include <pgpropertyeditor.h>
30 #include <pgpropertyfield_integer.h>
31 #include <pgpropertyfield_checkbox.h>
32 
33 
34 #include "../textfileparser.h"
35 #include "../textfile_evaluation.h"
36 
37 
38 
39 
40 void GameParameterEditorWidget :: runTextIO ( PropertyContainer& pc )
41 {
42  for ( int i = 0; i< gameparameternum; ++i ) {
43  if ( !gameParameterSettings[i].legacy ) {
44  pc.addInteger( gameParameterSettings[i].name, values[i], values[i] );
45  if ( pc.isReading() ) {
46  if ( ! (values[i] >= gameParameterSettings[i].minValue && values[i] <= gameParameterSettings[i].maxValue ))
47  values[i] = gameParameterSettings[i].defaultValue;
48  }
49  }
50  }
51 
52 }
53 
54 
55 
57 {
58  ASCString filename = selectFile( fileNamePattern, true );
59  if ( !filename.empty() ) {
60  tnfilestream s ( filename, tnstream::reading );
61 
62  TextFormatParser tfp ( &s );
63  auto_ptr<TextPropertyGroup> tpg ( tfp.run());
64 
65  PropertyReadingContainer pc ( blockName, tpg.get() );
66 
67  runTextIO( pc );
68  propertyEditor->Reload();
69  return true;
70  }
71  return false;
72 }
73 
75 {
76  ASCString filename = selectFile( fileNamePattern, false );
77  if ( !filename.empty() ) {
78  tn_file_buf_stream s ( filename, tnstream::writing );
80  runTextIO( pc );
81  return true;
82  }
83  return false;
84 }
85 
87 {
88  for ( int i = 0; i< gameparameternum; ++i )
89  values[i] = gameParameterSettings[i].defaultValue;
90  propertyEditor->Reload();
91  return true;
92 }
93 
94 GameParameterEditorWidget :: GameParameterEditorWidget ( GameMap* gamemap, PG_Widget* parent, const PG_Rect& rect ) : PG_Widget( parent, rect ), actmap ( gamemap )
95 {
96  SetTransparency(255);
97 
98  propertyEditor = new PG_PropertyEditor( this, PG_Rect( 0,0, rect.Width() - 110, rect.Height() ), "PropertyEditor", 70 );
99 
100  PG_Button* load = new PG_Button( this, PG_Rect( rect.Width() - 100, 0, 100, 30 ), "Load" );
101  load->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &GameParameterEditorWidget::LoadParameter )));
102 
103  PG_Button* save = new PG_Button( this, PG_Rect( rect.Width() - 100, 40, 100, 30 ), "Save" );
104  save->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &GameParameterEditorWidget::SaveParameter )));
105 
106  PG_Button* def = new PG_Button( this, PG_Rect( rect.Width() - 100, 80, 100, 30 ), "Default" );
107  def->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &GameParameterEditorWidget::ResetParameter )));
108 
109 
110  for ( int i = 0; i< gameparameternum; ++i ) {
111  values[i] = actmap->getgameparameter ( GameParameter(i) );
112  if ( values[i] < gameParameterSettings[i].minValue || values[i] > gameParameterSettings[i].maxValue )
113  values[i] = gameParameterSettings[i].defaultValue;
114 
115  if ( !gameParameterSettings[i].legacy ) {
116  if ( gameParameterSettings[i].minValue == 0 && gameParameterSettings[i].maxValue == 1 ) {
117  new PG_PropertyField_Checkbox<int>( propertyEditor , gameParameterSettings[i].longName, &values[i] );
118  } else {
119  PG_PropertyField_Integer<int>* ip = new PG_PropertyField_Integer<int>( propertyEditor , gameParameterSettings[i].longName, &values[i] );
120  ip->SetRange( gameParameterSettings[i].minValue, gameParameterSettings[i].maxValue );
121  }
122  }
123  }
124 };
125 
127 {
128  return propertyEditor->Valid( true );
129 }
130 
132 {
133  bool res = propertyEditor->Apply();
134  for ( int i = 0; i< gameparameternum; ++i )
135  actmap->setgameparameter ( GameParameter(i), values[i] );
136  return res;
137 }
138 
139 
140 const char* GameParameterEditorWidget :: fileNamePattern = "*.asc.gameparam";
141 const char* GameParameterEditorWidget :: blockName = "GameParam";
142 
143 
146 
147  bool ok()
148  {
149  if ( gpew->Valid() ) {
150  gpew->Apply();
151  quitModalLoop(0);
152 
153  return true;
154  } else
155  return false;
156  }
157 
158  public:
159  EditMapParameters( GameMap* actmap, PG_Widget* parent ) : ASC_PG_Dialog( parent, PG_Rect( -1, -1, 750, 500 ), "Edit Map Parameters")
160  {
161  gpew = new GameParameterEditorWidget ( actmap, this, PG_Rect( 10, GetTitlebarHeight(), Width() - 20, Height() - GetTitlebarHeight() - 40 ));
162  PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 100, Height() - 40, 90, 30), "OK" );
163  ok->sigClick.connect( sigc::hide(sigc::mem_fun( *this, &EditMapParameters::ok )));
164  };
165 
166 };
167 
168 
169 void setmapparameters ( GameMap* gamemap )
170 {
171  EditMapParameters emp( gamemap, NULL );
172  emp.Show();
173  emp.RunModal();
174 }
const int gameparameternum
The number of game paramters that can be specified for each map.
Definition: gamemap.h:75
TextPropertyGroup * run()
void setgameparameter(GameParameter num, int value)
Definition: gamemap.cpp:1058
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void setmapparameters(GameMap *gamemap)
EditMapParameters(GameMap *actmap, PG_Widget *parent)
bool quitModalLoop(int value)
Definition: paradialog.cpp:545
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
GameParameterSettings gameParameterSettings[gameparameternum]
Definition: gamemap.cpp:2258
Parses a .ASCTXT file and returns a TextPropertyGroup.
const char * longName
Definition: gamemap.h:124
GameParameter
Definition: gamemap.h:77
ASCString selectFile(const ASCString &ext, bool load, bool overwriteMessage)
int getgameparameter(GameParameter num) const
Definition: gamemap.cpp:1047
void addInteger(const ASCString &name, int &property)
static const char * fileNamePattern
Definition: editmapparam.h:40
GameParameterEditorWidget(GameMap *gamemap, PG_Widget *parent, const PG_Rect &rect)
GameMap * actmap
Definition: spfst.cpp:64
static const char * blockName
Definition: editmapparam.h:41
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182