src/gui/box.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  * Vertical or Horizontal Box
00020  *****************************************************************************/
00021 
00022 #include <SDL_gfxPrimitives.h>
00023 #include "box.h"
00024 #include "../graphic/colors.h"
00025 #include "../include/app.h"
00026 
00027 Box::Box(const Rectanglei &rect, bool _visible) : WidgetList( rect )
00028 {
00029   visible = _visible;
00030   margin = 5;
00031   border.SetValues(5, 5);
00032 }
00033 
00034 Box::~Box()
00035 {
00036 }
00037 
00038 void Box::Redraw(const Rectanglei& rect,
00039                  Surface& surf)
00040 {
00041   // Redraw bottom layer container
00042   WidgetList::Redraw(rect, surf);
00043 
00044   if ( visible ) {
00045     // Redraw
00046     surf.BoxColor(rect, defaultOptionColorBox);
00047   }
00048 }
00049 
00050 void Box::Update(const Point2i &mousePosition,
00051                  const Point2i &lastMousePosition,
00052                  Surface& surf)
00053 {
00054   if (need_redrawing) {
00055     Draw(mousePosition, surf);
00056   }
00057 
00058   WidgetList::Update(mousePosition, surf);
00059   need_redrawing = false;
00060 }
00061 
00062 void Box::Draw(const Point2i &mousePosition,
00063                Surface& surf) const
00064 {
00065   Rectanglei rect(position, size);
00066         
00067   if( visible ){
00068     surf.BoxColor(rect, defaultOptionColorBox);
00069     surf.RectangleColor(rect, defaultOptionColorRect,2);
00070   }
00071 }
00072 
00073 Widget* Box::Clic (const Point2i &mousePosition, uint button)
00074 {
00075   return WidgetList::Clic(mousePosition, button);
00076 }
00077 
00078 void Box::SetMargin (uint _margin)
00079 {
00080   margin = _margin;
00081 }
00082 
00083 void Box::SetBorder (const Point2i &newBorder)
00084 {
00085   border = newBorder;
00086 }
00087 
00088 // --------------------------------------------------
00089 
00090 VBox::VBox(const Rectanglei &rect, bool _visible) : Box(rect, _visible)
00091 {
00092   size.y = 1;
00093 }
00094 
00095 void VBox::DelFirstWidget()
00096 {
00097   int w_height = widget_list.front()->GetSizeY();
00098   WidgetList::DelFirstWidget();
00099   //Make all remaining widget go up:
00100   for( std::list<Widget*>::iterator it = widget_list.begin(); 
00101        it != widget_list.end(); 
00102        ++it )
00103   {
00104     (*it)->SetPositionY((*it)->GetPositionY() - w_height - margin);
00105   }
00106   size.y -= w_height + margin;
00107 }
00108 
00109 void VBox::AddWidget(Widget * a_widget)
00110 {
00111   assert(a_widget != NULL);
00112 
00113   uint _y;
00114 
00115   if(!widget_list.empty())
00116     _y = widget_list.back()->GetPositionY() + widget_list.back()->GetSizeY();
00117   else
00118     _y = position.y + border.y - margin;
00119 
00120   a_widget->SetSizePosition(Rectanglei(position.x + border.x, 
00121                             _y + margin, 
00122                             size.x - 2 * border.x,
00123                             a_widget->GetSizeY() ));
00124 
00125   size.y = a_widget->GetPositionY() + a_widget->GetSizeY() - position.y + border.y;
00126   WidgetList::AddWidget(a_widget);
00127 }
00128 
00129 void VBox::SetSizePosition(const Rectanglei &rect)
00130 {
00131   position = rect.GetPosition();
00132   int _y = rect.GetPositionY();
00133   std::list<Widget *>::iterator it;
00134   for( it = widget_list.begin(); 
00135        it != widget_list.end(); 
00136        ++it ){
00137 
00138     if( it == widget_list.begin() )
00139       _y += border.y - margin;
00140 
00141     (*it)->SetSizePosition( Rectanglei(position.x + border.x,
00142                            _y + margin,
00143                            (*it)->GetSizeX(),
00144                            (*it)->GetSizeY() ));
00145     _y = (*it)->GetPositionY() + (*it)->GetSizeY();
00146   }
00147 }
00148 
00149 // --------------------------------------------------
00150 
00151 HBox::HBox(const Rectanglei &rect, bool _visible) : Box(rect, _visible)
00152 {
00153   size.x = 1;
00154 }
00155 
00156 void HBox::AddWidget(Widget * a_widget)
00157 {
00158   assert(a_widget != NULL);
00159 
00160   uint _x;
00161 
00162   if (!widget_list.empty())
00163     _x = widget_list.back()->GetPositionX() + widget_list.back()->GetSizeX();
00164   else 
00165     _x = position.x + border.x - margin;
00166 
00167   a_widget->SetSizePosition( Rectanglei(_x + margin, 
00168                             position.y + border.y, 
00169                             a_widget->GetSizeX(), 
00170                             size.y - 2 * border.y) );
00171 
00172   size.x = a_widget->GetPositionX() + a_widget->GetSizeX() - position.x + border.x;
00173 
00174   WidgetList::AddWidget(a_widget);
00175 }
00176 
00177 void HBox::SetSizePosition(const Rectanglei &rect)
00178 {
00179   position = rect.GetPosition();
00180   int _x = rect.GetPositionX();
00181         
00182   std::list<Widget *>::iterator it;
00183   for( it = widget_list.begin(); 
00184        it != widget_list.end(); 
00185        ++it ){
00186 
00187     if( it == widget_list.begin() )
00188       _x += border.x - margin;
00189 
00190     (*it)->SetSizePosition( Rectanglei(_x + margin,
00191                            position.y + border.y,
00192                            (*it)->GetSizeX(),
00193                            (*it)->GetSizeY()) );
00194     _x = (*it)->GetPositionX()+ (*it)->GetSizeX();
00195   }
00196 }
00197 

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