src/gui/progress_bar.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  * Progress bar for GUI.
00020  *****************************************************************************/
00021 
00022 #include "progress_bar.h"
00023 #include <SDL.h>
00024 #include "../include/app.h"
00025 #include "../map/map.h"
00026 #include "../tool/math_tools.h"
00027 
00028 ProgressBar::ProgressBar(){
00029    border_color.SetColor(0, 0, 0, 255);
00030    value_color.SetColor(255, 255, 255, 255);
00031    background_color.SetColor(100, 100 ,100, 255);
00032    x = y = larg = haut = 0;
00033    val = min = max = 0;
00034    m_use_ref_val = false;
00035 }
00036 
00037 void ProgressBar::SetBorderColor(Color color){
00038    border_color = color;
00039 }
00040 
00041 void ProgressBar::SetBackgroundColor(Color color){
00042    background_color = color;
00043 }
00044 
00045 void ProgressBar::SetValueColor(Color color){
00046    value_color = color;
00047 }
00048 
00049 void ProgressBar::InitPos (uint px, uint py, uint plarg, uint phaut){
00050   assert (3 <= plarg);
00051   assert (3 <= phaut);
00052   x = px;
00053   y = py;
00054   larg = plarg;
00055   haut = phaut;
00056 
00057   image.NewSurface(Point2i(larg, haut), SDL_SWSURFACE|SDL_SRCALPHA, true);
00058 }
00059 
00060 /*
00061  * intitialize the progress bar
00062  * orientation is set with ProgressBar::PROG_BAR_VERTICAL or
00063  *                         ProgressBar::PROG_BAR_HORIZONTAL
00064  * default orientation is ProgressBar::PROG_BAR_HORIZONTAL
00065  */
00066 void ProgressBar::InitVal (long pval, long pmin, long pmax,
00067     enum orientation porientation){
00068   assert (pmin != pmax);
00069   assert (pmin < pmax);
00070   val = pval;
00071   min = pmin;
00072   max = pmax;
00073   orientation = porientation;
00074   val_barre = CalculeValBarre(val);
00075 }
00076 
00077 void ProgressBar::UpdateValue (long pval){
00078   val = CalculeVal(pval);
00079   val_barre = CalculeValBarre(val);
00080 }
00081 
00082 uint ProgressBar::CalculeVal (long val) const{
00083   return BorneLong(val, min, max);
00084 }
00085 
00086 uint ProgressBar::CalculeValBarre (long val) const{
00087   if(orientation == PROG_BAR_HORIZONTAL)
00088     return ( CalculeVal(val) -min)*(larg-2)/(max-min);
00089   else
00090     return ( CalculeVal(val) -min)*(haut-2)/(max-min);
00091 }
00092 
00093 void ProgressBar::Draw() const{
00094   DrawXY( Point2i(x, y) );
00095 }
00096 
00097 // TODO pass a Surface as parameter
00098 void ProgressBar::DrawXY(const Point2i &pos) const{
00099   int begin, end;
00100 
00101   // Bordure
00102   image.Fill(border_color);
00103 
00104   // Fond
00105   Rectanglei r_back(1, 1, larg - 2, haut - 2);
00106   image.FillRect(r_back, background_color);
00107 
00108   // Valeur
00109   if (m_use_ref_val) {
00110     int ref = CalculeValBarre (m_ref_val);
00111     if (val < m_ref_val) { // FIXME hum, this seems buggy
00112       begin = 1+val_barre;
00113       end = 1+ref;
00114     } else {
00115       begin = 1+ref;
00116       end = 1+val_barre;
00117     }
00118   } else {
00119     begin = 1;
00120     end = 1+val_barre;
00121   }
00122 
00123   Rectanglei r_value;
00124   if(orientation == PROG_BAR_HORIZONTAL)
00125     r_value = Rectanglei(begin, 1, end - begin, haut - 2);
00126   else
00127     r_value = Rectanglei(1, haut - end + begin - 1, larg - 2, end -1 );
00128 
00129   image.FillRect(r_value, value_color);
00130 
00131   if (m_use_ref_val) {
00132     int ref = CalculeValBarre (m_ref_val);
00133     Rectanglei r_ref;
00134     if(orientation == PROG_BAR_HORIZONTAL)
00135        r_ref = Rectanglei(1 + ref, 1, 1, haut - 2);
00136     else
00137        r_ref = Rectanglei(1, 1 + ref, larg - 2, 1);
00138     image.FillRect(r_ref, border_color);
00139   }
00140 
00141   // Marqueurs
00142   marqueur_it_const it=marqueur.begin(), fin=marqueur.end();
00143   for (; it != fin; ++it)
00144   {
00145     Rectanglei r_marq;
00146     if(orientation == PROG_BAR_HORIZONTAL)
00147       r_marq = Rectanglei(1 + it->val, 1, 1, haut - 2);
00148     else
00149       r_marq = Rectanglei(1, 1 + it->val, larg -2, 1);
00150     image.FillRect( r_marq, it->color);
00151   }
00152   Rectanglei dst(pos.x, pos.y, larg, haut);
00153   AppWormux::GetInstance()->video.window.Blit(image, pos);
00154 
00155   world.ToRedrawOnScreen(dst);
00156 }
00157 
00158 // Ajoute/supprime un marqueur
00159 ProgressBar::marqueur_it ProgressBar::AddTag (long val, const Color& color){
00160   marqueur_t m;
00161 
00162   m.val = CalculeValBarre (val);
00163   m.color = color;
00164   marqueur.push_back (m);
00165 
00166   return --marqueur.end();
00167 }
00168 
00169 void ProgressBar::ResetTag(){
00170   marqueur.clear();
00171 }
00172 
00173 void ProgressBar::SetReferenceValue (bool use, long value){
00174   m_use_ref_val = use;
00175   m_ref_val = CalculeVal(value);
00176 }
00177 

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