21 #define GETTEXT_DOMAIN "wesnoth-lib"
44 #define ERR_CFG LOG_STREAM(err , log_config)
47 #define ERR_FS LOG_STREAM(err, log_filesystem)
51 bool no_preferences_save =
false;
103 if (no_preferences_save)
return;
121 if (event.type != SDL_WINDOWEVENT)
return;
123 switch(event.window.event) {
124 case SDL_WINDOWEVENT_RESIZED:
129 case SDL_WINDOWEVENT_MAXIMIZED:
134 case SDL_WINDOWEVENT_RESTORED:
157 if(!prefs_file_existed) {
165 void set(
const std::string &key,
bool value)
170 void set(
const std::string &key,
int value)
175 void set(
const std::string &key,
char const *value)
180 void set(
const std::string &key,
const std::string &value)
192 prefs.recursive_clear_value(key);
196 prefs.clear_children(key);
197 prefs.add_child(key, val);
202 return prefs.optional_child(key);
205 void erase(
const std::string& key) {
206 prefs.remove_attribute(key);
210 return prefs.has_attribute(key);
213 std::string
get(
const std::string& key) {
217 std::string
get(
const std::string& key,
const std::string& def) {
221 bool get(
const std::string &key,
bool def)
223 return prefs[key].to_bool(def);
232 no_preferences_save =
true;
242 #ifdef DEFAULT_PREFS_PATH
248 read(user_prefs, *stream);
250 prefs.merge_with(user_prefs);
257 ERR_CFG <<
"Error loading preference, message: " <<
e.what();
266 prefs[
"show_ally_orb"] = show_orb;
273 prefs[
"show_status_on_ally_orb"] = show_orb;
280 prefs[
"show_enemy_orb"] = show_orb;
287 prefs[
"show_moved_orb"] = show_orb;
294 prefs[
"show_unmoved_orb"] = show_orb;
301 prefs[
"show_partial_orb"] = show_orb;
308 prefs[
"show_disengaged_orb"] = show_orb;
312 if (color.substr(0,4) ==
"orb_") {
313 if(color[4] >=
'0' && color[4] <=
'9') {
314 return color.substr(5);
316 return color.substr(4);
323 std::string ally_color =
get(
"ally_orb_color");
324 if (ally_color.empty())
329 prefs[
"ally_orb_color"] = color_id;
349 prefs[
"enemy_orb_color"] = color_id;
359 prefs[
"moved_orb_color"] = color_id;
369 prefs[
"unmoved_orb_color"] = color_id;
373 std::string partmoved_color =
get(
"partial_orb_color");
374 if (partmoved_color.empty())
379 prefs[
"partial_orb_color"] = color_id;
384 return get(
"scroll_to_action",
true);
389 prefs[
"scroll_to_action"] = ison;
394 const unsigned x_res =
prefs[
"xresolution"].to_unsigned();
395 const unsigned y_res =
prefs[
"yresolution"].to_unsigned();
398 if(x_res == 0 || y_res == 0) {
421 return get(
"auto_pixel_scale",
true);
426 prefs[
"auto_pixel_scale"] = choice;
436 return get(
"fullscreen",
true);
441 return get(
"vsync",
true);
452 prefs[
"maximized"] = ison;
457 prefs[
"fullscreen"] = ison;
462 prefs[
"vsync"] = ison;
471 return get(
"turbo",
false);
476 prefs[
"turbo"] = ison;
481 return prefs[
"turbo_speed"].to_double(2.0);
486 prefs[
"turbo_speed"] = speed;
507 return prefs[
"keepalive_timeout"].to_int(10);
512 prefs[
"keepalive_timeout"] = std::abs(seconds);
517 return get(
"idle_anim",
true);
522 prefs[
"idle_anim"] = ison;
527 return prefs[
"idle_anim_rate"].to_double(1.0);
532 prefs[
"idle_anim_rate"] = std::pow(2.0, -rate / 10.0);
537 return prefs[
"locale"];
547 return prefs[
"gui2_theme"];
557 return get(
"show_side_colors",
false);
567 return get(
"grid",
false);
580 const std::size_t buf_size = 4096;
582 const std::size_t buf_size = 1024;
585 return prefs[
"sound_buffer_size"].to_int(buf_size);
591 const char* buf_size =
"4096";
593 const char* buf_size =
"1024";
596 const std::string new_size = lexical_cast_default<std::string>(
size, buf_size);
597 if (
get(
"sound_buffer_size") == new_size)
607 return prefs[
"music_volume"].to_int(100);
616 prefs[
"music_volume"] = vol;
622 return prefs[
"sound_volume"].to_int(100);
631 prefs[
"sound_volume"] = vol;
637 return prefs[
"bell_volume"].to_int(100);
646 prefs[
"bell_volume"] = vol;
652 return prefs[
"UI_volume"].to_int(100);
661 prefs[
"UI_volume"] = vol;
667 return prefs[
"tile_size"].to_unsigned();
677 return get(
"turn_bell",
true);
701 return get(
"UI_sound",
true);
725 return get(
"message_bell",
true);
730 return get(
"sound",
true);
753 return get(
"music",
true);
779 return get(
"stop_music_in_background",
false);
793 const int value = std::clamp<int>(lexical_cast_default<int>(
get(
"scroll"), 50), 1, 100);
794 scroll = value/100.0;
801 prefs[
"scroll"] = new_speed;
802 scroll = new_speed / 100.0;
807 return get(
"middle_click_scrolls",
true);
812 return get(
"mouse_scrolling",
true);
817 set(
"mouse_scrolling", value);
822 return prefs[
"scroll_threshold"].to_int(10);
887 set(
"animate_map", value);
892 set(
"animate_water", value);
907 return prefs[
"draw_delay"].to_int(-1);
912 prefs[
"draw_delay"] = value;
917 return get(
"color_cursors",
true);
938 prefs.clear_children(
"hotkey");
941 void add_alias(
const std::string &alias,
const std::string &command)
944 alias_list[alias] = command;
955 return prefs[
"sample_rate"].to_int(44100);
963 prefs[
"sample_rate"] =
static_cast<int>(rate);
971 return get(
"confirm_load_save_from_different_version",
true);
976 return get(
"use_twelve_hour_clock_format",
false);
981 return get(
"disable_auto_moves",
false);
991 return get(
"damage_prediction_allow_monte_carlo_simulation",
true);
996 set(
"damage_prediction_allow_monte_carlo_simulation", value);
1001 return get(
"addon_manager_saved_order_name");
1006 set(
"addon_manager_saved_order_name", value);
1021 return get(
"selected_achievement_group");
1026 set(
"selected_achievement_group", content_for);
1029 bool achievement(
const std::string& content_for,
const std::string&
id)
1031 for(
config& ach :
prefs.child_range(
"achievements"))
1033 if(ach[
"content_for"].str() == content_for)
1035 std::vector<std::string> ids =
utils::split(ach[
"ids"]);
1036 return std::find(ids.begin(), ids.end(),
id) != ids.end();
1044 for(
config& ach :
prefs.child_range(
"achievements"))
1047 if(ach[
"content_for"].str() == content_for)
1049 std::vector<std::string> ids =
utils::split(ach[
"ids"]);
1055 else if(std::find(ids.begin(), ids.end(),
id) == ids.end())
1057 ach[
"ids"] = ach[
"ids"].str() +
"," +
id;
1059 ach.remove_children(
"in_progress", [&
id](
config cfg){
return cfg[
"id"].str() ==
id;});
1066 ach[
"content_for"] = content_for;
1068 prefs.add_child(
"achievements", ach);
1071 int progress_achievement(
const std::string& content_for,
const std::string&
id,
int limit,
int max_progress,
int amount)
1078 for(
config& ach :
prefs.child_range(
"achievements"))
1081 if(ach[
"content_for"].str() == content_for)
1086 if(in_progress[
"id"].str() ==
id)
1089 int starting_progress = in_progress[
"progress_at"].to_int();
1090 if(starting_progress >= limit) {
1091 return starting_progress;
1094 in_progress[
"progress_at"] = std::clamp(starting_progress + amount, 0, std::min(limit, max_progress));
1095 return in_progress[
"progress_at"].to_int();
1103 set_progress[
"id"] =
id;
1104 set_progress[
"progress_at"] = std::clamp(amount, 0, std::min(limit, max_progress));
1107 return child[
"progress_at"].to_int();
1119 set_progress[
"id"] =
id;
1120 set_progress[
"progress_at"] = std::clamp(amount, 0, std::min(limit, max_progress));
1122 ach[
"content_for"] = content_for;
1126 prefs.add_child(
"achievements", ach);
1127 return child[
"progress_at"].to_int();
1132 bool sub_achievement(
const std::string& content_for,
const std::string&
id,
const std::string& sub_id)
1140 for(
config& ach :
prefs.child_range(
"achievements"))
1142 if(ach[
"content_for"].str() == content_for)
1145 for(
const auto& in_progress : ach.child_range(
"in_progress"))
1147 if(in_progress[
"id"] ==
id)
1149 std::vector<std::string> sub_ids =
utils::split(in_progress[
"sub_ids"]);
1150 return std::find(sub_ids.begin(), sub_ids.end(), sub_id) != sub_ids.end();
1166 for(
config& ach :
prefs.child_range(
"achievements"))
1169 if(ach[
"content_for"].str() == content_for)
1174 if(in_progress[
"id"].str() ==
id)
1176 std::vector<std::string> sub_ids =
utils::split(ach[
"ids"]);
1178 if(std::find(sub_ids.begin(), sub_ids.end(), sub_id) == sub_ids.end())
1180 in_progress[
"sub_ids"] = in_progress[
"sub_ids"].str() +
"," + sub_id;
1183 in_progress[
"progress_at"] = sub_ids.size()+1;
1190 set_progress[
"id"] =
id;
1191 set_progress[
"sub_ids"] = sub_id;
1192 set_progress[
"progress_at"] = 1;
1193 ach.
add_child(
"in_progress", set_progress);
1202 set_progress[
"id"] =
id;
1203 set_progress[
"sub_ids"] = sub_id;
1204 set_progress[
"progress_at"] = 1;
1206 ach[
"content_for"] = content_for;
1209 ach.
add_child(
"in_progress", set_progress);
1210 prefs.add_child(
"achievements", ach);
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
child_itors child_range(config_key_type key)
config & add_child(config_key_type key)
virtual void join_global()
virtual void leave_global()
sdl_handler(sdl_handler &&)=delete
static game_config_view wrap(const config &cfg)
virtual void handle_window_event(const SDL_Event &event)
virtual void handle_event(const SDL_Event &)
Declarations for File-IO.
std::string id
Text to match against addon_info.tags()
New lexcical_cast header.
Standard logging facilities (interface).
void point(int x, int y)
Draw a single point.
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
filesystem::scoped_ostream ostream_file(const std::string &fname, std::ios_base::openmode mode, bool create_directory)
std::string get_prefs_file()
std::unique_ptr< std::istream > scoped_istream
std::unique_ptr< std::ostream > scoped_ostream
std::string get_default_prefs_file()
std::string partial_orb_color
std::string moved_orb_color
std::string unmoved_orb_color
std::string ally_orb_color
std::string enemy_orb_color
bool show_status_on_ally_orb
void save_hotkeys(config &cfg)
Save the non-default hotkeys to the config.
void reset_default_hotkeys()
Reset all hotkeys to the defaults.
void load_custom_hotkeys(const game_config_view &cfg)
Registers all hotkeys present in this config, overwriting any matching default hotkeys.
Modify, read and display user preferences.
void set_achievement(const std::string &content_for, const std::string &id)
Marks the specified achievement as completed.
void set_scroll_speed(const int new_speed)
void set_show_disengaged_orb(bool show_orb)
bool stop_music_in_background()
const int max_window_height
const int def_window_width
std::size_t sound_buffer_size()
void disable_preferences_save()
void set_turbo(bool ison)
void set_allied_color(const std::string &color_id)
void set_addon_manager_saved_order_direction(sort_order::type value)
bool have_setting(const std::string &key)
void set_music_volume(int vol)
void set_show_partial_orb(bool show_orb)
void set_idle_anim(const bool ison)
bool minimap_movement_coding()
void clear(const std::string &key)
void set_core_id(const std::string &core_id)
bool set_turn_bell(bool ison)
void set_show_enemy_orb(bool show_orb)
void set_sound_volume(int vol)
sort_order::type addon_manager_saved_order_direction()
void set_gui_theme(const std::string &s)
void save_sound_buffer_size(const std::size_t size)
const int min_pixel_scale
bool set_sound(bool ison)
bool use_twelve_hour_clock_format()
bool minimap_draw_villages()
void toggle_minimap_draw_villages()
int font_scaled(int size)
std::string moved_color()
void set_bell_volume(int vol)
void set_damage_prediction_allow_monte_carlo_simulation(bool value)
void set_turbo_speed(const double speed)
void set_draw_delay(int value)
void set(const std::string &key, bool value)
bool minimap_terrain_coding()
const int max_window_width
bool damage_prediction_allow_monte_carlo_simulation()
void set_unmoved_color(const std::string &color_id)
void set_show_fps(bool value)
int mouse_scroll_threshold()
Gets the threshold for when to scroll.
void set_idle_anim_rate(const int rate)
void set_addon_manager_saved_order_name(const std::string &value)
std::string addon_manager_saved_order_name()
void set_stop_music_in_background(bool ison)
bool minimap_draw_terrain()
optional_const_config get_alias()
std::string selected_achievement_group()
void set_animate_water(bool value)
const int max_font_scaling
void set_vsync(bool ison)
const int min_font_scaling
std::string partial_color()
const int def_window_height
config::attribute_value get_as_attribute(const std::string &key)
void toggle_minimap_draw_units()
void set_font_scaling(int scale)
bool mouse_scroll_enabled()
bool set_music(bool ison)
void enable_mouse_scroll(bool value)
prefs_event_handler event_handler_
void set_pixel_scale(const int scale)
bool show_status_on_ally_orb()
void set_show_ally_orb(bool show_orb)
std::string enemy_color()
void _set_fullscreen(bool ison)
void add_alias(const std::string &alias, const std::string &command)
void set_disable_auto_moves(bool value)
std::string allied_color()
void set_language(const std::string &s)
void set_sub_achievement(const std::string &content_for, const std::string &id, const std::string &sub_id)
Marks the specified sub-achievement as completed.
bool disable_auto_moves()
int progress_achievement(const std::string &content_for, const std::string &id, int limit, int max_progress, int amount)
Increments the achievement's current progress by amount if it hasn't already been completed.
void toggle_minimap_terrain_coding()
void _set_maximized(bool ison)
void set_UI_volume(int vol)
optional_const_config get_child(const std::string &key)
void save_sample_rate(const unsigned int rate)
unsigned int sample_rate()
std::string unmoved_color()
void set_auto_pixel_scale(bool choice)
const int max_pixel_scale
const int min_window_height
void set_child(const std::string &key, const config &val)
bool set_UI_sound(bool ison)
void set_tile_size(const unsigned int size)
static std::string fix_orb_color_name(const std::string &color)
void set_ellipses(bool ison)
bool confirm_load_save_from_different_version()
void set_selected_achievement_group(const std::string &content_for)
void set_enemy_color(const std::string &color_id)
void set_moved_color(const std::string &color_id)
bool minimap_draw_units()
const int min_window_width
void set_animate_map(bool value)
bool middle_click_scrolls()
void toggle_minimap_draw_terrain()
std::string get(const std::string &key)
void set_show_status_on_ally_orb(bool show_orb)
void set_show_moved_orb(bool show_orb)
void erase(const std::string &key)
void _set_color_cursors(bool value)
void set_show_unmoved_orb(bool show_orb)
bool achievement(const std::string &content_for, const std::string &id)
bool show_disengaged_orb()
void toggle_minimap_movement_coding()
void set_scroll_to_action(bool ison)
void set_partial_color(const std::string &color_id)
void _set_resolution(const point &res)
bool sub_achievement(const std::string &content_for, const std::string &id, const std::string &sub_id)
void set_bell_volume(int vol)
void set_music_volume(int vol)
void set_UI_volume(int vol)
void set_sound_volume(int vol)
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
std::vector< std::string > split(const config_attribute_value &val)
bool headless()
The game is running headless.
point window_size()
Returns the size of the window in display units / screen coordinates.
void scale(size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
static lg::log_domain log_filesystem("filesystem")
static lg::log_domain log_config("config")
void read(config &cfg, std::istream &in, abstract_validator *validator)
void write(std::ostream &out, const configr_of &cfg, unsigned int level)
An exception object used when an IO error occurs.
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.
static constexpr std::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
static map_location::DIRECTION s