From 514f9664357506a9e628920790e18c4c00931040 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Wed, 13 May 2020 11:49:12 +0200 Subject: [PATCH] Choice and Roller updates --- radio/src/thirdparty/libopenui/src/choice.h | 36 +++++++++++++++++++++ radio/src/thirdparty/libopenui/src/roller.h | 23 +++++++------ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/radio/src/thirdparty/libopenui/src/choice.h b/radio/src/thirdparty/libopenui/src/choice.h index 3141868a9..14da958d6 100644 --- a/radio/src/thirdparty/libopenui/src/choice.h +++ b/radio/src/thirdparty/libopenui/src/choice.h @@ -87,6 +87,42 @@ class Choice : public ChoiceBase { isValueAvailable = std::move(handler); } + unsigned getIndexFromValue(int value) + { + if (!isValueAvailable) { + return value - vmin; + } + + unsigned index = 0; + for (int i = vmin; i < value; i++) { + if (isValueAvailable(i)) { + index++; + } + } + return index; + } + + int getValueFromIndex(unsigned index) + { + if (!isValueAvailable) { + return vmin + index; + } + + int value = vmin; + while (index > 0) { + while (value < vmax && !isValueAvailable(value)) { + value++; + } + index--; + } + return value; + } + + int getValuesCount() + { + return getIndexFromValue(vmax + 1); + } + void setTextHandler(std::function handler) { textHandler = std::move(handler); diff --git a/radio/src/thirdparty/libopenui/src/roller.h b/radio/src/thirdparty/libopenui/src/roller.h index 9d66a2f78..bd7199c2f 100644 --- a/radio/src/thirdparty/libopenui/src/roller.h +++ b/radio/src/thirdparty/libopenui/src/roller.h @@ -53,8 +53,7 @@ class Roller: public Choice { setHeight(ROLLER_LINE_HEIGHT * 3 - 1); setPageHeight(ROLLER_LINE_HEIGHT); setInnerHeight(INFINITE_HEIGHT); - setScrollPositionY(ROLLER_LINE_HEIGHT * (this->getValue() - vmin - 1)); - lastScrollPositionY = scrollPositionY; + updateScrollPositionFromValue(); } #if defined(DEBUG_WINDOWS) @@ -73,9 +72,9 @@ class Roller: public Choice { coord_t yMax = scrollPositionY + 3 * ROLLER_LINE_HEIGHT; while (y < yMax) { - auto displayedValue = vmin + index % (vmax - vmin + 1); + auto displayedValue = getValueFromIndex(index % getValuesCount()); if (displayedValue < vmin) - displayedValue += vmax - vmin + 1; + displayedValue += getValuesCount(); auto fgColor = DISABLE_COLOR; @@ -104,8 +103,7 @@ class Roller: public Choice { Window::checkEvents(); if (lastEditMode != editMode) { - setScrollPositionY(ROLLER_LINE_HEIGHT * (this->getValue() - vmin - 1)); - lastScrollPositionY = scrollPositionY; + updateScrollPositionFromValue(); lastEditMode = editMode; } @@ -122,13 +120,20 @@ class Roller: public Choice { coord_t lastScrollPositionY = 0; bool lastEditMode = false; + void updateScrollPositionFromValue() + { + setScrollPositionY(ROLLER_LINE_HEIGHT * (getIndexFromValue(this->getValue()) - 1)); + lastScrollPositionY = scrollPositionY; + } + void updateValueFromScrollPosition() { lastScrollPositionY = scrollPositionY; - auto newValue = vmin + ((scrollPositionY / ROLLER_LINE_HEIGHT) + 1) % (vmax - vmin + 1); - if (newValue < vmin) - newValue += vmax - vmin + 1; + auto newValue = getValueFromIndex(((scrollPositionY / ROLLER_LINE_HEIGHT) + 1) % getValuesCount()); + if (newValue < vmin) { + newValue += getValuesCount(); + } setValue(newValue); invalidate();