|
| 1 | +/*============================================================================= |
| 2 | + Copyright (c) 2016-2024 Joel de Guzman |
| 3 | +
|
| 4 | + Distributed under the MIT License [ https://opensource.org/licenses/MIT ] |
| 5 | +=============================================================================*/ |
| 6 | +#if !defined(ELEMENTS_SPRITE_BUTTON_AUGUST_16_2024) |
| 7 | +#define ELEMENTS_SPRITE_BUTTON_AUGUST_16_2024 |
| 8 | + |
| 9 | +#include <elements/element/button.hpp> |
| 10 | +#include <elements/element/image.hpp> |
| 11 | + |
| 12 | +namespace cycfi::elements |
| 13 | +{ |
| 14 | + class sprite_button_styler : public sprite |
| 15 | + { |
| 16 | + public: |
| 17 | + sprite_button_styler(sprite const& s); |
| 18 | + |
| 19 | + void draw(context const& ctx) override; |
| 20 | + }; |
| 21 | + |
| 22 | + template <concepts::MomentaryButton Base = basic_button> |
| 23 | + inline proxy<sprite_button_styler, Base> |
| 24 | + momentary_button(sprite const& s) |
| 25 | + { |
| 26 | + return {sprite_button_styler{s}}; |
| 27 | + } |
| 28 | + |
| 29 | + template <concepts::MomentaryButton Base = basic_button> |
| 30 | + inline proxy<sprite_button_styler, Base> |
| 31 | + momentary_button(sprite& s) |
| 32 | + { |
| 33 | + return {sprite_button_styler{s}}; |
| 34 | + } |
| 35 | + |
| 36 | + template <concepts::ToggleButton Base = basic_toggle_button> |
| 37 | + inline proxy<sprite_button_styler, Base> |
| 38 | + toggle_button(sprite const& s) |
| 39 | + { |
| 40 | + return {sprite_button_styler{s}}; |
| 41 | + } |
| 42 | + |
| 43 | + template <concepts::ToggleButton Base = basic_toggle_button> |
| 44 | + inline proxy<sprite_button_styler, Base> |
| 45 | + toggle_button(sprite& s) |
| 46 | + { |
| 47 | + return {sprite_button_styler{s}}; |
| 48 | + } |
| 49 | + |
| 50 | + template <concepts::LatchingButton Base = basic_latching_button> |
| 51 | + inline proxy<sprite_button_styler, Base> |
| 52 | + latching_button(sprite const& s) |
| 53 | + { |
| 54 | + return {sprite_button_styler{s}}; |
| 55 | + } |
| 56 | + |
| 57 | + template <concepts::LatchingButton Base = basic_latching_button> |
| 58 | + inline proxy<sprite_button_styler, Base> |
| 59 | + latching_button(sprite& s) |
| 60 | + { |
| 61 | + return {sprite_button_styler{s}}; |
| 62 | + } |
| 63 | + |
| 64 | + //-------------------------------------------------------------------------- |
| 65 | + // Inlines |
| 66 | + //-------------------------------------------------------------------------- |
| 67 | + inline sprite_button_styler::sprite_button_styler(sprite const& s) |
| 68 | + : sprite(s) |
| 69 | + {} |
| 70 | + |
| 71 | + inline void sprite_button_styler::draw(context const& ctx) |
| 72 | + { |
| 73 | + auto btn = find_parent<basic_button*>(ctx); |
| 74 | + if (!btn) |
| 75 | + return; |
| 76 | + |
| 77 | + auto value = btn->value(); |
| 78 | + auto hilite = btn->hilite(); |
| 79 | + if (!ctx.enabled && num_frames() > 4) |
| 80 | + index(4); // disabled |
| 81 | + else |
| 82 | + index((value? 2 : 0) + hilite); // enabled |
| 83 | + basic_sprite::draw(ctx); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#endif |
0 commit comments