The Battle for Wesnoth  1.19.1+dev
canvas.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 /**
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  canvas();
87  canvas(const canvas&) = delete;
88  canvas& operator=(const canvas&) = delete;
89  canvas(canvas&& c) noexcept;
90 
91  /**
92  * Update the background blur texture, if relevant and necessary.
93  *
94  * This should be called sometime before draw().
95  * Updating it later is less important as it's quite expensive.
96  *
97  * @param screen_region The area of the screen underneath the canvas.
98  * @param force Regenerate the blur even if we already did it.
99  *
100  * @returns True if draw should continue, false otherwise.
101  */
102  bool update_blur(const rect& screen_region, const bool force = false);
103 
104  /** Clear the cached blur texture, forcing it to regenerate. */
105  void queue_reblur();
106 
107  /**
108  * Draw the canvas' shapes onto the screen.
109  *
110  * It makes sure the image on the canvas is up to date. Also executes the
111  * pre-blitting functions.
112  */
113  void draw();
114 
115  /**
116  * Sets the config.
117  *
118  * @param cfg The config object with the data to draw.
119  * @param force Whether to clear all shapes or not.
120  */
121  void set_cfg(const config& cfg, const bool force = false)
122  {
123  clear_shapes(force);
124  parse_cfg(cfg);
125  }
126 
127  /**
128  * Appends data to the config.
129  *
130  * @param cfg The config object with the data to draw.
131  */
132  void append_cfg(const config& cfg)
133  {
134  parse_cfg(cfg);
135  }
136 
137  /** Update WFL size variables. */
138  void update_size_variables();
139 
140  /***** ***** ***** setters / getters for members ***** ****** *****/
141 
142  unsigned get_width() const
143  {
144  return w_;
145  }
146 
147  unsigned get_height() const
148  {
149  return h_;
150  }
151 
152  void set_size(const point& size);
153 
154  void set_variable(const std::string& key, wfl::variant&& value)
155  {
156  variables_.add(key, std::move(value));
157  }
158 
159  wfl::variant get_variable(const std::string& key)
160  {
161  return variables_.query_value(key);
162  }
163 
164 private:
165  /** Vector with the shapes to draw. */
166  std::vector<std::unique_ptr<shape>> shapes_;
167 
168  /**
169  * The depth of the blur to use in the pre committing.
170  *
171  * @note at the moment there's one pre commit function, namely the
172  * blurring so use a variable here, might get more functions in the
173  * future. When that happens need to evaluate whether variables are the
174  * best thing to use.
175  */
176  unsigned blur_depth_;
177 
178  /** Blurred background texture. */
180 
181  /** The region of the screen we have blurred (if any). */
183 
184  /** Whether we have deferred rendering so we can capture for blur. */
185  bool deferred_;
186 
187  /** The full width of the canvas. */
188  unsigned w_;
189 
190  /** The full height of the canvas. */
191  unsigned h_;
192 
193  /** The variables of the canvas. */
195 
196  /** Action function definitions for the canvas. */
198 
199  /**
200  * Parses a config object.
201  *
202  * The config object is parsed and serialized by this function after which
203  * the config object is no longer required and thus not stored in the
204  * object.
205  *
206  * @param cfg The config object with the data to draw
207  */
208  void parse_cfg(const config& cfg);
209 
210  void clear_shapes(const bool force);
211 };
212 
213 } // namespace gui2
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
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: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:764
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:154
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
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:618
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:706
void queue_reblur()
Clear the cached blur texture, forcing it to regenerate.
Definition: canvas.cpp:673
void append_cfg(const config &cfg)
Appends data to the config.
Definition: canvas.hpp:132
unsigned get_height() const
Definition: canvas.hpp:147
canvas & operator=(const canvas &)=delete
std::vector< std::unique_ptr< shape > > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:166
wfl::variant get_variable(const std::string &key)
Definition: canvas.hpp:159
unsigned w_
The full width of the canvas.
Definition: canvas.hpp:188
void update_size_variables()
Update WFL size variables.
Definition: canvas.cpp:750
unsigned h_
The full height of the canvas.
Definition: canvas.hpp:191
void draw()
Draw the canvas' shapes onto the screen.
Definition: canvas.cpp:678
unsigned get_width() const
Definition: canvas.hpp:142
void set_size(const point &size)
Definition: canvas.cpp:757
void set_cfg(const config &cfg, const bool force=false)
Sets the config.
Definition: canvas.hpp:121
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
Generic file dialog.
std::size_t size(const std::string &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:47
mock_char c