Skip to content

Commit 1208ffe

Browse files
committed
Make sprite_button_styler generic. Allow decorations to be applied.
1 parent 36c99b0 commit 1208ffe

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

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

+32-18
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace cycfi::elements
1313
{
14-
class sprite_button_styler : public sprite
14+
template <typename Subject>
15+
class sprite_button_styler : public proxy<Subject>
1516
{
1617
public:
17-
sprite_button_styler(sprite const& s);
18+
19+
using base_type = proxy<Subject>;
20+
21+
sprite_button_styler(Subject subject);
1822

1923
void draw(context const& ctx) override;
2024
};
@@ -23,52 +27,62 @@ namespace cycfi::elements
2327
concepts::MomentaryButton Base = basic_button
2428
, concepts::SpriteSubject Styler
2529
>
26-
inline proxy<sprite_button_styler, Base>
30+
inline proxy<sprite_button_styler<remove_cvref_t<Styler>>, Base>
2731
momentary_button(Styler&& s)
2832
{
29-
return {sprite_button_styler{std::forward<Styler>(s)}};
33+
using styler_type = sprite_button_styler<remove_cvref_t<Styler>>;
34+
return {styler_type{std::forward<Styler>(s)}};
3035
}
3136

3237
template <
3338
concepts::ToggleButton Base = basic_toggle_button
3439
, concepts::SpriteSubject Styler
3540
>
36-
inline proxy<sprite_button_styler, Base>
41+
inline proxy<sprite_button_styler<remove_cvref_t<Styler>>, Base>
3742
toggle_button(Styler&& s)
3843
{
39-
return {sprite_button_styler{std::forward<Styler>(s)}};
44+
using styler_type = sprite_button_styler<remove_cvref_t<Styler>>;
45+
return {styler_type{std::forward<Styler>(s)}};
4046
}
4147

4248
template <
4349
concepts::LatchingButton Base = basic_latching_button
4450
, concepts::SpriteSubject Styler
4551
>
46-
inline proxy<sprite_button_styler, Base>
52+
inline proxy<sprite_button_styler<remove_cvref_t<Styler>>, Base>
4753
latching_button(Styler&& s)
4854
{
49-
return {sprite_button_styler{std::forward<Styler>(s)}};
55+
using styler_type = sprite_button_styler<remove_cvref_t<Styler>>;
56+
return {styler_type{std::forward<Styler>(s)}};
5057
}
5158

5259
//--------------------------------------------------------------------------
5360
// Inlines
5461
//--------------------------------------------------------------------------
55-
inline sprite_button_styler::sprite_button_styler(sprite const& s)
56-
: sprite(s)
62+
63+
template <typename Subject>
64+
inline sprite_button_styler<Subject>::sprite_button_styler(Subject subject)
65+
: base_type{std::move(subject)}
5766
{}
5867

59-
inline void sprite_button_styler::draw(context const& ctx)
68+
template <typename Subject>
69+
inline void sprite_button_styler<Subject>::draw(context const& ctx)
6070
{
6171
auto btn = find_parent<basic_button*>(ctx);
6272
if (!btn)
6373
return;
6474

65-
auto value = btn->value();
66-
auto hilite = btn->hilite();
67-
if (!ctx.enabled && num_frames() > 4)
68-
index(4); // disabled
69-
else
70-
index((value? 2 : 0) + hilite); // enabled
71-
basic_sprite::draw(ctx);
75+
auto sp = find_subject<sprite*>(this);
76+
if (sp)
77+
{
78+
auto value = btn->value();
79+
auto hilite = btn->hilite();
80+
if (!ctx.enabled && sp->num_frames() > 4)
81+
sp->index(4); // disabled
82+
else
83+
sp->index((value? 2 : 0) + hilite); // enabled
84+
sp->draw(ctx);
85+
}
7286
}
7387
}
7488

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ namespace cycfi::elements
1414
{
1515
template <concepts::SpriteSubject Styler>
1616
inline proxy<remove_cvref_t<Styler>, basic_dial>
17-
dial(Styler&& styler, double init_value = 0.0)
17+
dial(Styler&& styler, double init_value = 0.0);
18+
19+
//--------------------------------------------------------------------------
20+
// Inlines
21+
//--------------------------------------------------------------------------
22+
23+
template <concepts::SpriteSubject Styler>
24+
inline proxy<remove_cvref_t<Styler>, basic_dial>
25+
dial(Styler&& styler, double init_value)
1826
{
1927
return {std::forward<Styler>(styler), init_value};
2028
}

0 commit comments

Comments
 (0)