src/gui/spin_button_picture.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_picture.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 SpinButtonWithPicture::SpinButtonWithPicture (const std::string &label, const std::string &resource_id,
00029                                               const Rectanglei &rect,
00030                                               int value, int step, int min_value, int max_value)
00031 {
00032   position =  rect.GetPosition();
00033   size = rect.GetSize();
00034 
00035   Profile *res = resource_manager.LoadXMLProfile( "graphism.xml", false); 
00036   m_image = resource_manager.LoadImage(res, resource_id);
00037   resource_manager.UnLoadXMLProfile( res); 
00038 
00039   txt_label = new Text(label, dark_gray_color, Font::GetInstance(Font::FONT_NORMAL, Font::BOLD), false);
00040   txt_label->SetMaxWidth(GetSizeX());
00041 
00042   if ( min_value != -1 && min_value <= value)
00043     m_min_value = min_value;
00044   else m_min_value = value/2;
00045 
00046   if ( max_value != -1 && max_value >= value)
00047     m_max_value = max_value;
00048   else m_max_value = value*2;
00049 
00050   txt_value = new Text("", dark_gray_color, Font::GetInstance(Font::FONT_LARGE), false);
00051   SetValue(value);
00052 
00053   m_step = step;
00054 }
00055 
00056 SpinButtonWithPicture::~SpinButtonWithPicture ()
00057 {
00058   delete txt_label;
00059   delete txt_value;
00060 }
00061 
00062 void SpinButtonWithPicture::SetSizePosition(const Rectanglei &rect)
00063 {
00064   StdSetSizePosition(rect);
00065   txt_label->SetMaxWidth(GetSizeX());
00066 }
00067 
00068 void SpinButtonWithPicture::Draw(const Point2i &mousePosition, Surface& surf) const
00069 {
00070   // center the image on the first half
00071   uint tmp_x = GetPositionX() + (GetSizeX() - m_image.GetWidth())/4 ;
00072   uint tmp_y = GetPositionY() + (GetSizeY() - m_image.GetHeight() - txt_label->GetHeight() - 5) /2;
00073 
00074   AppWormux::GetInstance()->video.window.Blit(m_image, Point2i(tmp_x, tmp_y));
00075 
00076   tmp_x = GetPositionX() + (3*GetSizeX()/4);
00077   tmp_y = GetPositionY() + (GetSizeY()/2) - txt_label->GetHeight()/2;
00078 
00079   uint value_h = Font::GetInstance(Font::FONT_HUGE)->GetHeight();
00080 
00081   txt_value->DrawCenterTop(tmp_x, tmp_y - value_h/2);
00082 
00083   txt_label->DrawCenterTop( GetPositionX() + GetSizeX()/2, 
00084                             GetPositionY() + GetSizeY() - txt_label->GetHeight() );
00085 }
00086 
00087 Widget* SpinButtonWithPicture::Clic(const Point2i &mousePosition, uint button)
00088 {
00089   need_redrawing = true;
00090 
00091   if (button == SDL_BUTTON_LEFT && Contains(mousePosition)) {  
00092     
00093     m_value += m_step;
00094     if (m_value > m_max_value) SetValue(m_min_value);
00095     else SetValue(m_value);
00096 
00097   } else if (button == SDL_BUTTON_RIGHT && Contains(mousePosition)) {  
00098     
00099     m_value -= m_step;
00100     if (m_value < m_min_value) SetValue(m_max_value);
00101     else SetValue(m_value);
00102 
00103   } else if( button == SDL_BUTTON_WHEELDOWN && Contains(mousePosition) ) {
00104     
00105     SetValue(m_value - m_step);
00106     return this;
00107     
00108   } else if( button == SDL_BUTTON_WHEELUP && Contains(mousePosition) ) {
00109     
00110     SetValue(m_value + m_step);
00111     return this;
00112   }
00113   return NULL;
00114 }
00115 
00116 int SpinButtonWithPicture::GetValue() const
00117 {
00118   return m_value;
00119 }
00120 
00121 void SpinButtonWithPicture::SetValue(int value)  
00122 {
00123   m_value = BorneLong(value, m_min_value, m_max_value);  
00124 
00125   std::ostringstream value_s;
00126   value_s << m_value ;
00127 
00128   std::string s(value_s.str());
00129   txt_value->Set(s);
00130 
00131   ForceRedraw();
00132 }

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