-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
window_builder.hpp
234 lines (182 loc) · 6.08 KB
/
window_builder.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
/*
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 "gui/auxiliary/typed_formula.hpp"
#include "gui/core/linked_group_definition.hpp"
#include "gui/widgets/grid.hpp"
#include "color.hpp"
#include "utils/functional.hpp"
class config;
namespace gui2
{
class window;
/**
* Builds a window.
*
* @param type The type id string of the window, this window
* must be registered at startup.
*/
window* build(const std::string& type);
/** Contains the info needed to instantiate a widget. */
struct builder_widget
{
public:
/**
* The replacements type is used to define replacement types.
*
* Certain widgets need to build a part of themselves upon instantiation
* but at the time of the definition it's not yet known what exactly. By
* using and `[instance]' widget this decision can be postponed until
* instantiation.
*/
typedef std::map<std::string, std::shared_ptr<builder_widget> > replacements_map;
explicit builder_widget(const config& cfg);
virtual ~builder_widget()
{
}
virtual widget* build() const = 0;
virtual widget* build(const replacements_map& replacements) const = 0;
/** Parameters for the widget. */
std::string id;
std::string linked_group;
int debug_border_mode;
color_t debug_border_color;
};
typedef std::shared_ptr<builder_widget> builder_widget_ptr;
typedef std::shared_ptr<const builder_widget> builder_widget_const_ptr;
using widget_builder_func_t = std::function<builder_widget_ptr(config)>;
/**
* Registers a widget to be build.
*
* @warning This function runs before @ref main() so needs to be careful
* regarding the static initialization problem.
*
* @param id The id of the widget as used in WML.
* @param functor The functor to create the widget.
*/
void
register_builder_widget(const std::string& id, widget_builder_func_t functor);
/**
* Create a widget builder.
*
* This object holds the instance builder for a single widget.
*
* @param cfg The config object holding the information
* regarding the widget instance.
*
* @returns The builder for the widget instance.
*/
builder_widget_ptr create_builder_widget(const config& cfg);
/**
* Helper function to implement @ref build_single_widget_instance. This keeps the main
* logic in the implementation despite said function being a template and therefor
* needing to be fully implemented in the declaration.
*/
widget* build_single_widget_instance_helper(const std::string& type, const config& cfg);
/**
* Builds a single widget instance of the given type with the specified attributes.
*
* This should be used in place of creating a widget object directly, as it
* allows the widget-specific builder code to be executed.
*
* @tparam T The final widget type. The widget pointer will be
* cast to this.
*
* @param type String ID of the widget type.
* @param cfg Data config to pass to the widget's builder.
*/
template<typename T>
T* build_single_widget_instance(const std::string& type, const config& cfg = config())
{
return dynamic_cast<T*>(build_single_widget_instance_helper(type, cfg));
}
struct builder_grid : public builder_widget
{
public:
explicit builder_grid(const config& cfg);
unsigned rows;
unsigned cols;
/** The grow factor for the rows / columns. */
std::vector<unsigned> row_grow_factor;
std::vector<unsigned> col_grow_factor;
/** The flags per grid cell. */
std::vector<unsigned> flags;
/** The border size per grid cell. */
std::vector<unsigned> border_size;
/** The widgets per grid cell. */
std::vector<builder_widget_ptr> widgets;
grid* build() const;
widget* build(const replacements_map& replacements) const;
grid* build(grid* grid) const;
void build(grid& grid, const replacements_map& replacements) const;
};
typedef std::shared_ptr<builder_grid> builder_grid_ptr;
typedef std::shared_ptr<const builder_grid> builder_grid_const_ptr;
class builder_window
{
public:
explicit builder_window(const config& cfg)
: resolutions()
, id_(cfg["id"])
, description_(cfg["description"])
{
read(cfg);
}
struct window_resolution
{
public:
explicit window_resolution(const config& cfg);
unsigned window_width;
unsigned window_height;
bool automatic_placement;
typed_formula<unsigned> x;
typed_formula<unsigned> y;
typed_formula<unsigned> width;
typed_formula<unsigned> height;
typed_formula<bool> reevaluate_best_size;
wfl::function_symbol_table functions;
unsigned vertical_placement;
unsigned horizontal_placement;
unsigned maximum_width;
unsigned maximum_height;
bool click_dismiss;
std::string definition;
std::vector<linked_group_definition> linked_groups;
/** Helper struct to store information about the tips. */
struct tooltip_info
{
tooltip_info(const config& cfg, const std::string& tagname);
std::string id;
};
tooltip_info tooltip;
tooltip_info helptip;
builder_grid_ptr grid;
};
/**
* Resolution options for this window instance.
*
* The window widget handles resolution options differently from other widgets.
* Most specify their resolution options in their definitions. However, windows
* define different resolution options for each window *instance*. That enables
* each dialog to have its own set of options.
*/
std::vector<window_resolution> resolutions;
private:
void read(const config& cfg);
std::string id_;
std::string description_;
};
/**
* Builds a window.
*/
window* build(const builder_window::window_resolution* res);
} // namespace gui2