From a7d5148c8b3213a0276833c4f2ada9e6423653ac Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 9 Jun 2022 15:02:58 +0200 Subject: [PATCH] Implemented scrolling --- CMakeLists.txt | 1 + src/menudisplay.cpp | 31 +++++++++++++++------- src/menudisplay.h | 16 ++++++++++- src/selectorscrollinterface.h | 50 +++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/selectorscrollinterface.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 96fac73..e04ed1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ set(headers src/richtextrenderer.h src/screenmanager.h src/scrollinterface.h + src/selectorscrollinterface.h src/splitgraphdisplay.h src/textinterface.h src/textwithvaluehelper.h diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index c706ce3..62f458a 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -95,16 +95,25 @@ void MenuDisplay::redraw() { Base::redraw(); - if (m_upHoldingSince && espchrono::ago(*m_upHoldingSince) > 500ms) + // wait for holding since to be above initial scroll speed, then start scrolling in interval set by getScrollSpeed() + if (m_upHoldingSince && (espchrono::ago(*m_upHoldingSince) > getInitialScrollSpeed() || m_upScrolling)) { - m_upHoldingSince = espchrono::millis_clock::now(); - m_rotateOffset--; + if (espchrono::ago(*m_upHoldingSince) > getScrollSpeed()) + { + m_upScrolling = true; + m_rotateOffset--; + m_upHoldingSince = espchrono::millis_clock::now(); + } } - if (m_downHoldingSince && espchrono::ago(*m_downHoldingSince) > 500ms) + if (m_downHoldingSince && (espchrono::ago(*m_downHoldingSince) > getInitialScrollSpeed() || m_downScrolling)) { - m_downHoldingSince = espchrono::millis_clock::now(); - m_rotateOffset++; + if (espchrono::ago(*m_downHoldingSince) > getScrollSpeed()) + { + m_downScrolling = true; + m_rotateOffset++; + m_downHoldingSince = espchrono::millis_clock::now(); + } } tft.setTextFont(4); @@ -209,10 +218,10 @@ void MenuDisplay::buttonPressed(Button button) switch (button) { - case Button::Left: this->back(); break; - case Button::Right: m_pressed = true; break; - case Button::Up: m_rotateOffset--; m_upHoldingSince = espchrono::millis_clock::now(); break; - case Button::Down: m_rotateOffset++; m_downHoldingSince = espchrono::millis_clock::now(); break; + case Button::Left: this->back(); m_leftHoldingSince = espchrono::millis_clock::now(); m_leftScrolling = false; break; + case Button::Right: m_pressed = true; m_rightHoldingSince = espchrono::millis_clock::now(); m_rightScrolling = false; break; + case Button::Up: m_rotateOffset--; m_upHoldingSince = espchrono::millis_clock::now(); m_upScrolling = false; break; + case Button::Down: m_rotateOffset++; m_downHoldingSince = espchrono::millis_clock::now(); m_downScrolling = false; break; } } @@ -224,6 +233,8 @@ void MenuDisplay::buttonReleased(Button button) { case Button::Up: m_upHoldingSince = std::nullopt; break; case Button::Down: m_downHoldingSince = std::nullopt; break; + case Button::Left: m_leftHoldingSince = std::nullopt; break; + case Button::Right: m_rightHoldingSince = std::nullopt; break; default:; } } diff --git a/src/menudisplay.h b/src/menudisplay.h index deeb7b4..dcf42a7 100644 --- a/src/menudisplay.h +++ b/src/menudisplay.h @@ -9,19 +9,24 @@ #include #include #include + +// 3rdparty lib includes #include // local includes #include "displaywithtitle.h" +#include "selectorscrollinterface.h" #include "textinterface.h" #include "widgets/label.h" #include "menuitem.h" #include "backinterface.h" namespace espgui { + using namespace std::chrono_literals; class MenuDisplay : public DisplayWithTitle, - public virtual BackInterface + public virtual BackInterface, + public virtual SelectorScrollInterface<> { using Base = DisplayWithTitle; @@ -149,7 +154,16 @@ private: std::vector> m_menuItems; + bool m_upScrolling; std::optional m_upHoldingSince; + + bool m_downScrolling{}; std::optional m_downHoldingSince; + + bool m_leftScrolling{}; + std::optional m_leftHoldingSince; + + bool m_rightScrolling{}; + std::optional m_rightHoldingSince; }; } // namespace espgui diff --git a/src/selectorscrollinterface.h b/src/selectorscrollinterface.h new file mode 100644 index 0000000..91e9908 --- /dev/null +++ b/src/selectorscrollinterface.h @@ -0,0 +1,50 @@ +#pragma once + +// 3rdparty lib includes +#include + +namespace espgui { +namespace { +template +struct is_allowed +{ + static constexpr bool value = false; +}; + +template <> +struct is_allowed +{ + static constexpr bool value = true; +}; +template <> +struct is_allowed +{ + static constexpr bool value = true; +}; +template <> +struct is_allowed +{ + static constexpr bool value = true; +}; +template <> +struct is_allowed +{ + static constexpr bool value = true; +}; +} // namespace + +template +class SelectorScrollInterface { + static_assert(is_allowed::value, "Type not supported"); +public: + TimeType getInitialScrollSpeed() const { return initialScrollDelay; } + TimeType getScrollSpeed() const { return scrollDelay; } + + void setInitialScrollSpeed(TimeType speed) { initialScrollDelay = speed; } + void setScrollSpeed(TimeType speed) { scrollDelay = speed; } + +private: + TimeType initialScrollDelay{initialDelay}; + TimeType scrollDelay{delay}; +}; +} // namespace espgui