-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
group.hpp
200 lines (168 loc) · 4.78 KB
/
group.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Copyright (C) 2008 - 2017 The Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "gui/core/event/dispatcher.hpp"
#include "gui/core/log.hpp"
#include "gui/widgets/styled_widget.hpp"
#include "gui/widgets/selectable_item.hpp"
#include "gui/widgets/widget.hpp"
#include "utils/functional.hpp"
#include <map>
#include <vector>
namespace gui2
{
template<class T>
class group
{
using group_map = std::map<T, selectable_item*>;
using order_vector = std::vector<styled_widget*>;
public:
/**
* Adds a widget/value pair to the group map. A callback is set that toggles each members'
* state to false when clicked. This happens before individual widget handlers fire, ensuring
* that the clicked widget will remain the only one selected.
*/
void add_member(selectable_item* w, const T& value)
{
bool success;
std::tie(std::ignore, success) = members_.emplace(value, w);
if(!success) {
ERR_GUI_G << "Group member with value " << value << "already exists." << std::endl;
return;
}
dynamic_cast<widget*>(w)->connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(&group::group_operator, this), event::dispatcher::front_child);
member_order_.push_back(dynamic_cast<styled_widget*>(w));
}
/**
* Removes a member from the group map.
*/
void remove_member(const T& value)
{
members_.erase(value);
}
/**
* Clears the entire group of members.
*/
void clear()
{
members_.clear();
}
/**
* Group member getters
*/
group_map& members()
{
return members_;
}
const group_map& members() const
{
return members_;
}
template<typename W>
W& member(const T& value)
{
return dynamic_cast<W&>(*members_.at(value));
}
/**
* Returns the value paired with the currently actively toggled member of the group.
*/
T get_active_member_value()
{
for(auto& member : members_) {
if(member.second->get_value_bool()) {
return member.first;
}
}
return T();
}
/**
* Sets the toggle values for all widgets besides the one associated
* with the specified value to false.
*/
void set_member_states(const T& value)
{
for(auto& member : members_) {
member.second->set_value(member.first == value);
}
}
/**
* Sets a common callback function for all members.
*/
void set_callback_on_value_change(std::function<void(widget&)> func)
{
// Ensure this callback is only called on the member being activated
const auto callback = [func](widget& widget)->void {
if(dynamic_cast<selectable_item*>(&widget)->get_value_bool()) {
func(widget);
}
};
for(auto& member : members_) {
event::connect_signal_notify_modified(dynamic_cast<widget&>(*member.second), std::bind(callback, _1));
}
}
/**
* Wrapper for enabling or disabling member widgets.
* Each member widget will be enabled or disabled based on the result of the specified
* predicate, which takes its associated value.
*
* If a selected widget is to be disabled, it is deselected and the first active member
* selected instead. The same happens if no members were previously active at all.
*/
void set_members_enabled(std::function<bool(const T&)> predicate)
{
bool do_reselect = true;
for(auto& member : members_) {
const bool res = predicate(member.first);
selectable_item& w = *member.second;
dynamic_cast<styled_widget&>(w).set_active(res);
// Only select another member if this was selected
if(w.get_value_bool()) {
do_reselect = !res;
if(do_reselect) {
w.set_value_bool(false);
}
}
}
if(!do_reselect) {
return;
}
// Look for the first active member to select
for(auto& member : member_order_) {
if(member->get_active()) {
dynamic_cast<selectable_item&>(*member).set_value_bool(true);
break;
}
}
}
private:
/**
* Container map for group members, organized by member value, associated widget.
*/
group_map members_;
/**
* Since iterating over std::map is specified by operator< for it's key values, we can't
* guarantee the order would line up with the logical order - ie, that which the widgets
* appear in in a specific dialog. Keeping a separate vector here allows iterating over
* members in the order which they are added to the group.
*/
order_vector member_order_;
/**
* The default actions to take when clicking on one of the widgets in the group.
*/
void group_operator()
{
for(auto& member : members_) {
member.second->set_value(false);
}
}
};
} // namespace gui2