extract chat_handler into it's own separate header...

...and make chat_handler not depend on game_display. This makes it
possible to include chat_handler without including half the game's
headers for no reason
This commit is contained in:
Tomasz Śniatowski 2009-07-04 10:38:28 +01:00
parent f8c13515b7
commit f476e3cd0c
13 changed files with 70 additions and 42 deletions

View file

@ -239,7 +239,7 @@ void readonly_context_impl::log_message(const std::string& msg)
{
if(game_config::debug) {
get_info().disp.add_chat_message(time(NULL), "ai", get_side(), msg,
game_display::MESSAGE_PUBLIC, false);
events::chat_handler::MESSAGE_PUBLIC, false);
}
}

View file

@ -1683,7 +1683,7 @@ void formula_ai::handle_exception(game_logic::formula_error& e, const std::strin
void formula_ai::display_message(const std::string& msg) const
{
get_info().disp.add_chat_message(time(NULL), "fai", get_side(), msg,
game_display::MESSAGE_PUBLIC, false);
events::chat_handler::MESSAGE_PUBLIC, false);
}

45
src/chat_events.hpp Normal file
View file

@ -0,0 +1,45 @@
/* $Id$ */
/*
Copyright (C) 2006 - 2009 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
wesnoth playturn Copyright (C) 2003 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef CHAT_EVENTS_H_INCLUDED
#define CHAT_EVENTS_H_INCLUDED
#include "global.hpp"
namespace events {
class chat_handler
{
public:
chat_handler();
virtual ~chat_handler();
enum MESSAGE_TYPE { MESSAGE_PUBLIC, MESSAGE_PRIVATE };
protected:
void do_speak(const std::string& message, bool allies_only=false);
//called from do_speak
virtual void add_chat_message(const time_t& time,
const std::string& speaker, int side, const std::string& message,
MESSAGE_TYPE type=MESSAGE_PRIVATE)=0;
virtual void send_chat_message(const std::string& message, bool allies_only=false)=0;
void send_command(const std::string& cmd, const std::string& args="");
void change_logging(const std::string& data);
friend class chat_command_handler;
};
}
#endif

View file

@ -155,7 +155,7 @@ namespace gui{
} else {
std::string completion_list = utils::join(matches, ' ');
gui.add_chat_message(time(NULL), "", 0, completion_list,
game_display::MESSAGE_PRIVATE, false);
events::chat_handler::MESSAGE_PRIVATE, false);
}
box_->set_text(text);
break;

View file

@ -1099,7 +1099,7 @@ namespace {
}
void game_display::add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& message, game_display::MESSAGE_TYPE type,
int side, const std::string& message, events::chat_handler::MESSAGE_TYPE type,
bool bell)
{
const bool whisper = speaker.find("whisper: ") == 0;
@ -1113,7 +1113,7 @@ void game_display::add_chat_message(const time_t& time, const std::string& speak
preferences::admin_authentication(sender, message);
if (bell) {
if ((type == MESSAGE_PRIVATE && (!is_observer() || whisper))
if ((type == events::chat_handler::MESSAGE_PRIVATE && (!is_observer() || whisper))
|| utils::word_match(message, preferences::login())) {
sound::play_UI_sound(game_config::sounds::receive_message_highlight);
} else if (preferences::is_friend(sender)) {
@ -1159,7 +1159,7 @@ void game_display::add_chat_message(const time_t& time, const std::string& speak
std::stringstream str;
std::stringstream message_str;
if(type == MESSAGE_PUBLIC) {
if(type == events::chat_handler::MESSAGE_PUBLIC) {
if(action) {
str << "<" << speaker << " " << msg << ">";
message_colour = speaker_colour;

View file

@ -24,6 +24,7 @@ class unit;
class unit_map;
#include "animated.hpp"
#include "chat_events.hpp"
#include "display.hpp"
#include "image.hpp"
#include "pathfind.hpp"
@ -266,9 +267,8 @@ public:
void remove_observer(const std::string& name) { observers_.erase(name); }
const std::set<std::string>& observers() const { return observers_; }
enum MESSAGE_TYPE { MESSAGE_PUBLIC, MESSAGE_PRIVATE };
void add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& msg, MESSAGE_TYPE type, bool bell);
int side, const std::string& msg, events::chat_handler::MESSAGE_TYPE type, bool bell);
void clear_chat_messages() { prune_chat_messages(true); }
void begin_game();

View file

@ -247,7 +247,7 @@ static void show_wml_errors()
}
game_events::resources->screen->add_chat_message(time(NULL), caption, 0, msg.str(),
game_display::MESSAGE_PUBLIC, false);
events::chat_handler::MESSAGE_PUBLIC, false);
std::cerr << caption << ": " << msg.str() << '\n';
}
}
@ -271,7 +271,7 @@ static void show_wml_messages()
}
game_events::resources->screen->add_chat_message(time(NULL), caption, 0, msg.str(),
game_display::MESSAGE_PUBLIC, false);
events::chat_handler::MESSAGE_PUBLIC, false);
}
}

View file

@ -1628,7 +1628,7 @@ private:
void menu_handler::add_chat_message(const time_t& time,
const std::string& speaker, int side, const std::string& message,
game_display::MESSAGE_TYPE type)
events::chat_handler::MESSAGE_TYPE type)
{
gui_->add_chat_message(time, speaker, side, message, type, false);
}
@ -2490,7 +2490,7 @@ private:
data.add_child("whisper", cwhisper);
chat_handler_.add_chat_message(time(NULL),
"whisper to " + cwhisper["receiver"], 0,
cwhisper["message"], game_display::MESSAGE_PRIVATE);
cwhisper["message"], events::chat_handler::MESSAGE_PRIVATE);
network::send_data(data, 0, true);
}
@ -2505,7 +2505,7 @@ private:
data.add_child("message", cwhisper);
chat_handler_.add_chat_message(time(NULL),
cwhisper["room"] + ": " + preferences::login(), 0,
cwhisper["message"], game_display::MESSAGE_PRIVATE);
cwhisper["message"], events::chat_handler::MESSAGE_PRIVATE);
network::send_data(data, 0, true);
}
@ -2664,7 +2664,7 @@ private:
recorder.speak(cfg);
add_chat_message(time(NULL), cfg["id"], side, message,
private_message ? game_display::MESSAGE_PRIVATE : game_display::MESSAGE_PUBLIC);
private_message ? events::chat_handler::MESSAGE_PRIVATE : events::chat_handler::MESSAGE_PUBLIC);
}

View file

@ -18,6 +18,7 @@
#include "global.hpp"
#include "actions.hpp"
#include "chat_events.hpp"
#include "display.hpp"
#include "game_display.hpp"
#include "show_dialog.hpp"
@ -33,25 +34,7 @@ namespace events {
struct fallback_ai_to_human_exception {};
namespace events{
class chat_handler {
public:
chat_handler();
virtual ~chat_handler();
protected:
void do_speak(const std::string& message, bool allies_only=false);
//called from do_speak
virtual void add_chat_message(const time_t& time,
const std::string& speaker, int side, const std::string& message,
game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE)=0;
virtual void send_chat_message(const std::string& message, bool allies_only=false)=0;
void send_command(const std::string& cmd, const std::string& args="");
void change_logging(const std::string& data);
friend class chat_command_handler;
};
namespace events {
class menu_handler : private chat_handler {
public:
@ -121,7 +104,7 @@ public:
protected:
void add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& message,
game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE);
events::chat_handler::MESSAGE_TYPE type=events::chat_handler::MESSAGE_PRIVATE);
void send_chat_message(const std::string& message, bool allies_only=false);
private:
//console_handler is basically a sliced out part of menu_handler

View file

@ -473,7 +473,7 @@ void ui::handle_event(const SDL_Event& event)
}
}
void ui::add_chat_message(const time_t& time, const std::string& speaker, int /*side*/, const std::string& message, game_display::MESSAGE_TYPE /*type*/)
void ui::add_chat_message(const time_t& time, const std::string& speaker, int /*side*/, const std::string& message, events::chat_handler::MESSAGE_TYPE /*type*/)
{
chat_.add_message(time, speaker, message);
chat_.update_textbox(chat_textbox_);

View file

@ -130,7 +130,7 @@ protected:
/** Override chat_handler. */
void add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& message,
game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE);
events::chat_handler::MESSAGE_TYPE type=events::chat_handler::MESSAGE_PRIVATE);
void send_chat_message(const std::string& message, bool allies_only=false);
/** Process chat messages. */

