From c89a314062f6115a254c504b8a417fb81678e25c Mon Sep 17 00:00:00 2001 From: forntoh Date: Mon, 21 Oct 2024 20:28:19 +0200 Subject: [PATCH] Update ItemWidget and WidgetRange creation functions - 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 --- src/ItemWidget.h | 27 +++++++++++----------- src/widget/WidgetRange.h | 48 ++++++++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/ItemWidget.h b/src/ItemWidget.h index 74fd14b7..a659daa0 100644 --- a/src/ItemWidget.h +++ b/src/ItemWidget.h @@ -19,8 +19,10 @@ struct make_index_sequence<0, Is...> { // Updated ItemWidget class supporting zero or more widgets template class ItemWidget : public BaseItemManyWidgets { - protected: + public: using CallbackType = void (*)(Ts...); + + protected: CallbackType callback = nullptr; void handleCommit() override { @@ -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(text, widget0, widget1, widget2, callback) - -#define ITEM_2_WIDGET(text, widget0, widget1, callback, type0, type1) \ - new ItemWidget(text, widget0, widget1, callback) - -#define ITEM_1_WIDGET(text, widget, callback, type) \ - new ItemWidget(text, widget, callback) +template +/** + * @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::CallbackType callback, BaseWidgetValue*... widgetPtrs) { + return new ItemWidget(text, widgetPtrs..., callback); +} #endif // ITEM_WIDGET_H \ No newline at end of file diff --git a/src/widget/WidgetRange.h b/src/widget/WidgetRange.h index 0d2ec6cd..3a82fc8e 100644 --- a/src/widget/WidgetRange.h +++ b/src/widget/WidgetRange.h @@ -71,45 +71,75 @@ class WidgetRange : public Widget { }; /** - * @brief Macro to create a new WidgetRange instance. + * @brief Function to create a new WidgetRange 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(__VA_ARGS__)) +inline Widget* 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(value, step, min, max, format, cursorOffset, cycle, callback); +} /** - * @brief Macro to create a new WidgetRange instance. + * @brief Function to create a new WidgetRange 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(__VA_ARGS__)) +inline Widget* 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(value, step, min, max, format, cursorOffset, cycle, callback); +} /** - * @brief Macro to create a new WidgetRange instance. + * @brief Function to create a new WidgetRange 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(__VA_ARGS__)) +inline Widget* 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(value, step, min, max, format, cursorOffset, cycle, callback); +} #endif \ No newline at end of file