The Battle for Wesnoth  1.19.10+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  explicit shape(const config& cfg) : immutable_(cfg["immutable"].to_bool(false))
57  {
58  }
59 
60  virtual ~shape()
61  {
62  }
63 
64  /**
65  * Draws the canvas.
66  *
67  * @param variables The canvas can have formulas in it's
68  * definition, this parameter contains the values
69  * for these formulas.
70  */
71  virtual void draw(wfl::map_formula_callable& variables) = 0;
72 
73  bool immutable() const
74  {
75  return immutable_;
76  }
77 
78  private:
79  /**
80  * If this is true, this shape will not be removed from the canvas even if
81  * the canvas's content is reset.
82  */
83  bool immutable_;
84  };
85 
86  explicit canvas(const config& cfg);
87 
88  canvas(const canvas&) = delete;
89  canvas& operator=(const canvas&) = delete;
90 
91  canvas(canvas&& c) noexcept = default;
92  canvas& operator=(canvas&&) noexcept = default;
93 
94  /**
95  * Update the background blur texture, if relevant and necessary.
96  *
97  * This should be called sometime before draw().
98  * Updating it later is less important as it's quite expensive.
99  *
100  * @param screen_region The area of the screen underneath the canvas.
101  * @param force Regenerate the blur even if we already did it.
102  *
103  * @returns True if draw should continue, false otherwise.
104  */
105  bool update_blur(const rect& screen_region, const bool force = false);
106 
107  /** Clear the cached blur texture, forcing it to regenerate. */
108  void queue_reblur();
109 
110  /**
111  * Draw the canvas' shapes onto the screen.
112  *
113  * It makes sure the image on the canvas is up to date. Also executes the
114  * pre-blitting functions.
115  */
116  void draw();
117 
118  /**
119  * Sets the config.
120  *
121  * @param cfg The config object with the data to draw.
122  * @param force Whether to clear all shapes or not.
123  */
124  void set_shapes(const config& cfg, const bool force = false)
125  {
126  clear_shapes(force);
127  parse_cfg(cfg);
128  }
129 
130  /**
131  * Appends data to the config.
132  *
133  * @param cfg The config object with the data to draw.
134  */
135  void append_shapes(const config& cfg)
136  {
137  parse_cfg(cfg);
138  }
139 
140  /** Update WFL size variables. */
141  void update_size_variables();
142 
143  /***** ***** ***** setters / getters for members ***** ****** *****/
144 
145  unsigned get_width() const
146  {
147  return w_;
148  }
149 
150  unsigned get_height() const
151  {
152  return h_;
153  }
154 
155  void set_size(const point& size);
156 
157  void set_variable(const std::string& key, wfl::variant&& value)
158  {
159  variables_.add(key, std::move(value));
160  }
161 
162  wfl::variant get_variable(const std::string& key)
163  {
164  return variables_.query_value(key);
165  }
166 
167 private:
168  /** Vector with the shapes to draw. */
169  std::vector<std::unique_ptr<shape>> shapes_;
170 
171  /**
172  * The depth of the blur to use in the pre committing.
173  *
174  * @note at the moment there's one pre commit function, namely the
175  * blurring so use a variable here, might get more functions in the
176  * future. When that happens need to evaluate whether variables are the
177  * best thing to use.
178  */
179  unsigned blur_depth_;
180 
181  /** Blurred background texture. */
183 
184  /** The region of the screen we have blurred (if any). */
186 
187  /** Whether we have deferred rendering so we can capture for blur. */
188  bool deferred_;
189 
190  /** The full width of the canvas. */
191  unsigned w_;
192 
193  /** The full height of the canvas. */
194  unsigned h_;
195 
196  /** The variables of the canvas. */
198 
199  /** Action function definitions for the canvas. */
201 
202  /**
203  * Parses a config object.
204  *
205  * The config object is parsed and serialized by this function after which
206  * the config object is no longer required and thus not stored in the
207  * object.
208  *
209  * @param cfg The config object with the data to draw
210  */
211  void parse_cfg(const config& cfg);
212 
213  void clear_shapes(const bool force);
214 };
215 
216 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
Abstract base class for all other shapes.
Definition: canvas.hpp:54
shape(const config &cfg)
Definition: canvas.hpp:56
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:83
bool immutable() const
Definition: canvas.hpp:73
virtual void draw(wfl::map_formula_callable &variables)=0
Draws the canvas.
virtual ~shape()
Definition: canvas.hpp:60
A simple canvas which can be drawn upon.
Definition: canvas.hpp:45
texture blur_texture_
Blurred background texture.
Definition: canvas.hpp:182
canvas(canvas &&c) noexcept=default
bool deferred_
Whether we have deferred rendering so we can capture for blur.
Definition: canvas.hpp:188
canvas & operator=(canvas &&) noexcept=default
wfl::action_function_symbol_table functions_
Action function definitions for the canvas.
Definition: canvas.hpp:200
void clear_shapes(const bool force)
Definition: canvas.cpp:718
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:157
unsigned blur_depth_
The depth of the blur to use in the pre committing.
Definition: canvas.hpp:179
wfl::map_formula_callable variables_
The variables of the canvas.
Definition: canvas.hpp:197
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:576
rect blur_region_
The region of the screen we have blurred (if any).
Definition: canvas.hpp:185
void parse_cfg(const config &cfg)
Parses a config object.
Definition: canvas.cpp:664
void queue_reblur()
Clear the cached blur texture, forcing it to regenerate.
Definition: canvas.cpp:631
canvas(const config &cfg)
Definition: canvas.cpp:561
void append_shapes(const config &cfg)
Appends data to the config.
Definition: canvas.hpp:135
unsigned get_height() const
Definition: canvas.hpp:150
canvas & operator=(const canvas &)=delete
std::vector< std::unique_ptr< shape > > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:169
wfl::variant get_variable(const std::string &key)
Definition: canvas.hpp:162
void set_shapes(const config &cfg, const bool force=false)
Sets the config.
Definition: canvas.hpp:124
unsigned w_
The full width of the canvas.
Definition: canvas.hpp:191
void update_size_variables()
Update WFL size variables.
Definition: canvas.cpp:704
unsigned h_
The full height of the canvas.
Definition: canvas.hpp:194
unsigned get_width() const
Definition: canvas.hpp:145
void set_size(const point &size)
Definition: canvas.cpp:711
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).
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:85
Definition: contexts.hpp:43
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