src/menu/network_menu.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  * Game menu
00020  *****************************************************************************/
00021 
00022 #include "network_menu.h"
00023 
00024 #include "../game/game.h"
00025 #include "../game/config.h"
00026 #include "../game/game_mode.h"
00027 #include "../graphic/video.h"
00028 #include "../graphic/font.h"
00029 #include "../map/maps_list.h"
00030 #include "../network/network.h"
00031 #include "../include/app.h"
00032 #include "../include/action_handler.h"
00033 #include "../team/teams_list.h"
00034 #include "../tool/i18n.h"
00035 #include "../tool/string_tools.h"
00036 
00037 const uint MARGIN_TOP    = 5;
00038 const uint MARGIN_SIDE   = 5;
00039 const uint MARGIN_BOTTOM = 70;
00040 
00041 const uint TEAMS_W = 160;
00042 const uint TEAMS_BOX_H = 180;
00043 const uint TEAM_LOGO_H = 48;
00044 const uint OPTIONS_BOX_H = 150;
00045 
00046 NetworkMenu::NetworkMenu() :
00047   Menu("menu/bg_network")
00048 {
00049   Profile *res = resource_manager.LoadXMLProfile( "graphism.xml",false);
00050   Rectanglei rectZero(0, 0, 0, 0);
00051   Rectanglei stdRect (0, 0, 130, 30);
00052 
00053   Surface window = AppWormux::GetInstance()->video.window;
00054 
00055   // Calculate main box size
00056   uint mainBoxWidth = window.GetWidth() - 2*MARGIN_SIDE;
00057   uint mapBoxHeight = (window.GetHeight() - MARGIN_TOP - MARGIN_BOTTOM - 2*MARGIN_SIDE) 
00058     - TEAMS_BOX_H - OPTIONS_BOX_H;
00059 
00060   // ################################################
00061   // ##  TEAM SELECTION
00062   // ################################################
00063   team_box = new NetworkTeamsSelectionBox(Rectanglei(MARGIN_SIDE, MARGIN_TOP,
00064                                               mainBoxWidth, TEAMS_BOX_H));
00065   widgets.AddWidget(team_box);
00066 
00067   // ################################################
00068   // ##  MAP SELECTION
00069   // ################################################
00070   if(network.IsServer()) {
00071     map_box = new MapSelectionBox( Rectanglei(MARGIN_SIDE, team_box->GetPositionY()+team_box->GetSizeY()+ MARGIN_SIDE,
00072                                               mainBoxWidth, mapBoxHeight),
00073                                    false);
00074   } else {
00075     map_box = new MapSelectionBox( Rectanglei(MARGIN_SIDE, team_box->GetPositionY()+team_box->GetSizeY()+ MARGIN_SIDE,
00076                                               mainBoxWidth, mapBoxHeight),
00077                                    true);
00078   }
00079   widgets.AddWidget(map_box);
00080 
00081   // ################################################
00082   // ##  GAME OPTIONS
00083   // ################################################
00084 
00085   options_box = new HBox( Rectanglei(MARGIN_SIDE, map_box->GetPositionY()+map_box->GetSizeY()+ MARGIN_SIDE,
00086                                      mainBoxWidth, OPTIONS_BOX_H), true);
00087   options_box->AddWidget(new PictureWidget(Rectanglei(0,0,39,128), "menu/mode_label"));
00088   
00089   Box* tmp_box = new VBox( Rectanglei(0,0, 200,0), false);
00090   player_number = new SpinButton(_("Max number of players:"), rectZero, 
00091                                  GameMode::GetInstance()->max_teams, 1, 2, 
00092                                  GameMode::GetInstance()->max_teams);
00093   
00094   tmp_box->AddWidget(player_number);
00095 
00096   connected_players = new Label(Format(ngettext("%i player connected", "%i players connected", 0), 0), 
00097                                 rectZero, *Font::GetInstance(Font::FONT_SMALL));
00098   tmp_box->AddWidget(connected_players);
00099 
00100   inited_players = new Label(Format(ngettext("%i player ready", "%i players ready", 0), 0), 
00101                              rectZero, *Font::GetInstance(Font::FONT_SMALL));
00102   tmp_box->AddWidget(inited_players);
00103 
00104   options_box->AddWidget(tmp_box);
00105   widgets.AddWidget(options_box);
00106 
00107   // ################################################
00108   // ##  CHAT BOX
00109   // ################################################
00110   VBox* chat_box = new VBox(Rectanglei(options_box->GetPositionX() + options_box->GetSizeX() + MARGIN_SIDE,
00111                                        options_box->GetPositionY(),
00112                                        mainBoxWidth - options_box->GetSizeX() - MARGIN_SIDE,
00113                                        OPTIONS_BOX_H), false);
00114   chat_box->SetBorder(Point2i(0,0));
00115   
00116   msg_box = new MsgBox(Rectanglei( 0, 0, 400, OPTIONS_BOX_H - 20), Font::GetInstance(Font::FONT_SMALL));  
00117   msg_box->NewMessage(_("Join #wormux on irc.freenode.net to find some opponents."));
00118   msg_box->NewMessage(_("WARNING! Disconnections are not yet handled. So you have to restart Wormux after each disconnection!"));
00119 
00120   chat_box->AddWidget(msg_box);
00121 
00122   HBox* tmp2_box = new HBox(Rectanglei(0,0,chat_box->GetSizeX(),16), false);
00123   tmp2_box->SetMargin(4);
00124   tmp2_box->SetBorder(Point2i(0,0));
00125   line_to_send_tbox = new TextBox(" ",
00126                                   Rectanglei(0, 0, chat_box->GetSizeX()-20, 0), 
00127                                   *Font::GetInstance(Font::FONT_SMALL));
00128   tmp2_box->AddWidget(line_to_send_tbox);
00129   
00130   send_txt_bt = new Button(Point2i(0,0), res, "menu/send_txt", true);
00131   tmp2_box->AddWidget(send_txt_bt);
00132 
00133   chat_box->AddWidget(tmp2_box);
00134 
00135   widgets.AddWidget(chat_box);
00136 
00137   resource_manager.UnLoadXMLProfile(res);
00138 }
00139 
00140 NetworkMenu::~NetworkMenu()
00141 {
00142 }
00143 
00144 void NetworkMenu::OnClic(const Point2i &mousePosition, int button)
00145 {
00146   Widget* w = widgets.Clic(mousePosition, button);
00147 
00148   if(w == player_number)
00149   {
00150     network.max_player_number = player_number->GetValue();
00151   }
00152 
00153   if(w == send_txt_bt)
00154   {
00155     std::string empty = "";
00156     network.SendChatMessage(line_to_send_tbox->GetText());
00157     line_to_send_tbox->SetText(empty);
00158   }
00159 }
00160 
00161 void NetworkMenu::SaveOptions()
00162 {
00163   // map
00164   map_box->ValidMapSelection();
00165 
00166   // team
00167   team_box->ValidTeamsSelection();
00168 
00169   //Save options in XML
00170 //  Config::GetInstance()->Save();
00171 }
00172 
00173 void NetworkMenu::__sig_ok()
00174 {
00175   if(network.IsClient())
00176   {
00177     // Wait for the server, and stay in the menu map / team can still be changed
00178     Action a(Action::ACTION_CHANGE_STATE);
00179     network.SendAction(&a);
00180     while(network.state != Network::NETWORK_INIT_GAME)
00181     {
00182       Display(Point2i(-1,-1));
00183     }
00184   }
00185 
00186   SaveOptions();
00187   Game::GetInstance()->Start();
00188   network.network_menu = NULL;
00189 }
00190 
00191 void NetworkMenu::sig_ok()
00192 {
00193   if(network.IsServer())
00194   {
00195     if(network.connected_player != network.client_inited)
00196     {
00197       int nbr = network.connected_player - network.client_inited;
00198       std::string pl = Format(ngettext("Wait! %i player is not ready yet!", "Wait! %i players are not ready yet!", nbr), nbr);
00199       msg_box->NewMessage(pl);
00200       return;
00201     }
00202   }
00203   Menu::sig_ok();
00204 }
00205 
00206 void NetworkMenu::__sig_cancel()
00207 {
00208   network.Disconnect();
00209 }
00210 
00211 void NetworkMenu::Draw(const Point2i &mousePosition)
00212 {
00213   if(network.IsConnected())
00214   {
00215     //Refresh the number of connected players:
00216     int nbr = network.connected_player;
00217     std::string pl = Format(ngettext("%i player connected", "%i players connected", nbr), nbr);
00218     if(connected_players->GetText() != pl)
00219       connected_players->SetText(pl);
00220     //Refresh the number of players ready:
00221     nbr = network.client_inited;
00222     pl = Format(ngettext("%i player ready", "%i players ready", nbr), nbr);
00223     if(inited_players->GetText() != pl)
00224       inited_players->SetText(pl);
00225   }
00226   else {
00227     close_menu = true;
00228   }
00229   ActionHandler * action_handler = ActionHandler::GetInstance();
00230   action_handler->ExecActions();
00231 }
00232 
00233 void NetworkMenu::DelTeamCallback(std::string team_id)
00234 {
00235   if( close_menu )
00236     return;
00237 
00238   // Called from the action handler
00239   team_box->DelTeamCallback(team_id);
00240 }
00241 
00242 void NetworkMenu::AddTeamCallback(std::string team_id)
00243 {
00244   if ( close_menu )
00245     return;
00246 
00247   team_box->AddTeamCallback(team_id);
00248   msg_box->NewMessage(team_id + " selected");
00249 }
00250 
00251 void NetworkMenu::UpdateTeamCallback(std::string team_id)
00252 {
00253   if ( close_menu )
00254     return;
00255 
00256   team_box->UpdateTeamCallback(team_id);
00257   msg_box->NewMessage(team_id + " updated");
00258 }
00259 
00260 void NetworkMenu::ChangeMapCallback()
00261 {
00262   assert( !close_menu );
00263   // Called from the action handler
00264 
00265   map_box->ChangeMapCallback();
00266 }
00267 
00268 void NetworkMenu::ReceiveMsgCallback(std::string msg)
00269 {
00270   msg_box->NewMessage(msg);
00271 }

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