The Battle for Wesnoth  1.19.15+dev
canvas_private.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2025
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 #include "gui/core/canvas.hpp"
18 
19 #include "font/attributes.hpp"
20 
21 namespace gui2
22 {
23 
24 class line_shape : public canvas::shape
25 {
26 public:
27  /**
28  * Constructor.
29  *
30  * @param cfg The config object to define the line.
31  */
32  explicit line_shape(const config& cfg);
33 
34  void draw(wfl::map_formula_callable& variables) override;
35 
36 private:
37  typed_formula<unsigned> x1_; /**< The start x coordinate of the line. */
38  typed_formula<unsigned> y1_; /**< The start y coordinate of the line. */
39  typed_formula<unsigned> x2_; /**< The end x coordinate of the line. */
40  typed_formula<unsigned> y2_; /**< The end y coordinate of the line. */
41 
42  /** The color of the line. */
44 
45  /**
46  * The thickness of the line.
47  *
48  * if the value is odd the x and y are the middle of the line.
49  * if the value is even the x and y are the middle of a line
50  * with width - 1. (0 is special case, does nothing.)
51  */
52  unsigned thickness_;
53 };
54 
56 {
57 protected:
58  /**
59  * Constructor.
60  *
61  * @param cfg The config object to define the rectangle.
62  */
63  explicit rect_bounded_shape(const config& cfg)
64  : shape(cfg)
65  , x_(cfg["x"])
66  , y_(cfg["y"])
67  , w_(cfg["w"])
68  , h_(cfg["h"])
69  {
70  }
71 
72  typed_formula<int> x_; /**< The x coordinate of the rectangle. */
73  typed_formula<int> y_; /**< The y coordinate of the rectangle. */
74  typed_formula<int> w_; /**< The width of the rectangle. */
75  typed_formula<int> h_; /**< The height of the rectangle. */
76 };
77 
79 {
80 public:
81  /**
82  * Constructor.
83  *
84  * @param cfg The config object to define the rectangle.
85  */
86  explicit rectangle_shape(const config& cfg);
87 
88  void draw(wfl::map_formula_callable& variables) override;
89 
90 private:
91  /**
92  * Border thickness.
93  *
94  * If 0 the fill color is used for the entire widget.
95  */
97 
98  /**
99  * The border color of the rectangle.
100  *
101  * If the color is fully transparent the border isn't drawn.
102  */
104 
105  /**
106  * The border color of the rectangle.
107  *
108  * If the color is fully transparent the rectangle won't be filled.
109  */
111 };
112 
114 {
115 public:
116  /**
117  * Constructor.
118  *
119  * @param cfg The config object to define the round rectangle.
120  */
121  explicit round_rectangle_shape(const config& cfg);
122 
123  void draw(wfl::map_formula_callable& variables) override;
124 
125 private:
126  typed_formula<int> r_; /**< The radius of the corners. */
127 
128  /**
129  * Border thickness.
130  *
131  * If 0 the fill color is used for the entire widget.
132  */
134 
135  /**
136  * The border color of the rounded rectangle.
137  *
138  * If the color is fully transparent the border isn't drawn.
139  */
141 
142  /**
143  * The border color of the rounded rectangle.
144  *
145  * If the color is fully transparent the rounded rectangle won't be filled.
146  */
148 };
149 
151 {
152 public:
153  /**
154  * Constructor.
155  *
156  * @param cfg The config object to define the circle.
157  */
158  explicit circle_shape(const config& cfg);
159 
160  void draw( wfl::map_formula_callable& variables) override;
161 
162 private:
163  typed_formula<unsigned> x_; /**< The center x coordinate of the circle. */
164  typed_formula<unsigned> y_; /**< The center y coordinate of the circle. */
165 
166  /** The radius of the circle. */
168 
169  /** The border color of the circle. */
171 
172  /** The fill color of the circle. */
174 
175  /** The border thickness of the circle. */
176  unsigned int border_thickness_;
177 };
178 
180 {
181 public:
182  /**
183  * Constructor.
184  *
185  * @param cfg The config object to define the image.
186  * @param functions WFL functions to execute.
187  */
189 
190  void draw(wfl::map_formula_callable& variables) override;
191 
192 private:
193  typed_formula<unsigned> x_; /**< The x coordinate of the image. */
194  typed_formula<unsigned> y_; /**< The y coordinate of the image. */
195  typed_formula<unsigned> w_; /**< The width of the image. */
196  typed_formula<unsigned> h_; /**< The height of the image. */
197 
198  /** String to pass to the image loader. May be a Data URI, may include Image Path Functions. */
200 
201  /**
202  * Determines the way an image will be resized.
203  *
204  * If the image is smaller is needed it needs to resized, how is determined
205  * by the value of this enum.
206  */
207  enum class resize_mode {
208  scale,
209  scale_sharp,
210  stretch,
211  tile,
212  tile_center,
213  tile_highres,
214  };
215 
216  /** Converts a string to a resize mode. */
217  resize_mode get_resize_mode(const std::string& resize_mode);
218 
219  /** The resize mode for an image. */
221 
222  /** Mirror the image over the vertical axis. */
224 
225  // TODO: use a typed_formula?
227 
228  /**
229  * Prevents duplicate error logs when an image can't be loaded.
230  */
232 
233  static void dimension_validation(unsigned value, const std::string& name, const std::string& key);
234 };
235 
237 {
238 public:
239  /**
240  * Constructor.
241  *
242  * @param cfg The config object to define the text.
243  * @param functions WFL functions to execute.
244  */
245  explicit text_shape(const config& cfg, wfl::action_function_symbol_table& functions);
246 
247  void draw(wfl::map_formula_callable& variables) override;
248 
249 private:
250  /** The text font family. */
252 
253  /** The font size of the text. */
255 
256  /** The style of the text. */
258 
259  /** The alignment of the text. */
261 
262  /** The color of the text. */
264 
265  /** The text to draw. */
267 
268  /** Whether to parse text_ as WFL formula */
270 
271  /** The text markup switch of the text. */
273 
274  /** The link aware switch of the text. */
276 
277  /** The link color of the text. */
279 
280  /** The maximum width for the text. */
282 
283  /** The number of characters per line. */
285 
286  /** The maximum height for the text. */
288 
289  /** Start offset for highlight */
291 
292  /** End offset for highlight */
294 
295  /** The color to be used for highlighting */
297 
298  /** Spacing between lines */
300 
301  /** Whether to apply a text outline. */
303 
304  /** Any extra WFL actions to execute. */
306 
307  /** Any custom Pango text attributes. */
309 };
310 
311 }
This file contains the canvas object which is the part where the widgets draw (temporally) images on.
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
Helper class to encapsulate the management of a PangoAttrList.
Definition: attributes.hpp:28
Abstract base class for all other shapes.
Definition: canvas.hpp:54
shape(const config &cfg)
Definition: canvas.hpp:56
typed_formula< color_t > border_color_
The border color of the circle.
typed_formula< unsigned > x_
The center x coordinate of the circle.
typed_formula< unsigned > radius_
The radius of the circle.
unsigned int border_thickness_
The border thickness of the circle.
circle_shape(const config &cfg)
Constructor.
Definition: canvas.cpp:199
typed_formula< color_t > fill_color_
The fill color of the circle.
typed_formula< unsigned > y_
The center y coordinate of the circle.
void draw(wfl::map_formula_callable &variables) override
Draws the canvas.
Definition: canvas.cpp:214
typed_formula< std::string > image_name_
String to pass to the image loader.
resize_mode
Determines the way an image will be resized.
bool failure_logged_
Prevents duplicate error logs when an image can't be loaded.
typed_formula< unsigned > w_
The width of the image.
resize_mode get_resize_mode(const std::string &resize_mode)
Converts a string to a resize mode.
Definition: canvas.cpp:366
typed_formula< unsigned > x_
The x coordinate of the image.
static void dimension_validation(unsigned value, const std::string &name, const std::string &key)
Definition: canvas.cpp:255
typed_formula< unsigned > y_
The y coordinate of the image.
typed_formula< unsigned > h_
The height of the image.
resize_mode resize_mode_
The resize mode for an image.
wfl::formula actions_formula_
typed_formula< bool > mirror_
Mirror the image over the vertical axis.
void draw(wfl::map_formula_callable &variables) override
Draws the canvas.
Definition: canvas.cpp:264
image_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:237
typed_formula< color_t > color_
The color of the line.
typed_formula< unsigned > x1_
The start x coordinate of the line.
typed_formula< unsigned > y1_
The start y coordinate of the line.
unsigned thickness_
The thickness of the line.
typed_formula< unsigned > x2_
The end x coordinate of the line.
line_shape(const config &cfg)
Constructor.
Definition: canvas.cpp:50
typed_formula< unsigned > y2_
The end y coordinate of the line.
void draw(wfl::map_formula_callable &variables) override
Draws the canvas.
Definition: canvas.cpp:65
typed_formula< int > x_
The x coordinate of the rectangle.
typed_formula< int > w_
The width of the rectangle.
typed_formula< int > y_
The y coordinate of the rectangle.
rect_bounded_shape(const config &cfg)
Constructor.
typed_formula< int > h_
The height of the rectangle.
rectangle_shape(const config &cfg)
Constructor.
Definition: canvas.cpp:87
int border_thickness_
Border thickness.
typed_formula< color_t > fill_color_
The border color of the rectangle.
void draw(wfl::map_formula_callable &variables) override
Draws the canvas.
Definition: canvas.cpp:104
typed_formula< color_t > border_color_
The border color of the rectangle.
typed_formula< color_t > border_color_
The border color of the rounded rectangle.
void draw(wfl::map_formula_callable &variables) override
Draws the canvas.
Definition: canvas.cpp:152
typed_formula< int > r_
The radius of the corners.
round_rectangle_shape(const config &cfg)
Constructor.
Definition: canvas.cpp:134
int border_thickness_
Border thickness.
typed_formula< color_t > fill_color_
The border color of the rounded rectangle.
font::pango_text::FONT_STYLE font_style_
The style of the text.
typed_formula< bool > outline_
Whether to apply a text outline.
typed_formula< color_t > color_
The color of the text.
typed_formula< int > maximum_height_
The maximum height for the text.
bool parse_text_as_formula_
Whether to parse text_ as WFL formula.
typed_formula< bool > link_aware_
The link aware switch of the text.
typed_formula< PangoAlignment > text_alignment_
The alignment of the text.
typed_formula< color_t > highlight_color_
The color to be used for highlighting.
font::family_class font_family_
The text font family.
typed_formula< color_t > link_color_
The link color of the text.
typed_formula< int > maximum_width_
The maximum width for the text.
void draw(wfl::map_formula_callable &variables) override
Draws the canvas.
Definition: canvas.cpp:485
text_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:456
config::attribute_value text_
The text to draw.
typed_formula< int > highlight_end_
End offset for highlight.
float line_spacing_
Spacing between lines.
typed_formula< int > highlight_start_
Start offset for highlight.
typed_formula< unsigned > font_size_
The font size of the text.
unsigned characters_per_line_
The number of characters per line.
wfl::formula actions_formula_
Any extra WFL actions to execute.
font::attribute_list text_attributes_
Any custom Pango text attributes.
typed_formula< bool > text_markup_
The text markup switch of the text.
const config * cfg
Generic file dialog.