@@ -16,9 +16,18 @@ namespace cycfi::elements
16
16
{
17
17
using artist::image_ptr;
18
18
19
- // //////////////////////////////////////////////////////////////////////////
20
- // Images
21
- // //////////////////////////////////////////////////////////////////////////
19
+ /* *
20
+ * \class image
21
+ *
22
+ * \brief
23
+ * A class representing an image.
24
+ *
25
+ * The `image` class provides functionalities such as scaling, fitting
26
+ * to available space, drawing, and setting/retrieving the image
27
+ * source. The image source can be either an `image_ptr` (a pointer to
28
+ * an image) or a filesystem path to an image file (`fs::path`). JPEG,
29
+ * PNG and WEBP images are supported.
30
+ */
22
31
class image : public element
23
32
{
24
33
public:
@@ -50,35 +59,57 @@ namespace cycfi::elements
50
59
float _scale;
51
60
};
52
61
53
- // //////////////////////////////////////////////////////////////////////////
54
- // Images used as controls. Various frames are laid out in a single (big)
55
- // image but only one frame is drawn at any single time. Useful for
56
- // switches, knobs and basic (sprite) animation.
57
- //
58
- // Note on sprite_as_int and sprite_as_double: The tricky thing about
59
- // sprites is that they can act as both receiver<int> or receiver<double>
60
- // depending on usage. For example, buttons use it as a receiver<int>
61
- // where the int value reflects the current frame displayed. On the other
62
- // hand, dials regard it as a receiver<double>, where the value 0.0 to 1.0
63
- // reflects its state from 0 to num_frames()-1. Alas, we cannot directly
64
- // inherit from both because the overridden value() member function will
65
- // have an ambiguous return type (double or int?). The sprite_as_int and
66
- // sprite_as_double TMP trick solves this dilemma.
67
- // //////////////////////////////////////////////////////////////////////////
62
+ /* *
63
+ * \class sprite_as_int
64
+ *
65
+ * \brief
66
+ * A template structure to handle sprite images used as 'int' receivers.
67
+ *
68
+ * Receives an 'int' value which indicates which frame of sprite image
69
+ * should be displayed.
70
+ *
71
+ * \tparam Derived
72
+ * The derived class type.
73
+ */
68
74
template <typename Derived>
69
75
struct sprite_as_int : receiver<int >
70
76
{
71
77
void value (int val) override ;
72
78
int value () const override ;
73
79
};
74
80
81
+ /* *
82
+ * \class sprite_as_double
83
+ *
84
+ * \brief
85
+ * A template structure to handle sprite images used as 'double'
86
+ * receivers.
87
+ *
88
+ * Receives a 'double' value in the range of 0.0 to 1.0, which
89
+ * indicates which frame of the sprite image should be displayed. The
90
+ * double value essentially represents the frame number, scaled down to
91
+ * fit within the 0.0 to 1.0 range.
92
+ *
93
+ * \tparam Derived
94
+ * The derived class type.
95
+ */
75
96
template <typename Derived>
76
97
struct sprite_as_double : receiver<double >
77
98
{
78
99
void value (double val) override ;
79
100
double value () const override ;
80
101
};
81
102
103
+ /* *
104
+ * \class basic_sprite
105
+ *
106
+ * \brief
107
+ * Represents a basic sprite image for use in controls.
108
+ *
109
+ * `basic_sprite` extends the `image` class to provide sprite-specific
110
+ * functionality. It subdivides an image into slices that can be
111
+ * indexed to display a particular frame.
112
+ */
82
113
class basic_sprite : public image
83
114
{
84
115
public:
@@ -87,7 +118,7 @@ namespace cycfi::elements
87
118
view_limits limits (basic_context const & ctx) const override ;
88
119
89
120
std::size_t num_frames () const ;
90
- std::size_t index () const { return _index; }
121
+ std::size_t index () const ;
91
122
void index (std::size_t index_);
92
123
point size () const override ;
93
124
@@ -99,32 +130,117 @@ namespace cycfi::elements
99
130
float _height;
100
131
};
101
132
133
+ /* *
134
+ * \class sprite
135
+ *
136
+ * \brief
137
+ * A class representing a sprite image that can be used as controls.
138
+ *
139
+ * `sprite` extends `basic_sprite` to utilize both `sprite_as_int` and
140
+ * `sprite_as_double`, making it usable as both an 'int' and a 'double'
141
+ * receiver.
142
+ *
143
+ * Sprites are images used as controls. Various frames are laid out in
144
+ * a single (big) image but only one frame is drawn at any single time.
145
+ * Useful for switches, knobs and basic (sprite) animation.
146
+ *
147
+ * Note on sprite_as_int and sprite_as_double: The tricky thing about
148
+ * sprites is that they can act as both receiver<int> or
149
+ * receiver<double> depending on usage. For example, buttons use it as
150
+ * a receiver<int> where the int value reflects the current frame
151
+ * displayed. On the other hand, dials regard it as a receiver<double>,
152
+ * where the value 0.0 to 1.0 reflects its state from 0 to
153
+ * num_frames()-1. Alas, we cannot directly inherit from both because
154
+ * the overridden value() member function will have an ambiguous return
155
+ * type (double or int?). The sprite_as_int and sprite_as_double TMP
156
+ * trick solves this dilemma.
157
+ */
102
158
struct sprite : basic_sprite, sprite_as_int<sprite>, sprite_as_double<sprite>
103
159
{
104
160
using basic_sprite::basic_sprite;
105
161
};
106
162
163
+ // //////////////////////////////////////////////////////////////////////////
164
+ // Inlines
165
+ // //////////////////////////////////////////////////////////////////////////
166
+ namespace inlines {}
167
+
168
+ /* *
169
+ * \brief
170
+ * Retrieves the current frame index within the basic_sprite.
171
+ *
172
+ * \returns
173
+ * The current frame index as `std::size_t`.
174
+ */
175
+ inline std::size_t basic_sprite::index () const
176
+ {
177
+ return _index;
178
+ }
179
+
180
+ /* *
181
+ * \brief
182
+ * Returns the index of the currently displayed frame in sprite_as_int.
183
+ *
184
+ * \tparam Derived
185
+ * The derived class type.
186
+ *
187
+ * \returns
188
+ * The index of the currently displayed frame as an integer.
189
+ */
107
190
template <typename Derived>
108
191
int sprite_as_int<Derived>::value() const
109
192
{
110
193
auto this_ = static_cast <Derived const *>(this );
111
194
return this_->index ();
112
195
}
113
196
197
+ /* *
198
+ * \brief
199
+ * Sets the index of the sprite_as_int to the provided value.
200
+ *
201
+ * \tparam Derived
202
+ * The derived class type.
203
+ *
204
+ * \param val
205
+ * The value to set the index to (index of the frame to be displayed).
206
+ */
114
207
template <typename Derived>
115
208
void sprite_as_int<Derived>::value(int val)
116
209
{
117
210
auto this_ = static_cast <Derived*>(this );
118
211
this_->index (val);
119
212
}
120
213
214
+ /* *
215
+ * \brief
216
+ * Returns the index of the currently displayed frame in
217
+ * sprite_as_double as a fraction of the total frames.
218
+ *
219
+ * \tparam Derived
220
+ * The derived class type.
221
+ *
222
+ * \returns
223
+ * The index of the currently displayed frame as a fraction of the
224
+ * total frames (between 0.0 to 1.0).
225
+ */
121
226
template <typename Derived>
122
227
double sprite_as_double<Derived>::value() const
123
228
{
124
229
auto this_ = static_cast <Derived const *>(this );
125
230
return this_->index () / this_->num_frames ()-1 ;
126
231
}
127
232
233
+ /* *
234
+ * \brief
235
+ * Sets the index of the sprite_as_double to the provided value.
236
+ *
237
+ * \tparam Derived
238
+ * The derived class type.
239
+ *
240
+ * \param val
241
+ * The value to set the index to. It's a value between 0.0 and 1.0
242
+ * representing the relative position in the sequence of frames.
243
+ */
128
244
template <typename Derived>
129
245
void sprite_as_double<Derived>::value(double val)
130
246
{
0 commit comments