The Battle for Wesnoth  1.19.7+dev
window_builder.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
20 #include "gui/widgets/grid.hpp"
21 #include "color.hpp"
23 
24 
25 
26 namespace gui2
27 {
28 
29 class window;
30 
31 /** Contains the info needed to instantiate a widget. */
33 {
34  /**
35  * The replacements type is used to define replacement types.
36  *
37  * Certain widgets need to build a part of themselves upon instantiation
38  * but at the time of the definition it's not yet known what exactly. By
39  * using and `[instance]' widget this decision can be postponed until
40  * instantiation.
41  */
42  using replacements_map = std::map<std::string, std::shared_ptr<builder_widget>>;
43 
44  /**
45  * @todo: evaluate whether to combine the two @ref build() functions with this
46  * as the sole parameter.
47  */
49 
50  explicit builder_widget(const config& cfg);
51 
52  virtual ~builder_widget()
53  {
54  }
55 
56  virtual std::unique_ptr<widget> build() const = 0;
57 
58  virtual std::unique_ptr<widget> build(const replacements_map& replacements) const = 0;
59 
60  /** Parameters for the widget. */
61  std::string id;
62  std::string linked_group;
63 
66 };
67 
68 using builder_widget_ptr = std::shared_ptr<builder_widget>;
69 using builder_widget_const_ptr = std::shared_ptr<const builder_widget>;
70 
71 /**
72  * Create a widget builder.
73  *
74  * This object holds the instance builder for a single widget.
75  *
76  * @param cfg The config object holding the information
77  * regarding the widget instance.
78  *
79  * @returns The builder for the widget instance.
80  */
82 
83 /**
84  * Implementation detail for @ref build_single_widget_instance. Do not use directly!
85  *
86  * @param type String ID of the widget type.
87  * @param cfg Data config to pass to the widget's builder.
88  *
89  * @returns A shared_ptr of the base widget type containing
90  * the newly built widget.
91  */
92 std::unique_ptr<widget> build_single_widget_instance_helper(const std::string& type, const config& cfg);
93 
94 /**
95  * Builds a single widget instance of the given type with the specified attributes.
96  *
97  * This should be used in place of creating a widget object directly, as it
98  * allows the widget-specific builder code to be executed.
99  *
100  * This is equivalent to calling @c build() on the result of @ref create_widget_builder.
101  *
102  * @tparam T The final widget type. The widget pointer will be
103  * cast to this.
104  *
105  * @param cfg Data config to pass to the widget's builder.
106  *
107  * @returns A shared_ptr of the given type containing the
108  * newly build widget.
109  */
110 template<typename T>
111 std::unique_ptr<T> build_single_widget_instance(const config& cfg = {})
112 {
113  static_assert(std::is_base_of_v<widget, T>, "Type is not a widget type");
114  return std::unique_ptr<T>{ static_cast<T*>(build_single_widget_instance_helper(T::type(), cfg).release()) };
115 }
116 
118 {
119  explicit builder_grid(const config& cfg);
120 
121  unsigned rows;
122  unsigned cols;
123 
124  /** The grow factor for the rows / columns. */
125  std::vector<unsigned> row_grow_factor;
126  std::vector<unsigned> col_grow_factor;
127 
128  /** The flags per grid cell. */
129  std::vector<unsigned> flags;
130 
131  /** The border size per grid cell. */
132  std::vector<unsigned> border_size;
133 
134  /** The widgets per grid cell. */
135  std::vector<builder_widget_ptr> widgets;
136 
137  /** Inherited from @ref builder_widget. */
138  virtual std::unique_ptr<widget> build() const override;
139 
140  /** Inherited from @ref builder_widget. */
141  virtual std::unique_ptr<widget> build(const replacements_map& replacements) const override;
142 
143  void build(grid& grid, optional_replacements replacements = utils::nullopt) const;
144 };
145 
146 using builder_grid_ptr = std::shared_ptr<builder_grid>;
147 using builder_grid_const_ptr = std::shared_ptr<const builder_grid>;
148 using builder_grid_map = std::map<std::string, builder_grid_const_ptr>;
149 
151 {
152 public:
153  explicit builder_window(const config& cfg)
154  : resolutions()
155  , id_(cfg["id"])
156  , description_(cfg["description"])
157  {
158  read(cfg);
159  }
160 
162  {
163  explicit window_resolution(const config& cfg);
164 
165  unsigned window_width;
166  unsigned window_height;
167 
169 
175 
177 
180 
183 
185 
186  std::string definition;
187 
188  std::vector<linked_group_definition> linked_groups;
189 
190  /** Helper struct to store information about the tips. */
192  {
193  tooltip_info(const config& cfg, const std::string& tagname);
194 
195  std::string id;
196  };
197 
200 
202  };
203 
204  /**
205  * Resolution options for this window instance.
206  *
207  * The window widget handles resolution options differently from other widgets.
208  * Most specify their resolution options in their definitions. However, windows
209  * define different resolution options for each window *instance*. That enables
210  * each dialog to have its own set of options.
211  */
212  std::vector<window_resolution> resolutions;
213 
214 private:
215  void read(const config& cfg);
216 
217  std::string id_;
218  std::string description_;
219 };
220 
221 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
builder_window(const config &cfg)
std::vector< window_resolution > resolutions
Resolution options for this window instance.
void read(const config &cfg)
Base container class.
Definition: grid.hpp:32
A simple wrapper class for optional reference types.
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
std::shared_ptr< builder_widget > builder_widget_ptr
std::unique_ptr< T > build_single_widget_instance(const config &cfg={})
Builds a single widget instance of the given type with the specified attributes.
std::unique_ptr< widget > build_single_widget_instance_helper(const std::string &type, const config &cfg)
Implementation detail for build_single_widget_instance.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.
std::map< std::string, builder_grid_const_ptr > builder_grid_map
std::shared_ptr< const builder_widget > builder_widget_const_ptr
std::shared_ptr< const builder_grid > builder_grid_const_ptr
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
std::vector< unsigned > col_grow_factor
std::vector< unsigned > flags
The flags per grid cell.
std::vector< builder_widget_ptr > widgets
The widgets per grid cell.
builder_grid(const config &cfg)
std::vector< unsigned > row_grow_factor
The grow factor for the rows / columns.
virtual std::unique_ptr< widget > build() const override
Inherited from builder_widget.
std::vector< unsigned > border_size
The border size per grid cell.
Contains the info needed to instantiate a widget.
virtual std::unique_ptr< widget > build(const replacements_map &replacements) const =0
widget::debug_border debug_border_mode
virtual std::unique_ptr< widget > build() const =0
builder_widget(const config &cfg)
std::string id
Parameters for the widget.
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
Helper struct to store information about the tips.
tooltip_info(const config &cfg, const std::string &tagname)
std::vector< linked_group_definition > linked_groups
wfl::function_symbol_table functions
typed_formula< unsigned > maximum_height
typed_formula< unsigned > maximum_width