The Battle for Wesnoth  1.19.9+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 
61  find_widget<button>("reachmap_defaults"), std::bind(&reachmap_options::reset_reachmap_callback, this));
62 
63 }
64 
66 {
67  if(get_retval() != retval::OK) {
68  return;
69  }
70 
71  //set the colors and opacity based on selected options:
72 
73  prefs::get().set_reach_map_color(groups_["standard_color"].get_active_member_value());
74  prefs::get().set_reach_map_enemy_color(groups_["enemy_color"].get_active_member_value());
75 
76  prefs::get().set_reach_map_border_opacity(find_widget<slider>("reachmap_opacity_border").get_value());
77  prefs::get().set_reach_map_tint_opacity(find_widget<slider>("reachmap_opacity_tint").get_value());
78 }
79 
80 void reachmap_options::setup_reachmap_group(const std::string& base_id, const std::string& initial)
81 {
82 
83  //
84  // Set up the toggle group.
85  //
86  group<std::string>& group = groups_[base_id];
87 
88  // Grid containing each color option toggle.
89  const std::string prefix = get_reachmap_widget_prefix(base_id);
90  grid& selection = find_widget<grid>(prefix + "selection");
91 
92  for(iteration::bottom_up_iterator<true, false, true> iter(selection); !iter.at_end(); ++iter) {
93  if(toggle_button* button = dynamic_cast<toggle_button*>(iter.get())) {
94  const std::string& id = button->id();
95  group.add_member(button, id.substr(prefix.size()));
96  }
97  }
98 
99  group.set_member_states(initial);
100 }
101 
102 void reachmap_options::reset_reachmap_group(const std::string& base_id, const std::string& initial)
103 {
104  groups_[base_id].set_member_states(initial);
105 }
106 
107 void reachmap_options::reset_reachmap_slider(const std::string& base_id, const int& initial)
108 {
109  find_widget<slider>(base_id).set_value(initial);
110 }
111 
113 {
118 }
119 
120 } // 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:402
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_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_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:177
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35