The Battle for Wesnoth  1.19.9+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  */
188  image_shape(const config& cfg, wfl::action_function_symbol_table& functions);
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  /**
199  * Name of the image.
200  *
201  * This value is only used when the image name is a formula. If it isn't a
202  * formula the image will be loaded in the constructor. If it's a formula it
203  * will be loaded every draw cycles. This allows 'changing' images.
204  */
206 
207  /**
208  * Determines the way an image will be resized.
209  *
210  * If the image is smaller is needed it needs to resized, how is determined
211  * by the value of this enum.
212  */
213  enum class resize_mode {
214  scale,
215  scale_sharp,
216  stretch,
217  tile,
218  tile_center,
219  tile_highres,
220  };
221 
222  /** Converts a string to a resize mode. */
223  resize_mode get_resize_mode(const std::string& resize_mode);
224 
225  /** The resize mode for an image. */
227 
228  /** Mirror the image over the vertical axis. */
230 
231  // TODO: use a typed_formula?
233 
234  static void dimension_validation(unsigned value, const std::string& name, const std::string& key);
235 };
236 
238 {
239 public:
240  /**
241  * Constructor.
242  *
243  * @param cfg The config object to define the text.
244  * @param functions WFL functions to execute.
245  */
246  explicit text_shape(const config& cfg, wfl::action_function_symbol_table& functions);
247 
248  void draw(wfl::map_formula_callable& variables) override;
249 
250 private:
251  /** The text font family. */
253 
254  /** The font size of the text. */
256 
257  /** The style of the text. */
259 
260  /** The alignment of the text. */
262 
263  /** The color of the text. */
265 
266  /** The text to draw. */
268 
269  /** The text markup switch of the text. */
271 
272  /** The link aware switch of the text. */
274 
275  /** The link color of the text. */
277 
278  /** The maximum width for the text. */
280 
281  /** The number of characters per line. */
283 
284  /** The maximum height for the text. */
286 
287  /** Start offset for highlight */
289 
290  /** End offset for highlight */
292 
293  /** The color to be used for highlighting */
295 
296  /** Whether to apply a text outline. */
298 
299  /** Any extra WFL actions to execute. */
301 
302  /** Any custom Pango text attributes. */
304 };
305 
306 }
This file contains the canvas object which is the part where the widgets draw (temporally) images on.
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:200
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:215
typed_formula< std::string > image_name_
Name of the image.
resize_mode
Determines the way an image will be resized.
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:259
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:268
image_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:242
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:51
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:66
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:88
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:105
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:153
typed_formula< int > r_
The radius of the corners.
round_rectangle_shape(const config &cfg)
Constructor.
Definition: canvas.cpp:135
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.
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:479
text_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:452
typed_formula< int > highlight_end_
End offset for highlight.
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.
typed_formula< t_string > text_
The text to draw.
font::attribute_list text_attributes_
Any custom Pango text attributes.
typed_formula< bool > text_markup_
The text markup switch of the text.
family_class
Font classes for get_font_families().
Generic file dialog.