00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "spin_button.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 SpinButton::SpinButton (const std::string &label, const Rectanglei &rect,
00029 int value, int step, int min_value, int max_value,
00030 const Color& color, bool _shadowed)
00031 {
00032 position = rect.GetPosition();
00033 size = rect.GetSize();
00034 size.y = (*Font::GetInstance(Font::FONT_SMALL)).GetHeight();
00035 shadowed = _shadowed;
00036
00037 Profile *res = resource_manager.LoadXMLProfile( "graphism.xml", false);
00038
00039 txt_label = new Text(label, color, Font::GetInstance(Font::FONT_SMALL), shadowed);
00040
00041 if ( min_value != -1 && min_value <= value)
00042 m_min_value = min_value;
00043 else m_min_value = value/2;
00044
00045 if ( max_value != -1 && max_value >= value)
00046 m_max_value = max_value;
00047 else m_max_value = value*2;
00048
00049 txt_value = new Text("", color, Font::GetInstance(Font::FONT_SMALL), shadowed);
00050 SetValue(value);
00051
00052 std::ostringstream max_value_s;
00053 max_value_s << m_max_value ;
00054 uint max_value_w = (*Font::GetInstance(Font::FONT_SMALL)).GetWidth(max_value_s.str());
00055
00056 uint margin = 5;
00057
00058 m_plus = new Button( Point2i(position.x + size.x - 5, position.y), res, "menu/plus");
00059 m_minus = new Button( Point2i(position.x + size.x - max_value_w - 5 - 2 * margin, position.y), res, "menu/minus");
00060 resource_manager.UnLoadXMLProfile( res);
00061 m_step = step;
00062 }
00063
00064 SpinButton::~SpinButton ()
00065 {
00066 delete txt_label;
00067 delete txt_value;
00068 delete m_plus;
00069 delete m_minus;
00070 }
00071
00072 void SpinButton::SetSizePosition(const Rectanglei &rect)
00073 {
00074 StdSetSizePosition(rect);
00075
00076 std::ostringstream max_value_s;
00077 max_value_s << m_max_value ;
00078 uint max_value_w = (*Font::GetInstance(Font::FONT_SMALL)).GetWidth(max_value_s.str());
00079
00080 uint margin = 5;
00081
00082 m_plus->SetSizePosition( Rectanglei(position.x + size.x - 5, position.y, 5, 10) );
00083 m_minus->SetSizePosition( Rectanglei(position.x + size.x - max_value_w - 5 - 2 * margin, position.y, 5, 10) );
00084 }
00085
00086 void SpinButton::Draw(const Point2i &mousePosition, Surface& surf) const
00087 {
00088 txt_label->DrawTopLeft(position);
00089
00090 m_minus->Draw(mousePosition, surf);
00091 m_plus->Draw(mousePosition, surf);
00092
00093 uint center = (m_plus->GetPositionX() + 5 + m_minus->GetPositionX() )/2;
00094 txt_value->DrawCenterTop(center, position.y);
00095 }
00096
00097 Widget* SpinButton::Clic(const Point2i &mousePosition, uint button)
00098 {
00099 need_redrawing = true;
00100
00101 if( (button == SDL_BUTTON_WHEELDOWN && Contains(mousePosition)) ||
00102 (button == SDL_BUTTON_LEFT && m_minus->Contains(mousePosition)) ){
00103 SetValue(m_value - m_step);
00104 return this;
00105 } else
00106 if( (button == SDL_BUTTON_WHEELUP && Contains(mousePosition)) ||
00107 (button == SDL_BUTTON_LEFT && m_plus->Contains(mousePosition)) ){
00108 SetValue(m_value + m_step);
00109 return this;
00110 }
00111 return NULL;
00112 }
00113
00114 int SpinButton::GetValue() const
00115 {
00116 return m_value;
00117 }
00118
00119 void SpinButton::SetValue(int value)
00120 {
00121 m_value = BorneLong(value, m_min_value, m_max_value);
00122
00123 std::ostringstream value_s;
00124 value_s << m_value ;
00125
00126 std::string s(value_s.str());
00127 txt_value->Set(s);
00128 }