Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

OSLIDER.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

OSLIDER.CPP

Go to the documentation of this file.
00001 //Filename    : OSLIDER.CPP
00002 //Description : Object slider
00003 
00004 #include <ALL.H>
00005 #include <OVGA.H>
00006 #include <OSYS.H>
00007 #include <OFONT.H>
00008 #include <OMOUSE.H>
00009 #include <OSLIDER.H>
00010 
00011 //--------- Define macro constant -----------//
00012 
00013 enum {                                            // Default height of slider
00014     SLIDER_HEIGHT = 16
00015 };
00016 
00017 //-------- Begin of function Slider::init --------//
00033 void Slider::init( int x1, int y1, int barWidth, int sliderHeight, int barColor,
00034                    int* varPtr, int maxValue, int stdValue, int barColor2 ) {
00035     slider_x1    = x1;
00036     slider_y1    = y1;
00037     slider_x2    = x1+barWidth+3;
00038 
00039     if( sliderHeight )
00040         slider_y2 = y1+sliderHeight-1;
00041     else
00042         slider_y2 = y1+SLIDER_HEIGHT-1;
00043 
00044     bar_width    = barWidth;
00045     bar_color    = barColor;
00046 
00047     var_ptr      = varPtr;
00048     max_value    = maxValue;
00049 
00050     std_value    = stdValue;
00051     bar_color2   = barColor2;
00052 
00053     enable_flag  = 1;
00054 
00055     paint();
00056 }
00057 
00058 //--------- End of function Slider::init ---------//
00059 
00060 //-------- Begin of function Slider::paint --------//
00062 void Slider::paint() {
00063     vga.d3_panel_down( slider_x1, slider_y1, slider_x2, slider_y2 );
00064 
00065     refresh();
00066 }
00067 
00068 //--------- End of function Slider::paint ---------//
00069 
00070 //-------- Begin of function Slider::refresh --------//
00072 void Slider::refresh() {
00073     err_when( max_value==0 );
00074 
00075     int barWidth = (*var_ptr) * bar_width / max_value;
00076 
00077     if( barWidth > bar_width )
00078         barWidth = bar_width;
00079 
00080     mouse.hide_area(slider_x1, slider_y1, slider_x2, slider_y2);
00081 
00082     //----- paint the bar area ---------//
00083 
00084     if( barWidth > 0 ) {
00085         if( std_value ) {                             // if standard value is set, than use different color
00086             int stdBarWidth = std_value * bar_width / max_value;
00087 
00088             //--- use bar_color for the area < std_value ---//
00089 
00090             err_when( slider_x1+2 > slider_x1+1+min(barWidth,stdBarWidth) );
00091 
00092             Vga::active_buf->bar( slider_x1+2, slider_y1+2, slider_x1+1+min(barWidth,stdBarWidth),
00093                                   slider_y2-2, bar_color );
00094 
00095             //--- use bar_color2 for the area > std_value ---//
00096 
00097             if( barWidth > stdBarWidth ) {
00098                 err_when( slider_x1+2+stdBarWidth > slider_x1+1+barWidth );
00099 
00100                 Vga::active_buf->bar( slider_x1+2+stdBarWidth, slider_y1+2, slider_x1+1+barWidth,
00101                                       slider_y2-2, bar_color2 );
00102             }
00103         }
00104         else {
00105             err_when( slider_x1+2 > slider_x1+1+barWidth );
00106 
00107             Vga::active_buf->bar( slider_x1+2, slider_y1+2, slider_x1+1+barWidth,
00108                                   slider_y2-2, bar_color );
00109         }
00110     }
00111 
00112     //----- paint the non-bar area ---------//
00113 
00114     if( barWidth < bar_width ) {
00115         err_when( slider_x1+2+barWidth > slider_x2-2 );
00116 
00117         Vga::active_buf->bar( slider_x1+2+barWidth, slider_y1+2, slider_x2-2, slider_y2-2, Vga::active_buf->color_down );
00118     }
00119 
00120     vga_back.bar( slider_x1, slider_y1, slider_x2, slider_y2, V_RED );
00121 
00122     mouse.show_area();
00123 }
00124 
00125 //--------- End of function Slider::refresh ---------//
00126 
00127 //-------- Begin of function Slider::detect --------//
00132 int Slider::detect() {
00133     if( !mouse.single_click( slider_x1, slider_y1, slider_x2, slider_y2 ) )
00134         return 0;
00135 
00136     int lastVar = *var_ptr;
00137 
00138     do {
00139         sys.yield();
00140         mouse.get_event();
00141 
00142         *var_ptr = (long)(mouse.cur_x-slider_x1-2) * max_value / bar_width;
00143 
00144         if( *var_ptr < 0 )
00145             *var_ptr = 0;
00146 
00147         if( *var_ptr > max_value )
00148             *var_ptr = max_value;
00149 
00150         if( lastVar != *var_ptr )
00151             refresh();
00152     }
00153     while( mouse.left_press );
00154 
00155     return 1;
00156 }
00157 
00158 //--------- End of function Slider::detect ---------//
00159 
00160 //.........................................................//
00161 
00162 //-------- Begin of function SliderGroup::SliderGroup -------//
00163 
00164 SliderGroup::SliderGroup(int sliderNum) {
00165     slider_num     = sliderNum;
00166     slider_array   = new Slider[sliderNum];
00167 }
00168 
00169 //---------- End of function SliderGroup::SliderGroup -------//
00170 
00171 //--------- Begin of function SliderGroup::paint ----------//
00175 void SliderGroup::paint() {
00176     int i;
00177 
00178     for( i=0 ; i<slider_num ; i++ )
00179         slider_array[i].paint();
00180 }
00181 
00182 //----------- End of function SliderGroup::paint ----------//
00183 
00184 //--------- Begin of function SliderGroup::detect ----------//
00194 int SliderGroup::detect() {
00195     int i, oldValue;
00196 
00197     for( i=0 ; i<slider_num ; i++ ) {
00198         oldValue = *(slider_array[i].var_ptr);
00199 
00200         if( slider_array[i].detect() )
00201             break;
00202     }
00203 
00204     if( i==slider_num )                             // no detect
00205         return 0;
00206 
00207     //------- pull or give value to other slider ------//
00208 
00209     int j, decValue;
00210     int deltaValue = *(slider_array[i].var_ptr) - oldValue;
00211 
00212     //------- If it's the last slider ---------//
00213 
00214     if( i==slider_num-1 ) {
00215         if( deltaValue < 0 ) {                        // its value goes to the first slider
00216             *(slider_array[0].var_ptr) += -deltaValue;
00217             slider_array[0].refresh();
00218         }
00219         else {                                        // pull value from top sliders
00220             for( j=0 ; j<slider_num-1 && deltaValue>0 ; j++ ) {
00221                 decValue = min( *(slider_array[j].var_ptr), deltaValue );
00222 
00223                 *(slider_array[j].var_ptr) -= decValue;
00224                 deltaValue           -= decValue;
00225 
00226                 slider_array[j].refresh();
00227             }
00228         }
00229     }
00230 
00231     //----- If it's not the last slider -----//
00232 
00233     else {
00234         if( deltaValue < 0 ) {                        // its value goes to the last slider
00235             *(slider_array[slider_num-1].var_ptr) += -deltaValue;
00236             slider_array[slider_num-1].refresh();
00237         }
00238         else {                                        // pull value from bottom sliders
00239             for( j=slider_num-1 ; j>=0 && deltaValue>0 ; j-- ) {
00240                 if( j != i ) {                            // don't pull from itself
00241                     decValue = min( *(slider_array[j].var_ptr), deltaValue );
00242 
00243                     *(slider_array[j].var_ptr) -= decValue;
00244                     deltaValue      -= decValue;
00245 
00246                     slider_array[j].refresh();
00247                 }
00248             }
00249         }
00250     }
00251 
00252     return 1;
00253 }
00254 
00255 //----------- End of function SliderGroup::detect ----------//

Generated on Fri Aug 23 01:38:23 2002 for VirtualU by doxygen1.2.17