/
canvas.hpp
222 lines (185 loc) · 5.1 KB
/
canvas.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright (C) 2007 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* This file contains the canvas object which is the part where the widgets
* draw (temporally) images on.
*/
#pragma once
#include "config.hpp"
#include "formula/callable.hpp"
#include "formula/function.hpp"
#include "sdl/texture.hpp"
namespace wfl { class variant; }
namespace gui2
{
/**
* A simple canvas which can be drawn upon.
*
* The class has a config which contains what to draw.
*
* NOTE we might add some caching in a later state, for now every draw cycle
* does a full redraw.
*
* The copy constructor does a shallow copy of the shapes to draw.
* a clone() will be implemented if really needed.
*/
class canvas
{
public:
/**
* Abstract base class for all other shapes.
*
* The other shapes are declared and defined in canvas_private.hpp, since the
* implementation details are not interesting for users of the canvas.
*/
class shape
{
public:
explicit shape(const config& cfg) : immutable_(cfg["immutable"].to_bool(false))
{
}
virtual ~shape()
{
}
/**
* Draws the canvas.
*
* @param variables The canvas can have formulas in it's
* definition, this parameter contains the values
* for these formulas.
*/
virtual void draw(
const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
wfl::map_formula_callable& variables) = 0;
bool immutable() const
{
return immutable_;
}
private:
/**
* If this is true, this shape will not be removed from the canvas even if
* the canvas's content is reset.
*/
bool immutable_;
};
typedef std::shared_ptr<shape> shape_ptr;
typedef std::shared_ptr<const shape> const_shape_ptr;
canvas();
~canvas();
/**
* Draws the canvas.
*
* @param force If the canvas isn't dirty it isn't redrawn
* unless force is set to true.
*/
void draw(const bool force = false);
/**
* Copies the canvas texture to the screen renderer.
*
* This will re-render the canvas texture if necessary (ie, if marked dirty).
* It also executes the pre-commit functions such as blurring (@todo: reenable).
*/
void render();
/**
* Sets the config.
*
* @param cfg The config object with the data to draw, see
* http://www.wesnoth.org/wiki/GUICanvasWML for
* more information.
*/
void set_cfg(const config& cfg, const bool force = false)
{
clear_shapes(force);
parse_cfg(cfg);
}
/**
* Appends data to the config.
*
* @param cfg The config object with the data to draw, see
* http://www.wesnoth.org/wiki/GUICanvasWML for
* more information.
*/
void append_cfg(const config& cfg)
{
parse_cfg(cfg);
}
/***** ***** ***** setters / getters for members ***** ****** *****/
void set_width(const unsigned width)
{
w_ = width;
set_is_dirty(true);
}
unsigned get_width() const
{
return w_;
}
void set_height(const unsigned height)
{
h_ = height;
set_is_dirty(true);
}
unsigned get_height() const
{
return h_;
}
void set_variable(const std::string& key, const wfl::variant& value)
{
variables_.add(key, value);
set_is_dirty(true);
}
void set_is_dirty(const bool is_dirty)
{
is_dirty_ = is_dirty;
}
private:
/** Vector with the shapes to draw. */
std::vector<shape_ptr> shapes_;
/**
* The depth of the blur to use in the pre committing.
*
* @note at the moment there's one pre commit function, namely the
* blurring so use a variable here, might get more functions in the
* future. When that happens need to evaluate whether variables are the
* best thing to use.
*/
unsigned blur_depth_;
/** Width of the canvas. */
unsigned w_;
/** Height of the canvas. */
unsigned h_;
/** The texture onto which items are drawn. */
texture texture_;
/** A pointer to the window renderer. */
SDL_Renderer* renderer_;
/** The variables of the canvas. */
wfl::map_formula_callable variables_;
/** Action function definitions for the canvas. */
wfl::action_function_symbol_table functions_;
/** The dirty state of the canvas. */
bool is_dirty_;
/**
* Parses a config object.
*
* The config object is parsed and serialized by this function after which
* the config object is no longer required and thus not stored in the
* object.
*
* @param cfg The config object with the data to draw, see
* http://www.wesnoth.org/wiki/GUICanvasWML
*/
void parse_cfg(const config& cfg);
void clear_shapes(const bool force);
};
} // namespace gui2