src/menu/network_connection_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  * Network connection menu: this menu allows the user to choose between
00020  * hosting a game or connecting to a server.
00021  *****************************************************************************/
00022 
00023 
00024 #include "network_connection_menu.h"
00025 #include "network_menu.h"
00026 #include "internet_menu.h"
00027 
00028 #include "../game/game.h"
00029 #include "../game/config.h"
00030 #include "../game/game_mode.h"
00031 #include "../graphic/video.h"
00032 #include "../graphic/font.h"
00033 #include "../map/maps_list.h"
00034 #include "../network/network.h"
00035 #include "../network/index_server.h"
00036 #include "../include/app.h"
00037 #include "../include/action_handler.h"
00038 #include "../team/teams_list.h"
00039 #include "../tool/i18n.h"
00040 #include "../tool/string_tools.h"
00041 
00042 NetworkConnectionMenu::NetworkConnectionMenu() :
00043   Menu("menu/bg_network", vOkCancel)
00044 {
00045   Profile *res = resource_manager.LoadXMLProfile( "graphism.xml",false);
00046   Rectanglei rectZero(0, 0, 0, 0);
00047   
00048   Font * normal_font = Font::GetInstance(Font::FONT_NORMAL);
00049   Font * big_font = Font::GetInstance(Font::FONT_BIG);
00050 
00051   Rectanglei stdRect(0, 0, 300, 64);
00052 
00053   uint x_button = AppWormux::GetInstance()->video.window.GetWidth()/2 - stdRect.GetSizeX()/2;
00054   uint y_box = AppWormux::GetInstance()->video.window.GetHeight()/2 - 200;
00055 
00056   // Connection related widgets
00057   connection_box = new VBox(Rectanglei( x_button, y_box, stdRect.GetSizeX(), 1), false);
00058   connection_box->SetBorder(Point2i(0,0));
00059 
00060   // What do we want to do ?
00061   Box* action_box = new HBox(stdRect, false);
00062 
00063   previous_action_bt = new Button(Point2i(0, 0), res, "menu/big_minus", false);
00064   next_action_bt = new Button(Point2i(0, 0), res, "menu/big_plus", false);
00065   action_label = new Label(_("Connect to an internet game"), 
00066                            Rectanglei(0,0,250,0), 
00067                            *big_font, white_color, true);
00068   action_box->AddWidget(previous_action_bt);
00069   action_box->AddWidget(action_label);
00070   action_box->AddWidget(next_action_bt);
00071 
00072   connection_box->AddWidget(action_box);
00073 
00074   // Server address
00075   server_address_label = new Label(_("Server address:"), rectZero, *normal_font);
00076   connection_box->AddWidget(server_address_label);
00077   server_address = new TextBox("localhost", rectZero, *normal_font);
00078   connection_box->AddWidget(server_address);
00079 
00080   // Server port
00081   port_number_label = new Label(_("Port:"), rectZero, *normal_font);
00082   connection_box->AddWidget(port_number_label);
00083   port_number = new TextBox(WORMUX_NETWORK_PORT, rectZero, *normal_font);
00084   connection_box->AddWidget(port_number);
00085 
00086   // Available on internet ?
00087   internet_server = new CheckBox(_("Server available on Internet"),
00088                                  Rectanglei(0,0,0,0),
00089                                  true);
00090   connection_box->AddWidget(internet_server);
00091 
00092   widgets.AddWidget(connection_box);
00093 
00094   SetAction(NET_BROWSE_INTERNET);
00095 
00096   // Warning about experimental networking
00097   msg_box = new MsgBox(Rectanglei( AppWormux::GetInstance()->video.window.GetWidth()/2 - 300,
00098                                    y_box+connection_box->GetSizeY() + 30, 
00099                                    600, 200), 
00100                        Font::GetInstance(Font::FONT_SMALL));
00101   widgets.AddWidget(msg_box);
00102 
00103   msg_box->NewMessage(_("WARNING!! Network is still under developement and therefore a little experimental."));
00104   msg_box->NewMessage(""); // Skip a line
00105   msg_box->NewMessage(_("Some weapons are disabled, because of known bugs (ninjarope, airhammer, blowtorch, submachine gun) and surely many other things don't work either!"));
00106   msg_box->NewMessage(""); // Skip a line
00107   msg_box->NewMessage(_("Join #wormux on irc.freenode.net to find some opponents."));
00108   msg_box->NewMessage(""); // Skip a line
00109   msg_box->NewMessage(_("Have a good game!"));
00110   msg_box->NewMessage(""); // Skip a line
00111 
00112   resource_manager.UnLoadXMLProfile(res);
00113 }
00114 
00115 NetworkConnectionMenu::~NetworkConnectionMenu()
00116 {
00117 }
00118 
00119 void NetworkConnectionMenu::OnClic(const Point2i &mousePosition, int button)
00120 {
00121   Widget* w = widgets.Clic(mousePosition, button);
00122 
00123   if (w == next_action_bt) {
00124     switch (current_action) {
00125     case NET_HOST:
00126       SetAction(NET_CONNECT_LOCAL);
00127       break;
00128     case NET_CONNECT_LOCAL:
00129       SetAction(NET_BROWSE_INTERNET);
00130       break;
00131     case NET_BROWSE_INTERNET:
00132       SetAction(NET_HOST);
00133       break;
00134     }
00135     return;
00136   } 
00137 
00138   if (w == previous_action_bt) {
00139     switch (current_action) {
00140     case NET_HOST:
00141       SetAction(NET_BROWSE_INTERNET);
00142       break;
00143     case NET_CONNECT_LOCAL:
00144       SetAction(NET_HOST);
00145       break;
00146     case NET_BROWSE_INTERNET:
00147       SetAction(NET_CONNECT_LOCAL);
00148       break;
00149     }
00150     return;
00151   }
00152 }
00153 
00154 void NetworkConnectionMenu::SetAction(network_menu_action_t action)
00155 {
00156   current_action = action;
00157 
00158   switch (current_action) {
00159   case NET_HOST:
00160     action_label->SetText(_("Host a game"));
00161     server_address_label->SetVisible(false);
00162     server_address->SetVisible(false);
00163     port_number_label->SetVisible(true);
00164     port_number->SetVisible(true);
00165     internet_server->SetVisible(true);
00166     break;
00167   case NET_CONNECT_LOCAL:
00168     action_label->SetText(_("Connect to game"));
00169     server_address_label->SetVisible(true);
00170     server_address->SetVisible(true);
00171     port_number_label->SetVisible(true);
00172     port_number->SetVisible(true);
00173     internet_server->SetVisible(false);
00174     break;
00175   case NET_BROWSE_INTERNET:
00176     action_label->SetText(_("Connect to an internet game"));
00177     server_address_label->SetVisible(false);
00178     server_address->SetVisible(false);
00179     port_number_label->SetVisible(false);
00180     port_number->SetVisible(false);
00181     internet_server->SetVisible(false);
00182     break;
00183   } 
00184 }
00185 
00186 void NetworkConnectionMenu::Draw(const Point2i &mousePosition){}
00187 
00188 void NetworkConnectionMenu::sig_ok()
00189 {
00190   switch (current_action) {
00191   case NET_HOST: // Hosting your own server
00192     if( !internet_server->GetValue() )
00193       index_server.SetHiddenServer();
00194 
00195     if( !index_server.Connect() ) {
00196       msg_box->NewMessage(_("Error: Unable to contact index server to host a game"));
00197       return;
00198     }
00199 
00200     network.Init();
00201     network.ServerStart(port_number->GetText());
00202 
00203     index_server.SendServerStatus();
00204 
00205     if(network.IsConnected()) {
00206       network.client_inited = 1;
00207     } else {
00208       msg_box->NewMessage(_("Error: Unable to start server"));
00209       return;
00210     }
00211     break;
00212 
00213 
00214   case NET_CONNECT_LOCAL: // Direct connexion to a server
00215     network.Init();
00216     network.ClientConnect(server_address->GetText(), port_number->GetText());
00217     if (!network.IsConnected()) {
00218       msg_box->NewMessage(Format(_("Error: Unable to connect to %s:%s"),
00219                                  (server_address->GetText()).c_str(), (port_number->GetText()).c_str()));
00220       return;
00221     }
00222     break;
00223 
00224   case NET_BROWSE_INTERNET: // Search an internet game!
00225     if( !index_server.Connect() ) {
00226       msg_box->NewMessage(_("Error: Unable to contact index server to search an internet game"));
00227       return;
00228     }
00229 
00230     InternetMenu im;
00231     im.Run();
00232 
00233     index_server.Disconnect();
00234 
00235     // we don't go back into the main menu!
00236     // -> im.Run() may have connected to a host so the 
00237     // if(network.IsConnected()) just below will be catched and close the menu
00238     break;
00239   }
00240 
00241   if (network.IsConnected()) {
00242     // run the network menu ! :-)
00243     NetworkMenu nm;
00244     network.network_menu = &nm;
00245     nm.Run();
00246     network.network_menu = NULL;
00247     index_server.Disconnect();
00248 
00249     // back to main menu after playing
00250     Menu::sig_ok();
00251   } else {
00252     // TODO : add error sound and dialog
00253   }
00254 }
00255 
00256 void NetworkConnectionMenu::__sig_ok()
00257 {
00258   network.Disconnect();
00259 }
00260 
00261 void NetworkConnectionMenu::__sig_cancel()
00262 {
00263   network.Disconnect();
00264 }
00265 

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