View file

@ -85,14 +85,14 @@ turn_info::PROCESS_DATA_RESULT turn_info::process_network_data(const config& cfg
const int side = lexical_cast_default<int>(msg["side"],0);
gui_.add_chat_message(time(NULL), msg["sender"], side,
msg["message"], game_display::MESSAGE_PUBLIC,
msg["message"], events::chat_handler::MESSAGE_PUBLIC,
preferences::message_bell());
}
if (const config &msg = cfg.child("whisper") /*&& is_observer()*/)
{
gui_.add_chat_message(time(NULL), "whisper: " + msg["sender"], 0,
msg["message"], game_display::MESSAGE_PRIVATE,
msg["message"], events::chat_handler::MESSAGE_PRIVATE,
preferences::message_bell());
}

View file

@ -724,14 +724,14 @@ static void check_checksums(game_display& disp,const unit_map& units,const confi
std::stringstream message;
message << "non existant unit to checksum at " << loc.x+1 << "," << loc.y+1 << "!";
disp.add_chat_message(time(NULL), "verification", 1, message.str(),
game_display::MESSAGE_PRIVATE, false);
events::chat_handler::MESSAGE_PRIVATE, false);
continue;
}
if (get_checksum(u->second) != ch["value"]) {
std::stringstream message;
message << "checksum mismatch at " << loc.x+1 << "," << loc.y+1 << "!";
disp.add_chat_message(time(NULL), "verification", 1, message.str(),
game_display::MESSAGE_PRIVATE, false);
events::chat_handler::MESSAGE_PRIVATE, false);
}
}
}
@ -846,8 +846,8 @@ bool do_replay_handle(game_display& disp, const gamemap& map,
if (!get_replay_source().is_skipping() || is_whisper) {
const int side = lexical_cast_default<int>(child["side"],0);
disp.add_chat_message(time(NULL), speaker_name, side, message,
(team_name.empty() ? game_display::MESSAGE_PUBLIC
: game_display::MESSAGE_PRIVATE),
(team_name.empty() ? events::chat_handler::MESSAGE_PUBLIC
: events::chat_handler::MESSAGE_PRIVATE),
preferences::message_bell());
}
}