The Battle for Wesnoth  1.17.0-dev
canvas.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2021
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/surface.hpp"
28 
29 namespace wfl { class variant; }
30 
31 namespace gui2
32 {
33 
34 /**
35  * A simple canvas which can be drawn upon.
36  *
37  * The class has a config which contains what to draw.
38  *
39  * The copy constructor does a shallow copy of the shapes to draw.
40  * a clone() will be implemented if really needed.
41  */
42 class canvas
43 {
44 public:
45  /**
46  * Abstract base class for all other shapes.
47  *
48  * The other shapes are declared and defined in canvas_private.hpp, since the
49  * implementation details are not interesting for users of the canvas.
50  */
51  class shape
52  {
53  public:
54  explicit shape(const config& cfg) : immutable_(cfg["immutable"].to_bool(false))
55  {
56  }
57 
58  virtual ~shape()
59  {
60  }
61 
62  /**
63  * Draws the canvas.
64  *
65  * @param canvas The resulting image will be blitted upon this
66  * canvas.
67  * @param renderer The SDL_Renderer to use.
68  * @param view_bounds Part of the shape to render - this is the location of @a canvas
69  * within the coordinates of the shape.
70  * @param variables The canvas can have formulas in it's
71  * definition, this parameter contains the values
72  * for these formulas.
73  */
74  virtual void draw(surface& canvas, SDL_Renderer* renderer,
75  const SDL_Rect& view_bounds,
76  wfl::map_formula_callable& variables) = 0;
77 
78  bool immutable() const
79  {
80  return immutable_;
81  }
82 
83  private:
84  /**
85  * If this is true, this shape will not be removed from the canvas even if
86  * the canvas's content is reset.
87  */
88  bool immutable_;
89  };
90 
91  canvas();
92  canvas(const canvas&) = delete;
93  canvas& operator=(const canvas&) = delete;
94  canvas(canvas&& c) noexcept;
95 
96  private:
97  /**
98  * Internal part of the blit() function - prepares the contents of the internal viewport_
99  * surface, reallocating that surface if necessary.
100  *
101  * @param area_to_draw Currently-visible part of the widget, any area outside here won't be blitted to the parent.
102  * @param force If the canvas isn't dirty it isn't redrawn
103  * unless force is set to true.
104  */
105  void draw(const SDL_Rect& area_to_draw, const bool force = false);
106 
107  public:
108  /**
109  * Draw the canvas' shapes onto another surface.
110  *
111  * It makes sure the image on the canvas is up to date. Also executes the
112  * pre-blitting functions.
113  *
114  * @param surf The surface to blit upon.
115  * @param rect The place to blit to.
116  */
117  void blit(surface& surf, SDL_Rect rect);
118 
119  /**
120  * Sets the config.
121  *
122  * @param cfg The config object with the data to draw.
123  * @param force Whether to clear all shapes or not.
124  */
125  void set_cfg(const config& cfg, const bool force = false)
126  {
127  clear_shapes(force);
128  invalidate_cache();
129  parse_cfg(cfg);
130  }
131 
132  /**
133  * Appends data to the config.
134  *
135  * @param cfg The config object with the data to draw.
136  */
137  void append_cfg(const config& cfg)
138  {
139  parse_cfg(cfg);
140  }
141 
142  /***** ***** ***** setters / getters for members ***** ****** *****/
143 
144  void set_width(const unsigned width)
145  {
146  w_ = width;
147  set_is_dirty(true);
148  invalidate_cache();
149  }
150 
151  unsigned get_width() const
152  {
153  return w_;
154  }
155 
156  void set_height(const unsigned height)
157  {
158  h_ = height;
159  set_is_dirty(true);
160  invalidate_cache();
161  }
162 
163  unsigned get_height() const
164  {
165  return h_;
166  }
167 
168  void set_variable(const std::string& key, wfl::variant&& value)
169  {
170  variables_.add(key, std::move(value));
171  set_is_dirty(true);
172  invalidate_cache();
173  }
174 
175  void set_is_dirty(const bool is_dirty)
176  {
177  is_dirty_ = is_dirty;
178  }
179 
180 private:
181  /** Vector with the shapes to draw. */
182  std::vector<std::unique_ptr<shape>> shapes_;
183 
184  /**
185  * The depth of the blur to use in the pre committing.
186  *
187  * @note at the moment there's one pre commit function, namely the
188  * blurring so use a variable here, might get more functions in the
189  * future. When that happens need to evaluate whether variables are the
190  * best thing to use.
191  */
192  unsigned blur_depth_;
193 
194  /** Width of the canvas (the full size, not limited to the view_bounds_). */
195  unsigned w_;
196 
197  /** Height of the canvas (the full size, not limited to the view_bounds_). */
198  unsigned h_;
199 
200  /** The surface we draw all items on. */
202 
203  /**
204  * The placement and size of viewport_ in the coordinates of this widget; value is not useful when bool(viewport_) is false.
205  *
206  * For large widgets, a small viewport_ may be used that contains only the currently-visible part of the widget.
207  */
208  SDL_Rect view_bounds_;
209 
210  /** The variables of the canvas. */
212 
213  /** Action function definitions for the canvas. */
215 
216  /** The dirty state of the canvas. */
217  bool is_dirty_;
218 
219  /**
220  * Parses a config object.
221  *
222  * The config object is parsed and serialized by this function after which
223  * the config object is no longer required and thus not stored in the
224  * object.
225  *
226  * @param cfg The config object with the data to draw, see @ref GUICanvasWML
227  */
228  void parse_cfg(const config& cfg);
229 
230  void clear_shapes(const bool force);
231 
233  {
234  viewport_ = nullptr;
235  }
236 };
237 
238 } // namespace gui2
void invalidate_cache()
Definition: canvas.hpp:232
bool is_dirty_
The dirty state of the canvas.
Definition: canvas.hpp:217
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:168
unsigned h_
Height of the canvas (the full size, not limited to the view_bounds_).
Definition: canvas.hpp:198
virtual ~shape()
Definition: canvas.hpp:58
surface viewport_
The surface we draw all items on.
Definition: canvas.hpp:201
Definitions for the interface to Wesnoth Markup Language (WML).
void set_width(const unsigned width)
Definition: canvas.hpp:144
void set_cfg(const config &cfg, const bool force=false)
Sets the config.
Definition: canvas.hpp:125
wfl::map_formula_callable variables_
The variables of the canvas.
Definition: canvas.hpp:211
Generic file dialog.
Definition: field-fwd.hpp:23
SDL_Rect view_bounds_
The placement and size of viewport_ in the coordinates of this widget; value is not useful when bool(...
Definition: canvas.hpp:208
Abstract base class for all other shapes.
Definition: canvas.hpp:51
shape(const config &cfg)
Definition: canvas.hpp:54
unsigned get_width() const
Definition: canvas.hpp:151
void append_cfg(const config &cfg)
Appends data to the config.
Definition: canvas.hpp:137
wfl::action_function_symbol_table functions_
Action function definitions for the canvas.
Definition: canvas.hpp:214
std::vector< std::unique_ptr< shape > > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:182
A simple canvas which can be drawn upon.
Definition: canvas.hpp:42
void set_is_dirty(const bool is_dirty)
Definition: canvas.hpp:175
unsigned blur_depth_
The depth of the blur to use in the pre committing.
Definition: canvas.hpp:192
bool immutable_
If this is true, this shape will not be removed from the canvas even if the canvas&#39;s content is reset...
Definition: canvas.hpp:88
bool immutable() const
Definition: canvas.hpp:78
Definition: contexts.hpp:44
void set_height(const unsigned height)
Definition: canvas.hpp:156
unsigned get_height() const
Definition: canvas.hpp:163
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
mock_char c
std::size_t w_
unsigned w_
Width of the canvas (the full size, not limited to the view_bounds_).
Definition: canvas.hpp:195