src/gui/toggle.cpp

Go to the documentation of this file.
00001 #include "gui/gui.h"
00002 #include "gui/toggle.h"
00003 using namespace VGUI;
00004 
00005 
00006 Toggle::Toggle()
00007 {
00008 }
00009 
00010 Toggle::Toggle( Widget* w )
00011 {
00012         value = default_value = false;
00013         setting = "";
00014         spacing = 0.3;
00015         center = w->GetCenter();
00016         name = w->GetName();
00017         text = w->GetText();
00018         type = w->GetType();
00019         tip = w->GetTip();
00020         rel_width = w->GetRelWidth();
00021         rel_height = w->GetRelHeight();
00022         enabled = w->GetEnabled();
00023         selected = w->GetSelected();
00024         is_default = w->GetDefault();
00025         is_cancel = w->GetCancel();
00026         has_icon = w->GetHasIcon();
00027         width_auto = w->GetWidthAuto();
00028         height_auto = w->GetHeightAuto();
00029         down = false;
00030         font_size = w->GetFontSize();
00031         color_r = w->GetColorR();
00032         color_g = w->GetColorG();
00033         color_b = w->GetColorB();
00034 
00035         string skin_path = settings.GetSkinPath();
00036         on_up.Load( skin_path + "/textures/widgets/tog_on_up.png", false );
00037         on_down.Load( skin_path + "/textures/widgets/tog_on_down.png", false );
00038         off_up.Load( skin_path + "/textures/widgets/tog_off_up.png", false );
00039         off_down.Load( skin_path + "/textures/widgets/tog_off_down.png", false );
00040 
00041         tex_width = 0.0277;
00042         tex_height = 0.0370;
00043 
00044         // width = spacing between left of text and left of left arrow + spacing between left of left and right arrow + texture width
00045         if( width_auto )
00046         {
00047                 rel_width = spacing + 0.15 + tex_width;
00048         }
00049 
00050         // height = arrow texture height
00051         if( height_auto )
00052         {
00053                 rel_height = tex_height;
00054         }
00055 }
00056 
00057 void Toggle::SetSpacing( float new_spacing )
00058 {
00059         spacing = new_spacing;
00060         if( width_auto )
00061         {
00062                 rel_width = spacing + 0.15 + tex_width;
00063         }
00064 }
00065 
00066 Toggle::~Toggle()
00067 {
00068 }
00069 
00070 void Toggle::Draw()
00071 {
00072         Draw( 1.0 );
00073 }
00074 
00075 void Toggle::Draw( float opacity )
00076 {
00077         float center_x = center.GetXPercent();
00078         float center_y = center.GetYPercent();
00079         float rel_width_half = rel_width / 2.0f;
00080         float rel_height_half = rel_height / 2.0f;
00081         TEXTURE_HANDLE * t1 = value ? ( down ? &on_down : &on_up ) : ( down ? &off_down : &off_up );
00082         TEXTURE_HANDLE * t2 = value ? ( down ? &off_down : &off_up ) : ( down ? &on_down : &on_up );
00083         float opacity_txt = selected ? 0.9 : enabled ? 0.75 : 0.4;
00084         float l_opacity = selected ? 0.9 : enabled ? 0.75 : 0.4;
00085         float r_opacity = selected ? 0.9 : enabled ? 0.75 : 0.4;
00086  
00087         // draw toggle graphics
00088         utility.Draw2D( center_x - rel_width_half + spacing,
00089                         center_y - rel_height_half,
00090                         center_x - rel_width_half + spacing + tex_width,
00091                         center_y + rel_height_half,
00092                         t1, 0.0, 64, l_opacity * opacity );
00093         utility.Draw2D( center_x - rel_width_half + spacing + 0.1,
00094                         center_y - rel_height_half,
00095                         center_x - rel_width_half + spacing + 0.1 + tex_width,
00096                         center_y + rel_height_half,
00097                         t2, 0.0, 64, r_opacity * opacity );
00098 
00099         // draw text
00100         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 * opacity );
00101         font.Print( center_x - rel_width_half + spacing + 0.005 + tex_width, center_y - ( font.Height( true_txt.c_str(), 1, font_size ) / 2.0f ), true_txt.c_str(),  1, font_size, color_r, color_g, color_b, l_opacity * opacity );
00102         font.Print( center_x - rel_width_half + spacing + 0.105 + tex_width, center_y - ( font.Height( false_txt.c_str(), 1, font_size ) / 2.0f ), false_txt.c_str(),  1, font_size, color_r, color_g, color_b, r_opacity * opacity );
00103 
00104         // draw tip
00105         if( selected && ( tip != "" ) )
00106                 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 );
00107 }
00108 
00109 void Toggle::Save()
00110 {
00111         default_value = value;
00112         settings.Set( setting, value );
00113 }
00114 
00115 void Toggle::ResetValue()
00116 {
00117         bool default_val;
00118         settings.Get( setting, default_val );
00119         SetValue( default_val );
00120 }
00121 
00122 bool Toggle::MouseOver( float x, float y )
00123 {
00124         float min_x, min_y, max_x, max_y;
00125         min_x = center.GetXPercent() - ( rel_width / 2.0f );
00126         min_y = center.GetYPercent() - ( rel_height / 2.0f );
00127         max_x = center.GetXPercent() + ( rel_width / 2.0f );
00128         max_y = center.GetYPercent() + ( rel_height / 2.0f );
00129 
00130         return ( x > min_x ) && ( x < max_x ) && ( y > min_y ) && ( y < max_y );
00131 }
00132 
00133 string Toggle::MousePress( float x, float y )
00134 {
00135         down = true;
00136         return "";
00137 }
00138 
00139 string Toggle::MouseRelease( float x, float y )
00140 {
00141         down = false;
00142         return "";
00143 }
00144 
00145 void Toggle::Press()
00146 {
00147         down = true;
00148 }
00149 
00150 void Toggle::Release()
00151 {
00152         down = false;
00153 }

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