Skip to content

Commit

Permalink
preferences accessor cleanup
Browse files Browse the repository at this point in the history
have external access to the preferences go through explicitly named accessors as much as possible rather than directly using get() and set(). also have the general preferences file directly use its `prefs` config.
  • Loading branch information
Pentarctagon committed May 19, 2024
1 parent e6fe960 commit 106d331
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/controller_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ bool controller_base::handle_scroll(int mousex, int mousey, int mouse_flags)
{
const bool mouse_in_window =
video::window_has_mouse_focus()
|| preferences::get("scroll_when_mouse_outside", true);
|| preferences::get_scroll_when_mouse_outside(true);

int scroll_speed = preferences::scroll_speed();
double dx = 0.0, dy = 0.0;
Expand Down
1 change: 1 addition & 0 deletions src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "picture.hpp"
#include "preferences/game.hpp"
#include "preferences/display.hpp"
#include "sdl/utils.hpp"

#include <boost/logic/tribool.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/deprecation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ std::string deprecated_message(
const lg::logger& out_log = *log_ptr;
FORCE_LOG_TO(out_log, log_deprecate) << message;
// whether to show the error in the ingame chat area
if(preferences::get("show_deprecation", game_config::wesnoth_version.is_dev_version())) {
if(preferences::get_show_deprecation(game_config::wesnoth_version.is_dev_version())) {
lg::log_to_chat() << message << '\n';
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/desktop/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ inline std::string pretty_path(const std::string& path)

inline config get_bookmarks_config()
{
auto cfg = preferences::get_child("dir_bookmarks");
auto cfg = preferences::dir_bookmarks();
return cfg ? *cfg : config{};
}

inline void commit_bookmarks_config(config& cfg)
{
preferences::set_child("dir_bookmarks", cfg);
preferences::set_dir_bookmarks(cfg);
}

} // unnamed namespace
Expand Down
6 changes: 3 additions & 3 deletions src/game_config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ game_config_manager::game_config_manager(const commandline_options& cmdline_opts
}

// Clean the cache of any old Wesnoth version's cache data
if(const std::string last_cleaned = preferences::get("_last_cache_cleaned_ver"); !last_cleaned.empty()) {
if(const std::string last_cleaned = preferences::last_cache_cleared_version(); !last_cleaned.empty()) {
if(version_info{last_cleaned} < game_config::wesnoth_version) {
if(cache_.clean_cache()) {
preferences::set("_last_cache_cleaned_ver", game_config::wesnoth_version.str());
preferences::set_last_cache_cleared_version(game_config::wesnoth_version.str());
}
}
} else {
// If the preference wasn't set, set it, else the cleaning will never happen :P
preferences::set("_last_cache_cleaned_ver", game_config::wesnoth_version.str());
preferences::set_last_cache_cleared_version(game_config::wesnoth_version.str());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/game_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts)
const int xres = std::get<0>(*cmdline_opts_.resolution);
const int yres = std::get<1>(*cmdline_opts_.resolution);
if(xres > 0 && yres > 0) {
preferences::_set_resolution(point(xres, yres));
preferences::_set_maximized(false);
preferences::set_resolution(point(xres, yres));
preferences::set_maximized(false);
}
}
if(cmdline_opts_.screenshot) {
Expand Down
30 changes: 15 additions & 15 deletions src/gui/dialogs/multiplayer/mp_alerts_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,55 @@
namespace gui2::dialogs
{

static toggle_button* setup_pref_toggle_button(const std::string& id, bool def, window& window)
static toggle_button* setup_pref_toggle_button(const std::string& id, const std::string& type, bool def, window& window)
{
toggle_button* b = find_widget<toggle_button>(&window, id, false, true);
b->set_value(preferences::get(id, def));
toggle_button* b = find_widget<toggle_button>(&window, id+"_"+type, false, true);
b->set_value(preferences::mp_alert_option(id, type, def));

//ensure we have yes / no for the toggle button, so that the preference matches the toggle button for sure.
if (preferences::get(id).empty()) {
preferences::set(id, def);
if (preferences::has_mp_alert_option(id, type)) {
preferences::set_mp_alert_option(id, type, def);
}

connect_signal_mouse_left_click(*b, std::bind([b, id]() { preferences::set(id, b->get_value_bool()); }));
connect_signal_mouse_left_click(*b, std::bind([b, id, type]() { preferences::set_mp_alert_option(id, type, b->get_value_bool()); }));

return b;
}

static void setup_item(const std::string& item, window& window)
{
// Set up the sound checkbox
setup_pref_toggle_button(item+"_sound", mp::ui_alerts::get_def_pref_sound(item), window);
setup_pref_toggle_button(item, "sound", mp::ui_alerts::get_def_pref_sound(item), window);

// Set up the notification checkbox
toggle_button* notif = setup_pref_toggle_button(item+"_notif", mp::ui_alerts::get_def_pref_notif(item), window);
toggle_button* notif = setup_pref_toggle_button(item, "notif", mp::ui_alerts::get_def_pref_notif(item), window);

// Check if desktop notifications are available
if (!desktop::notifications::available()) {
notif->set_value(false);
notif->set_active(false);
preferences::set(item+"_notif", false);
preferences::set_mp_alert_option(item, "notif", false);
} else {
notif->set_active(true);
}

// Set up the in_lobby checkbox
setup_pref_toggle_button(item+"_lobby", mp::ui_alerts::get_def_pref_lobby(item), window);
setup_pref_toggle_button(item, "lobby", mp::ui_alerts::get_def_pref_lobby(item), window);
}

static void set_pref_and_button(const std::string& id, bool value, window& window)
static void set_pref_and_button(const std::string& id, const std::string& type, bool value, window& window)
{
preferences::set(id,value);
preferences::set_mp_alert_option(id, type, value);
toggle_button* button = find_widget<toggle_button>(&window, id, false, true);
button->set_value(value);
}

static void revert_to_default_pref_values(window& window)
{
for (const std::string& i : mp::ui_alerts::items) {
set_pref_and_button(i+"_sound", mp::ui_alerts::get_def_pref_sound(i), window);
set_pref_and_button(i+"_notif", mp::ui_alerts::get_def_pref_notif(i), window);
set_pref_and_button(i+"_lobby", mp::ui_alerts::get_def_pref_lobby(i), window);
set_pref_and_button(i, "sound", mp::ui_alerts::get_def_pref_sound(i), window);
set_pref_and_button(i, "notif", mp::ui_alerts::get_def_pref_notif(i), window);
set_pref_and_button(i, "lobby", mp::ui_alerts::get_def_pref_lobby(i), window);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/mp_ui_alerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ namespace
{
bool lobby_pref(const std::string& id)
{
return preferences::get(id + "_lobby", get_def_pref_lobby(id));
return preferences::mp_alert_option(id, "lobby", get_def_pref_lobby(id));
}

bool sound_pref(const std::string& id)
{
return preferences::get(id + "_sound", get_def_pref_sound(id));
return preferences::mp_alert_option(id, "sound", get_def_pref_sound(id));
}

bool notif_pref(const std::string& id)
{
return preferences::get(id + "_notif", get_def_pref_notif(id));
return preferences::mp_alert_option(id, "notif", get_def_pref_notif(id));
}

const std::string _player_joins = "player_joins";
Expand Down
9 changes: 7 additions & 2 deletions src/preferences/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@

namespace preferences {

bool use_color_cursors()
{
return get("color_cursors", true);
}

void set_color_cursors(bool value)
{
_set_color_cursors(value);
preferences::set("color_cursors", value);

cursor::set();
}

bool show_standing_animations()
{
return preferences::get("unit_standing_animations", true);
return get("unit_standing_animations", true);
}

void set_show_standing_animations(bool value)
Expand Down
1 change: 1 addition & 0 deletions src/preferences/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace preferences
{
bool use_color_cursors();
void set_color_cursors(bool value);

bool show_standing_animations();
Expand Down
2 changes: 0 additions & 2 deletions src/preferences/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace editor {
int auto_update_transitions();
void set_auto_update_transitions(int value);

//std::vector<std::string>* get_editor_history();

std::string default_dir();

bool draw_terrain_codes();
Expand Down
4 changes: 2 additions & 2 deletions src/preferences/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ void set_show_all_units_in_help(bool value);

compression::format save_compression_format();

std::set<std::string>&encountered_units();
std::set<t_translation::terrain_code>&encountered_terrains();
std::set<std::string>& encountered_units();
std::set<t_translation::terrain_code>& encountered_terrains();

std::string custom_command();
void set_custom_command(const std::string& command);
Expand Down
Loading

0 comments on commit 106d331

Please sign in to comment.