From 5f889d4d737932d87558c3ee80724c2896bdd6ef Mon Sep 17 00:00:00 2001 From: Florian Wetzel Date: Tue, 25 Feb 2025 14:48:40 +0100 Subject: [PATCH 1/4] Fix scrolling / skipScroll --- src/menudisplay.cpp | 23 +++++++---------------- src/menudisplay.h | 26 ++++++++++++++++++++++++++ src/scrollinterface.h | 5 ++++- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index 72f7f00..79259a8 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -59,20 +59,23 @@ void MenuDisplay::update() const auto offset = m_rotateOffset; m_rotateOffset = 0; - const auto itemCount = menuItemCount(); - - if (itemCount) + if (const auto itemCount = menuItemCount()) { if (m_selectedIndex == -1) m_selectedIndex = 0; - m_selectedIndex = m_selectedIndex + offset; + m_selectedIndex += offset; if (m_selectedIndex < 0) m_selectedIndex += itemCount; if (m_selectedIndex >= itemCount) m_selectedIndex -= itemCount; + if (getMenuItem(m_selectedIndex).skipScroll()) + { + m_selectedIndex = offset > 0 ? getNextAccessibleMenuItemIndex(m_selectedIndex) : getPreviousAccessibleMenuItemIndex(m_selectedIndex); + } + if (m_selectedIndex < m_scrollOffset) m_scrollOffset = m_selectedIndex; if (m_selectedIndex >= m_scrollOffset + m_labels.size()) @@ -87,18 +90,6 @@ void MenuDisplay::update() runForEveryMenuItem([&](MenuItem &item){ item.update(); }); - - if (m_selectedIndex >= 0 && m_selectedIndex < m_menuItems.size() && getMenuItem(m_selectedIndex).skipScroll()) - { - if (offset > 0) - { - m_rotateOffset++; - } - else if (offset < 0) - { - m_rotateOffset--; - } - } } if (m_pressed) diff --git a/src/menudisplay.h b/src/menudisplay.h index 35c2800..368bb07 100644 --- a/src/menudisplay.h +++ b/src/menudisplay.h @@ -56,6 +56,32 @@ public: return *m_menuItems[index].get(); } + int getNextAccessibleMenuItemIndex(int index) const + { + for (std::size_t i = index + 1; i < m_menuItems.size(); ++i) + if (!m_menuItems[i]->skipScroll()) + return i; + + for (std::size_t i = 0; i < index; ++i) + if (!m_menuItems[i]->skipScroll()) + return i; + + return -1; + } + + int getPreviousAccessibleMenuItemIndex(int index) const + { + for (std::size_t i = index - 1; i < m_menuItems.size(); --i) + if (!m_menuItems[i]->skipScroll()) + return i; + + for (std::size_t i = m_menuItems.size() - 1; i > index; --i) + if (!m_menuItems[i]->skipScroll()) + return i; + + return -1; + } + void runForEveryMenuItem(std::function &&callback) { for (const auto &ptr : m_menuItems) diff --git a/src/scrollinterface.h b/src/scrollinterface.h index dc0382f..0699745 100644 --- a/src/scrollinterface.h +++ b/src/scrollinterface.h @@ -7,8 +7,11 @@ public: }; template -class StaticScrollBehaviour : public ScrollInterface { +class StaticScrollBehaviour : public virtual ScrollInterface { public: bool skipScroll() const override { return TScroll; } }; + +using SkipScroll = StaticScrollBehaviour; + } // namespace espgui -- 2.50.1 From 20e359a1b9ac774acc084cd4e284d23ad64dd9af Mon Sep 17 00:00:00 2001 From: Florian Wetzel Date: Tue, 25 Feb 2025 14:49:01 +0100 Subject: [PATCH 2/4] Add ability to change the color --- src/widgets/progressbar.cpp | 18 ++++++++++++++++++ src/widgets/progressbar.h | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/widgets/progressbar.cpp b/src/widgets/progressbar.cpp index d2fca4f..bdb8ee6 100644 --- a/src/widgets/progressbar.cpp +++ b/src/widgets/progressbar.cpp @@ -31,4 +31,22 @@ void ProgressBar::redraw(TftInterface &tft, int value) m_lastValue = value; } + +void ProgressBar::changeColor(TftInterface &tft, const uint32_t color, const uint32_t backgroundColor) +{ + if (color != m_color) + { + // redraw already drawn area in new color + tft.fillRect(m_x+1, m_y+1, m_lastValue-m_x-1, m_height-2, color); + m_color = color; + } + + if (backgroundColor != m_backgroundColor) + { + // redraw background in new color + tft.fillRect(m_x+1, m_y+1, m_width-2, m_height-2, backgroundColor); + m_backgroundColor = backgroundColor; + } +} + } // namespace espgui diff --git a/src/widgets/progressbar.h b/src/widgets/progressbar.h index 009046e..aa02960 100644 --- a/src/widgets/progressbar.h +++ b/src/widgets/progressbar.h @@ -21,6 +21,8 @@ public: void start(TftInterface &tft); void redraw(TftInterface &tft, int value); + void changeColor(TftInterface &tft, const uint32_t color = TFT_YELLOW, const uint32_t backgroundColor = TFT_BLACK); + private: const int m_x; const int m_y; @@ -28,8 +30,8 @@ private: const int m_height; const int m_min; const int m_max; - const uint32_t m_color; - const uint32_t m_backgroundColor; + uint32_t m_color; + uint32_t m_backgroundColor; int m_lastValue{}; }; -- 2.50.1 From 3438b20b58c6121a30a3e4036df13f0efe6dea05 Mon Sep 17 00:00:00 2001 From: Florian Wetzel Date: Tue, 25 Feb 2025 15:12:01 +0100 Subject: [PATCH 3/4] Improve handling for initial cursor position --- src/menudisplay.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index 79259a8..608d9a6 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -73,7 +73,9 @@ void MenuDisplay::update() if (getMenuItem(m_selectedIndex).skipScroll()) { - m_selectedIndex = offset > 0 ? getNextAccessibleMenuItemIndex(m_selectedIndex) : getPreviousAccessibleMenuItemIndex(m_selectedIndex); + m_selectedIndex = offset >= 0 ? + getNextAccessibleMenuItemIndex(m_selectedIndex) : + getPreviousAccessibleMenuItemIndex(m_selectedIndex); } if (m_selectedIndex < m_scrollOffset) @@ -162,7 +164,7 @@ void MenuDisplay::redraw(TftInterface &tft) drawItemRect(*labelsIter, TFT_BLACK); *iconsIter = nullptr; labelsIter->start(tft); - } + } labelsIter->redraw(tft, item.text(), item.color(), selected ? TFT_GREY : TFT_BLACK, item.font()); -- 2.50.1 From 6074189811289a51b3d944988f1fa3bd044f0b12 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Wed, 9 Apr 2025 23:29:55 +0200 Subject: [PATCH 4/4] Configure background color via kconfig --- Kconfig.projbuild | 4 ++++ src/menudisplay.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 7e989b9..a8da8a6 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -4,4 +4,8 @@ config ESPGUI_MENUDISPLAY_ROWS int "Number of rows for MenuDisplays" default 10 +config ESPGUI_MENUITEM_BACKGROUND_COLOR + hex "MenuItem background color" + default 0x5AEB + endmenu diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index 72f7f00..e98b0d1 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -156,7 +156,7 @@ void MenuDisplay::redraw(TftInterface &tft) if (relativeIndex != m_highlightedIndex) { - drawItemRect(*labelsIter, TFT_GREY); + drawItemRect(*labelsIter, CONFIG_ESPGUI_MENUITEM_BACKGROUND_COLOR); *iconsIter = nullptr; labelsIter->start(tft); @@ -173,20 +173,20 @@ void MenuDisplay::redraw(TftInterface &tft) labelsIter->start(tft); } - labelsIter->redraw(tft, item.text(), item.color(), selected ? TFT_GREY : TFT_BLACK, item.font()); + labelsIter->redraw(tft, item.text(), item.color(), selected ? CONFIG_ESPGUI_MENUITEM_BACKGROUND_COLOR : TFT_BLACK, item.font()); if (const auto icon = item.icon(selected); icon != *iconsIter) { if (icon) tft.pushImage(6, labelsIter->y() + 1, *icon); else if (*iconsIter) - tft.fillRect(6, labelsIter->y() + 1, 24, 24, selected ? TFT_GREY : TFT_BLACK); + tft.fillRect(6, labelsIter->y() + 1, 24, 24, selected ? CONFIG_ESPGUI_MENUITEM_BACKGROUND_COLOR : TFT_BLACK); *iconsIter = icon; } // if (selected && (relativeIndex != m_highlightedIndex)) // { -// drawItemRect(*labelsIter, TFT_GREY); +// drawItemRect(*labelsIter, CONFIG_ESPGUI_MENUITEM_BACKGROUND_COLOR); // } labelsIter++; -- 2.50.1