/
window_builder.hpp
218 lines (166 loc) · 5.27 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
/*
Copyright (C) 2008 - 2016 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.
*/
#ifndef GUI_AUXILIARY_WINDOW_BUILDER_HPP_INCLUDED
#define GUI_AUXILIARY_WINDOW_BUILDER_HPP_INCLUDED
#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;
class CVideo;
namespace gui2
{
class window;
/**
* Builds a window.
*
* @param video The frame buffer to draw upon.
* @param type The type id string of the window, this window
* must be registered at startup.
*/
window* build(CVideo& video, 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;
/**
* 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,
std::function<builder_widget_ptr(config)> 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 to generate a widget from a WML widget instance.
*
* Mainly used as functor for @ref register_builder_widget.
*
* @param cfg The config with the information to
* Instantiate the widget.
*
* @returns A generic widget builder pointer.
*/
template <class T>
builder_widget_ptr build_widget(const config& cfg)
{
return std::make_shared<T>(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:
builder_window() : resolutions(), id_(), description_()
{
}
const std::string& read(const config& 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;
game_logic::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;
};
std::vector<window_resolution> resolutions;
private:
std::string id_;
std::string description_;
};
/**
* Builds a window.
*/
window* build(CVideo& video, const builder_window::window_resolution* res);
} // namespace gui2
#endif