Skip to content

Commit dd1593f

Browse files
committed
Merge branch 'skia_2024'
2 parents 567cd70 + 632afd4 commit dd1593f

22 files changed

+181
-174
lines changed

examples/buttons/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ set(ELEMENTS_APP_VERSION "1.0")
2525
set(ELEMENTS_APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
2626
set(ELEMENTS_APP_RESOURCES
2727
${CMAKE_CURRENT_SOURCE_DIR}/resources/power_180x632.png
28-
${CMAKE_CURRENT_SOURCE_DIR}/resources/phase_180x632.png
29-
${CMAKE_CURRENT_SOURCE_DIR}/resources/mail_180x632.png
28+
${CMAKE_CURRENT_SOURCE_DIR}/resources/phase_180x790.png
29+
${CMAKE_CURRENT_SOURCE_DIR}/resources/mail_180x790.png
3030
${CMAKE_CURRENT_SOURCE_DIR}/resources/transpo_180x632.png
3131
)
3232

examples/buttons/main.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ void my_custom_button::draw(context const& ctx)
4848
// necessary. For this simple example, we wiill deal only with `value` and
4949
// `hilite`.
5050

51-
auto state = value();
52-
bool value = state.value; // button is on or off
53-
bool hilite = state.hilite; // cursor is hovering over the button
51+
auto btn = find_parent<basic_button*>(ctx);
52+
if (!btn)
53+
return;
54+
55+
bool value = btn->value(); // button is on or off
56+
bool hilite = btn->hilite(); // cursor is hovering over the button
5457
bool enabled = ctx.enabled; // button is enabled or disabled
5558

5659
bounds = bounds.inset(1, 1);
@@ -270,18 +273,24 @@ auto make_controls(view& view_)
270273

271274
float const button_scale = 1.0/4;
272275
sprite power_button = sprite{"power_180x632.png", 158*button_scale, button_scale};
273-
sprite phase_button = sprite{"phase_180x632.png", 158*button_scale, button_scale};
274-
sprite mail_button = sprite{"mail_180x632.png", 158*button_scale, button_scale};
276+
sprite phase_button = sprite{"phase_180x790.png", 158*button_scale, button_scale};
277+
sprite mail_button = sprite{"mail_180x790.png", 158*button_scale, button_scale};
275278
sprite transpo_button = sprite{"transpo_180x632.png", 158*button_scale, button_scale};
276279

280+
// Note: When disabling a sprite button, the sprite must have a fifth
281+
// frame specifically for the disabled state.
282+
auto phase_disabled = toggle_button(phase_button);
283+
phase_disabled.enable(false);
284+
277285
auto sprite_buttons =
278286
group("Sprite Buttons",
279287
margin({10, 45, 20, 10},
280288
htile(
281289
align_center(toggle_button(power_button)),
282290
align_center(toggle_button(phase_button)),
283291
align_center(momentary_button(mail_button)),
284-
align_center(toggle_button(transpo_button))
292+
align_center(toggle_button(transpo_button)),
293+
align_center(phase_disabled)
285294
)
286295
)
287296
);
-70.7 KB
Binary file not shown.
75.5 KB
Loading
-56.8 KB
Binary file not shown.
71.1 KB
Loading

lib/include/elements/element/button.hpp

+4-30
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ namespace cycfi::elements
3737
* A struct for maintaining and managing the state of a button.
3838
*
3939
* This structure captures the various states that a button can have:
40-
* - `value`: The button's value; 0(off) or 1(on).
41-
* - `hilite`: True if the button is highlighted.
40+
* - `value` : The button's value; 0(off) or 1(on).
41+
* - `hilite` : True if the button is highlighted.
4242
* - `tracking`: True if the mouse button being pressed.
43-
* - `enabled`: True if the button is enabled.
43+
* - `enabled` : True if the button is enabled.
4444
*/
4545
struct button_state
4646
{
@@ -66,30 +66,6 @@ namespace cycfi::elements
6666
* button's visual presentation. With this pattern, different stylers
6767
* can be implemented for various visual representations, for instance,
6868
* plain buttons, radio buttons, slide switches, checkboxes, and more.
69-
*
70-
* The communication with the button styler is done via the
71-
* `receiver<button_state>` or a `receiver<int>` APIs. These APIs
72-
* provide a means for the `basic_button` to update the button styler
73-
* about changes in button's state to allow the styler to adjust the
74-
* visual representation accordingly.
75-
*
76-
* If the button styler follows a `receiver<int>` API, it will receive
77-
* in integer with these possible values:
78-
*
79-
* 0: value=`false`, hilite=`false`
80-
* 1: value=`false`, hilite=`true`
81-
* 2: value=`true`, hilite=`false`
82-
* 3: value=`true`, hilite=`true`
83-
*
84-
* If the button styler follows a `receiver<button_state>` API, it will
85-
* receive a `button_state` when the button's state changes. This has a
86-
* richer API compared to the former, allowing more nuanced button
87-
* rendering. See `button_state`.
88-
*
89-
* Take note that the button styler is just an element and does not
90-
* have to follow the `receiver` API. If that's the case, then the
91-
* button rendering will be static, and not adjust to state changes.
92-
* This may still be useful in certain cases.
9369
*/
9470
class basic_button : public proxy_base, public receiver<bool>
9571
{
@@ -117,14 +93,12 @@ namespace cycfi::elements
11793

11894
protected:
11995

120-
bool state(bool val);
96+
bool set_value(bool val);
12197
void tracking(bool val);
12298
void hilite(bool val);
12399

124100
private:
125101

126-
bool update_receiver();
127-
128102
button_state _state;
129103
};
130104

lib/include/elements/element/image.hpp

+1-65
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,6 @@ namespace cycfi::elements
116116
void draw(context const& ctx) override;
117117
};
118118

119-
/**
120-
* \class sprite_as_int
121-
*
122-
* \brief
123-
* A template structure to handle sprite images used as 'int' receivers.
124-
*
125-
* Receives an 'int' value which indicates which frame of sprite image
126-
* should be displayed.
127-
*
128-
* \tparam Derived
129-
* The derived class type.
130-
*/
131-
template <typename Derived>
132-
struct sprite_as_int : receiver<int>
133-
{
134-
void value(int val) override;
135-
int value() const override;
136-
};
137-
138119
/**
139120
* \class sprite_as_double
140121
*
@@ -200,19 +181,8 @@ namespace cycfi::elements
200181
* Sprites are images used as controls. Various frames are laid out in
201182
* a single (big) image but only one frame is drawn at any single time.
202183
* Useful for switches, knobs and basic (sprite) animation.
203-
*
204-
* Note on sprite_as_int and sprite_as_double: The tricky thing about
205-
* sprites is that they can act as both receiver<int> or
206-
* receiver<double> depending on usage. For example, buttons use it as
207-
* a receiver<int> where the int value reflects the current frame
208-
* displayed. On the other hand, dials regard it as a receiver<double>,
209-
* where the value 0.0 to 1.0 reflects its state from 0 to
210-
* num_frames()-1. Alas, we cannot directly inherit from both because
211-
* the overridden value() member function will have an ambiguous return
212-
* type (double or int?). The sprite_as_int and sprite_as_double TMP
213-
* trick solves this dilemma.
214184
*/
215-
struct sprite : basic_sprite, sprite_as_int<sprite>, sprite_as_double<sprite>
185+
struct sprite : basic_sprite, sprite_as_double<sprite>
216186
{
217187
using basic_sprite::basic_sprite;
218188
};
@@ -233,40 +203,6 @@ namespace cycfi::elements
233203
return _index;
234204
}
235205

