The Battle for Wesnoth  1.19.0-dev
select_orb_colors.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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 
22 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/grid.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 #include "game_config.hpp"
28 #include "preferences/general.hpp"
29 #include <functional>
30 
31 namespace gui2::dialogs
32 {
33 namespace
34 {
35 std::string get_orb_widget_prefix(const std::string& base_id)
36 {
37  return "orb_" + base_id + "_";
38 }
39 
40 } // namespace
41 
42 REGISTER_DIALOG(select_orb_colors)
43 
45  : modal_dialog(window_id())
46  , show_unmoved_(preferences::show_unmoved_orb())
47  , show_partial_(preferences::show_partial_orb())
48  , show_disengaged_(preferences::show_disengaged_orb())
49  , show_moved_(preferences::show_moved_orb())
50  , show_ally_(preferences::show_ally_orb())
51  , two_color_ally_(preferences::show_status_on_ally_orb())
52  , show_enemy_(preferences::show_enemy_orb())
53 {
54 }
55 
57 {
63 
65  find_widget<button>(&window, "orb_defaults", false), std::bind(&select_orb_colors::reset_orb_callback, this));
66 }
67 
69 {
70  if(get_retval() != retval::OK) {
71  return;
72  }
73 
81 
82  preferences::set_unmoved_color(groups_["unmoved"].get_active_member_value());
83  preferences::set_partial_color(groups_["partial"].get_active_member_value());
84  preferences::set_moved_color(groups_["moved"].get_active_member_value());
85  preferences::set_allied_color(groups_["ally"].get_active_member_value());
86  preferences::set_enemy_color(groups_["enemy"].get_active_member_value());
87 }
88 
89 void select_orb_colors::setup_orb_toggle(const std::string& base_id, bool& shown)
90 {
91  const std::string prefix = get_orb_widget_prefix(base_id);
92  toggle_button& toggle = find_widget<toggle_button>(get_window(), prefix + "show", false);
93  toggle.set_value_bool(shown);
94 
95  connect_signal_mouse_left_click(toggle, std::bind(&select_orb_colors::toggle_orb_callback, this, std::ref(shown)));
96 }
97 
98 void select_orb_colors::setup_orb_group(const std::string& base_id, bool& shown, const std::string& initial)
99 {
100  setup_orb_toggle(base_id, shown);
101 
102  //
103  // Set up the toggle group.
104  //
105  group<std::string>& group = groups_[base_id];
106 
107  // Grid containing each color option toggle.
108  const std::string prefix = get_orb_widget_prefix(base_id);
109  grid& selection = find_widget<grid>(get_window(), prefix + "selection", false);
110 
111  for(iteration::bottom_up_iterator<true, false, true> iter(selection); !iter.at_end(); ++iter) {
112  if(toggle_button* button = dynamic_cast<toggle_button*>(iter.get())) {
113  const std::string& id = button->id();
114  group.add_member(button, id.substr(prefix.size()));
115  }
116  }
117 
118  group.set_member_states(initial);
119 }
120 
121 void select_orb_colors::setup_orb_group_two_color(const std::string& base_id, bool& shown, bool& two_color, const std::string& initial)
122 {
123  setup_orb_group(base_id, shown, initial);
124 
125  const std::string prefix = get_orb_widget_prefix(base_id);
126  toggle_button& toggle = find_widget<toggle_button>(get_window(), prefix + "two_color", false);
127  toggle.set_value_bool(two_color);
128 
129  connect_signal_mouse_left_click(toggle, std::bind(&select_orb_colors::toggle_orb_callback, this, std::ref(two_color)));
130 }
131 
132 void select_orb_colors::reset_orb_toggle(const std::string& base_id, bool shown)
133 {
134  const std::string prefix = get_orb_widget_prefix(base_id);
135 
136  toggle_button& toggle = find_widget<toggle_button>(get_window(), prefix + "show", false);
137  toggle.set_value_bool(shown);
138 }
139 
140 void select_orb_colors::reset_orb_group(const std::string& base_id, bool shown, const std::string& initial)
141 {
142  reset_orb_toggle(base_id, shown);
143  groups_[base_id].set_member_states(initial);
144 }
145 
146 void select_orb_colors::reset_orb_group_two_color(const std::string& base_id, bool shown, bool two_color, const std::string& initial)
147 {
148  reset_orb_group(base_id, shown, initial);
149 
150  const std::string prefix = get_orb_widget_prefix(base_id);
151 
152  toggle_button& toggle = find_widget<toggle_button>(get_window(), prefix + "two_color", false);
153  toggle.set_value_bool(two_color);
154 }
155 
157 {
158  // The code for the two-color groups uses this for both the main setting and the two_color setting - if
159  // you add any extra logic here, check that it's still also applicable to the two_color setting.
160  storage = !storage;
161 }
162 
164 {
172 
178 }
179 
180 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
window * get_window()
Returns a pointer to the dialog's window.
void setup_orb_toggle(const std::string &base_id, bool &shown)
The display function.
void reset_orb_group(const std::string &base_id, bool shown, const std::string &initial)
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void setup_orb_group(const std::string &base_id, bool &shown, const std::string &initial)
Sets up the checkbox and row of color buttons for the one-color options, including connecting the cal...
void setup_orb_group_two_color(const std::string &base_id, bool &shown, bool &two_color, const std::string &initial)
Sets up two checkboxes and a row of color buttons.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
void reset_orb_group_two_color(const std::string &base_id, bool shown, bool two_color, const std::string &initial)
std::map< std::string, group< std::string > > groups_
void reset_orb_toggle(const std::string &base_id, bool shown)
Change the UI's ticked/unticked state.
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:42
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:111
The iterator class.
Definition: iterator.hpp:37
bool at_end() const
Has the iterator reached the end?
Definition: iterator.hpp:58
void set_value_bool(bool value, bool fire_event=false)
Class for a toggle button.
const std::string & id() const
Definition: widget.cpp:110
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
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 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
bool show_moved_orb
bool show_ally_orb
bool show_disengaged_orb
bool show_partial_orb
bool show_enemy_orb
bool show_unmoved_orb
REGISTER_DIALOG(tod_new_schedule)
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
Modify, read and display user preferences.
void set_show_disengaged_orb(bool show_orb)
Definition: general.cpp:307
void set_allied_color(const std::string &color_id)
Definition: general.cpp:328
void set_show_partial_orb(bool show_orb)
Definition: general.cpp:300
void set_show_enemy_orb(bool show_orb)
Definition: general.cpp:279
std::string moved_color()
Definition: general.cpp:352
void set_unmoved_color(const std::string &color_id)
Definition: general.cpp:368
std::string partial_color()
Definition: general.cpp:372
void set_show_ally_orb(bool show_orb)
Definition: general.cpp:265
std::string enemy_color()
Definition: general.cpp:342
std::string allied_color()
Definition: general.cpp:322
std::string unmoved_color()
Definition: general.cpp:362
void set_enemy_color(const std::string &color_id)
Definition: general.cpp:348
void set_moved_color(const std::string &color_id)
Definition: general.cpp:358
void set_show_status_on_ally_orb(bool show_orb)
Definition: general.cpp:272
void set_show_moved_orb(bool show_orb)
Definition: general.cpp:286
void set_show_unmoved_orb(bool show_orb)
Definition: general.cpp:293
void set_partial_color(const std::string &color_id)
Definition: general.cpp:378