The Battle for Wesnoth  1.19.0+dev
canvas_private.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 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 #include "gui/core/canvas.hpp"
18 
19 namespace gui2
20 {
21 
22 class line_shape : public canvas::shape
23 {
24 public:
25  /**
26  * Constructor.
27  *
28  * @param cfg The config object to define the line.
29  */
30  explicit line_shape(const config& cfg);
31 
32  void draw(wfl::map_formula_callable& variables) override;
33 
34 private:
35  typed_formula<unsigned> x1_; /**< The start x coordinate of the line. */
36  typed_formula<unsigned> y1_; /**< The start y coordinate of the line. */
37  typed_formula<unsigned> x2_; /**< The end x coordinate of the line. */
38  typed_formula<unsigned> y2_; /**< The end y coordinate of the line. */
39 
40  /** The color of the line. */
42 
43  /**
44  * The thickness of the line.
45  *
46  * if the value is odd the x and y are the middle of the line.
47  * if the value is even the x and y are the middle of a line
48  * with width - 1. (0 is special case, does nothing.)
49  */
50  unsigned thickness_;
51 };
52 
54 {
55 protected:
56  /**
57  * Constructor.
58  *
59  * @param cfg The config object to define the rectangle.
60  */
61  explicit rect_bounded_shape(const config& cfg)
62  : shape(cfg)
63  , x_(cfg["x"])
64  , y_(cfg["y"])
65  , w_(cfg["w"])
66  , h_(cfg["h"])
67  {
68  }
69 
70  typed_formula<int> x_; /**< The x coordinate of the rectangle. */
71  typed_formula<int> y_; /**< The y coordinate of the rectangle. */
72  typed_formula<int> w_; /**< The width of the rectangle. */
73  typed_formula<int> h_; /**< The height of the rectangle. */
74 };
75 
77 {
78 public:
79  /**
80  * Constructor.
81  *
82  * @param cfg The config object to define the rectangle.
83  */
84  explicit rectangle_shape(const config& cfg);
85 
86  void draw(wfl::map_formula_callable& variables) override;
87 
88 private:
89  /**
90  * Border thickness.
91  *
92  * If 0 the fill color is used for the entire widget.
93  */
95 
96  /**
97  * The border color of the rectangle.
98  *
99  * If the color is fully transparent the border isn't drawn.
100  */
102 
103  /**
104  * The border color of the rectangle.
105  *
106  * If the color is fully transparent the rectangle won't be filled.
107  */
109 };
110 
112 {
113 public:
114  /**
115  * Constructor.
116  *
117  * @param cfg The config object to define the round rectangle.
118  */
119  explicit round_rectangle_shape(const config& cfg);
120 
121  void draw(wfl::map_formula_callable& variables) override;
122 
123 private:
124  typed_formula<int> r_; /**< The radius of the corners. */
125 
126  /**
127  * Border thickness.
128  *
129  * If 0 the fill color is used for the entire widget.
130  */
132 
133  /**
134  * The border color of the rounded rectangle.
135  *
136  * If the color is fully transparent the border isn't drawn.
137  */
139 
140  /**
141  * The border color of the rounded rectangle.
142  *
143  * If the color is fully transparent the rounded rectangle won't be filled.
144  */
146 };
147 
149 {
150 public:
151  /**
152  * Constructor.
153  *
154  * @param cfg The config object to define the circle.
155  */
156  explicit circle_shape(const config& cfg);
157 
158  void draw( wfl::map_formula_callable& variables) override;
159 
160 private:
161  typed_formula<unsigned> x_; /**< The center x coordinate of the circle. */
162  typed_formula<unsigned> y_; /**< The center y coordinate of the circle. */
163 
164  /** The radius of the circle. */
166 
167  /** The border color of the circle. */
169 
170  /** The fill color of the circle. */
172 
173  /** The border thickness of the circle. */
174  unsigned int border_thickness_;
175 };
176 
178 {
179 public:
180  /**
181  * Constructor.
182  *
183  * @param cfg The config object to define the image.
184  * @param functions WFL functions to execute.
185  */
186  image_shape(const config& cfg, wfl::action_function_symbol_table& functions);
187 
188  void draw(wfl::map_formula_callable& variables) override;
189 
190 private:
191  typed_formula<unsigned> x_; /**< The x coordinate of the image. */
192  typed_formula<unsigned> y_; /**< The y coordinate of the image. */
193  typed_formula<unsigned> w_; /**< The width of the image. */
194  typed_formula<unsigned> h_; /**< The height of the image. */
195 
196  /**
197  * Name of the image.
198  *
199  * This value is only used when the image name is a formula. If it isn't a
200  * formula the image will be loaded in the constructor. If it's a formula it
201  * will be loaded every draw cycles. This allows 'changing' images.
202  */
204 
205  /**
206  * Determines the way an image will be resized.
207  *
208  * If the image is smaller is needed it needs to resized, how is determined
209  * by the value of this enum.
210  */
211  enum class resize_mode {
212  scale,
213  scale_sharp,
214  stretch,
215  tile,
216  tile_center,
217  tile_highres,
218  };
219 
220  /** Converts a string to a resize mode. */
221  resize_mode get_resize_mode(const std::string& resize_mode);
222 
223  /** The resize mode for an image. */
225 
226  /** Mirror the image over the vertical axis. */
228 
229  // TODO: use a typed_formula?
231 
232  static void dimension_validation(unsigned value, const std::string& name, const std::string& key);
233 };
234 
236 {
237 public:
238  /**
239  * Constructor.
240  *
241  * @param cfg The config object to define the text.
242  * @param functions WFL functions to execute.
243  */
244  explicit text_shape(const config& cfg, wfl::action_function_symbol_table& functions);
245 
246  void draw(wfl::map_formula_callable& variables) override;
247 
248 private:
249  /** The text font family. */
251 
252  /** The font size of the text. */
254 
255  /** The style of the text. */
257 
258  /** The alignment of the text. */
260 
261  /** The color of the text. */
263 
264  /** The text to draw. */
266 
267  /** The text markup switch of the text. */
269 
270  /** The link aware switch of the text. */
272 
273  /** The link color of the text. */
275 
276  /** The maximum width for the text. */
278 
279  /** The number of characters per line. */
281 
282  /** The maximum height for the text. */
284 
285  /** Start and end offsets for highlight */
286  std::string highlight_start_;
287  std::string highlight_end_;
288 
289  /** The color to be used for highlighting */
291 
292  /** Generic start and end offsets for various attributes */
293  std::string attr_start_;
294  std::string attr_end_;
295 
296  /**
297  * The attribute type
298  * Possible values :
299  * color/foreground, bgcolor/background, font_size/size,
300  * bold, italic, underline
301  * The first three require extra data
302  * the color for the first two, and font size for the last
303  */
304  std::string attr_name_;
305 
306  /** extra data for the attribute, if any */
307  std::string attr_data_;
308 
309  /** Whether to apply a text outline. */
311 
312  /** Any extra WFL actions to execute. */
314 };
315 
316 }
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:159
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:213
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:228
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:385
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:272
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:281
image_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:255
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:166
typed_formula< int > r_
The radius of the corners.
round_rectangle_shape(const config &cfg)
Constructor.
Definition: canvas.cpp:148
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.
std::string highlight_end_
std::string attr_name_
The attribute type Possible values : color/foreground, bgcolor/background, font_size/size,...
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.
std::string attr_start_
Generic start and end offsets for various attributes.
std::string attr_data_
extra data for the attribute, if any
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.
std::string highlight_start_
Start and end offsets for highlight.
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:450
text_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:415
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.
typed_formula< bool > text_markup_
The text markup switch of the text.
std::string attr_end_
family_class
Font classes for get_font_families().
Generic file dialog.