236-
/**
237-
* \brief
238-
* Returns the index of the currently displayed frame in sprite_as_int.
239-
*
240-
* \tparam Derived
241-
* The derived class type.
242-
*
243-
* \returns
244-
* The index of the currently displayed frame as an integer.
245-
*/
246-
template <typename Derived>
247-
int sprite_as_int<Derived>::value() const
248-
{
249-
auto this_ = static_cast<Derived const*>(this);
250-
return this_->index();
251-
}
252-
253-
/**
254-
* \brief
255-
* Sets the index of the sprite_as_int to the provided value.
256-
*
257-
* \tparam Derived
258-
* The derived class type.
259-
*
260-
* \param val
261-
* The value to set the index to (index of the frame to be displayed).
262-
*/
263-
template <typename Derived>
264-
void sprite_as_int<Derived>::value(int val)
265-
{
266-
auto this_ = static_cast<Derived*>(this);
267-
this_->index(val);
268-
}
269-
270206
/**
271207
* \brief
272208
* Returns the index of the currently displayed frame in

lib/include/elements/element/style.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <elements/element/style/radio_button.hpp>
2424
#include <elements/element/style/slide_switch.hpp>
2525
#include <elements/element/style/slider.hpp>
26+
#include <elements/element/style/sprite_button.hpp>
2627
#include <elements/element/style/tab.hpp>
2728
#include <elements/element/style/text_entry.hpp>
2829
#include <elements/element/style/thumbwheel.hpp>

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ namespace cycfi::elements
2222
* \brief Base class for button styling.
2323
*
2424
* The `button_styler_base` class is responsible for providing the basic
25-
* styling and behavior for buttons. It inherits from `element` and
26-
* `basic_receiver<button_state>`, allowing it to handle button states
27-
* and interact with the user interface.
25+
* styling and behavior for buttons.
2826
*
2927
* This class provides the foundational functionality for button styling,
3028
* including cursor handling and control requirements.
3129
*/
32-
struct button_styler_base : element, basic_receiver<button_state>
30+
struct button_styler_base : element
3331
{
3432
bool cursor(context const& ctx, point p, cursor_tracking status) override;
3533
bool wants_control() const override;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace cycfi::elements
1414
////////////////////////////////////////////////////////////////////////////
1515
// Check Box
1616
////////////////////////////////////////////////////////////////////////////
17-
struct check_box_styler : toggle_selector, basic_receiver<button_state>
17+
struct check_box_styler : toggle_selector
1818
{
1919
using toggle_selector::toggle_selector;
2020

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace cycfi::elements
1515
////////////////////////////////////////////////////////////////////////////
1616
// Icon Buttons
1717
////////////////////////////////////////////////////////////////////////////
18-
struct icon_button_styler_base : element, basic_receiver<button_state>
18+
struct icon_button_styler_base : element
1919
{
2020
icon_button_styler_base(float size)
2121
: _size(size)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace cycfi::elements
1414
////////////////////////////////////////////////////////////////////////////
1515
// Radio Button
1616
////////////////////////////////////////////////////////////////////////////
17-
struct radio_button_styler : toggle_selector, basic_receiver<button_state>
17+
struct radio_button_styler : toggle_selector
1818
{
1919
using toggle_selector::toggle_selector;
2020

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace cycfi::elements
1313
////////////////////////////////////////////////////////////////////////////
1414
// Slide Switch
1515
////////////////////////////////////////////////////////////////////////////
16-
class slide_switch_styler : public element, public basic_receiver<button_state>
16+
class slide_switch_styler : public element
1717
{
1818
public:
1919

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)