The Battle for Wesnoth  1.19.7+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 
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/grid.hpp"
24 #include "gui/widgets/window.hpp"
25 
26 #include "game_config.hpp"
28 #include <functional>
29 
30 namespace gui2::dialogs
31 {
32 namespace
33 {
34 std::string get_orb_widget_prefix(const std::string& base_id)
35 {
36  return "orb_" + base_id + "_";
37 }
38 
39 } // namespace
40 
41 REGISTER_DIALOG(select_orb_colors)
42 
44  : modal_dialog(window_id())
45  , show_unmoved_(prefs::get().show_unmoved_orb())
46  , show_partial_(prefs::get().show_partial_orb())
47  , show_disengaged_(prefs::get().show_disengaged_orb())
48  , show_moved_(prefs::get().show_moved_orb())
49  , show_ally_(prefs::get().show_ally_orb())
50  , two_color_ally_(prefs::get().show_status_on_ally_orb())
51  , show_enemy_(prefs::get().show_enemy_orb())
52 {
53 }
54 
56 {
57  setup_orb_group("unmoved", show_unmoved_, prefs::get().unmoved_color());
59  setup_orb_group("moved", show_moved_, prefs::get().moved_color());
61  setup_orb_group("enemy", show_enemy_, prefs::get().enemy_color());
62 
64  find_widget<button>("orb_defaults"), std::bind(&select_orb_colors::reset_orb_callback, this));
65 }
66 
68 {
69  if(get_retval() != retval::OK) {
70  return;
71  }
72 
73  prefs::get().set_show_unmoved_orb(show_unmoved_);
74  prefs::get().set_show_partial_orb(show_partial_);
75  prefs::get().set_show_disengaged_orb(show_disengaged_);
76  prefs::get().set_show_moved_orb(show_moved_);
77  prefs::get().set_show_ally_orb(show_ally_);
78  prefs::get().set_show_status_on_ally_orb(two_color_ally_);
79  prefs::get().set_show_enemy_orb(show_enemy_);
80 
81  prefs::get().set_unmoved_color(groups_["unmoved"].get_active_member_value());
82  prefs::get().set_partial_color(groups_["partial"].get_active_member_value());
83  prefs::get().set_moved_color(groups_["moved"].get_active_member_value());
84  prefs::get().set_allied_color(groups_["ally"].get_active_member_value());
85  prefs::get().set_enemy_color(groups_["enemy"].get_active_member_value());
86 }
87 
88 void select_orb_colors::setup_orb_toggle(const std::string& base_id, bool& shown)
89 {
90  const std::string prefix = get_orb_widget_prefix(base_id);
91  toggle_button& toggle = find_widget<toggle_button>(prefix + "show");
92  toggle.set_value_bool(shown);
93 
94  connect_signal_mouse_left_click(toggle, std::bind(&select_orb_colors::toggle_orb_callback, this, std::ref(shown)));
95 }
96 
97 void select_orb_colors::setup_orb_group(const std::string& base_id, bool& shown, const std::string& initial)
98 {
99  setup_orb_toggle(base_id, shown);
100 
101  //
102  // Set up the toggle group.
103  //
104  group<std::string>& group = groups_[base_id];
105 
106  // Grid containing each color option toggle.
107  const std::string prefix = get_orb_widget_prefix(base_id);
108  grid& selection = find_widget<grid>(prefix + "selection");
109 
110  for(iteration::bottom_up_iterator<true, false, true> iter(selection); !iter.at_end(); ++iter) {
111  if(toggle_button* button = dynamic_cast<toggle_button*>(iter.get())) {
112  const std::string& id = button->id();
113  group.add_member(button, id.substr(prefix.size()));
114  }
115  }
116 
117  group.set_member_states(initial);
118 }
119 
120 void select_orb_colors::setup_orb_group_two_color(const std::string& base_id, bool& shown, bool& two_color, const std::string& initial)
121 {
122  setup_orb_group(base_id, shown, initial);
123 
124  const std::string prefix = get_orb_widget_prefix(base_id);
125  toggle_button& toggle = find_widget<toggle_button>(prefix + "two_color");
126  toggle.set_value_bool(two_color);
127 
128  connect_signal_mouse_left_click(toggle, std::bind(&select_orb_colors::toggle_orb_callback, this, std::ref(two_color)));
129 }
130 
131 void select_orb_colors::reset_orb_toggle(const std::string& base_id, bool shown)
132 {
133  const std::string prefix = get_orb_widget_prefix(base_id);
134 
135  toggle_button& toggle = find_widget<toggle_button>(prefix + "show");
136  toggle.set_value_bool(shown);
137 }
138 
139 void select_orb_colors::reset_orb_group(const std::string& base_id, bool shown, const std::string& initial)
140 {
141  reset_orb_toggle(base_id, shown);
142  groups_[base_id].set_member_states(initial);
143 }
144 
145 void select_orb_colors::reset_orb_group_two_color(const std::string& base_id, bool shown, bool two_color, const std::string& initial)
146 {
147  reset_orb_group(base_id, shown, initial);
148 
149  const std::string prefix = get_orb_widget_prefix(base_id);
150 
151  toggle_button& toggle = find_widget<toggle_button>(prefix + "two_color");
152  toggle.set_value_bool(two_color);
153 }
154 
156 {
157  // The code for the two-color groups uses this for both the main setting and the two_color setting - if
158  // you add any extra logic here, check that it's still also applicable to the two_color setting.
159  storage = !storage;
160 }
161 
163 {
171 
177 }
178 
179 } // 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.
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 post_show() override
Actions to be taken after the window has been shown.
virtual void pre_show() 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.
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)
const std::string & id() const
Definition: widget.cpp:110
static prefs & get()
void set_allied_color(const std::string &color_id)
void set_enemy_color(const std::string &color_id)
void set_unmoved_color(const std::string &color_id)
void set_partial_color(const std::string &color_id)
void set_moved_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.
CURSOR_TYPE get()
Definition: cursor.cpp:216
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(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