The Battle for Wesnoth  1.19.21+dev
canvas.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 /**
17  * @file
18  * This file contains the canvas object which is the part where the widgets
19  * draw (temporally) images on.
20  */
21 
22 #pragma once
23 
24 #include "config.hpp"
25 #include "formula/callable.hpp"
26 #include "formula/function.hpp"
27 #include "sdl/texture.hpp"
28 #include "sdl/rect.hpp"
29 
30 namespace wfl { class variant; }
31 struct point;
32 
33 namespace gui2
34 {
35 
36 /**
37  * A simple canvas which can be drawn upon.
38  *
39  * The class has a config which contains what to draw.
40  *
41  * The copy constructor does a shallow copy of the shapes to draw.
42  * a clone() will be implemented if really needed.
43  */
44 class canvas
45 {
46 public:
47  /**
48  * Abstract base class for all other shapes.
49  *
50  * The other shapes are declared and defined in canvas_private.hpp, since the
51  * implementation details are not interesting for users of the canvas.
52  */
53  class shape
54  {
55  public:
56  shape() = default;
57 
58  explicit shape(const config& cfg) : immutable_(cfg["immutable"].to_bool(false))
59  {
60  }
61 
62  virtual ~shape()
63  {
64  }
65 
66  /**
67  * Draws the canvas.
68  *
69  * @param variables The canvas can have formulas in it's
70  * definition, this parameter contains the values
71  * for these formulas.
72  */
73  virtual void draw(wfl::map_formula_callable& variables) = 0;
74 
75  bool immutable() const
76  {
77  return immutable_;
78  }
79 
80  private:
81  /**
82  * If this is true, this shape will not be removed from the canvas even if
83  * the canvas's content is reset.
84  */
85  bool immutable_ = false;
86  };
87 
88  explicit canvas(const config& cfg);
89 
90  canvas(const canvas&) = delete;
91  canvas& operator=(const canvas&) = delete;
92 
93  canvas(canvas&& c) noexcept = default;
94  canvas& operator=(canvas&&) noexcept = default;
95 
96  /**
97  * Update the background blur texture, if relevant and necessary.
98  *
99  * This should be called sometime before draw().
100  * Updating it later is less important as it's quite expensive.
101  *
102  * @param screen_region The area of the screen underneath the canvas.
103  * @param force Regenerate the blur even if we already did it.
104  *
105  * @returns True if draw should continue, false otherwise.
106  */
107  bool update_blur(const rect& screen_region, const bool force = false);
108 
109  /** Clear the cached blur texture, forcing it to regenerate. */
110  void queue_reblur();
111 
112  /**
113  * Draw the canvas' shapes onto the screen.
114  *
115  * It makes sure the image on the canvas is up to date. Also executes the
116  * pre-blitting functions.
117  */
118  void draw();
119 
120  /**
121  * Sets the config.
122  *
123  * @param cfg The config object with the data to draw.
124  * @param force Whether to clear all shapes or not.
125  */
126  void set_shapes(const config& cfg, const bool force = false)
127  {
128  clear_shapes(force);
129  parse_cfg(cfg);
130  }
131 
132  void set_shapes(std::vector<std::unique_ptr<shape>>&& shapes, const bool force = false)
133  {
134  clear_shapes(force);
135  shapes_ = std::move(shapes);
136  }
137 
138  /**
139  * Appends data to the config.
140  *
141  * @param cfg The config object with the data to draw.
142  */
143  void append_shapes(const config& cfg)
144  {
145  parse_cfg(cfg);
146  }
147 
148  /** Update WFL size variables. */
149  void update_size_variables();
150 
151  /***** ***** ***** setters / getters for members ***** ****** *****/
152 
153  unsigned get_width() const
154  {
155  return w_;
156  }
157 
158  unsigned get_height() const
159  {
160  return h_;
161  }
162 
163  void set_size(const point& size);
164 
165  void set_variable(const std::string& key, wfl::variant&& value)
166  {
167  variables_.add(key, std::move(value));
168  }
169 
170  wfl::variant get_variable(const std::string& key)
171  {
172  return variables_.query_value(key);
173  }
174 
175 private:
176  /** Vector with the shapes to draw. */
177  std::vector<std::unique_ptr<shape>> shapes_;
178 
179  /**
180  * The depth of the blur to use in the pre committing.
181  *
182  * @note at the moment there's one pre commit function, namely the
183  * blurring so use a variable here, might get more functions in the
184  * future. When that happens need to evaluate whether variables are the
185  * best thing to use.
186  */
187  unsigned blur_depth_;
188 
189  /** Blurred background texture. */
191 
192  /** The region of the screen we have blurred (if any). */
194 
195  /** Whether we have deferred rendering so we can capture for blur. */
196  bool deferred_;
197 
198  /** The full width of the canvas. */
199  unsigned w_;
200 
201  /** The full height of the canvas. */
202  unsigned h_;
203 
204  /** The variables of the canvas. */
206 
207  /** Action function definitions for the canvas. */
209 
210  /**
211  * Parses a config object.
212  *
213  * The config object is parsed and serialized by this function after which
214  * the config object is no longer required and thus not stored in the
215  * object.
216  *
217  * @param cfg The config object with the data to draw
218  */
219  void parse_cfg(const config& cfg);
220 
221  void clear_shapes(const bool force);
222 };
223 
224 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
Abstract base class for all other shapes.
Definition: canvas.hpp:54
shape(const config &cfg)
Definition: canvas.hpp:58
bool immutable_
If this is true, this shape will not be removed from the canvas even if the canvas's content is reset...
Definition: canvas.hpp:85
bool immutable() const
Definition: canvas.hpp:75
virtual void draw(wfl::map_formula_callable &variables)=0
Draws the canvas.
virtual ~shape()
Definition: canvas.hpp:62
A simple canvas which can be drawn upon.
Definition: canvas.hpp:45
texture blur_texture_
Blurred background texture.
Definition: canvas.hpp:190
canvas(canvas &&c) noexcept=default
bool deferred_
Whether we have deferred rendering so we can capture for blur.
Definition: canvas.hpp:196
canvas & operator=(canvas &&) noexcept=default
wfl::action_function_symbol_table functions_
Action function definitions for the canvas.
Definition: canvas.hpp:208
void set_shapes(std::vector< std::unique_ptr< shape >> &&shapes, const bool force=false)
Definition: canvas.hpp:132
void clear_shapes(const bool force)
Definition: canvas.cpp:861
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:165
unsigned blur_depth_
The depth of the blur to use in the pre committing.
Definition: canvas.hpp:187
wfl::map_formula_callable variables_
The variables of the canvas.
Definition: canvas.hpp:205
canvas(const canvas &)=delete
bool update_blur(const rect &screen_region, const bool force=false)
Update the background blur texture, if relevant and necessary.
Definition: canvas.cpp:718
rect blur_region_
The region of the screen we have blurred (if any).
Definition: canvas.hpp:193
void parse_cfg(const config &cfg)
Parses a config object.
Definition: canvas.cpp:807
void queue_reblur()
Clear the cached blur texture, forcing it to regenerate.
Definition: canvas.cpp:773
canvas(const config &cfg)
Definition: canvas.cpp:703
void append_shapes(const config &cfg)
Appends data to the config.
Definition: canvas.hpp:143
unsigned get_height() const
Definition: canvas.hpp:158
canvas & operator=(const canvas &)=delete
std::vector< std::unique_ptr< shape > > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:177
wfl::variant get_variable(const std::string &key)
Definition: canvas.hpp:170
void set_shapes(const config &cfg, const bool force=false)
Sets the config.
Definition: canvas.hpp:126
unsigned w_
The full width of the canvas.
Definition: canvas.hpp:199
void update_size_variables()
Update WFL size variables.
Definition: canvas.cpp:847
unsigned h_
The full height of the canvas.
Definition: canvas.hpp:202
unsigned get_width() const
Definition: canvas.hpp:153
void set_size(const point &size)
Definition: canvas.cpp:854
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
variant query_value(const std::string &key) const
Definition: callable.hpp:50
map_formula_callable & add(const std::string &key, const variant &value)
Definition: callable.hpp:253
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
Definition: draw.hpp:43
Generic file dialog.
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:81
Definition: callable.hpp:26
Contains the SDL_Rect helper code.
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
mock_char c