src/menu/teams_selection_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  *  Teams selection box
00020  *****************************************************************************/
00021 
00022 #include "teams_selection_box.h"
00023 #include "../team/teams_list.h"
00024 #include "../tool/i18n.h"
00025 
00026 TeamsSelectionBox::TeamsSelectionBox(const Rectanglei &rect) : HBox(rect, true)
00027 {
00028   AddWidget(new PictureWidget(Rectanglei(0,0,38,150), "menu/teams_label"));
00029 
00030   // How many teams ?
00031   teams_nb = new SpinButtonBig(_("Number of teams:"), Rectanglei(0, 0, 130, 30),
00032                                2, 1,
00033                                2, MAX_NB_TEAMS);
00034   AddWidget(teams_nb);
00035 
00036   Box * top_n_bottom_team_options = new VBox( Rectanglei(0, 0,
00037                                                          rect.GetSizeX() - teams_nb->GetSizeX() - 60, 0)
00038                                               ,false);
00039   top_n_bottom_team_options->SetBorder(Point2i(5,0));
00040   top_n_bottom_team_options->SetMargin(10);
00041   Box * top_team_options = new HBox ( Rectanglei(0, 0, 0, rect.GetSizeY()/2 - 20), false);
00042   Box * bottom_team_options = new HBox ( Rectanglei(0, 0, 0, rect.GetSizeY()/2 - 20), false);
00043   top_team_options->SetBorder(Point2i(0,0));
00044   bottom_team_options->SetBorder(Point2i(0,0));
00045 
00046   // Initialize teams
00047   uint team_w_size= top_n_bottom_team_options->GetSizeX() * 2 / MAX_NB_TEAMS;
00048 
00049   for (uint i=0; i < MAX_NB_TEAMS; i++) {
00050     std::string player_name = _("Player") ;
00051     char num_player[4];
00052     sprintf(num_player, " %d", i+1);
00053     player_name += num_player;
00054     teams_selections.push_back(new TeamBox(player_name, Rectanglei(0,0,team_w_size,rect.GetSizeY()/2)));
00055     if ( i%2 == 0)
00056       top_team_options->AddWidget(teams_selections.at(i));
00057     else
00058       bottom_team_options->AddWidget(teams_selections.at(i));
00059   }
00060 
00061   top_n_bottom_team_options->AddWidget(top_team_options);
00062   top_n_bottom_team_options->AddWidget(bottom_team_options);
00063 
00064   AddWidget(top_n_bottom_team_options); 
00065 
00066   // Load Teams' list
00067   teams_list.full_list.sort(compareTeams);
00068 
00069   TeamsList::iterator
00070     it=teams_list.playing_list.begin(),
00071     end=teams_list.playing_list.end();
00072 
00073   uint j=0;
00074   for (; it != end && j<teams_selections.size(); ++it, j++)
00075     {
00076       teams_selections.at(j)->SetTeam((**it), true);
00077     }
00078   
00079   if (j < 2) {
00080     SetNbTeams(2);
00081     teams_nb->SetValue(2);
00082   } else {
00083     teams_nb->SetValue(j);
00084   }
00085 }
00086 
00087 Widget* TeamsSelectionBox::Clic (const Point2i &mousePosition, uint button)
00088 {
00089   if (!Contains(mousePosition)) return NULL;
00090 
00091   if (teams_nb->Clic(mousePosition, button)){
00092     SetNbTeams(teams_nb->GetValue());
00093 
00094   } else {
00095     for (uint i=0; i<teams_selections.size() ; i++) {
00096 
00097       if ( teams_selections.at(i)->Contains(mousePosition) ) {
00098         
00099         Widget * w = teams_selections.at(i)->Clic(mousePosition, button);
00100 
00101         if ( w == NULL ) {
00102           if ( button == SDL_BUTTON_LEFT || button == SDL_BUTTON_WHEELDOWN ) {
00103             NextTeam(i);
00104           } else if ( button == SDL_BUTTON_RIGHT || button == SDL_BUTTON_WHEELUP ) {
00105             PrevTeam(i);
00106           }
00107         } else {
00108           return w;
00109         }
00110         break;
00111       }
00112     }
00113   }
00114 
00115   return NULL;
00116 }
00117 
00118 void TeamsSelectionBox::PrevTeam(int i)
00119 {
00120   if (teams_selections.at(i)->GetTeam() == NULL) return;
00121 
00122   bool to_continue;
00123   Team* tmp;
00124   int previous_index = -1, index;  
00125 
00126   teams_list.FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);
00127 
00128   index = previous_index-1;
00129 
00130   do 
00131     {
00132       to_continue = false;
00133 
00134       // select the last team if we are outside list
00135       if ( index < 0 )
00136         index = int(teams_list.full_list.size())-1;
00137 
00138       // Get the team at current index
00139       tmp = teams_list.FindByIndex(index);
00140       
00141       // Check if that team is already selected
00142       for (int j = 0; j < teams_nb->GetValue(); j++) {
00143         if (j!= i && tmp == teams_selections.at(j)->GetTeam()) {
00144           index--;
00145           to_continue = true;
00146           break;
00147         }
00148       }
00149       
00150       // We have found a team which is not selected
00151       if (tmp != NULL && !to_continue)
00152         teams_selections.at(i)->SetTeam(*tmp);
00153     } while ( index != previous_index && to_continue);
00154 }
00155 
00156 void TeamsSelectionBox::NextTeam(int i)
00157 {
00158   if (teams_selections.at(i)->GetTeam() == NULL) return;
00159 
00160   bool to_continue;
00161   Team* tmp;
00162   int previous_index = -1, index;
00163 
00164   teams_list.FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);
00165 
00166   index = previous_index+1;
00167 
00168   do 
00169     {
00170       to_continue = false;
00171 
00172       // select the first team if we are outside list
00173       if ( index >= int(teams_list.full_list.size()) )
00174         index = 0;
00175 
00176       // Get the team at current index
00177       tmp = teams_list.FindByIndex(index);
00178       
00179       // Check if that team is already selected
00180       for (int j = 0; j < teams_nb->GetValue(); j++) {
00181         if (j!= i && tmp == teams_selections.at(j)->GetTeam()) {
00182           index++;
00183           to_continue = true;
00184           break;
00185         }
00186       }
00187       
00188       // We have found a team which is not selected
00189       if (tmp != NULL && !to_continue)
00190         teams_selections.at(i)->SetTeam(*tmp);
00191     } while ( index != previous_index && to_continue);
00192 }
00193 
00194 void TeamsSelectionBox::SetNbTeams(uint nb_teams)
00195 {
00196   // we hide the useless teams selector
00197   for (uint i=nb_teams; i<teams_selections.size(); i++) {
00198     teams_selections.at(i)->ClearTeam();
00199   }
00200 
00201   for (uint i=0; i<nb_teams;i++) {
00202     if (teams_selections.at(i)->GetTeam() == NULL) {
00203       // we should find an available team
00204       teams_selections.at(i)->SetTeam(*(teams_list.FindByIndex(i)));
00205       NextTeam(i);
00206     }
00207   }
00208 }
00209 
00210 void TeamsSelectionBox::ValidTeamsSelection()
00211 {
00212   uint nb_teams=0;
00213   for (uint i=0; i < teams_selections.size(); i++) {
00214     if (teams_selections.at(i)->GetTeam() != NULL)
00215       nb_teams++;
00216   }
00217 
00218   if (nb_teams >= 2) {
00219     std::list<uint> selection;
00220 
00221     for (uint i=0; i < teams_selections.size(); i++) {
00222       if (teams_selections.at(i)->GetTeam() != NULL) {
00223         int index = -1;
00224         teams_selections.at(i)->ValidOptions();
00225         teams_list.FindById(teams_selections.at(i)->GetTeam()->GetId(), index);
00226         if (index > -1)
00227           selection.push_back(uint(index));
00228       }
00229     }
00230     teams_list.ChangeSelection (selection);
00231   }
00232 }

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