00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "spin_button_big.h"
00021 #include <sstream>
00022 #include <iostream>
00023 #include "../include/app.h"
00024 #include "../tool/math_tools.h"
00025 #include "../tool/resource_manager.h"
00026 #include "../graphic/font.h"
00027
00028 SpinButtonBig::SpinButtonBig (const std::string &label, const Rectanglei &rect,
00029 int value, int step, int min_value, int max_value)
00030 {
00031 position = rect.GetPosition();
00032 size = rect.GetSize();
00033
00034 Profile *res = resource_manager.LoadXMLProfile( "graphism.xml", false);
00035
00036 txt_label = new Text(label, dark_gray_color, Font::GetInstance(Font::FONT_NORMAL, Font::BOLD), false);
00037 txt_label->SetMaxWidth(GetSizeX());
00038
00039 if ( min_value != -1 && min_value <= value)
00040 m_min_value = min_value;
00041 else m_min_value = value/2;
00042
00043 if ( max_value != -1 && max_value >= value)
00044 m_max_value = max_value;
00045 else m_max_value = value*2;
00046
00047 txt_value = new Text("", dark_gray_color, Font::GetInstance(Font::FONT_HUGE), false);
00048 SetValue(value);
00049
00050 std::ostringstream max_value_s;
00051 max_value_s << m_max_value ;
00052 uint max_value_w = (*Font::GetInstance(Font::FONT_HUGE)).GetWidth(max_value_s.str());
00053
00054 uint margin = 5;
00055
00056 m_plus = new Button( Point2i(position.x + size.x - 5, position.y), res, "menu/big_plus");
00057 m_minus = new Button( Point2i(position.x + size.x - max_value_w - 5 - 2 * margin, position.y), res, "menu/big_minus");
00058 resource_manager.UnLoadXMLProfile( res);
00059 m_step = step;
00060 }
00061
00062 SpinButtonBig::~SpinButtonBig ()
00063 {
00064 delete txt_label;
00065 delete txt_value;
00066 delete m_plus;
00067 delete m_minus;
00068 }
00069
00070 void SpinButtonBig::SetSizePosition(const Rectanglei &rect)
00071 {
00072 StdSetSizePosition(rect);
00073
00074
00075 txt_label->SetMaxWidth(GetSizeX());
00076
00077 std::ostringstream max_value_s;
00078 max_value_s << m_max_value ;
00079 uint max_value_w = Font::GetInstance(Font::FONT_HUGE)->GetWidth(max_value_s.str());
00080
00081
00082 uint center_x = GetPositionX() + GetSizeX()/2 ;
00083 uint center_y = GetPositionY() + GetSizeY()/2 - txt_label->GetHeight()/2;
00084
00085 m_minus->SetSizePosition( Rectanglei(center_x - max_value_w/2 - m_minus->GetSizeX() - 5,
00086 center_y - m_minus->GetSizeY()/2,
00087 m_minus->GetSizeX(), m_minus->GetSizeY()) );
00088 m_plus->SetSizePosition( Rectanglei(center_x + max_value_w/2 + 5,
00089 center_y - m_plus->GetSizeY()/2,
00090 m_plus->GetSizeX(), m_plus->GetSizeY()) );
00091 }
00092
00093 void SpinButtonBig::Draw(const Point2i &mousePosition, Surface& surf) const
00094 {
00095 m_minus->Draw(mousePosition, surf);
00096 m_plus->Draw(mousePosition, surf);
00097
00098 uint center_x = GetPositionX() + (GetSizeX()/2);
00099 uint center_y = GetPositionY() + (GetSizeY()/2) - txt_label->GetHeight()/2;
00100 uint value_h = Font::GetInstance(Font::FONT_HUGE)->GetHeight();
00101
00102 txt_value->DrawCenterTop(center_x, center_y - value_h/2);
00103
00104 txt_label->DrawCenterTop( GetPositionX() + GetSizeX()/2,
00105 GetPositionY() + GetSizeY() - txt_label->GetHeight() );
00106 }
00107
00108 Widget* SpinButtonBig::Clic(const Point2i &mousePosition, uint button)
00109 {
00110 need_redrawing = true;
00111
00112 if( (button == SDL_BUTTON_WHEELDOWN && Contains(mousePosition)) ||
00113 (button == SDL_BUTTON_LEFT && m_minus->Contains(mousePosition)) ){
00114 SetValue(m_value - m_step);
00115 return this;
00116 } else
00117 if( (button == SDL_BUTTON_WHEELUP && Contains(mousePosition)) ||
00118 (button == SDL_BUTTON_LEFT && m_plus->Contains(mousePosition)) ){
00119 SetValue(m_value + m_step);
00120 return this;
00121 }
00122 return NULL;
00123 }
00124
00125 int SpinButtonBig::GetValue() const
00126 {
00127 return m_value;
00128 }
00129
00130 void SpinButtonBig::SetValue(int value)
00131 {
00132 m_value = BorneLong(value, m_min_value, m_max_value);
00133
00134 std::ostringstream value_s;
00135 value_s << m_value ;
00136
00137 std::string s(value_s.str());
00138 txt_value->Set(s);
00139 }