add mp_ui_sounds namespace to handle prefs, sound, notifications

This takes these details out of mp_ui, and the experimental mp
lobby.
This commit is contained in:
Chris Beck 2014-10-11 14:35:37 -04:00
parent c81306fae3
commit 05448fe34b
5 changed files with 219 additions and 9 deletions

View file

@ -853,6 +853,7 @@ set(wesnoth-main_SRC
mp_game_settings.cpp
mp_game_utils.cpp
mp_options.cpp
mp_ui_sounds.cpp
multiplayer.cpp
multiplayer_configure.cpp
multiplayer_connect.cpp

View file

@ -475,6 +475,7 @@ wesnoth_sources = Split("""
mp_game_settings.cpp
mp_game_utils.cpp
mp_options.cpp
mp_ui_sounds.cpp
multiplayer.cpp
multiplayer_configure.cpp
multiplayer_connect.cpp

View file

@ -25,6 +25,7 @@
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/window.hpp"
#include "mp_ui_sounds.hpp"
#include "preferences.hpp"
#include "formula_string_utils.hpp"
@ -62,9 +63,6 @@ namespace gui2
* @end{table}
*/
// Note, this list of items must match those ids defined in lobby_sounds_options.cfg
const char * items[] = { "player_joins", "player_leaves", "private_message", "public_message", "server_message", "ready_to_start", "game_has_begun" };
static ttoggle_button * setup_pref_toggle_button(const std::string & id, bool def, twindow & window)
{
ttoggle_button * b = &find_widget<ttoggle_button>(&window, id, false);
@ -87,7 +85,7 @@ static void setup_item(const std::string & item, twindow & window)
{
// Set up the sound checkbox
std::string sound_id = item+"_sound";
ttoggle_button * sound = setup_pref_toggle_button(sound_id, item != "public_message", window);
ttoggle_button * sound = setup_pref_toggle_button(sound_id, mp_ui_sounds::get_def_pref_sound(item), window);
// Set up the sound checkbox tooltip
utils::string_map for_tooltip;
@ -96,10 +94,10 @@ static void setup_item(const std::string & item, twindow & window)
sound->set_tooltip(vgettext(orig.c_str(),for_tooltip));
// Set up the notification checkbox
setup_pref_toggle_button(item+"_notification", item != "public_message", window);
setup_pref_toggle_button(item+"_notification", mp_ui_sounds::get_def_pref_notif(item), window);
// Set up the in_lobby checkbox
setup_pref_toggle_button(item+"_in_lobby", item == "private_message" || item == "server_message", window);
setup_pref_toggle_button(item+"_in_lobby", mp_ui_sounds::get_def_pref_lobby(item), window);
}
REGISTER_DIALOG(lobby_sounds_options)
@ -110,9 +108,8 @@ tlobby_sounds_options::tlobby_sounds_options()
void tlobby_sounds_options::pre_show(CVideo& /*video*/, twindow& window)
{
BOOST_FOREACH(const char * i, items) {
std::string item(i);
setup_item(item, window);
BOOST_FOREACH(const std::string & i, mp_ui_sounds::items) {
setup_item(i, window);
}
ttoggle_button * in_lobby;

165
src/mp_ui_sounds.cpp Normal file
View file

@ -0,0 +1,165 @@
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
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 as published by
the Free Software Foundation; either version 2 of the License, 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.
*/
/**
* This namespace provides handlers which play the sounds / notificaitons
* for various mp server events, depending on the preference configuration.
*/
#include "mp_ui_sounds.hpp"
#include "global.hpp"
#include "desktop/notifications.hpp"
#include "game_config.hpp"
#include "gettext.hpp"
#include "preferences.hpp"
#include "sound.hpp"
#include <boost/assign/list_of.hpp>
#include <string>
#include <vector>
namespace mp_ui_sounds {
namespace {
bool lobby_pref(std::string id)
{
return preferences::get(id + "_in_lobby", get_def_pref_lobby(id));
}
bool sound_pref(std::string id)
{
return preferences::get(id + "_sound", get_def_pref_sound(id));
}
bool notif_pref(std::string id)
{
return preferences::get(id + "_notification", get_def_pref_notif(id));
}
} // end anonymous namespace
// Note: This list must agree with data/gui/.../lobby_sound_options.cfg
const std::vector<std::string> items = boost::assign::list_of("player_joins")("player_leaves")("private_message")("public_message")("server_message")("ready_to_start")("game_has_begun");
void player_enters(bool is_lobby)
{
std::string id = "player_enters";
if (is_lobby && !lobby_pref(id)) {
return ;
}
if (sound_pref(id)) {
sound::play_UI_sound(game_config::sounds::user_arrive);
}
if (notif_pref(id)) {
desktop::notifications::send(_("Wesnoth"), _("A player has joined"), desktop::notifications::OTHER);
}
}
void player_leaves(bool is_lobby)
{
std::string id = "player_leaves";
if (is_lobby && !lobby_pref(id)) {
return ;
}
if (sound_pref(id)) {
sound::play_UI_sound(game_config::sounds::user_leave);
}
if (notif_pref(id)) {
desktop::notifications::send(_("Wesnoth"), _("A player has left"), desktop::notifications::OTHER);
}
}
void public_message(bool is_lobby)
{
std::string id = "public_message";
if (is_lobby && !lobby_pref(id)) {
return ;
}
if (sound_pref(id)) {
sound::play_UI_sound(game_config::sounds::receive_message);
}
if (notif_pref(id)) {
desktop::notifications::send(_("Wesnoth"), _("Received a message"), desktop::notifications::OTHER);
}
}
void private_message(bool is_lobby)
{
std::string id = "private_message";
if (is_lobby && !lobby_pref(id)) {
return ;
}
if (sound_pref(id)) {
sound::play_UI_sound(game_config::sounds::receive_message_highlight);
}
if (notif_pref(id)) {
desktop::notifications::send(_("Wesnoth"), _("Someone is talking to you"), desktop::notifications::OTHER);
}
}
void server_message(bool is_lobby)
{
std::string id = "server_message";
if (is_lobby && !lobby_pref(id)) {
return ;
}
if (sound_pref(id)) {
sound::play_UI_sound(game_config::sounds::receive_message_server);
}
if (notif_pref(id)) {
desktop::notifications::send(_("Wesnoth"), _("The server has sent a message"), desktop::notifications::OTHER);
}
}
void ready_for_start()
{
std::string id = "ready_for_start";
if (sound_pref(id)) {
if (preferences::UI_sound_on()) {
sound::play_bell(game_config::sounds::party_full_bell); //this is play_bell instead of play_UI_sound to economize on sound channels. UI only has two sounds, and turn bell has a dedicated channel.
}
}
if (notif_pref(id)) {
desktop::notifications::send(_("Wesnoth"), _("Ready to start!"), desktop::notifications::OTHER);
}
}
void game_has_begun()
{
std::string id = "game_has_begun";
if (sound_pref(id)) {
sound::play_UI_sound(game_config::sounds::mp_game_begins);
}
if (notif_pref(id)) {
desktop::notifications::send(_("Wesnoth"), _ ("Game has begun!"), desktop::notifications::OTHER);
}
}
bool get_def_pref_sound(const std::string & id) {
return (id != "public_message");
}
bool get_def_pref_notif(const std::string & id) {
return (id == "private_message" || id == "ready_for_start" || id == "game_has_begun");
}
bool get_def_pref_lobby(const std::string & id) {
return (id == "private_message" || id == "server_message");
}
} // end namespace mp_ui_sounds

46
src/mp_ui_sounds.hpp Normal file
View file

@ -0,0 +1,46 @@
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
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 as published by
the Free Software Foundation; either version 2 of the License, 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.
*/
/**
* This namespace provides handlers which play the sounds / notificaitons
* for various mp server events, depending on the preference configuration.
*/
#ifndef INCL_MP_UI_SOUNDS_HPP_
#define INCL_MP_UI_SOUNDS_HPP_
#include<string>
#include<vector>
namespace mp_ui_sounds {
// Functions called when such an event occurs
void player_enters(bool is_lobby);
void player_leaves(bool is_lobby);
void public_message(bool is_lobby);
void private_message(bool is_lobby);
void server_message(bool is_lobby);
void ready_for_start();
void game_has_begun();
// Functions to calculate what the default preference should be
bool get_def_pref_sound(const std::string &);
bool get_def_pref_notif(const std::string &);
bool get_def_pref_lobby(const std::string &);
// Note, this list of items must match those ids defined in data/gui/dialogs/lobby_sounds_options.cfg
extern const std::vector<std::string> items;
}
#endif