src/menu/map_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  * Map selection box
00020  *****************************************************************************/
00021 
00022 #include "map_selection_box.h"
00023 #include "../include/action_handler.h"
00024 #include "../map/maps_list.h"
00025 #include "../network/network.h"
00026 
00027 MapSelectionBox::MapSelectionBox(const Rectanglei &rect, bool _display_only) : 
00028   HBox(rect, true)
00029 {
00030   display_only = _display_only;
00031 
00032   Profile *res = resource_manager.LoadXMLProfile( "graphism.xml",false);
00033   
00034   AddWidget(new PictureWidget(Rectanglei(0,0,46,100), "menu/map_label"));
00035 
00036   // PreviousMap/NextMap buttons
00037   bt_map_plus = new Button(Point2i(0, 0), res, "menu/big_plus", false);
00038   bt_map_minus = new Button(Point2i(0, 0), res, "menu/big_minus", false);
00039 
00040   resource_manager.UnLoadXMLProfile(res);
00041 
00042   Box * tmp_map_box = new VBox( Rectanglei(0, 0,
00043                                            rect.GetSizeX()-63, 0), false);
00044   tmp_map_box->SetBorder( Point2i(0,0) );
00045   tmp_map_box->SetMargin(0);
00046 
00047   // compute margin width between previews
00048   uint map_preview_height = rect.GetSizeY() -2*10 -40;
00049 
00050   // Previews
00051   Box* previews_box = new HBox( Rectanglei(0, 0, 0, map_preview_height+10 ), false);
00052   previews_box->SetBorder( Point2i(10,0) );
00053 
00054    // compute margin width between previews
00055   uint map_preview_width = map_preview_height*4/3;
00056   uint total_width_previews = map_preview_width + map_preview_width*3;
00057 
00058   uint margin = 0;
00059 
00060   if ( uint(tmp_map_box->GetSizeX() - 20) > uint(total_width_previews + bt_map_plus->GetSizeX() + bt_map_minus->GetSizeX())) {
00061     margin = (tmp_map_box->GetSizeX() - 20 -
00062               (total_width_previews + bt_map_plus->GetSizeX() + bt_map_minus->GetSizeX()) ) / 6;
00063   }
00064 
00065   if (margin < 5) {
00066     margin = 5;
00067     uint total_size_wo_margin = tmp_map_box->GetSizeX() - 20 - 6*margin - bt_map_plus->GetSizeX() - bt_map_minus->GetSizeX();
00068     map_preview_width = (total_size_wo_margin)/4; // <= total = w + 4*(3/4)w
00069     map_preview_height = 3/4 * map_preview_width;
00070   }
00071 
00072   previews_box->SetMargin(margin);
00073 
00074   if (!display_only) {
00075     previews_box->AddWidget(bt_map_minus);
00076   } else {
00077     previews_box->AddWidget(new NullWidget(*bt_map_minus));
00078   }
00079 
00080   map_preview_before2 = new PictureWidget(Rectanglei(0, 0, map_preview_width *3/4, map_preview_height*3/4));
00081   previews_box->AddWidget(map_preview_before2);
00082 
00083   map_preview_before = new PictureWidget(Rectanglei(0, 0, map_preview_width *3/4, map_preview_height*3/4));
00084   previews_box->AddWidget(map_preview_before);
00085 
00086   // Selected map...
00087   map_preview_selected = new PictureWidget(Rectanglei(0, 0, map_preview_width, map_preview_height));
00088   previews_box->AddWidget(map_preview_selected);
00089 
00090   map_preview_after = new PictureWidget(Rectanglei(0, 0, map_preview_width *3/4, map_preview_height*3/4));
00091   previews_box->AddWidget(map_preview_after);
00092 
00093   map_preview_after2 = new PictureWidget(Rectanglei(0, 0, map_preview_width *3/4, map_preview_height*3/4));
00094   previews_box->AddWidget(map_preview_after2);
00095 
00096   if (!display_only) {
00097     previews_box->AddWidget(bt_map_plus);
00098   }else {
00099     previews_box->AddWidget(new NullWidget(*bt_map_plus));
00100   }
00101 
00102   tmp_map_box->AddWidget(previews_box);
00103   
00104   if (display_only) {
00105     map_preview_before2->Disable();
00106     map_preview_before->Disable();
00107     map_preview_after->Disable();
00108     map_preview_after2->Disable();
00109   }
00110 
00111 
00112   // Map information
00113   map_name_label = new Label("Map", Rectanglei(0,0,0,0), *Font::GetInstance(Font::FONT_SMALL, Font::BOLD), dark_gray_color, true, false);
00114   tmp_map_box->AddWidget(map_name_label);
00115 
00116   map_author_label = new Label("Author", Rectanglei(0,0,0,0), *Font::GetInstance(Font::FONT_SMALL), dark_gray_color, true, false);
00117   tmp_map_box->AddWidget(map_author_label);
00118 
00119   AddWidget(tmp_map_box);
00120 
00121   // Load Maps' list
00122   std::sort(MapsList::GetInstance()->lst.begin(), MapsList::GetInstance()->lst.end(), compareMaps);
00123   ChangeMap(MapsList::GetInstance()->GetActiveMapIndex());
00124 }
00125 
00126 void MapSelectionBox::ChangeMapDelta(int delta_index)
00127 {
00128   assert(!display_only);
00129 
00130   int tmp = selected_map_index + delta_index;
00131 
00132   ChangeMap(tmp);
00133 }
00134 
00135 void MapSelectionBox::ChangeMap(int index)
00136 {
00137   if (index < 0 || index > int(MapsList::GetInstance()->lst.size() - 1)) return;
00138   
00139   selected_map_index = index;
00140 
00141   // Callback other network players
00142   if(network.IsServer())
00143     {
00144       ActionHandler::GetInstance()->NewAction (new Action(Action::ACTION_SET_MAP, 
00145                                             MapsList::GetInstance()->lst[selected_map_index].ReadName()));
00146     }
00147 
00148   // Set Map information
00149   map_preview_selected->SetSurface(MapsList::GetInstance()->lst[selected_map_index].ReadPreview(), true);
00150   map_name_label->SetText(MapsList::GetInstance()->lst[selected_map_index].ReadName());
00151   map_author_label->SetText(MapsList::GetInstance()->lst[selected_map_index].ReadAuthorInfo());
00152 
00153   // Set previews
00154   if (selected_map_index > 0)
00155     map_preview_before->SetSurface(MapsList::GetInstance()->lst[selected_map_index-1].ReadPreview(), true);
00156   else
00157     map_preview_before->SetNoSurface();
00158 
00159   if (selected_map_index > 1)
00160     map_preview_before2->SetSurface(MapsList::GetInstance()->lst[selected_map_index-2].ReadPreview(), true);
00161   else
00162     map_preview_before2->SetNoSurface();
00163 
00164   if (selected_map_index+1 < MapsList::GetInstance()->lst.size() )
00165     map_preview_after->SetSurface(MapsList::GetInstance()->lst[selected_map_index+1].ReadPreview(), true);
00166   else
00167     map_preview_after->SetNoSurface();
00168 
00169   if (selected_map_index+2 < MapsList::GetInstance()->lst.size() )
00170     map_preview_after2->SetSurface(MapsList::GetInstance()->lst[selected_map_index+2].ReadPreview(), true);
00171   else
00172     map_preview_after2->SetNoSurface();
00173 }
00174 
00175 Widget* MapSelectionBox::Clic (const Point2i &mousePosition, uint button)
00176 {
00177   if (display_only) return NULL;
00178 
00179   if (!Contains(mousePosition)) return NULL;
00180 
00181   if (button == SDL_BUTTON_LEFT && map_preview_before2->Contains(mousePosition) ) {
00182     ChangeMapDelta(-2);
00183   } else if (   (button == SDL_BUTTON_LEFT && bt_map_minus->Contains(mousePosition))
00184              || (button == SDL_BUTTON_LEFT && map_preview_before->Contains(mousePosition))
00185              || (button == SDL_BUTTON_WHEELUP )) {
00186     ChangeMapDelta(-1);
00187   } else if (   (button == SDL_BUTTON_LEFT && bt_map_plus->Contains(mousePosition))
00188              || (button == SDL_BUTTON_LEFT && map_preview_after->Contains(mousePosition))
00189              || (button == SDL_BUTTON_WHEELDOWN)) {
00190     ChangeMapDelta(+1);
00191   } else if (map_preview_after2->Contains(mousePosition) ) {
00192     ChangeMapDelta(+2);
00193   }
00194 
00195   return NULL;
00196 }  
00197 
00198 void MapSelectionBox::ValidMapSelection()
00199 {
00200   MapsList::GetInstance()->SelectMapByIndex(selected_map_index);
00201 }
00202 
00203 void MapSelectionBox::ChangeMapCallback()
00204 {
00205   int index = MapsList::GetInstance()->GetActiveMapIndex();
00206   ChangeMap(index);
00207 }

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