src/gui/button.cpp

Go to the documentation of this file.
00001 #include "gui/gui.h"
00002 #include "gui/button.h"
00003 using namespace VGUI;
00004 
00005 
00006 Button::Button( Widget* w )
00007 {
00008         action = "";
00009         center = w->GetCenter();
00010         name = w->GetName();
00011         text = w->GetText();
00012         type = w->GetType();
00013         tip = w->GetTip();
00014         rel_width = w->GetRelWidth();
00015         rel_height = w->GetRelHeight();
00016         enabled = w->GetEnabled();
00017         selected = w->GetSelected();
00018         is_default = w->GetDefault();
00019         is_cancel = w->GetCancel();
00020         has_icon = w->GetHasIcon();
00021         width_auto = w->GetWidthAuto();
00022         height_auto = w->GetHeightAuto();
00023         down = false;
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         btn_up.Load( skin_path + "/textures/widgets/btn_up.png", false );
00031         btn_down.Load( skin_path + "/textures/widgets/btn_down.png", false );
00032 
00033         tex_width = 0.0555;
00034         tex_height = 0.0740;
00035 
00036         // width = font width + width of the end caps + width of 0.01 between each cap and text
00037         if( width_auto )
00038         {
00039                 rel_width = font.Width( text.c_str(), 1, font_size ) + tex_width;
00040         }
00041 
00042         // height = button texture height
00043         if( height_auto )
00044         {
00045                 rel_height = tex_height;
00046         }
00047 }
00048 
00049 Button::~Button()
00050 {
00051 }
00052 
00053 void Button::Draw()
00054 {
00055         Draw( 1.0 );
00056 }
00057 
00058 void Button::Draw( float opacity )
00059 {
00060         float center_x = center.GetXPercent();
00061         float center_y = center.GetYPercent();
00062         float rel_height_half = rel_height / 2.0f;
00063         float text_width = font.Width( text.c_str(), 1, font_size );
00064         float text_width_half = text_width / 2.0f;
00065         float text_height = font.Height( text.c_str(), 1, font_size );
00066         float text_height_half = text_height / 2.0f;
00067         float opacity_txt = selected ? 1.0 : enabled ? 0.6 : 0.4;
00068         float btn_opacity = selected ? 0.9 : enabled ? 0.6 : 0.4;
00069 
00070         TEXTURE_HANDLE * btn_tex = down ? &btn_down : &btn_up;
00071 
00072         // draw button face
00073         utility.DrawButton( center_x - text_width_half,
00074                             center_y - rel_height_half,
00075                             center_x + text_width_half,
00076                             center_y + rel_height_half,
00077                             0.02777, btn_tex, btn_opacity * opacity );
00078 
00079         // draw text
00080         font.Print( center_x - text_width_half, center_y - text_height_half, text.c_str(), 1, font_size, color_r, color_g, color_b, opacity_txt * opacity );
00081 
00082         // draw tip
00083         if( selected && ( tip != "" ) )
00084                 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 );
00085 }
00086 
00087 bool Button::MouseOver( float x, float y )
00088 {
00089         float min_x, min_y, max_x, max_y;
00090         min_x = center.GetXPercent() - ( rel_width / 2.0f );
00091         min_y = center.GetYPercent() - ( rel_height / 2.0f );
00092         max_x = center.GetXPercent() + ( rel_width / 2.0f );
00093         max_y = center.GetYPercent() + ( rel_height / 2.0f );
00094 
00095         return ( x > min_x ) && ( x < max_x ) && ( y > min_y ) && ( y < max_y );
00096 }
00097 
00098 string Button::MousePress( float x, float y )
00099 {
00100         down = true;
00101         return "";
00102 }
00103 
00104 string Button::MouseRelease( float x, float y )
00105 {
00106         down = false;
00107         return "";
00108 }
00109 
00110 void Button::Press()
00111 {
00112         down = true;
00113 }
00114 
00115 void Button::Release()
00116 {
00117         down = false;
00118 }

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