The Battle for Wesnoth  1.17.21+dev
canvas.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2023
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 
29 namespace wfl { class variant; }
30 struct point;
31 
32 namespace gui2
33 {
34 
35 /**
36  * A simple canvas which can be drawn upon.
37  *
38  * The class has a config which contains what to draw.
39  *
40  * The copy constructor does a shallow copy of the shapes to draw.
41  * a clone() will be implemented if really needed.
42  */
43 class canvas
44 {
45 public:
46  /**
47  * Abstract base class for all other shapes.
48  *
49  * The other shapes are declared and defined in canvas_private.hpp, since the
50  * implementation details are not interesting for users of the canvas.
51  */
52  class shape
53  {
54  public:
55  explicit shape(const config& cfg) : immutable_(cfg["immutable"].to_bool(false))
56  {
57  }
58 
59  virtual ~shape()
60  {
61  }
62 
63  /**
64  * Draws the canvas.
65  *
66  * @param variables The canvas can have formulas in it's
67  * definition, this parameter contains the values
68  * for these formulas.
69  */
70  virtual void draw(wfl::map_formula_callable& variables) = 0;
71 
72  bool immutable() const
73  {
74  return immutable_;
75  }
76 
77  private:
78  /**
79  * If this is true, this shape will not be removed from the canvas even if
80  * the canvas's content is reset.
81  */
82  bool immutable_;
83  };
84 
85  canvas();
86  canvas(const canvas&) = delete;
87  canvas& operator=(const canvas&) = delete;
88  canvas(canvas&& c) noexcept;
89 
90  /**
91  * Draw the canvas' shapes onto the screen.
92  *
93  * It makes sure the image on the canvas is up to date. Also executes the
94  * pre-blitting functions.
95  */
96  void draw();
97 
98  /**
99  * Sets the config.
100  *
101  * @param cfg The config object with the data to draw.
102  * @param force Whether to clear all shapes or not.
103  */
104  void set_cfg(const config& cfg, const bool force = false)
105  {
106  clear_shapes(force);
107  parse_cfg(cfg);
108  }
109 
110  /**
111  * Appends data to the config.
112  *
113  * @param cfg The config object with the data to draw.
114  */
115  void append_cfg(const config& cfg)
116  {
117  parse_cfg(cfg);
118  }
119 
120  /** Update WFL size variables. */
121  void update_size_variables();
122 
123  /***** ***** ***** setters / getters for members ***** ****** *****/
124 
125  unsigned get_width() const
126  {
127  return w_;
128  }
129 
130  unsigned get_height() const
131  {
132  return h_;
133  }
134 
135  void set_size(const point& size);
136 
137  void set_variable(const std::string& key, wfl::variant&& value)
138  {
139  variables_.add(key, std::move(value));
140  }
141 
142 private:
143  /** Vector with the shapes to draw. */
144  std::vector<std::unique_ptr<shape>> shapes_;
145 
146  /**
147  * The depth of the blur to use in the pre committing.
148  *
149  * @note at the moment there's one pre commit function, namely the
150  * blurring so use a variable here, might get more functions in the
151  * future. When that happens need to evaluate whether variables are the
152  * best thing to use.
153  */
154  unsigned blur_depth_;
155 
156  /** Blurred background texture. */
158 
159  /** The full width of the canvas. */
160  unsigned w_;
161 
162  /** The full height of the canvas. */
163  unsigned h_;
164 
165  /** The variables of the canvas. */
167 
168  /** Action function definitions for the canvas. */
170 
171  /**
172  * Parses a config object.
173  *
174  * The config object is parsed and serialized by this function after which
175  * the config object is no longer required and thus not stored in the
176  * object.
177  *
178  * @param cfg The config object with the data to draw, see @ref GUICanvasWML
179  */
180  void parse_cfg(const config& cfg);
181 
182  void clear_shapes(const bool force);
183 };
184 
185 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Abstract base class for all other shapes.
Definition: canvas.hpp:53
shape(const config &cfg)
Definition: canvas.hpp:55
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:82
bool immutable() const
Definition: canvas.hpp:72
virtual void draw(wfl::map_formula_callable &variables)=0
Draws the canvas.
virtual ~shape()
Definition: canvas.hpp:59
A simple canvas which can be drawn upon.
Definition: canvas.hpp:44
texture blur_texture_
Blurred background texture.
Definition: canvas.hpp:157
wfl::action_function_symbol_table functions_
Action function definitions for the canvas.
Definition: canvas.hpp:169
void clear_shapes(const bool force)
Definition: canvas.cpp:593
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:137
unsigned blur_depth_
The depth of the blur to use in the pre committing.
Definition: canvas.hpp:154
wfl::map_formula_callable variables_
The variables of the canvas.
Definition: canvas.hpp:166
canvas(const canvas &)=delete
void parse_cfg(const config &cfg)
Parses a config object.
Definition: canvas.cpp:533
void append_cfg(const config &cfg)
Appends data to the config.
Definition: canvas.hpp:115
unsigned get_height() const
Definition: canvas.hpp:130
canvas & operator=(const canvas &)=delete
std::vector< std::unique_ptr< shape > > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:144
unsigned w_
The full width of the canvas.
Definition: canvas.hpp:160
void update_size_variables()
Update WFL size variables.
Definition: canvas.cpp:579
unsigned h_
The full height of the canvas.
Definition: canvas.hpp:163
void draw()
Draw the canvas' shapes onto the screen.
Definition: canvas.cpp:502
unsigned get_width() const
Definition: canvas.hpp:125
void set_size(const point &size)
Definition: canvas.cpp:586
void set_cfg(const config &cfg, const bool force=false)
Sets the config.
Definition: canvas.hpp:104
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
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:87
Definition: contexts.hpp:44
Holds a 2D point.
Definition: point.hpp:25
mock_char c