Skip to content

Commit

Permalink
Merge branch 'skia_2024'
Browse files Browse the repository at this point in the history
  • Loading branch information
djowel committed Aug 16, 2024
2 parents 567cd70 + 632afd4 commit dd1593f
Show file tree
Hide file tree
Showing 22 changed files with 181 additions and 174 deletions.
4 changes: 2 additions & 2 deletions examples/buttons/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ set(ELEMENTS_APP_VERSION "1.0")
set(ELEMENTS_APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(ELEMENTS_APP_RESOURCES
${CMAKE_CURRENT_SOURCE_DIR}/resources/power_180x632.png
${CMAKE_CURRENT_SOURCE_DIR}/resources/phase_180x632.png
${CMAKE_CURRENT_SOURCE_DIR}/resources/mail_180x632.png
${CMAKE_CURRENT_SOURCE_DIR}/resources/phase_180x790.png
${CMAKE_CURRENT_SOURCE_DIR}/resources/mail_180x790.png
${CMAKE_CURRENT_SOURCE_DIR}/resources/transpo_180x632.png
)

Expand Down
21 changes: 15 additions & 6 deletions examples/buttons/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ void my_custom_button::draw(context const& ctx)
// necessary. For this simple example, we wiill deal only with `value` and
// `hilite`.

auto state = value();
bool value = state.value; // button is on or off
bool hilite = state.hilite; // cursor is hovering over the button
auto btn = find_parent<basic_button*>(ctx);
if (!btn)
return;

bool value = btn->value(); // button is on or off
bool hilite = btn->hilite(); // cursor is hovering over the button
bool enabled = ctx.enabled; // button is enabled or disabled

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

float const button_scale = 1.0/4;
sprite power_button = sprite{"power_180x632.png", 158*button_scale, button_scale};
sprite phase_button = sprite{"phase_180x632.png", 158*button_scale, button_scale};
sprite mail_button = sprite{"mail_180x632.png", 158*button_scale, button_scale};
sprite phase_button = sprite{"phase_180x790.png", 158*button_scale, button_scale};
sprite mail_button = sprite{"mail_180x790.png", 158*button_scale, button_scale};
sprite transpo_button = sprite{"transpo_180x632.png", 158*button_scale, button_scale};

// Note: When disabling a sprite button, the sprite must have a fifth
// frame specifically for the disabled state.
auto phase_disabled = toggle_button(phase_button);
phase_disabled.enable(false);

auto sprite_buttons =
group("Sprite Buttons",
margin({10, 45, 20, 10},
htile(
align_center(toggle_button(power_button)),
align_center(toggle_button(phase_button)),
align_center(momentary_button(mail_button)),
align_center(toggle_button(transpo_button))
align_center(toggle_button(transpo_button)),
align_center(phase_disabled)
)
)
);
Expand Down
Binary file removed examples/buttons/resources/mail_180x632.png
Binary file not shown.
Binary file added examples/buttons/resources/mail_180x790.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed examples/buttons/resources/phase_180x632.png
Binary file not shown.
Binary file added examples/buttons/resources/phase_180x790.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 4 additions & 30 deletions lib/include/elements/element/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ namespace cycfi::elements
* A struct for maintaining and managing the state of a button.
*
* This structure captures the various states that a button can have:
* - `value`: The button's value; 0(off) or 1(on).
* - `hilite`: True if the button is highlighted.
* - `value` : The button's value; 0(off) or 1(on).
* - `hilite` : True if the button is highlighted.
* - `tracking`: True if the mouse button being pressed.
* - `enabled`: True if the button is enabled.
* - `enabled` : True if the button is enabled.
*/
struct button_state
{
Expand All @@ -66,30 +66,6 @@ namespace cycfi::elements
* button's visual presentation. With this pattern, different stylers
* can be implemented for various visual representations, for instance,
* plain buttons, radio buttons, slide switches, checkboxes, and more.
*
* The communication with the button styler is done via the
* `receiver<button_state>` or a `receiver<int>` APIs. These APIs
* provide a means for the `basic_button` to update the button styler
* about changes in button's state to allow the styler to adjust the
* visual representation accordingly.
*
* If the button styler follows a `receiver<int>` API, it will receive
* in integer with these possible values:
*
* 0: value=`false`, hilite=`false`
* 1: value=`false`, hilite=`true`
* 2: value=`true`, hilite=`false`
* 3: value=`true`, hilite=`true`
*
* If the button styler follows a `receiver<button_state>` API, it will
* receive a `button_state` when the button's state changes. This has a
* richer API compared to the former, allowing more nuanced button
* rendering. See `button_state`.
*
* Take note that the button styler is just an element and does not
* have to follow the `receiver` API. If that's the case, then the
* button rendering will be static, and not adjust to state changes.
* This may still be useful in certain cases.
*/
class basic_button : public proxy_base, public receiver<bool>
{
Expand Down Expand Up @@ -117,14 +93,12 @@ namespace cycfi::elements

protected:

bool state(bool val);
bool set_value(bool val);
void tracking(bool val);
void hilite(bool val);

private:

bool update_receiver();

button_state _state;
};

Expand Down
66 changes: 1 addition & 65 deletions lib/include/elements/element/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,6 @@ namespace cycfi::elements
void draw(context const& ctx) override;
};

/**
* \class sprite_as_int
*
* \brief
* A template structure to handle sprite images used as 'int' receivers.
*
* Receives an 'int' value which indicates which frame of sprite image
* should be displayed.
*
* \tparam Derived
* The derived class type.
*/
template <typename Derived>
struct sprite_as_int : receiver<int>
{
void value(int val) override;
int value() const override;
};

/**
* \class sprite_as_double
*
Expand Down Expand Up @@ -200,19 +181,8 @@ namespace cycfi::elements
* Sprites are images used as controls. Various frames are laid out in
* a single (big) image but only one frame is drawn at any single time.
* Useful for switches, knobs and basic (sprite) animation.
*
* Note on sprite_as_int and sprite_as_double: The tricky thing about
* sprites is that they can act as both receiver<int> or
* receiver<double> depending on usage. For example, buttons use it as
* a receiver<int> where the int value reflects the current frame
* displayed. On the other hand, dials regard it as a receiver<double>,
* where the value 0.0 to 1.0 reflects its state from 0 to
* num_frames()-1. Alas, we cannot directly inherit from both because
* the overridden value() member function will have an ambiguous return
* type (double or int?). The sprite_as_int and sprite_as_double TMP
* trick solves this dilemma.
*/
struct sprite : basic_sprite, sprite_as_int<sprite>, sprite_as_double<sprite>
struct sprite : basic_sprite, sprite_as_double<sprite>
{
using basic_sprite::basic_sprite;
};
Expand All @@ -233,40 +203,6 @@ namespace cycfi::elements
return _index;
}

