feat: ButtonNavigator::onPressAndContinuous helper function

This commit is contained in:
Istiak Tridip 2026-01-29 02:33:56 +06:00
parent a6b4ed5cf9
commit fe119239ad
No known key found for this signature in database
3 changed files with 22 additions and 34 deletions

View File

@ -139,23 +139,23 @@ void KeyboardEntryActivity::handleKeyPress() {
void KeyboardEntryActivity::loop() { void KeyboardEntryActivity::loop() {
// Handle navigation // Handle navigation
const auto upButtonCallback = [this] { buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Up}, [this] {
selectedRow = ButtonNavigator::previousIndex(selectedRow, NUM_ROWS); selectedRow = ButtonNavigator::previousIndex(selectedRow, NUM_ROWS);
const int maxCol = getRowLength(selectedRow) - 1; const int maxCol = getRowLength(selectedRow) - 1;
if (selectedCol > maxCol) selectedCol = maxCol; if (selectedCol > maxCol) selectedCol = maxCol;
updateRequired = true; updateRequired = true;
}; });
const auto downButtonCallback = [this] { buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Down}, [this] {
selectedRow = ButtonNavigator::nextIndex(selectedRow, NUM_ROWS); selectedRow = ButtonNavigator::nextIndex(selectedRow, NUM_ROWS);
const int maxCol = getRowLength(selectedRow) - 1; const int maxCol = getRowLength(selectedRow) - 1;
if (selectedCol > maxCol) selectedCol = maxCol; if (selectedCol > maxCol) selectedCol = maxCol;
updateRequired = true; updateRequired = true;
}; });
const auto leftButtonCallback = [this] { buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Left}, [this] {
const int maxCol = getRowLength(selectedRow) - 1; const int maxCol = getRowLength(selectedRow) - 1;
// Special bottom row case // Special bottom row case
@ -179,9 +179,9 @@ void KeyboardEntryActivity::loop() {
} }
updateRequired = true; updateRequired = true;
}; });
const auto rightButtonCallback = [this] { buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Right}, [this] {
const int maxCol = getRowLength(selectedRow) - 1; const int maxCol = getRowLength(selectedRow) - 1;
// Special bottom row case // Special bottom row case
@ -204,24 +204,7 @@ void KeyboardEntryActivity::loop() {
selectedCol = ButtonNavigator::nextIndex(selectedCol, maxCol + 1); selectedCol = ButtonNavigator::nextIndex(selectedCol, maxCol + 1);
} }
updateRequired = true; updateRequired = true;
}; });
constexpr auto upButton = MappedInputManager::Button::Up;
constexpr auto downButton = MappedInputManager::Button::Down;
constexpr auto leftButton = MappedInputManager::Button::Left;
constexpr auto rightButton = MappedInputManager::Button::Right;
buttonNavigator.onPress({upButton}, upButtonCallback);
buttonNavigator.onContinuous({upButton}, upButtonCallback);
buttonNavigator.onPress({downButton}, downButtonCallback);
buttonNavigator.onContinuous({downButton}, downButtonCallback);
buttonNavigator.onPress({leftButton}, leftButtonCallback);
buttonNavigator.onContinuous({leftButton}, leftButtonCallback);
buttonNavigator.onPress({rightButton}, rightButtonCallback);
buttonNavigator.onContinuous({rightButton}, rightButtonCallback);
// Selection // Selection
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) { if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {

View File

@ -6,11 +6,17 @@ void ButtonNavigator::onNext(const Callback& callback) {
onNextPress(callback); onNextPress(callback);
onNextContinuous(callback); onNextContinuous(callback);
} }
void ButtonNavigator::onPrevious(const Callback& callback) { void ButtonNavigator::onPrevious(const Callback& callback) {
onPreviousPress(callback); onPreviousPress(callback);
onPreviousContinuous(callback); onPreviousContinuous(callback);
} }
void ButtonNavigator::onPressAndContinuous(const Buttons& buttons, const Callback& callback) {
onPress(buttons, callback);
onContinuous(buttons, callback);
}
void ButtonNavigator::onNextPress(const Callback& callback) { onPress(getNextButtons(), callback); } void ButtonNavigator::onNextPress(const Callback& callback) { onPress(getNextButtons(), callback); }
void ButtonNavigator::onPreviousPress(const Callback& callback) { onPress(getPreviousButtons(), callback); } void ButtonNavigator::onPreviousPress(const Callback& callback) { onPress(getPreviousButtons(), callback); }

View File

@ -12,19 +12,10 @@ class ButtonNavigator final {
const uint16_t continuousStartMs; const uint16_t continuousStartMs;
const uint16_t continuousIntervalMs; const uint16_t continuousIntervalMs;
uint32_t lastContinuousNavTime = 0; uint32_t lastContinuousNavTime = 0;
bool continuousNavHold = false;
static const MappedInputManager* mappedInput; static const MappedInputManager* mappedInput;
[[nodiscard]] bool shouldNavigateContinuously() const; [[nodiscard]] bool shouldNavigateContinuously() const;
[[nodiscard]] static Buttons getNextButtons() {
return {MappedInputManager::Button::Down, MappedInputManager::Button::Right};
}
[[nodiscard]] static Buttons getPreviousButtons() {
return {MappedInputManager::Button::Up, MappedInputManager::Button::Left};
}
public: public:
explicit ButtonNavigator(const uint16_t continuousIntervalMs = 500, const uint16_t continuousStartMs = 500) explicit ButtonNavigator(const uint16_t continuousIntervalMs = 500, const uint16_t continuousStartMs = 500)
: continuousStartMs(continuousStartMs), continuousIntervalMs(continuousIntervalMs) {} : continuousStartMs(continuousStartMs), continuousIntervalMs(continuousIntervalMs) {}
@ -33,6 +24,7 @@ class ButtonNavigator final {
void onNext(const Callback& callback); void onNext(const Callback& callback);
void onPrevious(const Callback& callback); void onPrevious(const Callback& callback);
void onPressAndContinuous(const Buttons& buttons, const Callback& callback);
void onNextPress(const Callback& callback); void onNextPress(const Callback& callback);
void onPreviousPress(const Callback& callback); void onPreviousPress(const Callback& callback);
@ -51,4 +43,11 @@ class ButtonNavigator final {
[[nodiscard]] static int nextPageIndex(int currentIndex, int totalItems, int itemsPerPage); [[nodiscard]] static int nextPageIndex(int currentIndex, int totalItems, int itemsPerPage);
[[nodiscard]] static int previousPageIndex(int currentIndex, int totalItems, int itemsPerPage); [[nodiscard]] static int previousPageIndex(int currentIndex, int totalItems, int itemsPerPage);
[[nodiscard]] static Buttons getNextButtons() {
return {MappedInputManager::Button::Down, MappedInputManager::Button::Right};
}
[[nodiscard]] static Buttons getPreviousButtons() {
return {MappedInputManager::Button::Up, MappedInputManager::Button::Left};
}
}; };