The Battle for Wesnoth  1.19.9+dev
canvas.cpp
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 /**
17  * @file
18  * Implementation of canvas.hpp.
19  */
20 
21 #define GETTEXT_DOMAIN "wesnoth-lib"
22 
23 #include "gui/core/canvas.hpp"
25 
26 #include "draw.hpp"
27 #include "draw_manager.hpp"
28 #include "font/attributes.hpp"
29 #include "font/text.hpp"
30 #include "formatter.hpp"
31 #include "gettext.hpp"
33 #include "gui/core/log.hpp"
34 #include "gui/widgets/helper.hpp"
35 #include "font/font_config.hpp"
36 #include "font/standard_colors.hpp"
37 #include "picture.hpp"
38 #include "sdl/point.hpp"
39 #include "sdl/rect.hpp"
40 #include "sdl/texture.hpp"
41 #include "sdl/utils.hpp" // blur_surface
42 #include "video.hpp" // read_pixels_low_res, only used for blurring
43 #include "wml_exception.hpp"
44 
45 #include <iostream>
46 
47 namespace gui2
48 {
49 
50 /***** ***** ***** ***** ***** LINE ***** ***** ***** ***** *****/
51 
53  : shape(cfg)
54  , x1_(cfg["x1"])
55  , y1_(cfg["y1"])
56  , x2_(cfg["x2"])
57  , y2_(cfg["y2"])
58  , color_(cfg["color"])
59  , thickness_(cfg["thickness"].to_unsigned())
60 {
61  const std::string& debug = (cfg["debug"]);
62  if(!debug.empty()) {
63  DBG_GUI_P << "Line: found debug message '" << debug << "'.";
64  }
65 }
66 
68 {
69  /**
70  * @todo formulas are now recalculated every draw cycle which is a bit silly
71  * unless there has been a resize. So to optimize we should use an extra
72  * flag or do the calculation in a separate routine.
73  */
74 
75  const unsigned x1 = x1_(variables);
76  const unsigned y1 = y1_(variables);
77  const unsigned x2 = x2_(variables);
78  const unsigned y2 = y2_(variables);
79 
80  DBG_GUI_D << "Line: draw from " << x1 << ',' << y1 << " to " << x2 << ',' << y2 << ".";
81 
82  // @todo FIXME respect the thickness.
83 
84  draw::line(x1, y1, x2, y2, color_(variables));
85 }
86 
87 /***** ***** ***** ***** ***** Rectangle ***** ***** ***** ***** *****/
88 
90  : rect_bounded_shape(cfg)
91  , border_thickness_(cfg["border_thickness"].to_int())
92  , border_color_(cfg["border_color"], color_t::null_color())
93  , fill_color_(cfg["fill_color"], color_t::null_color())
94 {
95  // Check if a raw color string evaluates to a null color.
96  if(!border_color_.has_formula() && border_color_().null()) {
98  }
99 
100  const std::string& debug = (cfg["debug"]);
101  if(!debug.empty()) {
102  DBG_GUI_P << "Rectangle: found debug message '" << debug << "'.";
103  }
104 }
105 
107 {
108  const rect area {
109  x_(variables),
110  y_(variables),
111  w_(variables),
112  h_(variables)
113  };
114 
115  const color_t fill_color = fill_color_(variables);
116 
117  // Fill the background, if applicable
118  if(!fill_color.null()) {
119  DBG_GUI_D << "fill " << fill_color;
120  draw::set_color(fill_color);
121  draw::fill(area.padded_by(-border_thickness_));
122  }
123 
124  const color_t border_color = border_color_(variables);
125 
126  // Draw the border
127  draw::set_color(border_color);
128  DBG_GUI_D << "border thickness " << border_thickness_ << ", colour " << border_color;
129  for(int i = 0; i < border_thickness_; ++i) {
130  draw::rect(area.padded_by(-i));
131  }
132 }
133 
134 /***** ***** ***** ***** ***** Rounded Rectangle ***** ***** ***** ***** *****/
135 
137  : rect_bounded_shape(cfg)
138  , r_(cfg["corner_radius"])
139  , border_thickness_(cfg["border_thickness"].to_int())
140  , border_color_(cfg["border_color"], color_t::null_color())
141  , fill_color_(cfg["fill_color"], color_t::null_color())
142 {
143  // Check if a raw color string evaluates to a null color.
144  if(!border_color_.has_formula() && border_color_().null()) {
145  border_thickness_ = 0;
146  }
147 
148  const std::string& debug = (cfg["debug"]);
149  if(!debug.empty()) {
150  DBG_GUI_P << "Rounded Rectangle: found debug message '" << debug << "'.";
151  }
152 }
153 
155 {
156  const int x = x_(variables);
157  const int y = y_(variables);
158  const int w = w_(variables);
159  const int h = h_(variables);
160  const int r = r_(variables);
161 
162  DBG_GUI_D << "Rounded Rectangle: draw from " << x << ',' << y << " width " << w << " height " << h << ".";
163 
164  const color_t fill_color = fill_color_(variables);
165 
166  // Fill the background, if applicable
167  if(!fill_color.null() && w && h) {
168  draw::set_color(fill_color);
169 
170  draw::fill(rect{x + r, y + border_thickness_, w - r * 2, r - border_thickness_ + 1});
171  draw::fill(rect{x + border_thickness_, y + r + 1, w - border_thickness_ * 2, h - r * 2});
172  draw::fill(rect{x + r, y - r + h + 1, w - r * 2, r - border_thickness_});
173 
174  draw::disc(x + r, y + r, r, 0xc0);
175  draw::disc(x + w - r, y + r, r, 0x03);
176  draw::disc(x + r, y + h - r, r, 0x30);
177  draw::disc(x + w - r, y + h - r, r, 0x0c);
178  }
179 
180  const color_t border_color = border_color_(variables);
181 
182  // Draw the border
183  draw::set_color(border_color);
184 
185  for(int i = 0; i < border_thickness_; ++i) {
186  draw::line(x + r, y + i, x + w - r, y + i);
187  draw::line(x + r, y + h - i, x + w - r, y + h - i);
188 
189  draw::line(x + i, y + r, x + i, y + h - r);
190  draw::line(x + w - i, y + r, x + w - i, y + h - r);
191 
192  draw::circle(x + r, y + r, r - i, 0xc0);
193  draw::circle(x + w - r, y + r, r - i, 0x03);
194  draw::circle(x + r, y + h - r, r - i, 0x30);
195  draw::circle(x + w - r, y + h - r, r - i, 0x0c);
196  }
197 }
198 
199 /***** ***** ***** ***** ***** CIRCLE ***** ***** ***** ***** *****/
200 
202  : shape(cfg)
203  , x_(cfg["x"])
204  , y_(cfg["y"])
205  , radius_(cfg["radius"])
206  , border_color_(cfg["border_color"])
207  , fill_color_(cfg["fill_color"])
208  , border_thickness_(cfg["border_thickness"].to_int(1))
209 {
210  const std::string& debug = (cfg["debug"]);
211  if(!debug.empty()) {
212  DBG_GUI_P << "Circle: found debug message '" << debug << "'.";
213  }
214 }
215 
217 {
218  /**
219  * @todo formulas are now recalculated every draw cycle which is a bit
220  * silly unless there has been a resize. So to optimize we should use an
221  * extra flag or do the calculation in a separate routine.
222  */
223 
224  const int x = x_(variables);
225  const int y = y_(variables);
226  const unsigned radius = radius_(variables);
227 
228  DBG_GUI_D << "Circle: drawn at " << x << ',' << y << " radius " << radius << ".";
229 
230  const color_t fill_color = fill_color_(variables);
231  if(!fill_color.null() && radius) {
232  draw::disc(x, y, radius, fill_color);
233  }
234 
235  const color_t border_color = border_color_(variables);
236  for(unsigned int i = 0; i < border_thickness_; i++) {
237  draw::circle(x, y, radius - i, border_color);
238  }
239 }
240 
241 /***** ***** ***** ***** ***** IMAGE ***** ***** ***** ***** *****/
242 
244  : shape(cfg)
245  , x_(cfg["x"])
246  , y_(cfg["y"])
247  , w_(cfg["w"])
248  , h_(cfg["h"])
249  , image_name_(cfg["name"])
250  , resize_mode_(get_resize_mode(cfg["resize_mode"]))
251  , mirror_(cfg.get_old_attribute("mirror", "vertical_mirror", "image"))
252  , actions_formula_(cfg["actions"], &functions)
253 {
254  const std::string& debug = (cfg["debug"]);
255  if(!debug.empty()) {
256  DBG_GUI_P << "Image: found debug message '" << debug << "'.";
257  }
258 }
259 
260 void image_shape::dimension_validation(unsigned value, const std::string& name, const std::string& key)
261 {
262  const int as_int = static_cast<int>(value);
263 
264  VALIDATE_WITH_DEV_MESSAGE(as_int >= 0, _("Image doesn’t fit on canvas."),
265  formatter() << "Image '" << name << "', " << key << " = " << as_int << "."
266  );
267 }
268 
270 {
271  DBG_GUI_D << "Image: draw.";
272 
273  /**
274  * @todo formulas are now recalculated every draw cycle which is a bit
275  * silly unless there has been a resize. So to optimize we should use an
276  * extra flag or do the calculation in a separate routine.
277  */
278  const std::string& name = image_name_(variables);
279 
280  if(name.empty()) {
281  DBG_GUI_D << "Image: name is empty or contains invalid formula, will not be drawn.";
282  return;
283  }
284 
285  // Texture filtering mode must be set on texture creation,
286  // so check whether we need smooth scaling or not here.
290  {
292  }
294 
295  if(!tex) {
296  ERR_GUI_D << "Image: '" << name << "' not found and won't be drawn.";
297  return;
298  }
299 
300  wfl::map_formula_callable local_variables(variables);
301  local_variables.add("image_original_width", wfl::variant(tex.w()));
302  local_variables.add("image_original_height", wfl::variant(tex.h()));
303 
304  int w = w_(local_variables);
305  dimension_validation(w, name, "w");
306 
307  int h = h_(local_variables);
308  dimension_validation(h, name, "h");
309 
310  local_variables.add("image_width", wfl::variant(w ? w : tex.w()));
311  local_variables.add("image_height", wfl::variant(h ? h : tex.h()));
312 
313  const int x = x_(local_variables);
314  const int y = y_(local_variables);
315 
316  // used in gui/dialogs/story_viewer.cpp
317  local_variables.add("clip_x", wfl::variant(x));
318  local_variables.add("clip_y", wfl::variant(y));
319 
320  if (variables.has_key("fake_draw") && variables.query_value("fake_draw").as_bool()) {
321  variables.add("image_original_width", wfl::variant(tex.w()));
322  variables.add("image_original_height", wfl::variant(tex.h()));
323  variables.add("image_width", wfl::variant(w ? w : tex.w()));
324  variables.add("image_height", wfl::variant(h ? h : tex.h()));
325  return;
326  }
327 
328  // Execute the provided actions for this context.
329  wfl::variant(variables.fake_ptr()).execute_variant(actions_formula_.evaluate(local_variables));
330 
331  // If w or h is 0, assume it means the whole image.
332  if (!w) { w = tex.w(); }
333  if (!h) { h = tex.h(); }
334 
335  const SDL_Rect dst_rect { x, y, w, h };
336 
337  // What to do with the image depends on whether we need to tile it or not.
338  switch(resize_mode_) {
339  case resize_mode::tile:
340  draw::tiled(tex, dst_rect, false, mirror_(variables));
341  break;
343  draw::tiled(tex, dst_rect, true, mirror_(variables));
344  break;
346  draw::tiled_highres(tex, dst_rect, false, mirror_(variables));
347  break;
349  // Stretching is identical to scaling in terms of handling.
350  // Is this intended? That's what previous code was doing.
351  case resize_mode::scale:
352  // Filtering mode is set on texture creation above.
353  // Handling is otherwise identical to sharp scaling.
355  if(mirror_(variables)) {
356  draw::flipped(tex, dst_rect);
357  } else {
358  draw::blit(tex, dst_rect);
359  }
360  break;
361  default:
362  ERR_GUI_D << "Image: unrecognized resize mode.";
363  break;
364  }
365 }
366 
368 {
369  if(resize_mode == "tile") {
370  return resize_mode::tile;
371  } else if(resize_mode == "tile_center") {
373  } else if(resize_mode == "tile_highres") {
375  } else if(resize_mode == "stretch") {
376  return resize_mode::stretch;
377  } else if(resize_mode == "scale_sharp") {
379  } else if(resize_mode == "scale") {
380  return resize_mode::scale;
381  } else {
382  if(!resize_mode.empty()) {
383  ERR_GUI_E << "Invalid resize mode '" << resize_mode << "' falling back to 'scale'.";
384  }
385 
386  // Linear scaling just looks horrible as a default, especially on HDPI screens, and even
387  // for some non-pixel art (the logo, for example). Nearest-neighbor isn't perfect for those
388  // usecases, but it's definitely better, in my opinion.
389  //
390  // -- vultraz, 2022-08-20
392  }
393 }
394 
395 /***** ***** ***** ***** ***** TEXT ***** ***** ***** ***** *****/
396 
398  : rect_bounded_shape(cfg)
399  , cfg_(cfg)
400  , font_family_(font::str_to_family_class(cfg["font_family"]))
401  , font_size_(cfg["font_size"], font::SIZE_NORMAL)
402  , font_style_(decode_font_style(cfg["font_style"]))
403  , text_alignment_(cfg["text_alignment"])
404  , color_(cfg["color"])
405  , text_(cfg["text"])
406  , text_markup_(cfg["text_markup"], false)
407  , link_aware_(cfg["text_link_aware"], false)
408  , link_color_(cfg["text_link_color"], color_t::from_hex_string("ffff00"))
409  , maximum_width_(cfg["maximum_width"], -1)
410  , characters_per_line_(cfg["text_characters_per_line"].to_unsigned())
411  , maximum_height_(cfg["maximum_height"], -1)
412  , highlight_start_(cfg["highlight_start"])
413  , highlight_end_(cfg["highlight_end"])
414  , highlight_color_(cfg["highlight_color"], color_t::from_hex_string("215380"))
415  , outline_(cfg["outline"], false)
416  , actions_formula_(cfg["actions"], &functions)
417 {
418  const std::string& debug = (cfg["debug"]);
419  if(!debug.empty()) {
420  DBG_GUI_P << "Text: found debug message '" << debug << "'.";
421  }
422 }
423 
425 {
426  assert(variables.has_key("text"));
427 
428  // We first need to determine the size of the text which need the rendered
429  // text. So resolve and render the text first and then start to resolve
430  // the other formulas.
431  const t_string& text = cfg_["parse_text_as_formula"].to_bool(true) ? text_(variables) : cfg_["text"].t_str();
432 
433  if(text.empty()) {
434  DBG_GUI_D << "Text: no text to render, leave.";
435  return;
436  }
437 
438  font::attribute_list text_attributes;
439 
440  //
441  // Highlight
442  //
443  const int highlight_start = highlight_start_(variables);
444  const int highlight_end = highlight_end_(variables);
445 
446  if(highlight_start != highlight_end) {
447  add_attribute_bg_color(text_attributes, highlight_start, highlight_end, highlight_color_(variables));
448  }
449 
450  //
451  // Attribute subtags
452  //
453  for (const auto& attr : cfg_.child_range("attribute")) {
454  const std::string& name = attr["name"];
455 
456  if (name.empty()) {
457  continue;
458  }
459 
460  const unsigned start = attr["start"].to_int(0);
461  const unsigned end = attr["end"].to_int(text.size());
462 
463  if (name == "color" || name == "fgcolor" || name == "foreground") {
464  add_attribute_fg_color(text_attributes, start, end, attr["value"].empty() ? font::NORMAL_COLOR : font::string_to_color(attr["value"]));
465  } else if (name == "bgcolor" || name == "background") {
466  add_attribute_bg_color(text_attributes, start, end, attr["value"].empty() ? font::GOOD_COLOR : font::string_to_color(attr["value"]));
467  } else if (name == "font_size" || name == "size") {
468  add_attribute_size(text_attributes, start, end, attr["value"].to_int(font::SIZE_NORMAL));
469  } else if (name == "font_family" || name == "face") {
470  add_attribute_font_family(text_attributes, start, end, font::str_to_family_class(attr["value"]));
471  } else if (name == "weight") {
472  add_attribute_weight(text_attributes, start, end, decode_text_weight(attr["value"]));
473  } else if (name == "style") {
474  add_attribute_style(text_attributes, start, end, decode_text_style(attr["value"]));
475  } else if (name == "bold" || name == "b") {
476  add_attribute_weight(text_attributes, start, end, PANGO_WEIGHT_BOLD);
477  } else if (name == "italic" || name == "i") {
478  add_attribute_style(text_attributes, start, end, PANGO_STYLE_ITALIC);
479  } else if (name == "underline" || name == "u") {
480  add_attribute_underline(text_attributes, start, end, PANGO_UNDERLINE_SINGLE);
481  } else {
482  // Unsupported formatting or normal text
483  add_attribute_weight(text_attributes, start, end, PANGO_WEIGHT_NORMAL);
484  add_attribute_style(text_attributes, start, end, PANGO_STYLE_NORMAL);
485  }
486  }
487 
488  font::pango_text& text_renderer = font::get_text_renderer();
489  text_renderer.clear_attributes();
490 
491  text_renderer
492  .set_link_aware(link_aware_(variables))
493  .set_link_color(link_color_(variables))
494  .set_text(text, text_markup_(variables));
495 
496  text_renderer.set_family_class(font_family_)
497  .set_font_size(font_size_(variables))
499  .set_alignment(text_alignment_(variables))
500  .set_foreground_color(color_(variables))
501  .set_maximum_width(maximum_width_(variables))
502  .set_maximum_height(maximum_height_(variables), true)
503  .set_ellipse_mode(variables.has_key("text_wrap_mode")
504  ? static_cast<PangoEllipsizeMode>(variables.query_value("text_wrap_mode").as_int())
505  : PANGO_ELLIPSIZE_END)
507  .set_add_outline(outline_(variables));
508 
509  // Do this last so it can merge with attributes from markup
510  text_renderer.apply_attributes(text_attributes);
511 
512  wfl::map_formula_callable local_variables(variables);
513  const auto [tw, th] = text_renderer.get_size();
514 
515  // Translate text width and height back to draw-space, rounding up.
516  local_variables.add("text_width", wfl::variant(tw));
517  local_variables.add("text_height", wfl::variant(th));
518 
519  if (variables.has_key("fake_draw") && variables.query_value("fake_draw").as_bool()) {
520  variables.add("text_width", wfl::variant(tw));
521  variables.add("text_height", wfl::variant(th));
522  return;
523  }
524 
525  const int x = x_(local_variables);
526  const int y = y_(local_variables);
527  const int w = w_(local_variables);
528  const int h = h_(local_variables);
529  rect dst_rect{x, y, w, h};
530 
531  // Execute the provided actions for this context.
532  wfl::variant(variables.fake_ptr()).execute_variant(actions_formula_.evaluate(local_variables));
533 
534  texture tex = text_renderer.render_and_get_texture();
535  if(!tex) {
536  DBG_GUI_D << "Text: Rendering '" << text << "' resulted in an empty canvas, leave.";
537  return;
538  }
539 
540  dst_rect.w = std::min(dst_rect.w, tex.w());
541  dst_rect.h = std::min(dst_rect.h, tex.h());
542 
543  draw::blit(tex, dst_rect);
544 }
545 
546 /***** ***** ***** ***** ***** CANVAS ***** ***** ***** ***** *****/
547 
549  : shapes_()
550  , blur_depth_(0)
551  , blur_region_(sdl::empty_rect)
552  , deferred_(false)
553  , w_(0)
554  , h_(0)
555  , variables_()
556  , functions_()
557 {
558 }
559 
561  : shapes_(std::move(c.shapes_))
562  , blur_depth_(c.blur_depth_)
563  , blur_region_(c.blur_region_)
564  , deferred_(c.deferred_)
565  , w_(c.w_)
566  , h_(c.h_)
567  , variables_(c.variables_)
568  , functions_(c.functions_)
569 {
570 }
571 
572 // It would be better if the blur effect was managed at a higher level.
573 // But for now this works and should be both general and robust.
574 bool canvas::update_blur(const rect& screen_region, bool force)
575 {
576  if(!blur_depth_) {
577  // No blurring needed.
578  return true;
579  }
580 
581  if(screen_region != blur_region_) {
582  DBG_GUI_D << "blur region changed from " << blur_region_
583  << " to " << screen_region;
584  // something has changed. regenerate the texture.
586  blur_region_ = screen_region;
587  }
588 
589  if(blur_texture_ && !force) {
590  // We already made the blur. It's expensive, so don't do it again.
591  return true;
592  }
593 
594  // To blur what is underneath us, it must already be rendered somewhere.
595  // This is okay for sub-elements of an opaque window (panels on the main
596  // title screen for example) as the window will already have rendered
597  // its background to the render buffer before we get here.
598  // If however we are blurring elements behind the window, such as if
599  // the window itself is translucent (objectives popup), or it is
600  // transparent with a translucent element (character dialogue),
601  // then we need to render what will be behind it before capturing that
602  // and rendering a blur.
603  // We could use the previous render frame, but there could well have been
604  // another element there last frame such as a popup window which we
605  // don't want to be part of the blur.
606  // The stable solution is to render in multiple passes,
607  // so that is what we shall do.
608 
609  // For the first pass, this element and its children are not rendered.
610  if(!deferred_) {
611  DBG_GUI_D << "Deferring blur at " << screen_region;
612  deferred_ = true;
614  return false;
615  }
616 
617  // For the second pass we read the result of the first pass at
618  // this widget's location, and blur it.
619  DBG_GUI_D << "Blurring " << screen_region << " depth " << blur_depth_;
620  rect read_region = screen_region;
621  auto setter = draw::set_render_target({});
622  surface s = video::read_pixels_low_res(&read_region);
623  blur_surface(s, {0, 0, s->w, s->h}, blur_depth_);
625  deferred_ = false;
626  return true;
627 }
628 
630 {
632 }
633 
635 {
636  // This early-return has to come before the `validate(rect.w <= w_)` check, as during the boost_unit_tests execution
637  // the debug_clock widget will have no shapes, 0x0 size, yet be given a larger rect to draw.
638  if(shapes_.empty()) {
639  DBG_GUI_D << "Canvas: empty (no shapes to draw).";
640  return;
641  }
642 
643  if(deferred_) {
644  // We will draw next frame.
645  return;
646  }
647 
648  // Draw blurred background.
649  // TODO: hwaccel - this should be able to be removed at some point with shaders
650  if(blur_depth_ && blur_texture_) {
651  DBG_GUI_D << "blitting blur size " << blur_texture_.draw_size();
653  }
654 
655  // Draw items
656  for(auto& shape : shapes_) {
657  const lg::scope_logger inner_scope_logging_object__{log_gui_draw, "Canvas: draw shape."};
659  }
660 }
661 
662 void canvas::parse_cfg(const config& cfg)
663 {
664  log_scope2(log_gui_parse, "Canvas: parsing config.");
665 
666  for(const auto [type, data] : cfg.all_children_view())
667  {
668  DBG_GUI_P << "Canvas: found shape of the type " << type << ".";
669 
670  if(type == "line") {
671  shapes_.emplace_back(std::make_unique<line_shape>(data));
672  } else if(type == "rectangle") {
673  shapes_.emplace_back(std::make_unique<rectangle_shape>(data));
674  } else if(type == "round_rectangle") {
675  shapes_.emplace_back(std::make_unique<round_rectangle_shape>(data));
676  } else if(type == "circle") {
677  shapes_.emplace_back(std::make_unique<circle_shape>(data));
678  } else if(type == "image") {
679  shapes_.emplace_back(std::make_unique<image_shape>(data, functions_));
680  } else if(type == "text") {
681  shapes_.emplace_back(std::make_unique<text_shape>(data, functions_));
682  } else if(type == "pre_commit") {
683 
684  /* note this should get split if more preprocessing is used. */
685  for(const auto [func_key, func_cfg] : data.all_children_view())
686  {
687  if(func_key == "blur") {
688  blur_depth_ = func_cfg["depth"].to_unsigned();
689  } else {
690  ERR_GUI_P << "Canvas: found a pre commit function"
691  << " of an invalid type " << type << ".";
692  }
693  }
694 
695  } else {
696  ERR_GUI_P << "Canvas: found a shape of an invalid type " << type
697  << ".";
698  }
699  }
700 }
701 
703 {
705  variables_.add("width", wfl::variant(w_));
706  variables_.add("height", wfl::variant(h_));
707 }
708 
710 {
711  w_ = size.x;
712  h_ = size.y;
714 }
715 
716 void canvas::clear_shapes(const bool force)
717 {
718  if(force) {
719  shapes_.clear();
720  } else {
721  utils::erase_if(shapes_, [](const std::unique_ptr<shape>& s) { return !s->immutable(); });
722  }
723 }
724 
725 /***** ***** ***** ***** ***** SHAPE ***** ***** ***** ***** *****/
726 
727 } // namespace gui2
std::size_t w_
#define debug(x)
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
auto all_children_view() const
In-order iteration over all children.
Definition: config.hpp:796
child_itors child_range(config_key_type key)
Definition: config.cpp:268
Helper class to encapsulate the management of a PangoAttrList.
Definition: attributes.hpp:27
Text class.
Definition: text.hpp:78
pango_text & set_font_style(const FONT_STYLE font_style)
Definition: text.cpp:374
void clear_attributes()
Definition: text.cpp:305
point get_size()
Returns the size of the text, in drawing coordinates.
Definition: text.cpp:123
pango_text & set_characters_per_line(const unsigned characters_per_line)
Definition: text.cpp:409
pango_text & set_foreground_color(const color_t &color)
Definition: text.cpp:384
pango_text & set_family_class(font::family_class fclass)
Definition: text.cpp:352
void apply_attributes(const font::attribute_list &attrs)
Definition: text.cpp:310
pango_text & set_add_outline(bool do_add)
Definition: text.cpp:507
pango_text & set_ellipse_mode(const PangoEllipsizeMode ellipse_mode)
Definition: text.cpp:445
pango_text & set_alignment(const PangoAlignment alignment)
Definition: text.cpp:465
pango_text & set_font_size(unsigned font_size)
Definition: text.cpp:362
pango_text & set_link_aware(bool b)
Definition: text.cpp:488
bool set_text(const std::string &text, const bool markedup)
Sets the text to render.
Definition: text.cpp:319
pango_text & set_maximum_height(int height, bool multiline)
Definition: text.cpp:420
pango_text & set_maximum_width(int width)
Definition: text.cpp:393
texture render_and_get_texture()
Returns the cached texture, or creates a new one otherwise.
Definition: text.cpp:91
pango_text & set_link_color(const color_t &color)
Definition: text.cpp:497
std::ostringstream wrapper.
Definition: formatter.hpp:40
Abstract base class for all other shapes.
Definition: canvas.hpp:54
virtual void draw(wfl::map_formula_callable &variables)=0
Draws the canvas.
A simple canvas which can be drawn upon.
Definition: canvas.hpp:45
texture blur_texture_
Blurred background texture.
Definition: canvas.hpp:179
bool deferred_
Whether we have deferred rendering so we can capture for blur.
Definition: canvas.hpp:185
wfl::action_function_symbol_table functions_
Action function definitions for the canvas.
Definition: canvas.hpp:197
void clear_shapes(const bool force)
Definition: canvas.cpp:716
unsigned blur_depth_
The depth of the blur to use in the pre committing.
Definition: canvas.hpp:176
wfl::map_formula_callable variables_
The variables of the canvas.
Definition: canvas.hpp:194
bool update_blur(const rect &screen_region, const bool force=false)
Update the background blur texture, if relevant and necessary.
Definition: canvas.cpp:574
rect blur_region_
The region of the screen we have blurred (if any).
Definition: canvas.hpp:182
void parse_cfg(const config &cfg)
Parses a config object.
Definition: canvas.cpp:662
void queue_reblur()
Clear the cached blur texture, forcing it to regenerate.
Definition: canvas.cpp:629
std::vector< std::unique_ptr< shape > > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:166
unsigned w_
The full width of the canvas.
Definition: canvas.hpp:188
void update_size_variables()
Update WFL size variables.
Definition: canvas.cpp:702
unsigned h_
The full height of the canvas.
Definition: canvas.hpp:191
void draw()
Draw the canvas' shapes onto the screen.
Definition: canvas.cpp:634
void set_size(const point &size)
Definition: canvas.cpp:709
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:201
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:216
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:367
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:260
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:269
image_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:243
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.
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
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.
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:106
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:154
typed_formula< int > r_
The radius of the corners.
round_rectangle_shape(const config &cfg)
Constructor.
Definition: canvas.cpp:136
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.
config cfg_
the source config
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:424
text_shape(const config &cfg, wfl::action_function_symbol_table &functions)
Constructor.
Definition: canvas.cpp:397
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.
typed_formula< bool > text_markup_
The text markup switch of the text.
bool has_formula() const
Determine whether the class contains a formula.
Generic locator abstracting the location of an image.
Definition: picture.hpp:59
bool empty() const
Definition: tstring.hpp:195
std::string::size_type size() const
Definition: tstring.hpp:196
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
int w() const
The draw-space width of the texture, in pixels.
Definition: texture.hpp:103
void reset()
Releases ownership of the managed texture and resets the ptr to null.
Definition: texture.cpp:184
point draw_size() const
The size of the texture in draw-space.
Definition: texture.hpp:120
int h() const
The draw-space height of the texture, in pixels.
Definition: texture.hpp:112
formula_callable_ptr fake_ptr()
Definition: callable.hpp:42
variant query_value(const std::string &key) const
Definition: callable.hpp:50
bool has_key(const std::string &key) const
Definition: callable.hpp:82
static variant evaluate(const const_formula_ptr &f, const formula_callable &variables, formula_debugger *fdb=nullptr, variant default_res=variant(0))
Definition: formula.hpp:40
map_formula_callable & add(const std::string &key, const variant &value)
Definition: callable.hpp:253
variant execute_variant(const variant &to_exec)
Definition: variant.cpp:653
int as_int() const
Definition: variant.cpp:291
bool as_bool() const
Returns a boolean state of the variant value.
Definition: variant.cpp:313
Drawing functions, for drawing things on the screen.
std::size_t i
Definition: function.cpp:1029
int w
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define ERR_GUI_P
Definition: log.hpp:69
#define DBG_GUI_P
Definition: log.hpp:66
#define ERR_GUI_D
Definition: log.hpp:32
#define ERR_GUI_E
Definition: log.hpp:38
#define DBG_GUI_D
Definition: log.hpp:29
#define log_scope2(domain, description)
Definition: log.hpp:277
void request_extra_render_pass()
Request an extra render pass.
render_target_setter set_render_target(const texture &t)
Set the given texture as the active render target.
Definition: draw.cpp:671
void circle(int x, int y, int r, const color_t &c, uint8_t octants=0xff)
Draw a circle of the given colour.
Definition: draw.cpp:215
void tiled(const texture &tex, const SDL_Rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
Definition: draw.cpp:376
void disc(int x, int y, int r, const color_t &c, uint8_t octants=0xff)
Draw a solid disc of the given colour.
Definition: draw.cpp:257
void set_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Set the drawing colour.
Definition: draw.cpp:107
void flipped(const texture &tex, const SDL_Rect &dst, bool flip_h=true, bool flip_v=false)
Draws a texture, or part of a texture, at the given location, also mirroring/flipping the texture hor...
Definition: draw.cpp:347
void fill(const SDL_Rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
Definition: draw.cpp:50
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
Definition: draw.cpp:317
void tiled_highres(const texture &tex, const SDL_Rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
Definition: draw.cpp:404
void rect(const SDL_Rect &rect)
Draw a rectangle.
Definition: draw.cpp:157
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:187
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.
Graphical text output.
pango_text & get_text_renderer()
Returns a reference to a static pango_text object.
Definition: text.cpp:958
const color_t GOOD_COLOR
void add_attribute_size(attribute_list &list, unsigned offset_start, unsigned offset_end, int size)
Add Pango font size attribute to a specific portion of text.
Definition: attributes.cpp:72
void add_attribute_bg_color(attribute_list &list, unsigned offset_start, unsigned offset_end, const color_t &color)
Mark a specific portion of text for highlighting.
Definition: attributes.cpp:143
color_t string_to_color(const std::string &cmp_str)
Return the color the string represents.
void add_attribute_weight(attribute_list &list, unsigned offset_start, unsigned offset_end, PangoWeight weight)
Add Pango font weight attribute to a specific portion of text.
Definition: attributes.cpp:88
void add_attribute_underline(attribute_list &list, unsigned offset_start, unsigned offset_end, PangoUnderline underline)
Add Pango underline attribute to a specific portion of text.
Definition: attributes.cpp:114
void add_attribute_font_family(attribute_list &list, unsigned offset_start, unsigned offset_end, font::family_class family)
Add Pango font family attribute to a specific portion of text.
Definition: attributes.cpp:158
void add_attribute_style(attribute_list &list, unsigned offset_start, unsigned offset_end, PangoStyle style)
Add Pango font style attribute to a specific portion of text, used to set italic/oblique text.
Definition: attributes.cpp:101
family_class str_to_family_class(const std::string &str)
const int SIZE_NORMAL
Definition: constants.cpp:20
const color_t NORMAL_COLOR
void add_attribute_fg_color(attribute_list &list, unsigned offset_start, unsigned offset_end, const color_t &color)
Add Pango fg color attribute to a specific portion of text.
Definition: attributes.cpp:127
Generic file dialog.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:156
lg::log_domain log_gui_draw("gui/draw")
Definition: log.hpp:28
PangoWeight decode_text_weight(const std::string &weight)
Converts a text weight string to a PangoWeight.
Definition: helper.cpp:53
font::pango_text::FONT_STYLE decode_font_style(const std::string &style)
Converts a font style string to a font style.
Definition: helper.cpp:31
lg::log_domain log_gui_parse("gui/parse")
Definition: log.hpp:65
PangoStyle decode_text_style(const std::string &style)
Converts a text style string to a PangoStyle.
Definition: helper.cpp:74
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:922
scale_quality
Definition: picture.hpp:172
constexpr const SDL_Rect empty_rect
Definition: rect.hpp:32
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
Definition: general.hpp:106
surface read_pixels_low_res(SDL_Rect *r)
The same as read_pixels, but returns a low-resolution surface suitable for use with the old drawing s...
Definition: video.cpp:632
std::string_view data
Definition: picture.cpp:178
int x2_
Definition: pump.cpp:132
int y1_
Definition: pump.cpp:132
int x1_
Definition: pump.cpp:132
int y2_
Definition: pump.cpp:132
Contains the SDL_Rect helper code.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
constexpr bool null() const
Definition: color.hpp:186
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
mock_char c
static map_location::direction s
void blur_surface(surface &surf, SDL_Rect rect, int depth)
Cross-fades a surface in place.
Definition: utils.cpp:961
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WITH_DEV_MESSAGE(cond, message, dev_message)
#define h