The Battle for Wesnoth  1.19.27+dev
reachmap_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2025
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/grid.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "gui/widgets/slider.hpp"
26 
27 #include "game_config.hpp"
29 
30 #include <functional>
31 
32 namespace gui2::dialogs
33 {
34 namespace
35 {
36 std::string get_reachmap_widget_prefix(const std::string& base_id)
37 {
38  return "reachmap_" + base_id + "_";
39 }
40 
41 } // namespace
42 
43 REGISTER_DIALOG(reachmap_options)
44 
46  : modal_dialog(window_id())
47 {
48 }
49 
51 {
52  setup_reachmap_group("standard_color", prefs::get().reach_map_color());
54 
55  //set the sliders to the current value of opacity settings
56 
57  find_widget<slider>("reachmap_opacity_border").set_value(prefs::get().reach_map_border_opacity());
58  find_widget<slider>("reachmap_opacity_tint").set_value(prefs::get().reach_map_tint_opacity());
59  find_widget<slider>("reachmap_opacity_darken").set_value(prefs::get().reach_map_darken_opacity());
60  find_widget<toggle_button>("reachmap_darken_tex").set_value_bool(prefs::get().reach_map_darken_tex());
61 
63  find_widget<button>("reachmap_defaults"), std::bind(&reachmap_options::reset_reachmap_callback, this));
64 
65 }
66 
68 {
69  if(get_retval() != retval::OK) {
70  return;
71  }
72 
73  //set the colors and opacity based on selected options:
74 
75  prefs::get().set_reach_map_color(groups_["standard_color"].get_active_member_value());
76  prefs::get().set_reach_map_enemy_color(groups_["enemy_color"].get_active_member_value());
77 
78  prefs::get().set_reach_map_border_opacity(find_widget<slider>("reachmap_opacity_border").get_value());
79  prefs::get().set_reach_map_tint_opacity(find_widget<slider>("reachmap_opacity_tint").get_value());
80  prefs::get().set_reach_map_darken_opacity(find_widget<slider>("reachmap_opacity_darken").get_value());
81  prefs::get().set_reach_map_darken_tex(find_widget<toggle_button>("reachmap_darken_tex").get_value_bool());
82 }
83 
84 void reachmap_options::setup_reachmap_group(const std::string& base_id, const std::string& initial)
85 {
86 
87  //
88  // Set up the toggle group.
89  //
90  group<std::string>& group = groups_[base_id];
91 
92  // Grid containing each color option toggle.
93  const std::string prefix = get_reachmap_widget_prefix(base_id);
94  grid& selection = find_widget<grid>(prefix + "selection");
95 
96  for(iteration::bottom_up_iterator<true, false, true> iter(selection); !iter.at_end(); ++iter) {
97  if(toggle_button* button = dynamic_cast<toggle_button*>(iter.get())) {
98  const std::string& id = button->id();
99  group.add_member(button, id.substr(prefix.size()));
100  }
101  }
102 
103  group.set_member_states(initial);
104 }
105 
106 void reachmap_options::reset_reachmap_group(const std::string& base_id, const std::string& initial)
107 {
108  groups_[base_id].set_member_states(initial);
109 }
110 
111 void reachmap_options::reset_reachmap_slider(const std::string& base_id, const int& initial)
112 {
113  find_widget<slider>(base_id).set_value(initial);
114 }
115 
117 {
123  find_widget<toggle_button>("reachmap_darken_tex").set_value_bool(game_config::reach_map_darken_tex);
124 }
125 
126 } // namespace gui2::dialogs
Simple push button.
Definition: button.hpp:36
Abstract base class for all modal dialogs.
virtual void pre_show() override
Actions to be taken before showing the window.
virtual void post_show() override
Actions to be taken after the window has been shown.
void setup_reachmap_group(const std::string &base_id, const std::string &initial)
Sets up the checkbox and row of color buttons for the one-color options, including connecting the cal...
std::map< std::string, group< std::string > > groups_
void reset_reachmap_group(const std::string &base_id, const std::string &initial)
Change the UI's ticked/unticked state.
void reset_reachmap_slider(const std::string &base_id, const int &initial)
Base container class.
Definition: grid.hpp:32
void add_member(selectable_item *w, const T &value)
Adds a widget/value pair to the group map.
Definition: group.hpp:41
void set_member_states(const T &value)
Sets the toggle values for all widgets besides the one associated with the specified value to false.
Definition: group.hpp:110
The iterator class.
Definition: iterator.hpp:37
bool at_end() const
Has the iterator reached the end?
Definition: iterator.hpp:58
const std::string & id() const
Definition: widget.cpp:110
int get_retval()
Definition: window.hpp:394
void set_reach_map_enemy_color(const std::string &color_id)
void set_reach_map_border_opacity(const int new_opacity)
static prefs & get()
void set_reach_map_tint_opacity(const int new_opacity)
void set_reach_map_darken_opacity(const int new_opacity)
void set_reach_map_darken_tex(const bool value)
void set_reach_map_color(const std::string &color_id)
This file contains the window object, this object is a top level container which has the event manage...
Contains the base iterator class for the gui2 widgets.
std::string reach_map_enemy_color
std::string reach_map_color
int reach_map_border_opacity
int reach_map_darken_opacity
bool reach_map_darken_tex
int reach_map_tint_opacity
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:163
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35