Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

editmapparam.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-1999  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 #include <sstream>
00022 #include <pgimage.h>
00023 
00024 #include "selectionwindow.h"
00025 #include "sigc++/retype.h"
00026 #include "editmapparam.h"
00027 #include "fileselector.h"
00028 
00029 #include <pgpropertyeditor.h>
00030 #include <pgpropertyfield_integer.h>
00031 #include <pgpropertyfield_checkbox.h>
00032 
00033 
00034 #include "../textfileparser.h"
00035 #include "../textfile_evaluation.h"
00036 
00037 
00038 
00039      
00040 void GameParameterEditorWidget :: runTextIO ( PropertyContainer& pc ) 
00041 {
00042    for ( int i = 0; i< gameparameternum; ++i ) {
00043       if ( !gameParameterSettings[i].legacy ) {
00044          pc.addInteger( gameParameterSettings[i].name, values[i], values[i] );
00045          if ( pc.isReading() ) {
00046             if ( ! (values[i] >=  gameParameterSettings[i].minValue  && values[i] <= gameParameterSettings[i].maxValue  ))
00047                values[i] = gameParameterSettings[i].defaultValue;
00048          }
00049       }
00050    }
00051 
00052 }
00053 
00054 
00055 
00056 bool GameParameterEditorWidget :: LoadParameter()
00057 {
00058    ASCString filename = selectFile( fileNamePattern, true );
00059    if ( !filename.empty() ) {
00060       tnfilestream s ( filename, tnstream::reading );
00061       
00062       TextFormatParser tfp ( &s );
00063       auto_ptr<TextPropertyGroup> tpg ( tfp.run());
00064       
00065       PropertyReadingContainer pc ( blockName, tpg.get() );
00066    
00067       runTextIO( pc );
00068       propertyEditor->Reload();
00069       return true;  
00070    } 
00071    return false;
00072 }
00073 
00074 bool GameParameterEditorWidget :: SaveParameter()
00075 {
00076    ASCString filename = selectFile( fileNamePattern, false );
00077    if ( !filename.empty() ) {
00078       tn_file_buf_stream s ( filename, tnstream::writing );
00079       PropertyWritingContainer pc ( blockName, s );
00080       runTextIO( pc );
00081       return true;
00082    }
00083    return false;  
00084 }
00085 
00086 bool GameParameterEditorWidget :: ResetParameter()
00087 {
00088    for ( int i = 0; i< gameparameternum; ++i ) 
00089       values[i] = gameParameterSettings[i].defaultValue;
00090    propertyEditor->Reload(); 
00091    return true;  
00092 }
00093             
00094 GameParameterEditorWidget :: GameParameterEditorWidget ( GameMap* gamemap, PG_Widget* parent, const PG_Rect& rect ) : PG_Widget( parent, rect ), actmap ( gamemap )
00095 {
00096    SetTransparency(255);
00097    
00098    propertyEditor = new PG_PropertyEditor( this, PG_Rect( 0,0, rect.Width() - 110, rect.Height() ), "PropertyEditor", 70 );
00099    
00100    PG_Button* load = new PG_Button( this, PG_Rect( rect.Width() - 100, 0,  100, 30 ), "Load" );
00101    load->sigClick.connect( SigC::slot( *this, &GameParameterEditorWidget::LoadParameter ));
00102    
00103    PG_Button* save = new PG_Button( this, PG_Rect( rect.Width() - 100, 40, 100, 30 ), "Save" );
00104    save->sigClick.connect( SigC::slot( *this, &GameParameterEditorWidget::SaveParameter ));
00105    
00106    PG_Button* def = new PG_Button( this, PG_Rect( rect.Width() - 100, 80, 100, 30 ), "Default" );
00107    def->sigClick.connect( SigC::slot( *this, &GameParameterEditorWidget::ResetParameter ));
00108    
00109    
00110    for ( int i = 0; i< gameparameternum; ++i ) {
00111       values[i] = actmap->getgameparameter ( GameParameter(i) );
00112       if ( values[i] < gameParameterSettings[i].minValue || values[i] > gameParameterSettings[i].maxValue )
00113          values[i] = gameParameterSettings[i].defaultValue;
00114       
00115       if ( !gameParameterSettings[i].legacy ) {
00116          if ( gameParameterSettings[i].minValue == 0 && gameParameterSettings[i].maxValue == 1 ) {
00117             new PG_PropertyField_Checkbox<int>( propertyEditor , gameParameterSettings[i].longName, &values[i] );
00118          } else {
00119             PG_PropertyField_Integer<int>* ip = new PG_PropertyField_Integer<int>( propertyEditor , gameParameterSettings[i].longName, &values[i] );
00120             ip->SetRange( gameParameterSettings[i].minValue, gameParameterSettings[i].maxValue );
00121          }
00122       }   
00123    }
00124 };
00125 
00126 bool GameParameterEditorWidget :: Valid()
00127 {
00128    return propertyEditor->Valid( true );
00129 }
00130 
00131 bool GameParameterEditorWidget :: Apply()
00132 {
00133    bool res = propertyEditor->Apply();
00134    for ( int i = 0; i< gameparameternum; ++i )
00135       actmap->setgameparameter ( GameParameter(i), values[i] );
00136    return res;
00137 }
00138       
00139 
00140 const char* GameParameterEditorWidget :: fileNamePattern = "*.asc.gameparam";
00141 const char* GameParameterEditorWidget :: blockName = "GameParam";
00142 
00143 
00144 class EditMapParameters : public ASC_PG_Dialog {
00145       GameParameterEditorWidget* gpew;
00146       
00147       bool ok()
00148       {
00149          if ( gpew->Valid() ) {
00150             gpew->Apply();
00151             quitModalLoop(0);
00152 
00153             return true;
00154          } else
00155             return false;
00156       }
00157 
00158    public:
00159       EditMapParameters( GameMap* actmap, PG_Widget* parent ) : ASC_PG_Dialog( parent, PG_Rect( 50, 50, 500, 400 ), "Edit Map Parameters")
00160       {
00161          gpew = new GameParameterEditorWidget ( actmap, this, PG_Rect( 10, GetTitlebarHeight(), Width() - 20, Height() - GetTitlebarHeight() - 40 ));
00162          PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 100, Height() - 40, 90, 30), "OK" );
00163          ok->sigClick.connect( SigC::slot( *this, &EditMapParameters::ok ));
00164       };
00165 
00166 };
00167 
00168 
00169 void setmapparameters ( GameMap* gamemap )
00170 {
00171    EditMapParameters emp( gamemap, NULL );
00172    emp.Show();
00173    emp.RunModal();
00174 }

Generated on Tue Jun 24 01:27:40 2008 for Advanced Strategic Command by  doxygen 1.4.2