src/gui/slider.cpp

Go to the documentation of this file.
00001 #include "gui/gui.h"
00002 #include "gui/slider.h"
00003 using namespace VGUI;
00004 
00005 
00006 Slider::Slider( Widget* w )
00007 {
00008         value = default_value = 0.0;
00009         setting = "";
00010         center = w->GetCenter();
00011         name = w->GetName();
00012         text = w->GetText();
00013         type = w->GetType();
00014         tip = w->GetTip();
00015         rel_width = w->GetRelWidth();
00016         rel_height = w->GetRelHeight();
00017         enabled = w->GetEnabled();
00018         selected = w->GetSelected();
00019         is_default = w->GetDefault();
00020         is_cancel = w->GetCancel();
00021         has_icon = w->GetHasIcon();
00022         width_auto = w->GetWidthAuto();
00023         height_auto = w->GetHeightAuto();
00024         font_size = w->GetFontSize();
00025         color_r = w->GetColorR();
00026         color_g = w->GetColorG();
00027         color_b = w->GetColorB();
00028 
00029         string skin_path = settings.GetSkinPath();
00030         slider.Load( skin_path + "/textures/widgets/sld_wedge.png", false );
00031         cursor.Load( skin_path + "/textures/widgets/sld_cursor.png", false );
00032 
00033         wedge_tex_width = 0.148;
00034         wedge_width = 0.1186;
00035         wedge_height = 0.1975;
00036 
00037         cursor_width = 0.0370;
00038         cursor_height = 0.04938;
00039         cursor_offset = value * ( wedge_width );
00040 
00041         // width = spacing between left of text and wedge + wedge width
00042         if( width_auto )
00043         {
00044                 rel_width = 0.3 + wedge_width;
00045         }
00046 
00047         // height = wedge texture height
00048         if( height_auto )
00049         {
00050                 rel_height = cursor_height;
00051         }
00052 }
00053 
00054 Slider::~Slider()
00055 {
00056 }
00057 
00058 void Slider::Draw()
00059 {
00060         Draw( 1.0 );
00061 }
00062 
00063 void Slider::Draw( float opacity )
00064 {
00065         float center_x = center.GetXPercent();
00066         float center_y = center.GetYPercent();
00067         float rel_width_half = rel_width / 2.0f;
00068         float rel_height_half = rel_height / 2.0f;
00069         cursor_offset = value * ( wedge_width - 0.013 );
00070         float gfx_opacity = selected ? 0.9 : enabled ? 0.75 : 0.4;
00071         float opacity_txt = selected ? 0.9 : enabled ? 0.75 : 0.4;
00072 
00073         // draw wedge and cursor
00074         utility.Draw2D( center_x - rel_width_half + 0.30,
00075                         center_y - rel_height_half,
00076                         center_x - rel_width_half + 0.30 + wedge_tex_width,
00077                         center_y + wedge_height,
00078                         &slider, 0.0, 0, gfx_opacity * opacity );
00079         utility.Draw2D( center_x - rel_width_half + 0.30 + cursor_offset + 0.004,
00080                         center_y - rel_height_half + 0.001,
00081                         center_x - rel_width_half + 0.30 + cursor_offset + 0.004 + cursor_width,
00082                         center_y + rel_height_half + 0.006,
00083                         &cursor, 0.0, 64, gfx_opacity * opacity );
00084 
00085         // draw text
00086         font.Print( center_x - rel_width_half, center_y - ( font.Height( text.c_str(), 1, font_size ) / 2.0f ), text.c_str(), 1, font_size, color_r, color_g, color_b, opacity_txt );
00087 
00088         // draw tip
00089         if( selected && ( tip != "" ) )
00090                 font.Print( 0.5 - ( font.Width( tip.c_str(), 1, 6 ) / 2.0f ), 0.90, tip.c_str(), 1, 6, 0.7, 0.7, 0.7, 0.9 * opacity );
00091 }
00092 
00093 void Slider::IncValuePress()
00094 {
00095         if( value < 0.9 )
00096         {
00097                 value += 0.1;
00098         }
00099         else
00100         {
00101                 value = 1.0;
00102         }
00103 }
00104 
00105 void Slider::IncValueRelease()
00106 {
00107 }
00108 
00109 void Slider::DecValuePress()
00110 {
00111         if( value > 0.1 )
00112         {
00113                 value -= 0.1;
00114         }
00115         else
00116         {
00117                 value = 0.0;
00118         }
00119 }
00120 
00121 void Slider::DecValueRelease()
00122 {
00123 }
00124 
00125 void Slider::Save()
00126 {
00127         default_value = value;
00128         settings.Set( setting, value );
00129 }
00130 
00131 void Slider::ResetValue()
00132 {
00133         float default_val;
00134         settings.Get( setting, default_val );
00135         SetValue( default_val );
00136 }
00137 
00138 bool Slider::MouseOver( float x, float y )
00139 {
00140         float min_x, min_y, max_x, max_y;
00141         min_x = center.GetXPercent() - ( rel_width / 2.0f );
00142         min_y = center.GetYPercent() - ( rel_height / 2.0f );
00143         max_x = center.GetXPercent() + ( rel_width / 2.0f );
00144         max_y = center.GetYPercent() + ( rel_height / 2.0f ) + 0.01;
00145 
00146         return ( x > min_x ) && ( x < max_x ) && ( y > min_y ) && ( y < max_y );
00147 }
00148 
00149 string Slider::MousePress( float x, float y )
00150 {
00151         float min_x, max_x, x_len, x_offset, new_value = value;
00152         min_x = center.GetXPercent() - ( rel_width / 2.0f ) + 0.304;
00153         max_x = center.GetXPercent() - ( rel_width / 2.0f ) + 0.3 + wedge_width - 0.009;
00154 
00155         if( ( x >= min_x ) && ( x <= max_x ) )
00156         {
00157                 x_offset = x - min_x;
00158                 x_len = max_x - min_x;
00159 
00160                 new_value = x_offset / x_len;
00161         }
00162         else if( x < min_x )
00163         {
00164                 new_value = 0.0;
00165         }
00166         else if( x > max_x )
00167         {
00168                 new_value = 1.0;
00169         }
00170 
00171         SetValue( new_value );
00172 
00173         return "";
00174 }
00175 
00176 string Slider::MouseRelease( float x, float y )
00177 {
00178         return "";
00179 }

Generated on Thu Oct 19 04:05:52 2006 by  doxygen 1.4.6