mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 14:47:37 +03:00
fix: button navigator triggers
This commit is contained in:
parent
73c5f05843
commit
b7b36f02e2
@ -15,9 +15,9 @@ void ButtonNavigator::onNextPress(const Callback& callback) { onPress(getNextBut
|
|||||||
|
|
||||||
void ButtonNavigator::onPreviousPress(const Callback& callback) { onPress(getPreviousButtons(), callback); }
|
void ButtonNavigator::onPreviousPress(const Callback& callback) { onPress(getPreviousButtons(), callback); }
|
||||||
|
|
||||||
void ButtonNavigator::onNextRelease(const Callback& callback) const { onRelease(getNextButtons(), callback); }
|
void ButtonNavigator::onNextRelease(const Callback& callback) { onRelease(getNextButtons(), callback); }
|
||||||
|
|
||||||
void ButtonNavigator::onPreviousRelease(const Callback& callback) const { onRelease(getPreviousButtons(), callback); }
|
void ButtonNavigator::onPreviousRelease(const Callback& callback) { onRelease(getPreviousButtons(), callback); }
|
||||||
|
|
||||||
void ButtonNavigator::onNextContinuous(const Callback& callback) { onContinuous(getNextButtons(), callback); }
|
void ButtonNavigator::onNextContinuous(const Callback& callback) { onContinuous(getNextButtons(), callback); }
|
||||||
|
|
||||||
@ -26,49 +26,39 @@ void ButtonNavigator::onPreviousContinuous(const Callback& callback) { onContinu
|
|||||||
void ButtonNavigator::onPress(const Buttons& buttons, const Callback& callback) {
|
void ButtonNavigator::onPress(const Buttons& buttons, const Callback& callback) {
|
||||||
if (!mappedInput) return;
|
if (!mappedInput) return;
|
||||||
|
|
||||||
bool buttonPressed = false;
|
|
||||||
for (const MappedInputManager::Button button : buttons) {
|
for (const MappedInputManager::Button button : buttons) {
|
||||||
if (mappedInput->wasPressed(button)) {
|
if (mappedInput->wasPressed(button)) {
|
||||||
buttonPressed = true;
|
callback();
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buttonPressed && !recentlyNavigatedContinuously()) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void ButtonNavigator::onRelease(const Buttons& buttons, const Callback& callback) const {
|
|
||||||
|
void ButtonNavigator::onRelease(const Buttons& buttons, const Callback& callback) {
|
||||||
if (!mappedInput) return;
|
if (!mappedInput) return;
|
||||||
|
|
||||||
bool buttonReleased = false;
|
|
||||||
for (const MappedInputManager::Button button : buttons) {
|
for (const MappedInputManager::Button button : buttons) {
|
||||||
if (mappedInput->wasReleased(button)) {
|
if (mappedInput->wasReleased(button)) {
|
||||||
buttonReleased = true;
|
if (lastContinuousNavTime == 0) {
|
||||||
break;
|
callback();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (buttonReleased && !recentlyNavigatedContinuously()) {
|
lastContinuousNavTime = 0;
|
||||||
callback();
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ButtonNavigator::onContinuous(const Buttons& buttons, const Callback& callback) {
|
void ButtonNavigator::onContinuous(const Buttons& buttons, const Callback& callback) {
|
||||||
if (!mappedInput) return;
|
if (!mappedInput) return;
|
||||||
|
|
||||||
bool buttonPressed = false;
|
|
||||||
for (const MappedInputManager::Button button : buttons) {
|
for (const MappedInputManager::Button button : buttons) {
|
||||||
if (mappedInput->isPressed(button)) {
|
if (mappedInput->isPressed(button) && shouldNavigateContinuously()) {
|
||||||
buttonPressed = true;
|
callback();
|
||||||
break;
|
lastContinuousNavTime = millis();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buttonPressed && shouldNavigateContinuously()) {
|
|
||||||
callback();
|
|
||||||
lastContinuousNavTime = millis();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ButtonNavigator::shouldNavigateContinuously() const {
|
bool ButtonNavigator::shouldNavigateContinuously() const {
|
||||||
@ -80,15 +70,6 @@ bool ButtonNavigator::shouldNavigateContinuously() const {
|
|||||||
return buttonHeldLongEnough && navigationIntervalElapsed;
|
return buttonHeldLongEnough && navigationIntervalElapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ButtonNavigator::recentlyNavigatedContinuously() const {
|
|
||||||
const int elapsedTime = millis() - lastContinuousNavTime;
|
|
||||||
if (elapsedTime < 50) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ButtonNavigator::nextIndex(const int currentIndex, const int totalItems) {
|
int ButtonNavigator::nextIndex(const int currentIndex, const int totalItems) {
|
||||||
if (totalItems <= 0) return 0;
|
if (totalItems <= 0) return 0;
|
||||||
|
|
||||||
|
|||||||
@ -12,11 +12,11 @@ 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]] bool recentlyNavigatedContinuously() const;
|
|
||||||
|
|
||||||
[[nodiscard]] static Buttons getNextButtons() {
|
[[nodiscard]] static Buttons getNextButtons() {
|
||||||
return {MappedInputManager::Button::Down, MappedInputManager::Button::Right};
|
return {MappedInputManager::Button::Down, MappedInputManager::Button::Right};
|
||||||
@ -38,9 +38,9 @@ class ButtonNavigator final {
|
|||||||
void onPreviousPress(const Callback& callback);
|
void onPreviousPress(const Callback& callback);
|
||||||
void onPress(const Buttons& buttons, const Callback& callback);
|
void onPress(const Buttons& buttons, const Callback& callback);
|
||||||
|
|
||||||
void onNextRelease(const Callback& callback) const;
|
void onNextRelease(const Callback& callback);
|
||||||
void onPreviousRelease(const Callback& callback) const;
|
void onPreviousRelease(const Callback& callback);
|
||||||
void onRelease(const Buttons& buttons, const Callback& callback) const;
|
void onRelease(const Buttons& buttons, const Callback& callback);
|
||||||
|
|
||||||
void onNextContinuous(const Callback& callback);
|
void onNextContinuous(const Callback& callback);
|
||||||
void onPreviousContinuous(const Callback& callback);
|
void onPreviousContinuous(const Callback& callback);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user