The Battle for Wesnoth  1.19.21+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 "color.hpp"
17 #include "gui/core/canvas.hpp"
19 
20 #include "font/attributes.hpp"
21 
22 namespace gui2
23 {
24 
25 class line_shape : public canvas::shape
26 {
27 public:
28  /**
29  * Constructor.
30  *
31  * @param cfg The config object to define the line.
32  */
33  explicit line_shape(const config& cfg);
34 
35  void draw(wfl::map_formula_callable& variables) override;
36 
37 private:
38  typed_formula<unsigned> x1_; /**< The start x coordinate of the line. */
39  typed_formula<unsigned> y1_; /**< The start y coordinate of the line. */
40  typed_formula<unsigned> x2_; /**< The end x coordinate of the line. */
41  typed_formula<unsigned> y2_; /**< The end y coordinate of the line. */
42 
43  /** The color of the line. */
45 
46  /**
47  * The thickness of the line.
48  *
49  * if the value is odd the x and y are the middle of the line.
50  * if the value is even the x and y are the middle of a line
51  * with width - 1. (0 is special case, does nothing.)
52  */
53  unsigned thickness_;
54 };
55 
57 {
58 protected:
59 
60  explicit rect_bounded_shape(const rect& bounds)
61  : shape()
62  , x_(bounds.x)
63  , y_(bounds.y)
64  , w_(bounds.w)
65  , h_(bounds.h)
66  {
67  }
68 
69  explicit rect_bounded_shape(const point& origin, const std::string& w_f, const std::string& h_f)
70  : shape()
71  , x_(origin.x)
72  , y_(origin.y)
73  , w_(w_f)
74  , h_(h_f)
75  {
76  }
77 
78  /**
79  * Constructor.
80  *
81  * @param cfg The config object to define the rectangle.
82  */
83  explicit rect_bounded_shape(const config& cfg)
84  : shape(cfg)
85  , x_(cfg["x"])
86  , y_(cfg["y"])
87  , w_(cfg["w"])
88  , h_(cfg["h"])
89  {
90  }
91 
92  typed_formula<int> x_; /**< The x coordinate of the rectangle. */
93  typed_formula<int> y_; /**< The y coordinate of the rectangle. */
94  typed_formula<int> w_; /**< The width of the rectangle. */
95  typed_formula<int> h_; /**< The height of the rectangle. */
96 };
97 
99 {
100 public:
101  /**
102  * Constructor.
103  *
104  * @param cfg The config object to define the rectangle.
105  */
106  explicit rectangle_shape(const config& cfg);
107 
109  const rect& bounds,
110  const color_t& border_color,
111  const unsigned thickness = 1,
112  const color_t& fill_color = color_t::null_color());
113 
114  void draw(wfl::map_formula_callable& variables) override;
115 
116 private:
117  /**
118  * Border thickness.
119  *
120  * If 0 the fill color is used for the entire widget.
121  */
123 
124  /**
125  * The border color of the rectangle.
126  *
127  * If the color is fully transparent the border isn't drawn.
128  */
130 
131  /**
132  * The border color of the rectangle.
133  *
134  * If the color is fully transparent the rectangle won't be filled.
135  */
137 };
138 
140 {
141 public:
142  /**
143  * Constructor.
144  *
145  * @param cfg The config object to define the round rectangle.
146  */
147  explicit round_rectangle_shape(const config& cfg);
148 
149  void draw(wfl::map_formula_callable& variables) override;
150 
151 private:
152  typed_formula<int> r_; /**< The radius of the corners. */
153 
154  /**
155  * Border thickness.
156  *
157  * If 0 the fill color is used for the entire widget.
158  */
160 
161  /**
162  * The border color of the rounded rectangle.
163  *
164  * If the color is fully transparent the border isn't drawn.
165  */
167 
168  /**
169  * The border color of the rounded rectangle.
170  *
171  * If the color is fully transparent the rounded rectangle won't be filled.
172  */
174 };
175 
177 {
178 public:
179  /**
180  * Constructor.
181  *
182  * @param cfg The config object to define the circle.
183  */
184  explicit circle_shape(const config& cfg);
185 
186  void draw(wfl::map_formula_callable& variables) override;
187 
188 private:
189  typed_formula<unsigned> x_; /**< The center x coordinate of the circle. */
190  typed_formula<unsigned> y_; /**< The center y coordinate of the circle. */
191 
192  /** The radius of the circle. */
194 
195  /** The border color of the circle. */
197 
198  /** The fill color of the circle. */
200 
201  /** The border thickness of the circle. */
202  unsigned int border_thickness_;
203 };
204 
206 {
207 public:
208  /**
209  * Constructor.
210  *
211  * @param cfg The config object to define the image.
212  * @param functions WFL functions to execute.
213  */
215 
216  image_shape(
217  const point& origin,
218  const std::string& img_path);
219 
220  void draw(wfl::map_formula_callable& variables) override;
221 
222 private:
223  typed_formula<unsigned> x_; /**< The x coordinate of the image. */
224  typed_formula<unsigned> y_; /**< The y coordinate of the image. */
225  typed_formula<unsigned> w_; /**< The width of the image. */
226  typed_formula<unsigned> h_; /**< The height of the image. */
227 
228  /** Image path from which the image will be loaded. May also be a Data URI, and may include Image Path Functions. */
230 
231  /**
232  * Determines the way an image will be resized.
233  *
234  * If the image is smaller is needed it needs to resized, how is determined
235  * by the value of this enum.
236  */
237  enum class resize_mode {
238  scale,
239  scale_sharp,
240  stretch,
241  tile,
242  tile_center,
243  tile_highres,
244  };
245 
246  /** Converts a string to a resize mode. */
247  resize_mode get_resize_mode(const std::string& resize_mode);
248 
249  /** The resize mode for an image. */
251 
252  /** Mirror the image over the vertical axis. */
254 
255  // TODO: use a typed_formula?
257 
258  /**
259  * Prevents duplicate error logs when an image can't be loaded.
260  */
262 
263  static void dimension_validation(unsigned value, const std::string& name, const std::string& key);
264 };
265 
267 {
268 public:
269  /**
270  * Constructor.
271  *
272  * @param cfg The config object to define the text.
273  * @param functions WFL functions to execute.
274  */
275  explicit text_shape(const config& cfg, wfl::action_function_symbol_table& functions);
276 
277  text_shape(
278  const point& origin,
279  font::family_class family,
280  const unsigned size,
282  const std::string& align,
283  const unsigned wrap_width);
284 
285  t_string get_text() const;
286  void set_text(const t_string& text);
287  std::pair<std::size_t, std::size_t> add_text(const t_string& text);
288  void add_attribute(
289  const std::string& attr_name,
290  const std::string& extra_data = "",
291  std::size_t start = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING,
292  std::size_t end = PANGO_ATTR_INDEX_TO_TEXT_END);
294  void add_attributes_from(text_shape& tshape2, const unsigned attr_start);
295  void set_wrap_width(const unsigned wrap_width);
296 
297  void draw(wfl::map_formula_callable& variables) override;
298 
299 private:
300  /** The text font family. */
302 
303  /** The font size of the text. */
305 
306  /** The style of the text. */
308 
309  /** The alignment of the text. */
311 
312  /** The color of the text. */
314 
315  /** The text to draw. */
317 
318  /** Whether to parse text_ as WFL formula */
320 
321  /** The text markup switch of the text. */
323 
324  /** The link aware switch of the text. */
326 
327  /** The link color of the text. */
329 
330  /** The maximum width for the text. */
332 
333  /** The number of characters per line. */
335 
336  /** The maximum height for the text. */
338 
339  /** Start offset for highlight */
341 
342  /** End offset for highlight */
344 
345  /** The color to be used for highlighting */
347 
348  /** Spacing between lines */
350 
351  /** Whether to apply a text outline. */
353 
354  /** Any extra WFL actions to execute. */
356 
357  /** Any custom Pango text attributes. */
359 };
360 
361 }
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:157
Helper class to encapsulate the management of a PangoAttrList.
Definition: attributes.hpp:28
Abstract base class for all other shapes.
Definition: canvas.hpp:54
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_
Image path from which the image will be loaded.
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:395
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:284
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:293
image_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:251
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:52
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:67
rect_bounded_shape(const point &origin, const std::string &w_f, const std::string &h_f)
typed_formula< int > x_
The x coordinate of the rectangle.
rect_bounded_shape(const rect &bounds)
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:89
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:118
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.
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.
t_string get_text() const
Definition: canvas.cpp:544
typed_formula< bool > link_aware_
The link aware switch of the text.
typed_formula< PangoAlignment > text_alignment_
The alignment of the text.
void set_text(const t_string &text)
Definition: canvas.cpp:548
void add_attributes_from(text_shape &tshape2, const unsigned attr_start)
Definition: canvas.cpp:610
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:615
text_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:485
std::pair< std::size_t, std::size_t > add_text(const t_string &text)
Definition: canvas.cpp:552
config::attribute_value text_
The text to draw.
typed_formula< int > highlight_end_
End offset for highlight.
void add_attributes(font::attribute_list &&other_attrs)
void set_wrap_width(const unsigned wrap_width)
Definition: canvas.cpp:561
float line_spacing_
Spacing between lines.
void add_attribute(const std::string &attr_name, const std::string &extra_data="", std::size_t start=PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING, std::size_t end=PANGO_ATTR_INDEX_TO_TEXT_END)
Definition: canvas.cpp:565
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
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Generic file dialog.
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:81
int w
Definition: pathfind.cpp:188
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:51
static constexpr color_t null_color()
Definition of a 'null' color - fully transparent black.
Definition: color.hpp:245
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
#define h