7
7
#define ELEMENTS_IMAGE_APRIL_24_2016
8
8
9
9
#include < elements/element/element.hpp>
10
+ #include < elements/element/proxy.hpp>
10
11
#include < elements/support/receiver.hpp>
11
12
#include < artist/image.hpp>
12
13
#include < artist/canvas.hpp>
@@ -59,47 +60,6 @@ namespace cycfi::elements
59
60
float _scale;
60
61
};
61
62
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
- */
74
- template <typename Derived>
75
- struct sprite_as_int : receiver<int >
76
- {
77
- void value (int val) override ;
78
- int value () const override ;
79
- };
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
- */
96
- template <typename Derived>
97
- struct sprite_as_double : receiver<double >
98
- {
99
- void value (double val) override ;
100
- double value () const override ;
101
- };
102
-
103
63
/* *
104
64
* \class basic_sprite
105
65
*
@@ -130,35 +90,33 @@ namespace cycfi::elements
130
90
float _height;
131
91
};
132
92
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
- */
158
- struct sprite : basic_sprite, sprite_as_int<sprite>, sprite_as_double<sprite>
93
+ using sprite = basic_sprite;
94
+
95
+ namespace detail
159
96
{
160
- using basic_sprite::basic_sprite;
161
- };
97
+ template <typename T>
98
+ constexpr auto is_sprite ()
99
+ {
100
+ if constexpr (std::is_base_of_v<proxy_base, T>)
101
+ return is_sprite<typename T::subject_type>();
102
+ else
103
+ return std::false_type{};
104
+ }
105
+
106
+ template <>
107
+ constexpr auto is_sprite<sprite>()
108
+ {
109
+ return std::true_type{};
110
+ }
111
+ }
112
+
113
+ namespace concepts
114
+ {
115
+ template <typename T>
116
+ concept SpriteSubject
117
+ = concepts::Element<T> &&
118
+ decltype (detail::is_sprite<std::decay_t <T>>())::value;
119
+ }
162
120
163
121
// --------------------------------------------------------------------------
164
122
// Inlines
@@ -175,77 +133,6 @@ namespace cycfi::elements
175
133
{
176
134
return _index;
177
135
}
178
-
179
- /* *
180
- * \brief
181
- * Returns the index of the currently displayed frame in sprite_as_int.
182
- *
183
- * \tparam Derived
184
- * The derived class type.
185
- *
186
- * \returns
187
- * The index of the currently displayed frame as an integer.
188
- */
189
- template <typename Derived>
190
- int sprite_as_int<Derived>::value() const
191
- {
192
- auto this_ = static_cast <Derived const *>(this );
193
- return this_->index ();
194
- }
195
-
196
- /* *
197
- * \brief
198
- * Sets the index of the sprite_as_int to the provided value.
199
- *
200
- * \tparam Derived
201
- * The derived class type.
202
- *
203
- * \param val
204
- * The value to set the index to (index of the frame to be displayed).
205
- */
206
- template <typename Derived>
207
- void sprite_as_int<Derived>::value(int val)
208
- {
209
- auto this_ = static_cast <Derived*>(this );
210
- this_->index (val);
211
- }
212
-
213
- /* *
214
- * \brief
215
- * Returns the index of the currently displayed frame in
216
- * sprite_as_double as a fraction of the total frames.
217
- *
218
- * \tparam Derived
219
- * The derived class type.
220
- *
221
- * \returns
222
- * The index of the currently displayed frame as a fraction of the
223
- * total frames (between 0.0 to 1.0).
224
- */
225
- template <typename Derived>
226
- double sprite_as_double<Derived>::value() const
227
- {
228
- auto this_ = static_cast <Derived const *>(this );
229
- return this_->index () / this_->num_frames ()-1 ;
230
- }
231
-
232
- /* *
233
- * \brief
234
- * Sets the index of the sprite_as_double to the provided value.
235
- *
236
- * \tparam Derived
237
- * The derived class type.
238
- *
239
- * \param val
240
- * The value to set the index to. It's a value between 0.0 and 1.0
241
- * representing the relative position in the sequence of frames.
242
- */
243
- template <typename Derived>
244
- void sprite_as_double<Derived>::value(double val)
245
- {
246
- auto this_ = static_cast <Derived*>(this );
247
- this_->index (val * (this_->num_frames ()-1 ));
248
- }
249
136
}
250
137
251
138
#endif
0 commit comments