multiplayer_ui.hpp

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

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:06 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs