The Battle for Wesnoth  1.15.1+dev
canvas.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * This file contains the canvas object which is the part where the widgets
18  * draw (temporally) images on.
19  */
20 
21 #pragma once
22 
23 #include "config.hpp"
24 #include "formula/callable.hpp"
25 #include "formula/function.hpp"
26 #include "sdl/surface.hpp"
27 
28 namespace wfl { class variant; }
29 
30 namespace gui2
31 {
32 
33 /**
34  * A simple canvas which can be drawn upon.
35  *
36  * The class has a config which contains what to draw.
37  *
38  * The copy constructor does a shallow copy of the shapes to draw.
39  * a clone() will be implemented if really needed.
40  */
41 class canvas
42 {
43 public:
44  /**
45  * Abstract base class for all other shapes.
46  *
47  * The other shapes are declared and defined in canvas_private.hpp, since the
48  * implementation details are not interesting for users of the canvas.
49  */
50  class shape
51  {
52  public:
53  explicit shape(const config& cfg) : immutable_(cfg["immutable"].to_bool(false))
54  {
55  }
56 
57  virtual ~shape()
58  {
59  }
60 
61  /**
62  * Draws the canvas.
63  *
64  * @param canvas The resulting image will be blitted upon this
65  * canvas.
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(surface& canvas, SDL_Renderer* renderer,
71  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  typedef std::shared_ptr<shape> shape_ptr;
87  typedef std::shared_ptr<const shape> const_shape_ptr;
88 
89  canvas();
90  canvas(const canvas&) = delete;
91  canvas(canvas&& c) noexcept;
92 
93  ~canvas();
94 
95  /**
96  * Draws the canvas.
97  *
98  * @param force If the canvas isn't dirty it isn't redrawn
99  * unless force is set to true.
100  */
101  void draw(const bool force = false);
102 
103  /**
104  * Blits the canvas unto another surface.
105  *
106  * It makes sure the image on the canvas is up to date. Also executes the
107  * pre-blitting functions.
108  *
109  * @param surf The surface to blit upon.
110  * @param rect The place to blit to.
111  */
112  void blit(surface& surf, SDL_Rect rect);
113 
114  /**
115  * Sets the config.
116  *
117  * @param cfg The config object with the data to draw, see
118  * https://www.wesnoth.org/wiki/GUICanvasWML for
119  * more information.
120  */
121  void set_cfg(const config& cfg, const bool force = false)
122  {
123  clear_shapes(force);
124  invalidate_cache();
125  parse_cfg(cfg);
126  }
127 
128  /**
129  * Appends data to the config.
130  *
131  * @param cfg The config object with the data to draw, see
132  * https://www.wesnoth.org/wiki/GUICanvasWML for
133  * more information.
134  */
135  void append_cfg(const config& cfg)
136  {
137  parse_cfg(cfg);
138  }
139 
140  /***** ***** ***** setters / getters for members ***** ****** *****/
141 
142  void set_width(const unsigned width)
143  {
144  w_ = width;
145  set_is_dirty(true);
146  invalidate_cache();
147  }
148 
149  unsigned get_width() const
150  {
151  return w_;
152  }
153 
154  void set_height(const unsigned height)
155  {
156  h_ = height;
157  set_is_dirty(true);
158  invalidate_cache();
159  }
160 
161  unsigned get_height() const
162  {
163  return h_;
164  }
165 
167  {
168  return canvas_;
169  }
170 
171  void set_variable(const std::string& key, const wfl::variant& value)
172  {
173  variables_.add(key, value);
174  set_is_dirty(true);
175  invalidate_cache();
176  }
177 
178  void set_is_dirty(const bool is_dirty)
179  {
180  is_dirty_ = is_dirty;
181  }
182 
183 private:
184  /** Vector with the shapes to draw. */
185  std::vector<shape_ptr> shapes_;
186 
187  /** All shapes which have been already drawn. Kept around in case
188  * the cache needs to be invalidated. */
189  std::vector<shape_ptr> drawn_shapes_;
190 
191  /**
192  * The depth of the blur to use in the pre committing.
193  *
194  * @note at the moment there's one pre commit function, namely the
195  * blurring so use a variable here, might get more functions in the
196  * future. When that happens need to evaluate whether variables are the
197  * best thing to use.
198  */
199  unsigned blur_depth_;
200 
201  /** Width of the canvas. */
202  unsigned w_;
203 
204  /** Height of the canvas. */
205  unsigned h_;
206 
207  /** The surface we draw all items on. */
209 
210  SDL_Renderer* renderer_;
211 
212  /** The variables of the canvas. */
214 
215  /** Action function definitions for the canvas. */
217 
218  /** The dirty state of the canvas. */
219  bool is_dirty_;
220 
221  /**
222  * Parses a config object.
223  *
224  * The config object is parsed and serialized by this function after which
225  * the config object is no longer required and thus not stored in the
226  * object.
227  *
228  * @param cfg The config object with the data to draw, see
229  * https://www.wesnoth.org/wiki/GUICanvasWML
230  */
231  void parse_cfg(const config& cfg);
232 
233  void clear_shapes(const bool force);
234  void invalidate_cache();
235 };
236 
237 } // namespace gui2
bool is_dirty_
The dirty state of the canvas.
Definition: canvas.hpp:219
std::vector< shape_ptr > drawn_shapes_
All shapes which have been already drawn.
Definition: canvas.hpp:189
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:171
unsigned h_
Height of the canvas.
Definition: canvas.hpp:205
virtual ~shape()
Definition: canvas.hpp:57
Definitions for the interface to Wesnoth Markup Language (WML).
std::shared_ptr< const shape > const_shape_ptr
Definition: canvas.hpp:87
void set_width(const unsigned width)
Definition: canvas.hpp:142
void set_cfg(const config &cfg, const bool force=false)
Sets the config.
Definition: canvas.hpp:121
wfl::map_formula_callable variables_
The variables of the canvas.
Definition: canvas.hpp:213
Generic file dialog.
Definition: field-fwd.hpp:22
SDL_Renderer * renderer_
Definition: canvas.hpp:210
Abstract base class for all other shapes.
Definition: canvas.hpp:50
shape(const config &cfg)
Definition: canvas.hpp:53
unsigned get_width() const
Definition: canvas.hpp:149
void append_cfg(const config &cfg)
Appends data to the config.
Definition: canvas.hpp:135
wfl::action_function_symbol_table functions_
Action function definitions for the canvas.
Definition: canvas.hpp:216
A simple canvas which can be drawn upon.
Definition: canvas.hpp:41
void set_is_dirty(const bool is_dirty)
Definition: canvas.hpp:178
unsigned blur_depth_
The depth of the blur to use in the pre committing.
Definition: canvas.hpp:199
surface & surf()
Definition: canvas.hpp:166
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:83
bool immutable() const
Definition: canvas.hpp:73
std::vector< shape_ptr > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:185
Definition: contexts.hpp:42
void set_height(const unsigned height)
Definition: canvas.hpp:154
std::shared_ptr< shape > shape_ptr
Definition: canvas.hpp:86
unsigned get_height() const
Definition: canvas.hpp:161
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
mock_char c
surface canvas_
The surface we draw all items on.
Definition: canvas.hpp:208
std::size_t w_
unsigned w_
Width of the canvas.
Definition: canvas.hpp:202