00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "network_teams_selection_box.h"
00023 #include "../include/action_handler.h"
00024 #include "../team/teams_list.h"
00025 #include "../tool/i18n.h"
00026
00027 NetworkTeamsSelectionBox::NetworkTeamsSelectionBox(const Rectanglei &rect) : HBox(rect, true)
00028 {
00029 AddWidget(new PictureWidget(Rectanglei(0,0,38,150), "menu/teams_label"));
00030 Rectanglei rectZero(0, 0, 0, 0);
00031
00032
00033 local_teams_nb = new SpinButtonBig(_("Local teams:"), Rectanglei(0, 0, 130, 30),
00034 0, 1,
00035 0, NMAX_NB_TEAMS);
00036 AddWidget(local_teams_nb);
00037
00038 Box * top_n_bottom_team_options = new VBox( Rectanglei(0, 0,
00039 rect.GetSizeX() - local_teams_nb->GetSizeX() - 60, 0)
00040 ,false);
00041 top_n_bottom_team_options->SetBorder(Point2i(5,0));
00042 top_n_bottom_team_options->SetMargin(10);
00043 Box * top_team_options = new HBox ( Rectanglei(0, 0, 0, rect.GetSizeY()/2 - 20), false);
00044 Box * bottom_team_options = new HBox ( Rectanglei(0, 0, 0, rect.GetSizeY()/2 - 20), false);
00045 top_team_options->SetBorder(Point2i(0,0));
00046 bottom_team_options->SetBorder(Point2i(0,0));
00047
00048
00049 uint team_w_size= top_n_bottom_team_options->GetSizeX() * 2 / NMAX_NB_TEAMS;
00050
00051 for (uint i=0; i < NMAX_NB_TEAMS; i++) {
00052 std::string player_name = _("Player") ;
00053 char num_player[4];
00054 sprintf(num_player, " %d", i+1);
00055 player_name += num_player;
00056 teams_selections.push_back(new TeamBox(player_name, Rectanglei(0,0,team_w_size,rect.GetSizeY()/2)));
00057 if ( i%2 == 0)
00058 top_team_options->AddWidget(teams_selections.at(i));
00059 else
00060 bottom_team_options->AddWidget(teams_selections.at(i));
00061 }
00062
00063 top_n_bottom_team_options->AddWidget(top_team_options);
00064 top_n_bottom_team_options->AddWidget(bottom_team_options);
00065
00066 AddWidget(top_n_bottom_team_options);
00067
00068
00069 teams_list.full_list.sort(compareTeams);
00070 teams_list.Clear();
00071
00072
00073 for (uint i=0; i<teams_selections.size(); i++) {
00074 teams_selections.at(i)->ClearTeam();
00075 }
00076 }
00077
00078 Widget* NetworkTeamsSelectionBox::Clic (const Point2i &mousePosition, uint button)
00079 {
00080 if (!Contains(mousePosition)) return NULL;
00081
00082 uint current_nb_teams = local_teams_nb->GetValue();
00083
00084 if (local_teams_nb->Clic(mousePosition, button)){
00085 SetNbLocalTeams(local_teams_nb->GetValue(), current_nb_teams);
00086
00087 } else {
00088 for (uint i=0; i<teams_selections.size() ; i++) {
00089
00090 if ( teams_selections.at(i)->Contains(mousePosition) &&
00091 teams_selections.at(i)->IsLocal() ) {
00092
00093 Widget * w = teams_selections.at(i)->Clic(mousePosition, button);
00094
00095 if ( w == NULL ) {
00096 if ( button == SDL_BUTTON_LEFT || button == SDL_BUTTON_WHEELDOWN ) {
00097 NextTeam(i);
00098 } else if ( button == SDL_BUTTON_RIGHT || button == SDL_BUTTON_WHEELUP ) {
00099 PrevTeam(i);
00100 }
00101 } else {
00102 return w;
00103 }
00104 break;
00105 }
00106 }
00107 }
00108
00109 return NULL;
00110 }
00111
00112 void NetworkTeamsSelectionBox::PrevTeam(uint i)
00113 {
00114 if (teams_selections.at(i)->GetTeam() == NULL) return;
00115
00116 bool to_continue;
00117 Team* tmp;
00118 int previous_index = -1, index;
00119
00120 teams_list.FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);
00121
00122 index = previous_index-1;
00123
00124 do
00125 {
00126 to_continue = false;
00127
00128
00129 if ( index < 0 )
00130 index = int(teams_list.full_list.size())-1;
00131
00132
00133 tmp = teams_list.FindByIndex(index);
00134
00135
00136 for (uint j = 0; j < NMAX_NB_TEAMS; j++) {
00137 if (j!= i && tmp == teams_selections.at(j)->GetTeam()) {
00138 index--;
00139 to_continue = true;
00140 break;
00141 }
00142 }
00143
00144
00145 if (tmp != NULL && !to_continue) {
00146 SetLocalTeam(i, *tmp, true);
00147 }
00148 } while ( index != previous_index && to_continue);
00149 }
00150
00151 void NetworkTeamsSelectionBox::NextTeam(uint i,
00152 bool check_null_prev_team)
00153 {
00154 if (check_null_prev_team &&
00155 teams_selections.at(i)->GetTeam() == NULL)
00156 return;
00157
00158 bool to_continue;
00159 Team* tmp;
00160 int previous_index = -1, index;
00161
00162 if (check_null_prev_team) {
00163 teams_list.FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);
00164 }
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 (uint j = 0; j < NMAX_NB_TEAMS; 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 SetLocalTeam(i, *tmp, check_null_prev_team);
00191 }
00192 } while ( index != previous_index && to_continue);
00193 }
00194
00195 void NetworkTeamsSelectionBox::SetNbLocalTeams(uint nb_teams, uint previous_nb)
00196 {
00197
00198 int delta_team = nb_teams - previous_nb;
00199
00200 if (delta_team < 0) {
00201
00202 for (uint i=teams_selections.size()-1; int(i) >= 0 && delta_team < 0; i--) {
00203 if (teams_selections.at(i)->GetTeam() != NULL &&
00204 teams_selections.at(i)->IsLocal()) {
00205 RemoveLocalTeam(i);
00206 delta_team++;
00207 }
00208 }
00209 } else if (delta_team > 0) {
00210
00211 for (uint i=0; delta_team > 0 && i < teams_selections.size(); i++) {
00212 if (teams_selections.at(i)->GetTeam() == NULL) {
00213 AddLocalTeam(i);
00214 delta_team--;
00215 }
00216 }
00217
00218 }
00219 }
00220
00221 void NetworkTeamsSelectionBox::AddLocalTeam(uint i)
00222 {
00223
00224 NextTeam(i, false);
00225 }
00226
00227 void NetworkTeamsSelectionBox::RemoveLocalTeam(uint i)
00228 {
00229 if ( teams_selections.at(i)->GetTeam() != NULL ) {
00230 ActionHandler::GetInstance()->NewAction (new Action(Action::ACTION_DEL_TEAM,
00231 teams_selections.at(i)->GetTeam()->GetId()));
00232 }
00233 }
00234
00235 void NetworkTeamsSelectionBox::SetLocalTeam(uint i, Team& team, bool remove_previous_team)
00236 {
00237 if (remove_previous_team) {
00238 RemoveLocalTeam(i);
00239 }
00240
00241 team.SetLocal();
00242 #ifdef WIN32
00243 team.SetPlayerName(getenv("USERNAME"));
00244 #else
00245 team.SetPlayerName(getenv("USER"));
00246 #endif
00247 std::string team_id = team.GetId();
00248
00249 Action* a = new Action(Action::ACTION_NEW_TEAM, team_id);
00250 a->Push(team.GetPlayerName());
00251 a->Push(int(team.GetNbCharacters()));
00252 ActionHandler::GetInstance()->NewAction (a);
00253 }
00254
00255 void NetworkTeamsSelectionBox::AddTeamCallback(std::string team_id)
00256 {
00257 for (uint i=0; i < teams_selections.size(); i++) {
00258 if (teams_selections.at(i)->GetTeam() == NULL) {
00259 int index = 0;
00260 Team * tmp = teams_list.FindById(team_id, index);
00261
00262 teams_selections.at(i)->SetTeam(*tmp, true);
00263 break;
00264 }
00265 }
00266
00267
00268 uint nb_local_teams=0;
00269 for (uint i=0; i < teams_selections.size(); i++) {
00270 if (teams_selections.at(i)->GetTeam() != NULL &&
00271 teams_selections.at(i)->IsLocal()) {
00272 nb_local_teams++;
00273 }
00274 }
00275 local_teams_nb->SetValue(nb_local_teams);
00276 }
00277
00278 void NetworkTeamsSelectionBox::UpdateTeamCallback(std::string team_id)
00279 {
00280 for (uint i=0; i < teams_selections.size(); i++) {
00281 if (teams_selections.at(i)->GetTeam() != NULL &&
00282 teams_selections.at(i)->GetTeam()->GetId() == team_id) {
00283 int index = 0;
00284 Team * tmp = teams_list.FindById(team_id, index);
00285
00286 teams_selections.at(i)->SetTeam(*tmp, true);
00287 std::cout << "Update " << team_id << std::endl;
00288 break;
00289 }
00290 }
00291 }
00292
00293 void NetworkTeamsSelectionBox::DelTeamCallback(std::string team_id)
00294 {
00295 for (uint i=0; i < teams_selections.size(); i++) {
00296 if (teams_selections.at(i)->GetTeam() != NULL &&
00297 teams_selections.at(i)->GetTeam()->GetId() == team_id) {
00298
00299 teams_selections.at(i)->ClearTeam();
00300 break;
00301 }
00302 }
00303
00304
00305 uint nb_local_teams=0;
00306 for (uint i=0; i < teams_selections.size(); i++) {
00307 if (teams_selections.at(i)->GetTeam() != NULL &&
00308 teams_selections.at(i)->IsLocal()) {
00309 nb_local_teams++;
00310 }
00311 }
00312 local_teams_nb->SetValue(nb_local_teams);
00313 }
00314
00315
00316 void NetworkTeamsSelectionBox::ValidTeamsSelection()
00317 {
00318 std::list<uint> selection;
00319
00320 uint nb_teams=0;
00321 for (uint i=0; i < teams_selections.size(); i++) {
00322 if (teams_selections.at(i)->GetTeam() != NULL)
00323 nb_teams++;
00324 }
00325
00326 if (nb_teams >= 2) {
00327 std::list<uint> selection;
00328
00329 for (uint i=0; i < teams_selections.size(); i++) {
00330 if (teams_selections.at(i)->GetTeam() != NULL) {
00331 int index = -1;
00332 teams_selections.at(i)->ValidOptions();
00333 teams_list.FindById(teams_selections.at(i)->GetTeam()->GetId(), index);
00334 if (index > -1)
00335 selection.push_back(uint(index));
00336 }
00337 }
00338 teams_list.ChangeSelection (selection);
00339 }
00340 }