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

Control.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id:  $
00022 */
00023 
00024 
00025 #include "Teddy/Behaviour/Control.h"
00026 using namespace Teddy::MixIn;
00027 
00028 namespace Teddy     {
00029 namespace Behaviour {
00030 
00031 
00032 /*static*/ const unsigned long Control::RESERVED        = 0ul;
00033 /*static*/ const unsigned long Control::MORE            = 1ul;
00034 /*static*/ const unsigned long Control::LESS            = 2ul;
00035 /*static*/ const unsigned long Control::STOP            = 3ul;
00036 /*static*/ const unsigned long Control::ACTIVE          = 4ul;
00037 /*static*/ const unsigned long Control::INHIBIT         = 5ul;
00038 /*static*/ const unsigned long Control::DAMPEN_LINEAR   = 6ul;
00039 /*static*/ const unsigned long Control::DAMPEN_MULTIPLY = 7ul;
00040 /*static*/ const unsigned long Control::DAMPEN_MASTER   = 8ul;
00041                                                         
00042 /*static*/ const unsigned long Control::RESERVED_M        = (1ul<<Control::RESERVED       );
00043 /*static*/ const unsigned long Control::MORE_M            = (1ul<<Control::MORE           );
00044 /*static*/ const unsigned long Control::LESS_M            = (1ul<<Control::LESS           );
00045 /*static*/ const unsigned long Control::STOP_M            = (1ul<<Control::STOP           );
00046 /*static*/ const unsigned long Control::ACTIVE_M          = (1ul<<Control::ACTIVE         );
00047 /*static*/ const unsigned long Control::INHIBIT_M         = (1ul<<Control::INHIBIT        );
00048 /*static*/ const unsigned long Control::DAMPEN_LINEAR_M   = (1ul<<Control::DAMPEN_LINEAR  );
00049 /*static*/ const unsigned long Control::DAMPEN_MULTIPLY_M = (1ul<<Control::DAMPEN_MULTIPLY);
00050 /*static*/ const unsigned long Control::DAMPEN_MASTER_M   = (1ul<<Control::DAMPEN_MASTER  );
00051 
00052 /*static*/ const unsigned long Control::CLEAR_M =
00053     Control::MORE_M    |  
00054     Control::LESS_M    |  
00055     Control::STOP_M    |  
00056     Control::ACTIVE_M  |  
00057     Control::INHIBIT_M;
00058 
00059 
00061 Control::Control( double max_delta, double max, Options options, double dampen_const )
00062 :
00063 Options     (options),
00064 //c_more      (false ),
00065 //c_less      (false ),
00066 //c_stop      (false ),
00067 //c_active    (false ),
00068 //c_inhibit   (false ),
00069 //c_dampen_lin(dampen_lin),
00070 //c_dampen_mul(dampen_mul),
00071 dampen_const(dampen_const),
00072 max_delta   (max_delta),
00073 min         (-max ),
00074 max         ( max ),
00075 c_delta     (0),
00076 c_value     (0)
00077 {
00078 }
00079 
00080 
00081 void Control::setValue( double value ){
00082     c_value = value;
00083 }
00084 
00085 
00086 double Control::getValue() const {
00087     return c_value;
00088 }
00089 
00090 
00091 double Control::getMin() const {
00092     return min;
00093 }
00094 
00095 
00096 double Control::getMax() const {
00097     return max;
00098 }
00099 
00100 
00101 double Control::getDelta() const {
00102     return max_delta;
00103 }
00104 
00105 
00106 double Control::getDampConst() const {
00107     return dampen_const;
00108 }
00109 
00110 
00111 void Control::setDelta( double max_delta ){
00112     this->max_delta = max_delta;
00113 }
00114 
00115 
00116 void Control::setDampConst( double dampen_const ){
00117     this->dampen_const = dampen_const;
00118 }
00119 
00120 
00121 void Control::clear(){
00122     disable( Control::CLEAR_M );
00123 //  c_more    = false;
00124 //  c_less    = false;
00125 //  c_stop    = false;
00126 //  c_active  = false;
00127 //  c_inhibit = false;
00128     c_delta   = 0;
00129     c_value   = 0;
00130 }
00131 
00132 
00134 /*virtual*/ void Control::tick(){
00135     static const double age = 10.0;
00136 
00137     if( isEnabled(Control::ACTIVE_M) && isDisabled(Control::INHIBIT_M) ){
00138         adjust( c_delta * age );
00139     }
00140     dampen();
00141 }
00142 
00143 
00145 void Control::adjust( double delta ){
00146     c_value += delta;
00147 
00148     if( c_value>max ){
00149         c_value = max;
00150     }else if( c_value<min ){
00151         c_value = min;
00152     }
00153 }
00154 
00155 
00157 void Control::dampen(){
00158     static const double age = 10.0;
00159 
00160     if( isEnabled(Control::DAMPEN_MULTIPLY_M) ){
00161 
00162         c_value *= dampen_const;
00163 
00164     }else if( isEnabled(Control::DAMPEN_LINEAR_M) && isDisabled(Control::ACTIVE_M) ){
00165 
00166         if( c_value > max_delta ){
00167             c_value -= max_delta*age; 
00168             if( c_value < max_delta ){
00169                 c_value = 0;
00170             }
00171         }else if( c_value < -max_delta ){
00172             c_value += max_delta*age;
00173             if( c_value > -max_delta ){
00174                 c_value = 0;
00175             }
00176         }else{
00177             c_value *= dampen_const;
00178         }
00179     }
00180 }
00181 
00182 
00184 void Control::more( bool apply ){
00185     if( apply ){
00186         enable( Control::MORE_M | Control::ACTIVE_M );
00187         c_delta  =  max_delta;
00188     }else{
00189         disable( Control::MORE_M );
00190         if( isEnabled(Control::LESS_M) ){
00191             c_delta  = -max_delta;
00192         }else{
00193             disable( Control::ACTIVE_M );
00194             c_delta  =  0;
00195         }
00196     }
00197 }
00198 
00199 
00201 void Control::less( bool apply ){
00202     if( apply ){
00203         enable( Control::LESS_M | Control::ACTIVE_M );
00204         c_delta  = -max_delta;
00205     }else{
00206         disable( Control::LESS_M );
00207         if( isEnabled(Control::MORE_M) ){
00208             c_delta  =  max_delta;
00209         }else{
00210             disable( Control::ACTIVE_M );
00211             c_delta  =  0;
00212         }
00213     }
00214 }
00215 
00216 
00218 void Control::stop( bool apply ){
00219     if( apply ){
00220         enable( Control::STOP_M );
00221         if( c_value > 0 ){
00222             c_delta  = -max_delta;
00223         }else if( c_value < 0 ){
00224             c_delta  =  max_delta;
00225         }
00226     }else{
00227         disable( Control::STOP_M );
00228         if( isEnabled(Control::LESS_M) && isDisabled(Control::MORE_M) ){
00229             c_delta = -max_delta;
00230         }else if( isEnabled(Control::MORE_M) && isDisabled(Control::LESS_M) ){
00231             c_delta =  max_delta;
00232         }
00233     }
00234 }
00235 
00236 
00237 };  //  namespace Behaviour
00238 };  //  namespace Teddy
00239