preferences accessor cleanup

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.
This commit is contained in:
pentarctagon 2024-05-18 22:10:40 -05:00 committed by Pentarctagon
parent 80d09b47c1
commit 21d0e79cc3
16 changed files with 133 additions and 88 deletions

View file

@ -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;

View file

@ -22,6 +22,7 @@
#include "picture.hpp"
#include "preferences/game.hpp"
#include "preferences/display.hpp"
#include "sdl/utils.hpp"
#include <boost/logic/tribool.hpp>

View file

@ -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';
}
}

View file

@ -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

View file

@ -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());
}
}

View file

@ -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) {

View file

@ -34,17 +34,17 @@
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;
}
@ -52,27 +52,27 @@ static toggle_button* setup_pref_toggle_button(const std::string& id, bool def,
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);
}
@ -80,9 +80,9 @@ static void set_pref_and_button(const std::string& id, bool value, window& windo
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);
}
}

View file

@ -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";

View file

@ -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)

View file

@ -18,6 +18,7 @@
namespace preferences
{
bool use_color_cursors();
void set_color_cursors(bool value);
bool show_standing_animations();

View file

@ -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();

View file

@ -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);

View file

@ -122,17 +122,17 @@ void prefs_event_handler::handle_window_event(const SDL_Event& event)
switch(event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
_set_resolution(video::window_size());
set_resolution(video::window_size());
break;
case SDL_WINDOWEVENT_MAXIMIZED:
_set_maximized(true);
set_maximized(true);
break;
case SDL_WINDOWEVENT_RESTORED:
_set_maximized(fullscreen() || false);
set_maximized(fullscreen() || false);
break;
}
@ -443,18 +443,18 @@ bool vsync()
return get("vsync", true);
}
void _set_resolution(const point& res)
void set_resolution(const point& res)
{
preferences::set("xresolution", std::to_string(res.x));
preferences::set("yresolution", std::to_string(res.y));
prefs["xresolution"] = std::to_string(res.x);
prefs["yresolution"] = std::to_string(res.y);
}
void _set_maximized(bool ison)
void set_maximized(bool ison)
{
prefs["maximized"] = ison;
}
void _set_fullscreen(bool ison)
void set_fullscreen(bool ison)
{
prefs["fullscreen"] = ison;
}
@ -541,7 +541,7 @@ std::string language()
void set_language(const std::string& s)
{
preferences::set("locale", s);
prefs["locale"] = s;
}
std::string gui_theme()
@ -551,7 +551,7 @@ std::string gui_theme()
void set_gui_theme(const std::string& s)
{
preferences::set("gui2_theme", s);
prefs["gui2_theme"] = s;
}
bool ellipses()
@ -561,7 +561,7 @@ bool ellipses()
void set_ellipses(bool ison)
{
preferences::set("show_side_colors", ison);
prefs["show_side_colors"] = ison;
}
bool grid()
@ -571,7 +571,7 @@ bool grid()
void set_grid(bool ison)
{
preferences::set("grid", ison);
prefs["grid"] = ison;
}
std::size_t sound_buffer_size()
@ -599,7 +599,7 @@ void save_sound_buffer_size(const std::size_t size)
if (get("sound_buffer_size") == new_size)
return;
preferences::set("sound_buffer_size", new_size);
prefs["sound_buffer_size"] = new_size;
sound::reset_sound();
}
@ -682,15 +682,15 @@ bool turn_bell()
bool set_turn_bell(bool ison)
{
if(!turn_bell() && ison) {
preferences::set("turn_bell", true);
prefs["turn_bell"] = true;
if(!music_on() && !sound_on() && !UI_sound_on()) {
if(!sound::init_sound()) {
preferences::set("turn_bell", false);
prefs["turn_bell"] = false;
return false;
}
}
} else if(turn_bell() && !ison) {
preferences::set("turn_bell", false);
prefs["turn_bell"] = false;
sound::stop_bell();
if(!music_on() && !sound_on() && !UI_sound_on())
sound::close_sound();
@ -706,15 +706,15 @@ bool UI_sound_on()
bool set_UI_sound(bool ison)
{
if(!UI_sound_on() && ison) {
preferences::set("UI_sound", true);
prefs["UI_sound"] = true;
if(!music_on() && !sound_on() && !turn_bell()) {
if(!sound::init_sound()) {
preferences::set("UI_sound", false);
prefs["UI_sound"] = false;
return false;
}
}
} else if(UI_sound_on() && !ison) {
preferences::set("UI_sound", false);
prefs["UI_sound"] = false;
sound::stop_UI_sound();
if(!music_on() && !sound_on() && !turn_bell())
sound::close_sound();
@ -734,15 +734,15 @@ bool sound_on()
bool set_sound(bool ison) {
if(!sound_on() && ison) {
preferences::set("sound", true);
prefs["sound"] = true;
if(!music_on() && !turn_bell() && !UI_sound_on()) {
if(!sound::init_sound()) {
preferences::set("sound", false);
prefs["sound"] = false;
return false;
}
}
} else if(sound_on() && !ison) {
preferences::set("sound", false);
prefs["sound"] = false;
sound::stop_sound();
if(!music_on() && !turn_bell() && !UI_sound_on())
sound::close_sound();
@ -757,17 +757,17 @@ bool music_on()
bool set_music(bool ison) {
if(!music_on() && ison) {
preferences::set("music", true);
prefs["music"] = true;
if(!sound_on() && !turn_bell() && !UI_sound_on()) {
if(!sound::init_sound()) {
preferences::set("music", false);
prefs["music"] = false;
return false;
}
}
else
sound::play_music();
} else if(music_on() && !ison) {
preferences::set("music", false);
prefs["music"] = false;
if(!sound_on() && !turn_bell() && !UI_sound_on())
sound::close_sound();
else
@ -783,7 +783,7 @@ bool stop_music_in_background()
void set_stop_music_in_background(bool ison)
{
preferences::set("stop_music_in_background", ison);
prefs["stop_music_in_background"] = ison;
}
int scroll_speed()
@ -818,17 +818,17 @@ int mouse_scroll_threshold()
bool animate_map()
{
return preferences::get("animate_map", true);
return get("animate_map", true);
}
bool animate_water()
{
return preferences::get("animate_water", true);
return get("animate_water", true);
}
bool minimap_movement_coding()
{
return preferences::get("minimap_movement_coding", true);
return get("minimap_movement_coding", true);
}
void toggle_minimap_movement_coding()
@ -838,7 +838,7 @@ void toggle_minimap_movement_coding()
bool minimap_terrain_coding()
{
return preferences::get("minimap_terrain_coding", true);
return get("minimap_terrain_coding", true);
}
void toggle_minimap_terrain_coding()
@ -848,7 +848,7 @@ void toggle_minimap_terrain_coding()
bool minimap_draw_units()
{
return preferences::get("minimap_draw_units", true);
return get("minimap_draw_units", true);
}
void toggle_minimap_draw_units()
@ -858,7 +858,7 @@ void toggle_minimap_draw_units()
bool minimap_draw_villages()
{
return preferences::get("minimap_draw_villages", true);
return get("minimap_draw_villages", true);
}
void toggle_minimap_draw_villages()
@ -868,7 +868,7 @@ void toggle_minimap_draw_villages()
bool minimap_draw_terrain()
{
return preferences::get("minimap_draw_terrain", true);
return get("minimap_draw_terrain", true);
}
void toggle_minimap_draw_terrain()
@ -906,16 +906,6 @@ void set_draw_delay(int value)
prefs["draw_delay"] = value;
}
bool use_color_cursors()
{
return get("color_cursors", true);
}
void _set_color_cursors(bool value)
{
preferences::set("color_cursors", value);
}
void load_hotkeys()
{
hotkey::load_custom_hotkeys(game_config_view::wrap(prefs));
@ -977,7 +967,7 @@ bool disable_auto_moves()
void set_disable_auto_moves(bool value)
{
preferences::set("disable_auto_moves", value);
prefs["disable_auto_moves"] = value;
}
bool damage_prediction_allow_monte_carlo_simulation()
@ -1214,4 +1204,45 @@ std::string editor_chosen_addon()
return prefs["editor_chosen_addon"];
}
void set_mp_alert_option(const std::string& id, const std::string& type, bool value)
{
prefs[id+"_"+type] = value;
}
bool mp_alert_option(const std::string& id, const std::string& type, bool def)
{
return prefs[id+"_"+type].to_bool(def);
}
bool has_mp_alert_option(const std::string& id, const std::string& type)
{
return have_setting(id+"_"+type);
}
void set_last_cache_cleared_version(const std::string& version)
{
prefs["_last_cache_cleaned_ver"] = version;
}
std::string last_cache_cleared_version()
{
return prefs["_last_cache_cleaned_ver"];
}
bool get_show_deprecation(bool def)
{
return get("show_deprecation", def);
}
bool get_scroll_when_mouse_outside(bool def)
{
return get("scroll_when_mouse_outside", def);
}
void set_dir_bookmarks(const config& cfg)
{
set_child("dir_bookmarks", cfg);
}
optional_const_config dir_bookmarks()
{
return get_child("dir_bookmarks");
}
} // end namespace preferences

View file

@ -73,7 +73,7 @@ namespace preferences {
void set_scroll_to_action(bool ison);
point resolution();
void _set_resolution(const point& res);
void set_resolution(const point& res);
int pixel_scale();
void set_pixel_scale(const int scale);
@ -82,10 +82,10 @@ namespace preferences {
void set_auto_pixel_scale(bool choice);
bool maximized();
void _set_maximized(bool ison);
void set_maximized(bool ison);
bool fullscreen();
void _set_fullscreen(bool ison);
void set_fullscreen(bool ison);
bool vsync();
void set_vsync(bool ison);
@ -163,7 +163,6 @@ namespace preferences {
void add_alias(const std::string& alias, const std::string& command);
optional_const_config get_alias();
std::string allied_color();
void set_allied_color(const std::string& color_id);
@ -200,9 +199,6 @@ namespace preferences {
bool show_disengaged_orb();
void set_show_disengaged_orb(bool show_orb);
bool use_color_cursors();
void _set_color_cursors(bool value);
int scroll_speed();
void set_scroll_speed(const int scroll);
@ -330,4 +326,18 @@ namespace preferences {
*/
std::string editor_chosen_addon();
void set_mp_alert_option(const std::string& id, const std::string& type, bool value);
bool mp_alert_option(const std::string& id, const std::string& type, bool def = false);
bool has_mp_alert_option(const std::string& id, const std::string& type);
void set_last_cache_cleared_version(const std::string& version);
std::string last_cache_cleared_version();
bool get_show_deprecation(bool def);
bool get_scroll_when_mouse_outside(bool def);
void set_dir_bookmarks(const config& cfg);
optional_const_config dir_bookmarks();
} // end namespace preferences

View file

@ -22,7 +22,6 @@ bool whisper_friends_only();
void set_whisper_friends_only(bool v);
bool auto_open_whisper_windows();
void set_auto_open_whisper_windows(bool v);
bool fi_invert();
void set_fi_invert(bool value);

View file

@ -790,7 +790,7 @@ void set_fullscreen(bool fullscreen)
}
// Update the config value in any case.
preferences::_set_fullscreen(fullscreen);
preferences::set_fullscreen(fullscreen);
}
void toggle_fullscreen()
@ -822,8 +822,8 @@ bool set_resolution(const point& resolution)
// Change the saved values in preferences.
LOG_DP << "updating resolution to " << resolution;
preferences::_set_resolution(resolution);
preferences::_set_maximized(false);
preferences::set_resolution(resolution);
preferences::set_maximized(false);
return true;
}