-
-
Notifications
You must be signed in to change notification settings - Fork 993
/
generator.hpp
376 lines (317 loc) · 12.2 KB
/
generator.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of 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 "widget.hpp"
#include "tstring.hpp"
#include <boost/dynamic_bitset.hpp>
#include <array>
typedef std::map<std::string, t_string> string_map;
namespace gui2
{
struct builder_grid;
typedef std::shared_ptr<const builder_grid> builder_grid_const_ptr;
class grid;
/**
* Abstract base class for the generator.
*
* A generator is a class which holds multiple grids and controls their
* placement on the screen. The final class is policy based, more info about
* the possible policies is documented in the build() function. This function
* is the factory to generate the classes as well.
*/
class generator_base : public widget
{
friend class debug_layout_graph;
public:
virtual ~generator_base()
{
}
/** Determines how the items are placed. */
enum placement {
horizontal_list,
vertical_list,
table,
independent
};
/**
* Create a new generator.
*
* @param has_minimum Does one item need to be selected.
* @param has_maximum Is one the maximum number of items that can
* be selected?
* @param placement The placement of the grids, see tplacement
* for more info.
* @param select If a grid is selected, what should happen?
* If true the grid is selected, if false the
* grid is shown.
*
* @returns A pointer to a new object. The caller gets
* ownership of the new object.
*/
static generator_base* build(const bool has_minimum,
const bool has_maximum,
const placement placement,
const bool select);
/**
* Deletes an item.
*/
virtual void delete_item(const unsigned index) = 0;
/** Deletes all items. */
virtual void clear() = 0;
/**
* (De)selects an item.
*
* @param index The item to (de)select.
* @param select If true selects, if false deselects.
*/
virtual void select_item(const unsigned index, const bool select) = 0;
/**
* Toggles the selection state of an item.
*
* @param index The item to toggle.
*/
void toggle_item(const unsigned index)
{
select_item(index, !is_selected(index));
}
/** Returns whether the item is selected. */
virtual bool is_selected(const unsigned index) const = 0;
/**
* Shows or hides an item.
*
* The caller is responsible for reformatting the grid.
*
* @param index The item to show or hide.
* @param show If true shows the item, else hides it.
*/
virtual void set_item_shown(const unsigned index, const bool show) = 0;
/** Returns whether the item is shown. */
virtual bool get_item_shown(const unsigned index) const = 0;
/** Returns the visibility of all the items as a bit set. */
boost::dynamic_bitset<> get_items_shown() const
{
boost::dynamic_bitset<> items_shown(get_item_count());
for (unsigned int i = 0u; i < get_item_count(); ++i)
{
items_shown[i] = get_item_shown(i);
}
return items_shown;
}
/** Returns the number of items. */
virtual unsigned get_item_count() const = 0;
/** Returns the number of selected items. */
virtual unsigned get_selected_item_count() const = 0;
/**
* Returns the selected item.
*
* If a list has multiple selected items it looks whether it knows the last
* item actually selected, if that item is selected that one is chosen.
* Else is goes through all selected items and returns the first one
* selected.
*
* @note stacked_widget depends on that behavior it always has all items
* selected and thus shown and by default the last selected item (the top
* one) is active.
*
* @returns The selected item, -1 if none selected.
*/
virtual int get_selected_item() const = 0;
/** Gets the grid of an item. */
virtual grid& item(const unsigned index) = 0;
/** Gets the grid of an item. */
virtual const grid& item(const unsigned index) const = 0;
/***** ***** ***** ***** Create items ***** ***** ***** *****/
/**
* Creates a new item.
*
* The item_data is used for the first widget found, this normally should
* be used when there's one widget in an item.
*
* @param index The item before which to add the new item,
* 0 == begin, -1 == end.
* @param list_builder A grid builder that's will build the
* contents of the new item.
* @param item_data The data to initialize the parameters of
* the new item.
* @param callback The callback function to call when an item
* in the grid is (de)selected.
*
* @returns A reference to the newly created grid.
*/
virtual grid& create_item(const int index,
builder_grid_const_ptr list_builder,
const string_map& item_data,
const std::function<void(widget&)>& callback)
= 0;
/**
* Creates a new item.
*
* The item_data is used by id, and is meant to set multiple widgets in
* an item.
*
* @param index The item before which to add the new item,
* 0 == begin, -1 == end.
* @param list_builder A grid builder that's will build the
* contents of the new item.
* @param data The data to initialize the parameters of
* the new item.
* @param callback The callback function to call when an item
* in the grid is (de)selected.
*
* @returns A reference to the newly created grid.
*/
virtual grid&
create_item(const int index,
builder_grid_const_ptr list_builder,
const std::map<std::string /* widget id */, string_map>& data,
const std::function<void(widget&)>& callback) = 0;
/**
* Creates one or more new item(s).
*
* For every item in item_data a new item is generated. This version
* expects one widget per item.
*
* @param index The item before which to add the new item,
* 0 == begin, -1 == end.
* @param list_builder A grid builder that's will build the
* contents of the new item.
* @param data The data to initialize the parameters of
* the new item.
* @param callback The callback function to call when an item
* in the grid is (de)selected.
*/
virtual void create_items(const int index,
builder_grid_const_ptr list_builder,
const std::vector<string_map>& data,
const std::function<void(widget&)>& callback)
= 0;
/**
* Creates one or more new item(s).
*
* For every item in item_data a new item is generated. This version
* expects multiple widgets per item.
*
* @param index The item before which to add the new item,
* 0 == begin, -1 == end.
* @param list_builder A grid builder that's will build the
* contents of the new item.
* @param data The data to initialize the parameters of
* the new item.
* @param callback The callback function to call when an item
* in the grid is (de)selected.
*/
virtual void create_items(
const int index,
builder_grid_const_ptr list_builder,
const std::vector<std::map<std::string /*widget id*/, string_map> >&
data,
const std::function<void(widget&)>& callback) = 0;
typedef std::function<bool (unsigned, unsigned)> torder_func;
virtual void set_order(const torder_func& order) = 0;
/***** ***** ***** ***** Inherited ***** ***** ***** *****/
/*
* These functions must be defined in our child classes so make sure they
* become pure virtuals.
*/
/** See @ref widget::layout_initialize. */
virtual void layout_initialize(const bool full_initialization) override = 0;
/** See @ref widget::request_reduce_width. */
virtual void request_reduce_width(const unsigned maximum_width) override
= 0;
/** See @ref widget::request_reduce_height. */
virtual void request_reduce_height(const unsigned maximum_height) override
= 0;
/** See @ref widget::calculate_best_size. */
virtual point calculate_best_size() const override = 0;
/** See @ref widget::place. */
virtual void place(const point& origin, const point& size) override = 0;
/** See @ref widget::set_origin. */
virtual void set_origin(const point& origin) override = 0;
/** See @ref widget::set_visible_rectangle. */
virtual void set_visible_rectangle(const SDL_Rect& rectangle) override = 0;
/** See @ref widget::impl_draw_children. */
virtual void impl_draw_children(int x_offset, int y_offset) override = 0;
/** See @ref widget::find_at. */
virtual widget* find_at(const point& coordinate,
const bool must_be_active) override = 0;
/** See @ref widget::find_at. */
virtual const widget* find_at(const point& coordinate,
const bool must_be_active) const override
= 0;
/***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
/**
* Up arrow key pressed.
*
* @param modifier The SDL keyboard modifier when the key was
* pressed.
* @param handled If the function handles the key it should
* set handled to true else do not modify it.
* This is used in the keyboard event
* changing.
*/
virtual void handle_key_up_arrow(SDL_Keymod modifier, bool& handled) = 0;
/**
* Down arrow key pressed.
*
* @param modifier The SDL keyboard modifier when the key was
* pressed.
* @param handled If the function handles the key it should
* set handled to true else do not modify it.
* This is used in the keyboard event
* changing.
*/
virtual void handle_key_down_arrow(SDL_Keymod modifier, bool& handled) = 0;
/**
* Left arrow key pressed.
*
* @param modifier The SDL keyboard modifier when the key was
* pressed.
* @param handled If the function handles the key it should
* set handled to true else do not modify it.
* This is used in the keyboard event
* changing.
*/
virtual void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) = 0;
/**
* Right arrow key pressed.
*
* @param modifier The SDL keyboard modifier when the key was
* pressed.
* @param handled If the function handles the key it should
* set handled to true else do not modify it.
* This is used in the keyboard event
* changing.
*/
virtual void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) = 0;
protected:
/**
* Selects a not selected item.
*
* @param index The index of a not selected item.
*/
virtual void do_select_item(const unsigned index) = 0;
/**
* Deselects a selected item.
*
* @param index The index of a selected item.
*/
virtual void do_deselect_item(const unsigned index) = 0;
/** Gets the grid of an item. */
virtual grid& item_ordered(const unsigned index) = 0;
/** Gets the grid of an item. */
virtual const grid& item_ordered(const unsigned index) const = 0;
public:
virtual unsigned get_ordered_index(unsigned index) const = 0;
virtual unsigned get_item_at_ordered(unsigned index_ordered) const = 0;
};
using generator_sort_array = std::array<generator_base::torder_func, 2>;
} // namespace gui2