Skip to content

Commit

Permalink
Update ItemWidget and WidgetRange creation functions
Browse files Browse the repository at this point in the history
- Refactored ItemWidget class to support zero or more widgets
- Changed access specifier for CallbackType in ItemWidget
- Added a new function makeItemWidget to create an ItemWidget object with specified parameters
- Converted macros for creating WidgetRange instances into inline functions with default values and callbacks
  • Loading branch information
forntoh committed Oct 21, 2024
1 parent 99e83c8 commit c89a314
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
27 changes: 14 additions & 13 deletions src/ItemWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ struct make_index_sequence<0, Is...> {
// Updated ItemWidget class supporting zero or more widgets
template <typename... Ts>
class ItemWidget : public BaseItemManyWidgets {
protected:
public:
using CallbackType = void (*)(Ts...);

protected:
CallbackType callback = nullptr;

void handleCommit() override {
Expand Down Expand Up @@ -58,17 +60,16 @@ class ItemWidget : public BaseItemManyWidgets {
}
};

// ############################################################################
// # Shortcuts for creating new ItemWidget instances
// ############################################################################

#define ITEM_3_WIDGET(text, widget0, widget1, widget2, callback, type0, type1, type2) \
new ItemWidget<type0, type1, type2>(text, widget0, widget1, widget2, callback)

#define ITEM_2_WIDGET(text, widget0, widget1, callback, type0, type1) \
new ItemWidget<type0, type1>(text, widget0, widget1, callback)

#define ITEM_1_WIDGET(text, widget, callback, type) \
new ItemWidget<type>(text, widget, callback)
template <typename... Ts>
/**
* @brief Create an ItemWidget object
*
* @param text the text of the item
* @param callback reference to callback function to call when the value of the item is changed
* @param widgetPtrs pointers to the widgets associated with this item
*/
MenuItem* ITEM_WIDGET(const char* text, typename ItemWidget<Ts...>::CallbackType callback, BaseWidgetValue<Ts>*... widgetPtrs) {
return new ItemWidget<Ts...>(text, widgetPtrs..., callback);
}

#endif // ITEM_WIDGET_H
48 changes: 39 additions & 9 deletions src/widget/WidgetRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,75 @@ class WidgetRange : public Widget<T> {
};

/**
* @brief Macro to create a new WidgetRange<float> instance.
* @brief Function to create a new WidgetRange<float> instance.
*
* @param value The initial value of the widget.
* @param step The step value for incrementing/decrementing.
* @param min The minimum value of the range.
* @param max The maximum value of the range.
* @param format The format string for displaying the value.
* @param format The format string for displaying the value (default is "%2.1f").
* @param cursorOffset The offset for the cursor (default is 0).
* @param cycle Whether the value should cycle when out of range (default is false).
* @param callback The callback function to call when the value changes (default is nullptr).
*/
#define WIDGET_FLOAT_RANGE(...) (new WidgetRange<float>(__VA_ARGS__))
inline Widget<float>* WIDGET_FLOAT_RANGE(
float value,
float step,
float min,
float max,
const char* format = "%2.1f",
uint8_t cursorOffset = 0,
bool cycle = false,
void (*callback)(float) = nullptr) {
return new WidgetRange<float>(value, step, min, max, format, cursorOffset, cycle, callback);
}

/**
* @brief Macro to create a new WidgetRange<int> instance.
* @brief Function to create a new WidgetRange<int> instance.
*
* @param value The initial value of the widget.
* @param step The step value for incrementing/decrementing.
* @param min The minimum value of the range.
* @param max The maximum value of the range.
* @param format The format string for displaying the value.
* @param format The format string for displaying the value (default is "%d").
* @param cursorOffset The offset for the cursor (default is 0).
* @param cycle Whether the value should cycle when out of range (default is false).
* @param callback The callback function to call when the value changes (default is nullptr).
*/
#define WIDGET_INT_RANGE(...) (new WidgetRange<int>(__VA_ARGS__))
inline Widget<int>* WIDGET_INT_RANGE(
int value,
int step,
int min,
int max,
const char* format = "%d",
uint8_t cursorOffset = 0,
bool cycle = false,
void (*callback)(int) = nullptr) {
return new WidgetRange<int>(value, step, min, max, format, cursorOffset, cycle, callback);
}

/**
* @brief Macro to create a new WidgetRange<uint8_t> instance.
* @brief Function to create a new WidgetRange<uint8_t> instance.
*
* @param value The initial value of the widget.
* @param step The step value for incrementing/decrementing.
* @param min The minimum value of the range.
* @param max The maximum value of the range.
* @param format The format string for displaying the value.
* @param format The format string for displaying the value (default is "%d").
* @param cursorOffset The offset for the cursor (default is 0).
* @param cycle Whether the value should cycle when out of range (default is false).
* @param callback The callback function to call when the value changes (default is nullptr).
*/
#define WIDGET_UINT8_RANGE(...) (new WidgetRange<uint8_t>(__VA_ARGS__))
inline Widget<uint8_t>* WIDGET_UINT8_RANGE(
uint8_t value,
uint8_t step,
uint8_t min,
uint8_t max,
const char* format = "%d",
uint8_t cursorOffset = 0,
bool cycle = false,
void (*callback)(uint8_t) = nullptr) {
return new WidgetRange<uint8_t>(value, step, min, max, format, cursorOffset, cycle, callback);
}

#endif

0 comments on commit c89a314

Please sign in to comment.