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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
#include "turns/domain/turn_order.hpp"
#include "turns/domain/participant.hpp"
#include <algorithm>
#include <compare>
#include <limits>
#include <typeinfo>
#include <glibmm/refptr.h>
namespace turns::domain
{
namespace
{
auto constexpr comparator = [](auto lhs, auto rhs) {
return *lhs > *rhs;
};
auto constexpr equal_comparator = [](auto lhs, auto rhs) {
return (lhs->get_name() == rhs->get_name()) && (lhs->get_priority() && rhs->get_priority());
};
} // namespace
/** Construction */
turn_order::turn_order()
: Glib::ObjectBase{typeid(turn_order)}
, Gio::ListModel{}
{
}
auto turn_order::create() -> Glib::RefPtr<turn_order>
{
return Glib::make_refptr_for_instance(new turn_order{});
}
/** Queries */
auto turn_order::is_empty() const -> Glib::PropertyProxy_ReadOnly<is_empty_type>
{
return m_is_empty.get_proxy();
}
auto turn_order::has_next() const -> Glib::PropertyProxy_ReadOnly<has_next_type>
{
return m_has_next.get_proxy();
}
auto turn_order::has_previous() const -> Glib::PropertyProxy_ReadOnly<has_previous_type>
{
return m_has_previous.get_proxy();
}
auto turn_order::is_running() const -> Glib::PropertyProxy_ReadOnly<is_running_type>
{
return m_is_running.get_proxy();
}
auto turn_order::round_number() const -> Glib::PropertyProxy_ReadOnly<round_number_type>
{
return m_round_number.get_proxy();
}
/** Modifiers */
auto turn_order::add(Glib::ustring const & name, float priority, disposition disposition) -> void
{
auto entry = participant::create(name, priority, disposition);
entry->priority().signal_changed().connect(sigc::bind(sigc::mem_fun(*this, &turn_order::handle_priority_changed), entry));
auto position = std::distance(m_data.cbegin(), insert(entry));
items_changed(position, 0, 1);
if (get_n_items() == 1)
{
m_is_empty = false;
m_has_next = true;
}
}
auto turn_order::clear() -> void
{
m_is_running = false;
m_is_empty = true;
m_has_next = false;
m_has_previous = false;
m_active.reset();
m_round_number = invalid_round_number;
auto old_size = get_n_items();
m_data.clear();
items_changed(0, old_size, 0);
}
auto turn_order::next() -> void
{
auto old_active = *m_active;
m_active = m_active.transform([this](auto index) { return (index + 1) % get_n_items(); });
m_has_previous = true;
m_data[old_active]->is_active() = false;
m_data[*m_active]->is_active() = true;
if (m_active == 0)
{
m_round_number = m_round_number + 1;
}
}
auto turn_order::previous() -> void
{
if (!(m_has_previous && m_is_running))
{
return;
}
auto old_active = *m_active;
m_active = m_active.transform([this](auto index) { return index ? index - 1 : get_n_items() - 1; });
m_has_previous = m_round_number > 0 || m_active > 0;
m_data[old_active]->is_active() = false;
m_data[*m_active]->is_active() = true;
if (m_active == 0)
{
m_round_number = m_round_number - 1;
}
}
auto turn_order::remove(unsigned index) -> void
{
if (index >= get_n_items())
{
return;
}
auto position = m_data.begin() + index;
m_data.erase(position);
items_changed(index, 1, 0);
if (get_n_items() == 0)
{
m_is_empty = true;
m_is_running = false;
m_has_next = false;
}
}
auto turn_order::start() -> void
{
if (!m_active)
{
m_active = 0;
m_data[*m_active]->is_active() = true;
}
if (m_round_number == invalid_round_number)
{
m_round_number = 0;
}
m_is_running = true;
}
auto turn_order::stop() -> void
{
m_is_running = false;
}
/** ListModel implementation */
auto turn_order::get_item_type_vfunc() -> GType
{
return participant::get_type();
}
auto turn_order::get_n_items_vfunc() -> unsigned
{
return m_data.size();
}
auto turn_order::get_item_vfunc(unsigned position) -> void *
{
if (position >= get_n_items())
{
return nullptr;
}
auto item = m_data[position];
item->reference();
return item->gobj();
}
/** Signal handlers */
auto turn_order::handle_priority_changed(value_type entry) -> void
{
auto original_position = find(entry);
auto original_index = distance(m_data.cbegin(), original_position);
auto target_position = std::ranges::upper_bound(m_data, entry, comparator);
if (original_position == target_position)
{
return;
}
m_data.erase(original_position);
auto inserted_position = insert(entry);
items_changed(0, get_n_items(), get_n_items());
if (m_active == original_index)
{
m_active = distance(m_data.cbegin(), inserted_position);
m_has_previous = m_round_number > 0 || m_active > 0;
}
}
/** Data management */
auto turn_order::find(value_type entry) const -> const_iterator
{
return std::ranges::find(m_data, entry);
}
auto turn_order::insert(value_type entry) -> const_iterator
{
return m_data.insert(std::ranges::upper_bound(m_data, entry, comparator), entry);
}
} // namespace turns::domain
|