multiplayer_ui.hpp

Go to the documentation of this file.
00001 /* $Id: multiplayer_ui.hpp 23842 2008-02-16 08:47:16Z mordante $ */
00002 /*
00003    Copyright (C) 2007 - 2008
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 #ifndef MULTIPLAYER_UI_HPP_INCLUDED
00015 #define MULTIPLAYER_UI_HPP_INCLUDED
00016 
00017 #include "hotkeys.hpp"
00018 #include "network.hpp"
00019 #include "preferences_display.hpp"
00020 #include "widgets/label.hpp"
00021 #include "widgets/menu.hpp"
00022 #include "widgets/textbox.hpp"
00023 #include "menu_events.hpp"
00024 
00025 #include <deque>
00026 #include <string>
00027 
00028 class display;
00029 class config;
00030 class game_state;
00031 
00032 namespace mp {
00033 
00034 enum controller { CNTR_NETWORK = 0, CNTR_LOCAL, CNTR_COMPUTER, CNTR_EMPTY, CNTR_LAST };
00035 
00036 void check_response(network::connection res, const config& data);
00037 
00038 void level_to_gamestate(config& level, game_state& state, bool saved_game=false);
00039 
00040 std::string get_colour_string(int id);
00041 
00042 //this class memorizes a chat session.
00043 class chat
00044 {
00045 public:
00046     chat();
00047 
00048     void add_message(const time_t& time, const std::string& user,
00049             const std::string& message);
00050 
00051     void init_textbox(gui::textbox& textbox);
00052     void update_textbox(gui::textbox& textbox);
00053 
00054 private:
00055     struct msg {
00056         msg(const time_t& time, const std::string& user, const std::string& message)
00057             : time(time), user(user), message(message) {};
00058         time_t time;
00059         std::string user;
00060         std::string message;
00061     };
00062     typedef std::deque<msg> msg_hist;
00063 
00064     std::string format_message(const msg& message);
00065 
00066     msg_hist message_history_;
00067     msg_hist::size_type last_update_;
00068 };
00069 
00070 //a base class for the different multiplayer base dialogs: game list, create
00071 //game, wait game, game setup
00072 class ui : public gui::widget, private events::chat_handler, private font::floating_label_context
00073 {
00074 public:
00075     enum result { CONTINUE, JOIN, OBSERVE, CREATE, PREFERENCES, PLAY, QUIT };
00076 
00077     ui(game_display& d, const std::string& title,
00078             const config& cfg, chat& c, config& gamelist);
00079 
00080     // Asks the multiplayer_ui to pump some data from the network, and then
00081     // to process it. The actual processing will be left to the child
00082     // classes, through process_network_data and process_network_error
00083     void process_network();
00084 
00085     // Returns the result of the current widget. While the result is equal
00086     // to continue, the widget should not be destroyed.
00087     result get_result();
00088 
00089     // Hides children, moves them (using layout_children), then shows them.
00090     // The methodes hide_children and layout_children are supposed to be
00091     // overridden by subclasses of this class which add new sub-widgets.
00092     void set_location(const SDL_Rect& rect);
00093     using widget::set_location;
00094 
00095 protected:
00096     int xscale(int x) const;
00097     int yscale(int y) const;
00098     static const int xscale_base;
00099     static const int yscale_base;
00100 
00101     SDL_Rect client_area() const;
00102 
00103     game_display& disp_;
00104     game_display& disp() { return disp_; };
00105 
00106     // Returns the main game config, as defined by loading the preprocessed
00107     // WML files. Children of this class may need this to obtain, for
00108     // example, the list of available eras.
00109     const config& game_config() const;
00110 
00111     virtual void draw_contents();
00112 
00113     virtual void process_event();
00114 
00115     virtual void handle_event(const SDL_Event& event);
00116     virtual void handle_key_event(const SDL_KeyboardEvent& event);
00117 
00118     // Override chat_handler
00119     void add_chat_message(const time_t& time, const std::string& speaker,
00120             int side, const std::string& message,
00121             game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE);
00122     void send_chat_message(const std::string& message, bool allies_only=false);
00123 
00124     //! Process chat messages.
00125     void process_message(const config& msg, const bool whisper=false);
00126 
00127     // Processes any pending network data. Called by the public
00128     // process_network() method. Overridden by subclasses who add more
00129     // behaviour for network.
00130     virtual void process_network_data(const config& data, const network::connection sock);
00131 
00132     // Processes any pending network error. Called by the public
00133     // process_network() method. Overridden by subclasses
00134     virtual void process_network_error(network::error& error);
00135 
00136     // Return true if we must accept incoming connections, false if not.
00137     // Defaults to not.
00138     virtual bool accept_connections() { return false; };
00139 
00140     // Processes a pending network connection.
00141     virtual void process_network_connection(const network::connection sock);
00142 
00143     // Hides or shows all gui::widget children of this widget. Should be
00144     // overridden by subclasses which add their own children.
00145     virtual void hide_children(bool hide=true);
00146 
00147     // Lays the children out. This method is to be overridden by the
00148     // subclasses of the mp_ui class; it will be called
00149     virtual void layout_children(const SDL_Rect& rect);
00150 
00151     // Sets the result of this dialog, to be checked by get_result()
00152     result set_result(result res);
00153 
00154     // Sets the name of the selected game which is used to highlight
00155     // the names of the players which have joined this game
00156     void set_selected_game(const std::string game_name);
00157 
00158     // Called each time the gamelist_ variable is updated. May be
00159     // overridden by child classes to add custom gamelist behaviour.
00160     virtual void gamelist_updated(bool silent=true);
00161 
00162     // Sets the user list
00163     void set_user_list(const std::vector<std::string>&, bool silent);
00164     void set_user_menu_items(const std::vector<std::string>& list);
00165 
00166     // Returns the current gamelist
00167     config& gamelist() { return gamelist_; };
00168 
00169     void append_to_title(const std::string& name);
00170     const gui::label& title() const;
00171 
00172 private:
00173     /** Set to true when the widgets are intialized. Allows delayed
00174      * initialization on first positioning. */
00175     bool initialized_;
00176     bool gamelist_initialized_;
00177 
00178     // Ensures standard hotkeys are coorectly handled
00179     const hotkey::basic_handler hotkey_handler_;
00180 
00181     const preferences::display_manager disp_manager_;
00182 
00183     // The main game configuration, as defined by loading the preprocessed
00184     // WML files. Access using the game_config() method if necessary.
00185     const config& game_config_;
00186 
00187     chat& chat_;
00188 
00189     config& gamelist_;
00190 
00191     gui::label title_;
00192 #ifndef USE_TINY_GUI
00193     gui::textbox entry_textbox_;
00194 #endif
00195     gui::textbox chat_textbox_;
00196 
00197     gui::menu users_menu_;
00198 
00199     std::vector<std::string> user_list_;
00200 
00201     std::string selected_game_;
00202 
00203     result result_;
00204 
00205     bool gamelist_refresh_;
00206 
00207     Uint32 lobby_clock_;
00208 
00209 public:
00210     enum user_relation { ME, FRIEND, NEUTRAL, IGNORED };
00211     enum user_state    { LOBBY, GAME, SEL_GAME };
00212 
00213 private:
00214     struct user_info
00215     {
00216         std::string    name;
00217         std::string    game_id;
00218         std::string    location;
00219         user_relation  relation;
00220         user_state     state;
00221         bool operator> (const user_info& b) const;
00222     };
00223 };
00224 
00225 }
00226 
00227 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs