Skip to content

Commit f16bd93

Browse files
committed
image documentation
1 parent ac706b6 commit f16bd93

File tree

1 file changed

+135
-19
lines changed

1 file changed

+135
-19
lines changed

lib/include/elements/element/image.hpp

+135-19
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ namespace cycfi::elements
1616
{
1717
using artist::image_ptr;
1818

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+
*/
2231
class image : public element
2332
{
2433
public:
@@ -50,35 +59,57 @@ namespace cycfi::elements
5059
float _scale;
5160
};
5261

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+
*/
6874
template <typename Derived>
6975
struct sprite_as_int : receiver<int>
7076
{
7177
void value(int val) override;
7278
int value() const override;
7379
};
7480

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+
*/
7596
template <typename Derived>
7697
struct sprite_as_double : receiver<double>
7798
{
7899
void value(double val) override;
79100
double value() const override;
80101
};
81102

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+
*/
82113
class basic_sprite : public image
83114
{
84115
public:
@@ -87,7 +118,7 @@ namespace cycfi::elements
87118
view_limits limits(basic_context const& ctx) const override;
88119

89120
std::size_t num_frames() const;
90-
std::size_t index() const { return _index; }
121+
std::size_t index() const;
91122
void index(std::size_t index_);
92123
point size() const override;
93124

@@ -99,32 +130,117 @@ namespace cycfi::elements
99130
float _height;
100131
};
101132

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+
*/
102158
struct sprite : basic_sprite, sprite_as_int<sprite>, sprite_as_double<sprite>
103159
{
104160
using basic_sprite::basic_sprite;
105161
};
106162

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+
*/
107190
template <typename Derived>
108191
int sprite_as_int<Derived>::value() const
109192
{
110193
auto this_ = static_cast<Derived const*>(this);
111194
return this_->index();
112195
}
113196

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+
*/
114207
template <typename Derived>
115208
void sprite_as_int<Derived>::value(int val)
116209
{
117210
auto this_ = static_cast<Derived*>(this);
118211
this_->index(val);
119212
}
120213

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+
*/
121226
template <typename Derived>
122227
double sprite_as_double<Derived>::value() const
123228
{
124229
auto this_ = static_cast<Derived const*>(this);
125230
return this_->index() / this_->num_frames()-1;
126231
}
127232

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+
*/
128244
template <typename Derived>
129245
void sprite_as_double<Derived>::value(double val)
130246
{

0 commit comments

Comments
 (0)