/**
* \brief
* Returns the index of the currently displayed frame in sprite_as_int.
*
* \tparam Derived
* The derived class type.
*
* \returns
* The index of the currently displayed frame as an integer.
*/
template <typename Derived>
int sprite_as_int<Derived>::value() const
{
auto this_ = static_cast<Derived const*>(this);
return this_->index();
}

/**
* \brief
* Sets the index of the sprite_as_int to the provided value.
*
* \tparam Derived
* The derived class type.
*
* \param val
* The value to set the index to (index of the frame to be displayed).
*/
template <typename Derived>
void sprite_as_int<Derived>::value(int val)
{
auto this_ = static_cast<Derived*>(this);
this_->index(val);
}

/**
* \brief
* Returns the index of the currently displayed frame in
Expand Down
1 change: 1 addition & 0 deletions lib/include/elements/element/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <elements/element/style/radio_button.hpp>
#include <elements/element/style/slide_switch.hpp>
#include <elements/element/style/slider.hpp>
#include <elements/element/style/sprite_button.hpp>
#include <elements/element/style/tab.hpp>
#include <elements/element/style/text_entry.hpp>
#include <elements/element/style/thumbwheel.hpp>
Expand Down
6 changes: 2 additions & 4 deletions lib/include/elements/element/style/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ namespace cycfi::elements
* \brief Base class for button styling.
*
* The `button_styler_base` class is responsible for providing the basic
* styling and behavior for buttons. It inherits from `element` and
* `basic_receiver<button_state>`, allowing it to handle button states
* and interact with the user interface.
* styling and behavior for buttons.
*
* This class provides the foundational functionality for button styling,
* including cursor handling and control requirements.
*/
struct button_styler_base : element, basic_receiver<button_state>
struct button_styler_base : element
{
bool cursor(context const& ctx, point p, cursor_tracking status) override;
bool wants_control() const override;
Expand Down
2 changes: 1 addition & 1 deletion lib/include/elements/element/style/check_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace cycfi::elements
////////////////////////////////////////////////////////////////////////////
// Check Box
////////////////////////////////////////////////////////////////////////////
struct check_box_styler : toggle_selector, basic_receiver<button_state>
struct check_box_styler : toggle_selector
{
using toggle_selector::toggle_selector;

Expand Down
2 changes: 1 addition & 1 deletion lib/include/elements/element/style/icon_button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace cycfi::elements
////////////////////////////////////////////////////////////////////////////
// Icon Buttons
////////////////////////////////////////////////////////////////////////////
struct icon_button_styler_base : element, basic_receiver<button_state>
struct icon_button_styler_base : element
{
icon_button_styler_base(float size)
: _size(size)
Expand Down
2 changes: 1 addition & 1 deletion lib/include/elements/element/style/radio_button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace cycfi::elements
////////////////////////////////////////////////////////////////////////////
// Radio Button
////////////////////////////////////////////////////////////////////////////
struct radio_button_styler : toggle_selector, basic_receiver<button_state>
struct radio_button_styler : toggle_selector
{
using toggle_selector::toggle_selector;

Expand Down
2 changes: 1 addition & 1 deletion lib/include/elements/element/style/slide_switch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace cycfi::elements
////////////////////////////////////////////////////////////////////////////
// Slide Switch
////////////////////////////////////////////////////////////////////////////
class slide_switch_styler : public element, public basic_receiver<button_state>
class slide_switch_styler : public element
{
public:

Expand Down
87 changes: 87 additions & 0 deletions lib/include/elements/element/style/sprite_button.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*=============================================================================
Copyright (c) 2016-2024 Joel de Guzman
Distributed under the MIT License [ https://opensource.org/licenses/MIT ]
=============================================================================*/
#if !defined(ELEMENTS_SPRITE_BUTTON_AUGUST_16_2024)
#define ELEMENTS_SPRITE_BUTTON_AUGUST_16_2024

#include <elements/element/button.hpp>
#include <elements/element/image.hpp>

namespace cycfi::elements
{
class sprite_button_styler : public sprite
{
public:
sprite_button_styler(sprite const& s);

void draw(context const& ctx) override;
};

template <concepts::MomentaryButton Base = basic_button>
inline proxy<sprite_button_styler, Base>
momentary_button(sprite const& s)
{
return {sprite_button_styler{s}};
}

template <concepts::MomentaryButton Base = basic_button>
inline proxy<sprite_button_styler, Base>
momentary_button(sprite& s)
{
return {sprite_button_styler{s}};
}

template <concepts::ToggleButton Base = basic_toggle_button>
inline proxy<sprite_button_styler, Base>
toggle_button(sprite const& s)
{
return {sprite_button_styler{s}};
}

template <concepts::ToggleButton Base = basic_toggle_button>
inline proxy<sprite_button_styler, Base>
toggle_button(sprite& s)
{
return {sprite_button_styler{s}};
}

template <concepts::LatchingButton Base = basic_latching_button>
inline proxy<sprite_button_styler, Base>
latching_button(sprite const& s)
{
return {sprite_button_styler{s}};
}

template <concepts::LatchingButton Base = basic_latching_button>
inline proxy<sprite_button_styler, Base>
latching_button(sprite& s)
{
return {sprite_button_styler{s}};
}

//--------------------------------------------------------------------------
// Inlines
//--------------------------------------------------------------------------
inline sprite_button_styler::sprite_button_styler(sprite const& s)
: sprite(s)
{}

inline void sprite_button_styler::draw(context const& ctx)
{
auto btn = find_parent<basic_button*>(ctx);
if (!btn)
return;

auto value = btn->value();
auto hilite = btn->hilite();
if (!ctx.enabled && num_frames() > 4)
index(4); // disabled
else
index((value? 2 : 0) + hilite); // enabled
basic_sprite::draw(ctx);
}
}

#endif
Loading

0 comments on commit dd1593f

Please sign in to comment.