src/gui/spin_button_big.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
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   // label can be multiline
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   // center the value
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 }

Generated on Mon Jan 1 13:10:57 2007 for Wormux by  doxygen 1.4.7