src/network/nConfig.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #ifndef ArmageTron_NET_CONFIGURATION_H
00029 #define ArmageTron_NET_CONFIGURATION_H
00030 
00031 #include "tConfiguration.h"
00032 #include "nNetwork.h"
00033 
00034 template<class T> inline bool sn_compare(T&a, T&b)
00035 {
00036     return (a!=b);
00037 }
00038 
00039 inline bool sn_compare(float&a, float &b)
00040 {
00041     return (1000 * fabs(a-b) > fabs(a) + fabs(b));
00042 }
00043 
00044 class nConfItemBase;
00045 
00047 class nIConfItemWatcher
00048 {
00049 public:
00050     nIConfItemWatcher( nConfItemBase & item );            
00051 
00052     inline void Change( bool nonDefault );                
00053     inline bool Writable() const;                         
00054 protected:
00055     virtual ~nIConfItemWatcher()=0;                       
00056 
00057     nConfItemBase & watched_;                             
00058 private:
00059     virtual void OnChange( bool nonDefault ) = 0;         
00060     virtual bool DoWritable() const = 0;                  
00061 
00062     // disable copying and default construction
00063     nIConfItemWatcher();            
00064     nIConfItemWatcher(nIConfItemWatcher const &);            
00065     nIConfItemWatcher& operator=(nIConfItemWatcher const &); 
00066 };
00067 
00069 class nConfItemBase:public virtual tConfItemBase
00070 {
00071     friend class nIConfItemWatcher;
00072 private:
00073     double lastChangeTime_;             
00074     unsigned long lastChangeMessage_;   
00075     nIConfItemWatcher * watcher_;       
00076 protected:
00077     nConfItemBase();
00078 public:
00079     //  nConfItemBase(const char *title,const char *help);
00080     nConfItemBase(const char *title);
00081     virtual ~nConfItemBase();
00082 
00083     virtual void NetReadVal(nMessage &m)=0;
00084     virtual void NetWriteVal(nMessage &m)=0;
00085 
00086     static void s_GetConfigMessage(nMessage &m);
00087 
00088     virtual void WasChanged(bool nonDefault);
00089 
00090     virtual bool Writable();
00091 
00092     static void s_SendConfig(bool force=true, int peer=-1);
00093     void          SendConfig(bool force=true, int peer=-1);
00094 
00095     static void s_RevertToDefaults();       
00096     inline void RevertToDefaults();         
00097 
00098     static void s_SaveValues();             
00099     inline void SaveValue();                
00100 
00101     static void s_RevertToSavedValues();    
00102     inline void RevertToSavedValue();       
00103 private:
00104     virtual void OnRevertToDefaults()=0;      
00105 
00106     virtual void OnSaveValue()=0;             
00107     virtual void OnRevertToSavedValue()=0;    
00108 };
00109 
00110 
00111 template<class T> class nConfItem
00112             : public nConfItemBase,
00113             public tConfItem<T>,
00114             public virtual tConfItemBase
00115 {
00116 protected:
00117     nConfItem(T& t)
00118             :tConfItemBase(""), tConfItem<T>("", t),default_(t),saved_(t){}
00119 
00120 public:
00121     nConfItem(const char *title,const char *help,T& t)
00122             :tConfItem<T>(t),
00123             tConfItemBase(title, help ),
00124             default_(t),
00125     saved_(t){}
00126     virtual ~nConfItem(){}
00127 
00128 
00129     virtual void NetReadVal(nMessage &m){
00130         T dummy;
00131         m >> dummy;
00132         if (sn_compare(dummy,*this->target)){
00133             if (printChange)
00134             {
00135                 tOutput o;
00136                 o.SetTemplateParameter(1, title);
00137                 o.SetTemplateParameter(2, *this->target);
00138                 o.SetTemplateParameter(3, dummy);
00139                 o << "$nconfig_value_changed";
00140                 con << con.ColorString(1,.3,.3) << o;
00141             }
00142             *this->target=dummy;
00143             this->ExecuteCallback();
00144             changed=true;
00145         }
00146     }
00147 
00148     virtual void NetWriteVal(nMessage &m){
00149         m << *this->target;
00150     }
00151 
00152     void Set( const T& newval )
00153     {
00154         bool changed = ( newval != *this->target );
00155         *this->target = newval;
00156 
00157         if ( changed )
00158         {
00159             WasChanged();
00160         }
00161     }
00162 
00163     virtual void OnRevertToDefaults()      
00164     {
00165         Set( default_ );
00166     }
00167 
00168     virtual void OnSaveValue()             
00169     {
00170         saved_ = *this->target;
00171     }
00172 
00173     virtual void OnRevertToSavedValue()    
00174     {
00175         Set( saved_ );
00176     }
00177 private:
00178     void WasChanged()
00179     {
00180         nConfItemBase::WasChanged( ! (*this->target == default_) );
00181     }
00182 
00183     T default_;     
00184     T saved_;       
00185 };
00186 
00187 template<class T> class nSettingItem:public nConfItem<T>{
00188 private:
00189 public:
00190     //  nSettingItem(const char *title,const char *help,T& t)
00191     //    :nConfItem<T>(t), tConfItemBase(title, help){}
00192     //  virtual ~nSettingItem(){}
00193 
00194     nSettingItem(const char *title,T& t,tConfItemBase::callbackFunc *cb=0)
00195             :tConfItemBase(title, cb), nConfItem<T>(t){}
00196     virtual ~nSettingItem(){}
00197 
00198     virtual bool Save(){return false;}
00199 };
00200 
00201 
00202 class nConfItemLine:public nConfItem<tString>{
00203 protected:
00204 public:
00205     //  nConfItemLine(const char *title,const tOutput &help,tString &s)
00206     //    :nConfItem<tString>(s), tConfItemBase(title,help){};
00207     nConfItemLine(const char *title,tString &s);
00208 #ifndef SWIG
00209     nConfItemLine(const char *title,tString &s, callbackFunc *cb);
00210 #endif
00211 
00212     virtual ~nConfItemLine();
00213 
00214     virtual void ReadVal(std::istream &s);
00215 };
00216 
00218 enum nConfigItemBehavior
00219 {
00220     Behavior_Nothing = 0, 
00221     Behavior_Revert = 1,  
00222     Behavior_Block = 2,   
00223     Behavior_Default = 3  
00224 };
00225 
00226 class nConfItemVersionWatcher;
00227 tCONFIG_ENUM( nConfigItemBehavior );
00228 
00230 class nConfItemVersionWatcher: public nIConfItemWatcher
00231 {
00232 public:
00234     enum Group
00235     {
00236         Group_Breaking,  
00237         Group_Bumpy,     
00238         Group_Annoying,  
00239         Group_Cheating,  
00240         Group_Visual,    
00241 
00242         Group_Max
00243     };
00244 
00245     typedef nConfigItemBehavior Behavior;
00246 
00247     nConfItemVersionWatcher( nConfItemBase & item, Group group, int min, int max = -1 );          
00248     virtual ~nConfItemVersionWatcher();                      
00249 
00250     static void AdaptVersion( nVersion & version );          
00251     static void OnVersionChange( nVersion const & version ); 
00252 
00253     Behavior GetBehavior( void ) const;                      
00254     nConfItemVersionWatcher const & GetBehavior( Behavior & behavior ) const;   
00255 
00256     void FillTemplateParameters( tOutput & o ) const;        
00257 private:
00258     virtual void OnChange( bool nonDefault );                
00259     virtual bool DoWritable() const;                         
00260 
00261     nVersion version_;                                       
00262     bool nonDefault_;                                        
00263     bool reverted_;                                          
00264 
00265     Group group_;                                            
00266     Behavior overrideGroupBehavior_;                         
00267     tSettingItem< Behavior > overrideGroupBehaviorConf_;     
00268 };
00269 
00271 template<class T> class nSettingItemWatched
00272 {
00273 public:
00274     typedef nConfItemVersionWatcher::Group Group;
00275 
00276     // constructor
00277     nSettingItemWatched( char const * title, T & value, Group group, int min, int max = -1 )
00278             : setting_( title, value )
00279             , watcher_( setting_, group, min, max )
00280     {
00281     }
00282 
00283     void Set( T const & value )
00284     {
00285         this->setting_.Set( value );
00286     }
00287 private:
00288     nSettingItem< T > setting_;
00289     nConfItemVersionWatcher watcher_;
00290 };
00291 
00292 // *******************************************************************************************
00293 // *
00294 // *    Change
00295 // *
00296 // *******************************************************************************************
00300 // *******************************************************************************************
00301 
00302 void nIConfItemWatcher::Change( bool nonDefault )
00303 {
00304     this->OnChange( nonDefault );
00305 }
00306 
00307 // *******************************************************************************************
00308 // *
00309 // *    RevertToDefaults
00310 // *
00311 // *******************************************************************************************
00314 // *******************************************************************************************
00315 
00316 void nConfItemBase::RevertToDefaults( void )
00317 {
00318     // inform watcher
00319     if (this->watcher_ )
00320         this->watcher_->Change( false );
00321 
00322     // reset last change information
00323     lastChangeMessage_ = 0;
00324     lastChangeTime_ = -1000000;
00325 
00326     this->OnRevertToDefaults();
00327 }
00328 
00329 // *******************************************************************************************
00330 // *
00331 // *    SaveValue
00332 // *
00333 // *******************************************************************************************
00336 // *******************************************************************************************
00337 
00338 void nConfItemBase::SaveValue( void )
00339 {
00340     this->OnSaveValue();
00341 }
00342 
00343 // *******************************************************************************************
00344 // *
00345 // *    RevertToSavedValue
00346 // *
00347 // *******************************************************************************************
00350 // *******************************************************************************************
00351 
00352 void nConfItemBase::RevertToSavedValue( void )
00353 {
00354     this->OnRevertToSavedValue();
00355 }
00356 
00357 // *******************************************************************************************
00358 // *
00359 // *    Writable
00360 // *
00361 // *******************************************************************************************
00365 // *******************************************************************************************
00366 
00367 bool nIConfItemWatcher::Writable( void ) const
00368 {
00369     return this->DoWritable();
00370 }
00371 
00372 #endif
00373 

Generated on Sat Mar 15 22:55:49 2008 for Armagetron Advanced by  doxygen 1.5.4