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