00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
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
00135 if ( index < 0 )
00136 index = int(teams_list.full_list.size())-1;
00137
00138
00139 tmp = teams_list.FindByIndex(index);
00140
00141
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
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
00173 if ( index >= int(teams_list.full_list.size()) )
00174 index = 0;
00175
00176
00177 tmp = teams_list.FindByIndex(index);
00178
00179
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
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
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
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 }