The Battle for Wesnoth  1.17.10+dev
select_orb_colors.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2022
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"
24 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/window.hpp"
27 
28 #include "game_config.hpp"
29 #include "preferences/general.hpp"
30 #include <functional>
31 
32 namespace gui2::dialogs
33 {
34 namespace
35 {
36 std::string get_orb_widget_prefix(const std::string& base_id)
37 {
38  return "orb_" + base_id + "_";
39 }
40 
41 } // namespace
42 
43 REGISTER_DIALOG(select_orb_colors)
44 
46  : modal_dialog(window_id())
47  , show_unmoved_(preferences::show_unmoved_orb())
48  , show_partial_(preferences::show_partial_orb())
49  , show_disengaged_(preferences::show_disengaged_orb())
50  , show_moved_(preferences::show_moved_orb())
51  , show_ally_(preferences::show_allied_orb())
52  , show_enemy_(preferences::show_enemy_orb())
53 {
54 }
55 
57 {
60  setup_orb_toggle("disengaged", show_disengaged_);
64 
66  find_widget<button>(&window, "orb_defaults", false), std::bind(&select_orb_colors::reset_orb_callback, this));
67 }
68 
70 {
71  if(get_retval() != retval::OK) {
72  return;
73  }
74 
81 
82  preferences::set_unmoved_color(groups_["unmoved"].get_active_member_value());
83  preferences::set_partial_color(groups_["partial"].get_active_member_value());
84  // For the minimap, show disengaged units as partially moved
85  preferences::set_disengaged_color(groups_["partial"].get_active_member_value());
86  preferences::set_moved_color(groups_["moved"].get_active_member_value());
87  preferences::set_allied_color(groups_["ally"].get_active_member_value());
88  preferences::set_enemy_color(groups_["enemy"].get_active_member_value());
89 }
90 
91 void select_orb_colors::setup_orb_toggle(const std::string& base_id, bool& shown)
92 {
93  const std::string prefix = get_orb_widget_prefix(base_id);
94  toggle_button& toggle = find_widget<toggle_button>(get_window(), prefix + "show", false);
95  toggle.set_value_bool(shown);
96 
97  connect_signal_mouse_left_click(toggle, std::bind(&select_orb_colors::toggle_orb_callback, this, std::ref(shown)));
98 }
99 
100 void select_orb_colors::setup_orb_group(const std::string& base_id, bool& shown, const std::string& initial)
101 {
102  setup_orb_toggle(base_id, shown);
103 
104  //
105  // Set up the toggle group.
106  //
107  group<std::string>& group = groups_[base_id];
108 
109  // Grid containing each color option toggle.
110  const std::string prefix = get_orb_widget_prefix(base_id);
111  grid& selection = find_widget<grid>(get_window(), prefix + "selection", false);
112 
113  for(iteration::bottom_up_iterator<true, false, true> iter(selection); !iter.at_end(); ++iter) {
114  if(toggle_button* button = dynamic_cast<toggle_button*>(iter.get())) {
115  const std::string& id = button->id();
116  group.add_member(button, id.substr(prefix.size()));
117  }
118  }
119 
120  group.set_member_states(initial);
121 }
122 
123 void select_orb_colors::reset_orb_toggle(const std::string& base_id, bool& shown)
124 {
125  const std::string prefix = get_orb_widget_prefix(base_id);
126 
127  toggle_button& toggle = find_widget<toggle_button>(get_window(), prefix + "show", false);
128  toggle.set_value_bool(shown);
129 }
130 
131 void select_orb_colors::reset_orb_group(const std::string& base_id, bool& shown, const std::string& initial)
132 {
133  reset_orb_toggle(base_id, shown);
134  groups_[base_id].set_member_states(initial);
135 }
136 
138 {
139  storage = !storage;
140 }
141 
143 {
150 
153  reset_orb_toggle("disengaged", show_disengaged_);
157 }
158 
159 } // namespace dialogs
void set_show_enemy_orb(bool show_orb)
Definition: general.cpp:272
std::string unmoved_color()
Definition: general.cpp:355
void set_value_bool(bool value, bool fire_event=false)
bool show_unmoved_orb
void set_moved_color(const std::string &color_id)
Definition: general.cpp:351
void set_allied_color(const std::string &color_id)
Definition: general.cpp:321
void set_unmoved_color(const std::string &color_id)
Definition: general.cpp:361
void reset_orb_group(const std::string &base_id, bool &shown, const std::string &initial)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:179
void set_show_unmoved_orb(bool show_orb)
Definition: general.cpp:286
const std::string & id() const
Definition: widget.cpp:111
This file contains the window object, this object is a top level container which has the event manage...
bool show_allied_orb()
Definition: general.cpp:262
std::string enemy_orb_color
bool show_enemy_orb
bool show_ally_orb
Base container class.
Definition: grid.hpp:31
std::string ally_orb_color
bool at_end() const
Has the iterator reached the end?
Definition: iterator.hpp:58
void set_enemy_color(const std::string &color_id)
Definition: general.cpp:341
This file contains the settings handling of the widget library.
void setup_orb_group(const std::string &base_id, bool &shown, const std::string &initial)
void set_partial_color(const std::string &color_id)
Definition: general.cpp:371
void set_show_partial_orb(bool show_orb)
Definition: general.cpp:293
Modify, read and display user preferences.
std::string moved_orb_color
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
void set_show_moved_orb(bool show_orb)
Definition: general.cpp:279
window * get_window()
Returns a pointer to the dialog&#39;s window.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void add_member(selectable_item *w, const T &value)
Adds a widget/value pair to the group map.
Definition: group.hpp:42
std::string allied_color()
Definition: general.cpp:315
std::string unmoved_orb_color
std::string moved_color()
Definition: general.cpp:345
void reset_orb_toggle(const std::string &base_id, bool &shown)
bool show_moved_orb
void setup_orb_toggle(const std::string &base_id, bool &shown)
The display function.
The iterator class.
Definition: iterator.hpp:36
std::string partial_color()
Definition: general.cpp:365
int get_retval() const
Returns the cached window exit code.
void set_show_disengaged_orb(bool show_orb)
Definition: general.cpp:300
Abstract base class for all modal dialogs.
Simple push button.
Definition: button.hpp:36
Contains the base iterator class for the gui2 widgets.
bool show_partial_orb
std::string partial_orb_color
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::map< std::string, group< std::string > > groups_
void set_disengaged_color(const std::string &color_id)
Definition: general.cpp:381
bool show_disengaged_orb
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66
void set_show_allied_orb(bool show_orb)
Definition: general.cpp:265
std::string enemy_color()
Definition: general.cpp:335
Class for a toggle button.
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