Skip to content

Commit bdc9fe4

Browse files
committed
Sprites are no longer reliant on the receiver API
1 parent 1208ffe commit bdc9fe4

File tree

4 files changed

+38
-93
lines changed

4 files changed

+38
-93
lines changed

lib/include/elements/element/dial.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ namespace cycfi::elements
6767

6868
basic_dial(double init_value = 0.0);
6969

70-
void prepare_subject(context& ctx) override;
7170
element* hit_test(context const& ctx, point p, bool leaf, bool control) override;
72-
7371
bool scroll(context const& ctx, point dir, point p) override;
7472
void keep_tracking(context const& ctx, tracker_info& track_info) override;
7573

lib/include/elements/element/image.hpp

+1-77
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,6 @@ namespace cycfi::elements
6060
float _scale;
6161
};
6262

63-
/**
64-
* \class sprite_as_double
65-
*
66-
* \brief
67-
* A template structure to handle sprite images used as 'double'
68-
* receivers.
69-
*
70-
* Receives a 'double' value in the range of 0.0 to 1.0, which
71-
* indicates which frame of the sprite image should be displayed. The
72-
* double value essentially represents the frame number, scaled down to
73-
* fit within the 0.0 to 1.0 range.
74-
*
75-
* \tparam Derived
76-
* The derived class type.
77-
*/
78-
template <typename Derived>
79-
struct sprite_as_double : receiver<double>
80-
{
81-
void value(double val) override;
82-
double value() const override;
83-
};
84-
8563
/**
8664
* \class basic_sprite
8765
*
@@ -112,24 +90,7 @@ namespace cycfi::elements
11290
float _height;
11391
};
11492

115-
/**
116-
* \class sprite
117-
*
118-
* \brief
119-
* A class representing a sprite image that can be used as controls.
120-
*
121-
* `sprite` extends `basic_sprite` to utilize both `sprite_as_int` and
122-
* `sprite_as_double`, making it usable as both an 'int' and a 'double'
123-
* receiver.
124-
*
125-
* Sprites are images used as controls. Various frames are laid out in
126-
* a single (big) image but only one frame is drawn at any single time.
127-
* Useful for switches, knobs and basic (sprite) animation.
128-
*/
129-
struct sprite : basic_sprite, sprite_as_double<sprite>
130-
{
131-
using basic_sprite::basic_sprite;
132-
};
93+
using sprite = basic_sprite;
13394

13495
namespace detail
13596
{
@@ -172,43 +133,6 @@ namespace cycfi::elements
172133
{
173134
return _index;
174135
}
175-
176-
/**
177-
* \brief
178-
* Returns the index of the currently displayed frame in
179-
* sprite_as_double as a fraction of the total frames.
180-
*
181-
* \tparam Derived
182-
* The derived class type.
183-
*
184-
* \returns
185-
* The index of the currently displayed frame as a fraction of the
186-
* total frames (between 0.0 to 1.0).
187-
*/
188-
template <typename Derived>
189-
double sprite_as_double<Derived>::value() const
190-
{
191-
auto this_ = static_cast<Derived const*>(this);
192-
return this_->index() / this_->num_frames()-1;
193-
}
194-
195-
/**
196-
* \brief
197-
* Sets the index of the sprite_as_double to the provided value.
198-
*
199-
* \tparam Derived
200-
* The derived class type.
201-
*
202-
* \param val
203-
* The value to set the index to. It's a value between 0.0 and 1.0
204-
* representing the relative position in the sequence of frames.
205-
*/
206-
template <typename Derived>
207-
void sprite_as_double<Derived>::value(double val)
208-
{
209-
auto this_ = static_cast<Derived*>(this);
210-
this_->index(val * (this_->num_frames()-1));
211-
}
212136
}
213137

214138
#endif

lib/include/elements/element/style/sprite_dial.hpp

+36-3
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,52 @@
1212

1313
namespace cycfi::elements
1414
{
15+
template <typename Subject>
16+
class sprite_dial_styler : public proxy<Subject>
17+
{
18+
public:
19+
20+
using base_type = proxy<Subject>;
21+
22+
sprite_dial_styler(Subject subject);
23+
24+
void draw(context const& ctx) override;
25+
};
26+
1527
template <concepts::SpriteSubject Styler>
16-
inline proxy<remove_cvref_t<Styler>, basic_dial>
28+
inline proxy<sprite_dial_styler<remove_cvref_t<Styler>>, basic_dial>
1729
dial(Styler&& styler, double init_value = 0.0);
1830

1931
//--------------------------------------------------------------------------
2032
// Inlines
2133
//--------------------------------------------------------------------------
2234

35+
template <typename Subject>
36+
inline sprite_dial_styler<Subject>::sprite_dial_styler(Subject subject)
37+
: base_type{std::move(subject)}
38+
{}
39+
40+
template <typename Subject>
41+
inline void sprite_dial_styler<Subject>::draw(context const& ctx)
42+
{
43+
auto dial = find_parent<basic_dial*>(ctx);
44+
if (!dial)
45+
return;
46+
47+
auto sp = find_subject<sprite*>(this);
48+
if (sp)
49+
{
50+
sp->index(dial->value() * (sp->num_frames()-1));
51+
sp->draw(ctx);
52+
}
53+
}
54+
2355
template <concepts::SpriteSubject Styler>
24-
inline proxy<remove_cvref_t<Styler>, basic_dial>
56+
inline proxy<sprite_dial_styler<remove_cvref_t<Styler>>, basic_dial>
2557
dial(Styler&& styler, double init_value)
2658
{
27-
return {std::forward<Styler>(styler), init_value};
59+
using styler_type = sprite_dial_styler<remove_cvref_t<Styler>>;
60+
return {styler_type{std::forward<Styler>(styler)}, init_value};
2861
}
2962
}
3063

lib/src/element/dial.cpp

+1-11
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,9 @@ namespace cycfi::elements
2020
clamp(_value, 0.0, 1.0);
2121
}
2222

23-
void basic_dial::prepare_subject(context& ctx)
24-
{
25-
proxy_base::prepare_subject(ctx);
26-
if (auto* rcvr = find_subject<receiver<double>*>(this))
27-
rcvr->value(_value);
28-
}
29-
3023
void basic_dial::value(double val)
3124
{
3225
_value = clamp(val, 0.0, 1.0);
33-
if (auto* rcvr = find_subject<receiver<double>*>(this))
34-
rcvr->value(_value);
3526
}
3627

3728
namespace
@@ -44,10 +35,9 @@ namespace cycfi::elements
4435
}
4536
}
4637

47-
void basic_dial::edit(view& view_, param_type val)
38+
void basic_dial::edit(view& /*view_*/, param_type val)
4839
{
4940
edit_value(this, val);
50-
receiver<double>::notify_edit(view_);
5141
}
5242

5343
double basic_dial::radial_value(context const& ctx, tracker_info& track_info)

0 commit comments

Comments
 (0)